From cd722dc1c1183f8c813875acda47f866c476b702 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sun, 27 Mar 2022 07:14:12 +0700 Subject: [PATCH] change case expression keywords --- SYNTAX.md | 2 +- crates/hir/src/lib.rs | 12 ++++++------ crates/lexer/src/lib.rs | 10 +++++----- crates/parser/src/lib.rs | 6 +++--- example/case.hz | 2 +- .../err/{case_no_default.hz => match_no_default.hz} | 4 ++-- example/factorial.hz | 2 +- example/io/read.hz | 4 ++-- example/rule110.hz | 4 ++-- test.hz | 3 +++ 10 files changed, 26 insertions(+), 23 deletions(-) rename example/err/{case_no_default.hz => match_no_default.hz} (89%) create mode 100644 test.hz diff --git a/SYNTAX.md b/SYNTAX.md index f092a1c..81eed5f 100644 --- a/SYNTAX.md +++ b/SYNTAX.md @@ -68,7 +68,7 @@ Hazure is also [expression-oriented](https://en.wikipedia.org/wiki/Expression-or ``` 9) Case matching ```sml - case 1 + 1 of + match 1 + 1 with | 2 -> @write("Yes"); | else @write("How?"); end; diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 0d6fe2d..79f226c 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -211,12 +211,12 @@ pub fn expr_to_ir(expr: &Expr) -> (Option, Option) { } // If there is no `Hole` in the args then return early - if indexes.is_empty() { - return_err!(LoweringError { - span: rhs.1.clone(), - message: "Expected hole in piping".to_string(), - }); - } + // if indexes.is_empty() { + // return_err!(LoweringError { + // span: rhs.1.clone(), + // message: "Expected hole in piping".to_string(), + // }); + // } // Remove the `Hole` from the args let mut new_args = args.0.clone(); diff --git a/crates/lexer/src/lib.rs b/crates/lexer/src/lib.rs index 980543e..16210c8 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, - KwCase, KwOf, + KwMatch, KwWith, 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::KwCase => write!(f, "case"), - Token::KwOf => write!(f, "of"), + Token::KwMatch => write!(f, "match"), + Token::KwWith => write!(f, "with"), 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, - "case" => Token::KwCase, - "of" => Token::KwOf, + "match" => Token::KwMatch, + "with" => Token::KwWith, "return" => Token::KwReturn, "pub" => Token::KwPub, _ => Token::Identifier(s), diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index a7b2322..3a1dfe6 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -319,9 +319,9 @@ fn expr_parser() -> impl Parser>, Error = Simple ) }); - let case = just(Token::KwCase) + let match_ = just(Token::KwMatch) .ignore_then(expr.clone()) - .then_ignore(just(Token::KwOf)) + .then_ignore(just(Token::KwWith)) .then( just(Token::Pipe) .ignore_then(expr.clone()) @@ -356,7 +356,7 @@ fn expr_parser() -> impl Parser>, Error = Simple .or(return_) .or(do_block) .or(if_block) - .or(case) + .or(match_) .or(pipeline) }).labelled("expression"); diff --git a/example/case.hz b/example/case.hz index 6ef53bb..8e30d3c 100644 --- a/example/case.hz +++ b/example/case.hz @@ -1,7 +1,7 @@ fun main: void = do let foo: int = 3; - case foo of + match foo with | 1 -> @write("One"); | 2 -> @write("Two"); | 3 -> do diff --git a/example/err/case_no_default.hz b/example/err/match_no_default.hz similarity index 89% rename from example/err/case_no_default.hz rename to example/err/match_no_default.hz index f80bf74..43ec8a0 100644 --- a/example/err/case_no_default.hz +++ b/example/err/match_no_default.hz @@ -1,7 +1,7 @@ fun main: void = do let foo: int = 69; - case foo of + match foo with | 1 -> @write("One"); | 2 -> @write("Two"); | 3 -> do @@ -9,4 +9,4 @@ fun main: void = do end; -- \ @write("idk"); end; -end; \ No newline at end of file +end; diff --git a/example/factorial.hz b/example/factorial.hz index f58f6a5..1de4e13 100644 --- a/example/factorial.hz +++ b/example/factorial.hz @@ -1,5 +1,5 @@ fun factorial (n: int): int = do - case n of + match n with | 0 -> return 1; | else return n * factorial(n - 1); end; diff --git a/example/io/read.hz b/example/io/read.hz index 90c2af9..f6bd55e 100644 --- a/example/io/read.hz +++ b/example/io/read.hz @@ -7,8 +7,8 @@ end; fun main: void = do let input: string = @read("Enter your name:"); - case input of + match input with | "" -> @write("I don't know your name :("); \ say_hi(input); end; -end; \ No newline at end of file +end; diff --git a/example/rule110.hz b/example/rule110.hz index c1dbf02..8618237 100644 --- a/example/rule110.hz +++ b/example/rule110.hz @@ -1,5 +1,5 @@ fun print_single (cell: bool): void = do - case cell of + match cell with | true -> @write("█"); | else @write(" "); end; @@ -112,4 +112,4 @@ fun main: void = do @write("\n"); iter(vec, 20, 0); -end; \ No newline at end of file +end; diff --git a/test.hz b/test.hz new file mode 100644 index 0000000..7a7089d --- /dev/null +++ b/test.hz @@ -0,0 +1,3 @@ +fun main : int = do + "Hello, World" |> @write(); +end;