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

Compare commits

..

No commits in common. "6b984f87110cec808c97ee21d78f601bd24cedca" and "9c1e0927da6f86a89832c9b4260eb8a7d4ab6a80" have entirely different histories.

7 changed files with 115 additions and 119 deletions

View file

@ -277,6 +277,7 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
}); });
let do_block = expr.clone() let do_block = expr.clone()
.then_ignore(just(Token::SemiColon))
.repeated() .repeated()
.delimited_by( .delimited_by(
just(Token::KwDo), just(Token::KwDo),
@ -298,13 +299,13 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
.then( .then(
do_block.clone() do_block.clone()
.or(expr.clone()) .or(expr.clone())
) ).then_ignore(just(Token::SemiColon))
.then_ignore(just(Token::KwElse)) .then_ignore(just(Token::KwElse))
.then( .then(
do_block.clone() do_block.clone()
.or(expr.clone()) .or(expr.clone())
) ).then_ignore(just(Token::SemiColon))
.then_ignore(just(Token::KwEnd)) .then_ignore(just(Token::KwEnd))
.map(|((cond, then), else_)| { .map(|((cond, then), else_)| {
@ -326,12 +327,14 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
.ignore_then(expr.clone()) .ignore_then(expr.clone())
.then_ignore(just(Token::Arrow)) .then_ignore(just(Token::Arrow))
.then(expr.clone()) .then(expr.clone())
.then_ignore(just(Token::SemiColon))
.repeated() .repeated()
) )
.then( .then(
just(Token::Pipe) just(Token::Pipe)
.ignore_then(just(Token::KwElse)) .ignore_then(just(Token::KwElse))
.ignore_then(expr.clone()) .ignore_then(expr.clone())
.then_ignore(just(Token::SemiColon))
) )
.then_ignore(just(Token::KwEnd)) .then_ignore(just(Token::KwEnd))
.map(|((expr, cases), default)| { .map(|((expr, cases), default)| {
@ -358,6 +361,7 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
}).labelled("expression"); }).labelled("expression");
expr expr
.then_ignore(just(Token::SemiColon))
.repeated() .repeated()
.then_ignore(end()) .then_ignore(end())
} }
@ -385,6 +389,7 @@ mod tests {
(Token::Identifier("Int".to_string()), 7..10), (Token::Identifier("Int".to_string()), 7..10),
(Token::Assign, 11..12), (Token::Assign, 11..12),
(Token::Int(1), 13..14), (Token::Int(1), 13..14),
(Token::SemiColon, 14..15),
], 15); ], 15);
assert_eq!(err, vec![]); assert_eq!(err, vec![]);

View file

@ -1,14 +1,14 @@
fun add2 (lhs: int) (rhs: int): int = do fun add2 (lhs: int) (rhs: int): int = do
return lhs + rhs return lhs + rhs;
end end;
fun main: void = do fun main: void = do
let result: int = add2(34, 35) let result: int = add2(34, 35);
@write(result) @write(result);
@write("\n") @write("\n");
if result == 69 then if result == 69 then
@write("big cool") @write("big cool");
else else
@write("not cool") @write("not cool");
end end;
end end;

12
example/case.hz Normal file
View file

@ -0,0 +1,12 @@
fun main: void = do
let foo: int = 3;
match foo with
| 1 -> @write("One");
| 2 -> @write("Two");
| 3 -> do
@write("Three");
end;
| else @write("idk");
end;
end;

View file

@ -1,19 +1,10 @@
fun factorial (n: int) : int = do fun factorial (n: int): int = do
match n with match n with
| 0 -> return 1 | 0 -> return 1;
| else return n * factorial(n - 1) | else return n * factorial(n - 1);
end end;
end end;
fun also_factorial (n: int) : int = do fun main: void = do
if n == 0 then factorial(5) |> @write(_);
return 1 end;
else
return n * also_factorial(n - 1)
end
end
fun main : void = do
let result : int = factorial(5)
@write(result)
end

View file

@ -1,12 +0,0 @@
fun main: void = do
let foo: int = 3
match foo with
| 1 -> @write("One")
| 2 -> @write("Two")
| 3 -> do
@write("Three")
end
| else @write("idk")
end
end

View file

