1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

change case expression keywords

This commit is contained in:
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 9) Case matching
```sml ```sml
case 1 + 1 of match 1 + 1 with
| 2 -> @write("Yes"); | 2 -> @write("Yes");
| else @write("How?"); | else @write("How?");
end; 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 there is no `Hole` in the args then return early
if indexes.is_empty() { // if indexes.is_empty() {
return_err!(LoweringError { // return_err!(LoweringError {
span: rhs.1.clone(), // span: rhs.1.clone(),
message: "Expected hole in piping".to_string(), // message: "Expected hole in piping".to_string(),
}); // });
} // }
// Remove the `Hole` from the args // Remove the `Hole` from the args
let mut new_args = args.0.clone(); let mut new_args = args.0.clone();

View file

@ -6,7 +6,7 @@ pub enum Token {
KwLet, KwMut, KwFun, KwLet, KwMut, KwFun,
KwDo, KwEnd, KwDo, KwEnd,
KwIf, KwThen, KwElse, KwIf, KwThen, KwElse,
KwCase, KwOf, KwMatch, KwWith,
KwReturn, KwReturn,
KwPub, KwPub,
@ -41,8 +41,8 @@ impl std::fmt::Display for Token {
Token::KwIf => write!(f, "if"), Token::KwIf => write!(f, "if"),
Token::KwThen => write!(f, "then"), Token::KwThen => write!(f, "then"),
Token::KwElse => write!(f, "else"), Token::KwElse => write!(f, "else"),
Token::KwCase => write!(f, "case"), Token::KwMatch => write!(f, "match"),
Token::KwOf => write!(f, "of"), Token::KwWith => write!(f, "with"),
Token::KwReturn => write!(f, "return"), Token::KwReturn => write!(f, "return"),
Token::KwPub => write!(f, "pub"), Token::KwPub => write!(f, "pub"),
@ -134,8 +134,8 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = Simple<char>> {
"if" => Token::KwIf, "if" => Token::KwIf,
"then" => Token::KwThen, "then" => Token::KwThen,
"else" => Token::KwElse, "else" => Token::KwElse,
"case" => Token::KwCase, "match" => Token::KwMatch,
"of" => Token::KwOf, "with" => Token::KwWith,
"return" => Token::KwReturn, "return" => Token::KwReturn,
"pub" => Token::KwPub, "pub" => Token::KwPub,
_ => Token::Identifier(s), _ => 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()) .ignore_then(expr.clone())
.then_ignore(just(Token::KwOf)) .then_ignore(just(Token::KwWith))
.then( .then(
just(Token::Pipe) just(Token::Pipe)
.ignore_then(expr.clone()) .ignore_then(expr.clone())
@ -356,7 +356,7 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
.or(return_) .or(return_)
.or(do_block) .or(do_block)
.or(if_block) .or(if_block)
.or(case) .or(match_)
.or(pipeline) .or(pipeline)
}).labelled("expression"); }).labelled("expression");

View file

@ -1,7 +1,7 @@
fun main: void = do fun main: void = do
let foo: int = 3; let foo: int = 3;
case foo of match foo with
| 1 -> @write("One"); | 1 -> @write("One");
| 2 -> @write("Two"); | 2 -> @write("Two");
| 3 -> do | 3 -> do

View file

@ -1,7 +1,7 @@
fun main: void = do fun main: void = do
let foo: int = 69; let foo: int = 69;
case foo of match foo with
| 1 -> @write("One"); | 1 -> @write("One");
| 2 -> @write("Two"); | 2 -> @write("Two");
| 3 -> do | 3 -> do

View file

@ -1,5 +1,5 @@
fun factorial (n: int): int = do fun factorial (n: int): int = do
case n of match n with
| 0 -> return 1; | 0 -> return 1;
| else return n * factorial(n - 1); | else return n * factorial(n - 1);
end; end;

View file

@ -7,7 +7,7 @@ end;
fun main: void = do fun main: void = do
let input: string = @read("Enter your name:"); let input: string = @read("Enter your name:");
case input of match input with
| "" -> @write("I don't know your name :("); | "" -> @write("I don't know your name :(");
\ say_hi(input); \ say_hi(input);
end; end;

View file

@ -1,5 +1,5 @@
fun print_single (cell: bool): void = do fun print_single (cell: bool): void = do
case cell of match cell with
| true -> @write("█"); | true -> @write("█");
| else @write(" "); | else @write(" ");
end; end;

3
test.hz Normal file
View file

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