From fac86781dd3d6c2a3e5ec7f142ceb7ac74155c44 Mon Sep 17 00:00:00 2001 From: azur <73991665+azur1s@users.noreply.github.com> Date: Mon, 18 Apr 2022 10:41:12 +0700 Subject: [PATCH] change match keyword --- crates/lexer/src/lib.rs | 10 +++++----- crates/parser/src/lib.rs | 4 ++-- example/factorial.hz | 10 +--------- example/fibonacci.hz | 8 ++++---- example/hello.hz | 3 +++ example/io/emit.hz | 3 --- example/io/hello.hz | 3 --- example/io/read.hz | 14 -------------- example/io/read_file.hz | 5 ----- example/io/write_file.hz | 3 --- example/rule110.hz | 4 ++-- example/tuples.hz | 2 +- 12 files changed, 18 insertions(+), 51 deletions(-) create mode 100644 example/hello.hz delete mode 100644 example/io/emit.hz delete mode 100644 example/io/hello.hz delete mode 100644 example/io/read.hz delete mode 100644 example/io/read_file.hz delete mode 100644 example/io/write_file.hz diff --git a/crates/lexer/src/lib.rs b/crates/lexer/src/lib.rs index 3cc9506..a8c9357 100644 --- a/crates/lexer/src/lib.rs +++ b/crates/lexer/src/lib.rs @@ -6,7 +6,7 @@ pub enum Token { KwLet, KwMut, KwFun, KwDo, KwEnd, KwIf, KwThen, KwElse, - KwMatch, KwWith, + KwCase, KwOf, KwReturn, KwPub, @@ -41,8 +41,8 @@ impl std::fmt::Display for Token { Token::KwIf => write!(f, "if"), Token::KwThen => write!(f, "then"), Token::KwElse => write!(f, "else"), - Token::KwMatch => write!(f, "match"), - Token::KwWith => write!(f, "with"), + Token::KwCase => write!(f, "case"), + Token::KwOf => write!(f, "of"), Token::KwReturn => write!(f, "return"), Token::KwPub => write!(f, "pub"), @@ -134,8 +134,8 @@ pub fn lexer() -> impl Parser, Error = Simple> { "if" => Token::KwIf, "then" => Token::KwThen, "else" => Token::KwElse, - "match" => Token::KwMatch, - "with" => Token::KwWith, + "case" => Token::KwCase, + "of" => Token::KwOf, "return" => Token::KwReturn, "pub" => Token::KwPub, _ => Token::Identifier(s), diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index ccbea32..3a85b90 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -335,9 +335,9 @@ fn expr_parser() -> impl Parser>, Error = Simple ) }); - let match_ = just(Token::KwMatch) + let match_ = just(Token::KwCase) .ignore_then(expr.clone()) - .then_ignore(just(Token::KwWith)) + .then_ignore(just(Token::KwOf)) .then( just(Token::Pipe) .ignore_then(expr.clone()) diff --git a/example/factorial.hz b/example/factorial.hz index c0cea7c..3e3cb07 100644 --- a/example/factorial.hz +++ b/example/factorial.hz @@ -1,18 +1,10 @@ fun factorial (n: int) : int = do - match n with + case n of | 0 -> return 1 | else return n * factorial(n - 1) end end -fun also_factorial (n: int) : int = do - if n == 0 then - return 1 - else - return n * also_factorial(n - 1) - end -end - fun main : void = do let result : int = factorial(5) @write(result) diff --git a/example/fibonacci.hz b/example/fibonacci.hz index 586653f..be8cac9 100644 --- a/example/fibonacci.hz +++ b/example/fibonacci.hz @@ -1,8 +1,8 @@ fun fib (n: int): int = do - if n < 2 then - return n - else - return fib(n - 1) + fib(n - 2) + case n of + | 1 -> return 1 + | 2 -> return 1 + | else -> return fib(n - 1) + fib(n - 2) end end diff --git a/example/hello.hz b/example/hello.hz new file mode 100644 index 0000000..55a80ea --- /dev/null +++ b/example/hello.hz @@ -0,0 +1,3 @@ +fun main: void = do + @write("Hello, World!") +end \ No newline at end of file diff --git a/example/io/emit.hz b/example/io/emit.hz deleted file mode 100644 index e4d9090..0000000 --- a/example/io/emit.hz +++ /dev/null @@ -1,3 +0,0 @@ -fun main: void = do - @emit("console.log('Hello, World!')"); -end; \ No newline at end of file diff --git a/example/io/hello.hz b/example/io/hello.hz deleted file mode 100644 index a014ee4..0000000 --- a/example/io/hello.hz +++ /dev/null @@ -1,3 +0,0 @@ -fun main: void = do - @write("Hello, World!"); -end; \ No newline at end of file diff --git a/example/io/read.hz b/example/io/read.hz deleted file mode 100644 index f6bd55e..0000000 --- a/example/io/read.hz +++ /dev/null @@ -1,14 +0,0 @@ -fun say_hi (name: string): void = do - @write("Hi "); - @write(name); - @write("!"); -end; - -fun main: void = do - let input: string = @read("Enter your name:"); - - match input with - | "" -> @write("I don't know your name :("); - \ say_hi(input); - end; -end; diff --git a/example/io/read_file.hz b/example/io/read_file.hz deleted file mode 100644 index b7af16c..0000000 --- a/example/io/read_file.hz +++ /dev/null @@ -1,5 +0,0 @@ --- Run this file from root path -fun main: void = do - @read_file("./example/io/read_file.hz") - |> @write(_); -end; \ No newline at end of file diff --git a/example/io/write_file.hz b/example/io/write_file.hz deleted file mode 100644 index f205262..0000000 --- a/example/io/write_file.hz +++ /dev/null @@ -1,3 +0,0 @@ -fun main: void = do - @write_file("console.log('Hello, World!')", "hello.js"); -end; \ No newline at end of file diff --git a/example/rule110.hz b/example/rule110.hz index 319033a..59d1518 100644 --- a/example/rule110.hz +++ b/example/rule110.hz @@ -1,5 +1,5 @@ fun print_single (cell: bool): void = do - match cell with + case cell of | true -> @write("█") | else @write(" ") end @@ -47,7 +47,7 @@ fun cell_merger (a: bool) (b: bool) (c: bool): bool = do end fun next (state: vec_bool) (pointer: int): bool = do - match pointer with + case pointer of | 0 -> do let a: bool = false let b: bool = @get(state, 1) diff --git a/example/tuples.hz b/example/tuples.hz index 354e483..9ca8b40 100644 --- a/example/tuples.hz +++ b/example/tuples.hz @@ -16,7 +16,7 @@ end fun every (a : [any]) (b : [any]): bool = return every_(a, b, 0) fun eqTuples (a : [any]) (b : [any]) : bool = - match @len(a) == @len(b) with + case @len(a) == @len(b) of | true -> return every(a, b) | else return false end