From 01b7d33181ef28599e6d09a24c2d27d2074fd54c Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Thu, 17 Feb 2022 21:09:19 +0700 Subject: [PATCH] use box instead of vec --- src/front/parse.rs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/front/parse.rs b/src/front/parse.rs index 19e756d..a39a9bb 100644 --- a/src/front/parse.rs +++ b/src/front/parse.rs @@ -115,18 +115,18 @@ pub enum Expr { Let { name: String, - value: Vec, + value: Box, }, Fun { name: String, args: Vec, - body: Vec, + body: Box, }, If { cond: Box, - then: Vec, - else_: Vec, + then: Box, + else_: Box, }, Do { body: Vec }, @@ -236,18 +236,19 @@ fn expr_parser() -> impl Parser> + Clone { expr.clone() .then_ignore(just(Token::Semicolon)) .repeated()) - .then_ignore(just(Token::End)); + .then_ignore(just(Token::End)) + .map(|body| Expr::Do { body }); let declare_var = just(Token::Let) .ignore_then(ident) .then_ignore(just(Token::Assign)) .then( do_block.clone() - .or(decl.clone().repeated().at_most(1)) + .or(decl.clone()) ) .map(|(name, value)| Expr::Let { name, - value, + value: Box::new(value), }).labelled("variable"); let declare_fun = just(Token::Fun) @@ -256,12 +257,12 @@ fn expr_parser() -> impl Parser> + Clone { .then_ignore(just(Token::Assign)) .then( do_block.clone() - .or(decl.clone().repeated().at_most(1)) + .or(decl.clone()) ) .map(|((name, args), body)| Expr::Fun { name, args, - body, + body: Box::new(body), }).labelled("function"); let declare_import = just(Token::Import) @@ -273,31 +274,25 @@ fn expr_parser() -> impl Parser> + Clone { .then_ignore(just(Token::Then)) .then( do_block.clone() - .or(decl.clone() - .repeated() - .at_most(1) - .then_ignore(just(Token::Semicolon))) + .or(decl.clone().then_ignore(just(Token::Semicolon).or_not())) ) .then_ignore(just(Token::Else)) .then( do_block.clone() - .or(decl.clone() - .repeated() - .at_most(1) - .then_ignore(just(Token::Semicolon))) + .or(decl.clone().then_ignore(just(Token::Semicolon).or_not())) ) .then_ignore(just(Token::End)) .map(|((cond, then), else_)| Expr::If { cond: Box::new(cond), - then, - else_, + then: Box::new(then), + else_: Box::new(else_), }).labelled("if"); declare_var .or(declare_fun) .or(declare_import) .or(if_cond) - .or(do_block.map(|body| Expr::Do { body })) + .or(do_block) .or(expr) }).labelled("declare");