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

in in let syntax

This commit is contained in:
Natapat Samutpong 2022-02-17 22:36:00 +07:00
parent c3e9ede0ef
commit 9344bb1977
2 changed files with 11 additions and 2 deletions

View file

@ -1,5 +1,6 @@
fun foo a b = a * b; fun foo a b = a * b;
let bar = foo( 3 3 ); let bar = foo( 3 3 );
let baz = bar in print(baz);
if bar == 9 then if bar == 9 then
do do

View file

@ -16,7 +16,7 @@ pub enum Token {
// Keywords // Keywords
Import, Import,
Let, Fun, Let, In, Fun,
If, Then, Else, End, If, Then, Else, End,
Do, Do,
} }
@ -74,6 +74,7 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = Simple<char>> {
"import" => Token::Import, "import" => Token::Import,
"let" => Token::Let, "let" => Token::Let,
"in" => Token::In,
"fun" => Token::Fun, "fun" => Token::Fun,
"if" => Token::If, "if" => Token::If,
"then" => Token::Then, "then" => Token::Then,
@ -116,6 +117,7 @@ pub enum Expr {
Let { Let {
name: String, name: String,
value: Box<Self>, value: Box<Self>,
then: Box<Option<Self>>,
}, },
Fun { Fun {
name: String, name: String,
@ -244,9 +246,15 @@ fn expr_parser() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
do_block.clone() do_block.clone()
.or(decl.clone()) .or(decl.clone())
) )
.map(|(name, value)| Expr::Let { .then(just(Token::In)
.ignore_then(do_block.clone()
.or(decl.clone()))
.or_not()
)
.map(|((name, value), then)| Expr::Let {
name, name,
value: Box::new(value), value: Box::new(value),
then: Box::new(then),
}).labelled("variable"); }).labelled("variable");
let declare_fun = just(Token::Fun) let declare_fun = just(Token::Fun)