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

use box instead of vec

This commit is contained in:
Natapat Samutpong 2022-02-17 21:09:19 +07:00
parent 63fb726e6e
commit 01b7d33181

View file

@ -115,18 +115,18 @@ pub enum Expr {
Let { Let {
name: String, name: String,
value: Vec<Self>, value: Box<Self>,
}, },
Fun { Fun {
name: String, name: String,
args: Vec<String>, args: Vec<String>,
body: Vec<Self>, body: Box<Self>,
}, },
If { If {
cond: Box<Self>, cond: Box<Self>,
then: Vec<Self>, then: Box<Self>,
else_: Vec<Self>, else_: Box<Self>,
}, },
Do { body: Vec<Self> }, Do { body: Vec<Self> },
@ -236,18 +236,19 @@ fn expr_parser() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
expr.clone() expr.clone()
.then_ignore(just(Token::Semicolon)) .then_ignore(just(Token::Semicolon))
.repeated()) .repeated())
.then_ignore(just(Token::End)); .then_ignore(just(Token::End))
.map(|body| Expr::Do { body });
let declare_var = just(Token::Let) let declare_var = just(Token::Let)
.ignore_then(ident) .ignore_then(ident)
.then_ignore(just(Token::Assign)) .then_ignore(just(Token::Assign))
.then( .then(
do_block.clone() do_block.clone()
.or(decl.clone().repeated().at_most(1)) .or(decl.clone())
) )
.map(|(name, value)| Expr::Let { .map(|(name, value)| Expr::Let {
name, name,
value, value: Box::new(value),
}).labelled("variable"); }).labelled("variable");
let declare_fun = just(Token::Fun) let declare_fun = just(Token::Fun)
@ -256,12 +257,12 @@ fn expr_parser() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
.then_ignore(just(Token::Assign)) .then_ignore(just(Token::Assign))
.then( .then(
do_block.clone() do_block.clone()
.or(decl.clone().repeated().at_most(1)) .or(decl.clone())
) )
.map(|((name, args), body)| Expr::Fun { .map(|((name, args), body)| Expr::Fun {
name, name,
args, args,
body, body: Box::new(body),
}).labelled("function"); }).labelled("function");
let declare_import = just(Token::Import) let declare_import = just(Token::Import)
@ -273,31 +274,25 @@ fn expr_parser() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
.then_ignore(just(Token::Then)) .then_ignore(just(Token::Then))
.then( .then(
do_block.clone() do_block.clone()
.or(decl.clone() .or(decl.clone().then_ignore(just(Token::Semicolon).or_not()))
.repeated()
.at_most(1)
.then_ignore(just(Token::Semicolon)))
) )
.then_ignore(just(Token::Else)) .then_ignore(just(Token::Else))
.then( .then(
do_block.clone() do_block.clone()
.or(decl.clone() .or(decl.clone().then_ignore(just(Token::Semicolon).or_not()))
.repeated()
.at_most(1)
.then_ignore(just(Token::Semicolon)))
) )
.then_ignore(just(Token::End)) .then_ignore(just(Token::End))
.map(|((cond, then), else_)| Expr::If { .map(|((cond, then), else_)| Expr::If {
cond: Box::new(cond), cond: Box::new(cond),
then, then: Box::new(then),
else_, else_: Box::new(else_),
}).labelled("if"); }).labelled("if");
declare_var declare_var
.or(declare_fun) .or(declare_fun)
.or(declare_import) .or(declare_import)
.or(if_cond) .or(if_cond)
.or(do_block.map(|body| Expr::Do { body })) .or(do_block)
.or(expr) .or(expr)
}).labelled("declare"); }).labelled("declare");