change match keyword

pull/4/head
azur 2022-04-18 10:41:12 +07:00
parent 1a517da5a1
commit fac86781dd
12 changed files with 18 additions and 51 deletions

View File

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

View File

@ -335,9 +335,9 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
)
});
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())

View File

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

View File

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

3
example/hello.hz Normal file
View File

@ -0,0 +1,3 @@
fun main: void = do
@write("Hello, World!")
end

View File

@ -1,3 +0,0 @@
fun main: void = do
@emit("console.log('Hello, World!')");
end;

View File

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

View File

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

View File

@ -1,5 +0,0 @@
-- Run this file from root path
fun main: void = do
@read_file("./example/io/read_file.hz")
|> @write(_);
end;

View File

@ -1,3 +0,0 @@
fun main: void = do
@write_file("console.log('Hello, World!')", "hello.js");
end;

View File

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

View File

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