change case expression keywords

pull/4/head
Natapat Samutpong 2022-03-27 07:14:12 +07:00
parent 7e707d6103
commit cd722dc1c1
10 changed files with 26 additions and 23 deletions

View File

@ -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;

View File

@ -211,12 +211,12 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
}
// 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();

View File

@ -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<char, Vec<(Token, Span)>, Error = Simple<char>> {
"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),

View File

@ -319,9 +319,9 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
)
});
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<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
.or(return_)
.or(do_block)
.or(if_block)
.or(case)
.or(match_)
.or(pipeline)
}).labelled("expression");

View File

@ -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

View File

@ -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;
end;

View File

@ -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;

View File

@ -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;
end;

View File

@ -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;
end;

3
test.hz Normal file
View File

@ -0,0 +1,3 @@
fun main : int = do
"Hello, World" |> @write();
end;