@ -1,93 +1,93 @@
fun print_single (cell: bool): void = do fun print_single (cell: bool): void = do
match cell with match cell with
| true -> @write("█") | true -> @write("█");
| else @write(" ") | else @write(" ");
end end;
end end;
fun print_vec (state: vec_bool) (length: int) (curr: int): void = do fun print_vec (state: vec_bool) (length: int) (curr: int): void = do
if curr == length then @write("") else if curr == length then @write(""); else
do do
@get(state, curr) |> print_single(_) @get(state, curr) |> print_single(_);
print_vec(state, length, curr + 1) print_vec(state, length, curr + 1);
end end;
end end;
end end;
fun cell_merger (a: bool) (b: bool) (c: bool): bool = do fun cell_merger (a: bool) (b: bool) (c: bool): bool = do
if a then if a then
if b then if b then
if c then if c then
return false return false;
else else
return true return true;
end end;
else else
if c then if c then
return true return true;
else else
return false return false;
end end;
end end;
else else
if b then if b then
if c then if c then
return true return true;
else else
return true return true;
end end;
else else
if c then if c then
return true return true;
else else
return false return false;
end end;
end end;
end end;
end end;
fun next (state: vec_bool) (pointer: int): bool = do fun next (state: vec_bool) (pointer: int): bool = do
match pointer with case pointer of
| 0 -> do | 0 -> do
let a: bool = false let a: bool = false;
let b: bool = @get(state, 1) let b: bool = @get(state, 1);
let c: bool = @get(state, 2) let c: bool = @get(state, 2);
return cell_merger(a, b, c) return cell_merger(a, b, c);
end end;
| 1 -> do | 1 -> do
let a: bool = @get(state, 0) let a: bool = @get(state, 0);
let b: bool = @get(state, 1) let b: bool = @get(state, 1);
let c: bool = @get(state, 2) let c: bool = @get(state, 2);
return cell_merger(a, b, c) return cell_merger(a, b, c);
end end;
| 2 -> do | 2 -> do
let a: bool = @get(state, 1) let a: bool = @get(state, 1);
let b: bool = @get(state, 2) let b: bool = @get(state, 2);
let c: bool = @get(state, 3) let c: bool = @get(state, 3);
return cell_merger(a, b, c) return cell_merger(a, b, c);
end end;
| 3 -> do | 3 -> do
let a: bool = @get(state, 2) let a: bool = @get(state, 2);
let b: bool = @get(state, 3) let b: bool = @get(state, 3);
let c: bool = @get(state, 4) let c: bool = @get(state, 4);
return cell_merger(a, b, c) return cell_merger(a, b, c);
end end;
| 4 -> do | 4 -> do
let a: bool = @get(state, 3) let a: bool = @get(state, 3);
let b: bool = @get(state, 4) let b: bool = @get(state, 4);
let c: bool = @get(state, 5) let c: bool = @get(state, 5);
return cell_merger(a, b, c) return cell_merger(a, b, c);
end end;
| 5 -> do | 5 -> do
return true return true;
end end;
| else return false | else return false;
end end;
end end;
fun iter (state: vec_bool) (for: int) (curr: int): void = do fun iter (state: vec_bool) (for: int) (curr: int): void = do
if curr == for then if curr == for then
@write("") @write("");
else else
do do
let next_state: vec_bool = [ let next_state: vec_bool = [
@ -97,19 +97,19 @@ fun iter (state: vec_bool) (for: int) (curr: int): void = do
next(state, 3), next(state, 3),
next(state, 4), next(state, 4),
next(state, 5) next(state, 5)
] ];
print_vec(next_state, 6, 0) print_vec(next_state, 6, 0);
@write("\n") @write("\n");
iter(next_state, for, curr + 1) iter(next_state, for, curr + 1);
end end;
end end;
end end;
fun main: void = do fun main: void = do
let vec: vec_bool = [false, false, false, false, false, true] let vec: vec_bool = [false, false, false, false, false, true];
print_vec(vec, 6, 0) print_vec(vec, 6, 0);
@write("\n") @write("\n");
iter(vec, 20, 0) iter(vec, 20, 0);
end end;

View file

@ -1,16 +1,16 @@
fun main: void = do fun main: void = do
let str: string = "Hello, World" let str: string = "Hello, World";
-- Matching string(s) -- Matching string(s)
match str with match str with
| "Hello, World" -> @write("yes\n") | "Hello, World" -> @write("yes\n");
| else @write("no\n") | else @write("no\n");
end end;
-- Indexing character in string -- Indexing character in string
let foo: string = @get(str, 0) let foo: string = @get(str, 0);
match foo with match foo with
| "H" -> @write("still yes") | "H" -> @write("still yes");
| else @write("no H") | else @write("no H");
end end;
end end;