diff --git a/rust/src/evaluator.rs b/rust/src/evaluator.rs index 6db0598..000dd49 100644 --- a/rust/src/evaluator.rs +++ b/rust/src/evaluator.rs @@ -44,8 +44,8 @@ pub fn eval(ast: &parser::Stmt, env: &mut Env) { env.set(id.clone(), eval_expr(expr, env)), parser::Stmt::Conditional(guarded_blocks, default_block) => { let mut matched = false; - for GuardedBlock { guard, block } in guarded_blocks { - let res = eval_expr(&guard, env); + for GuardedBlock { guard, block } in guarded_blocks { + let res = eval_expr(guard, env); match res { Atom::Bool(true) => { matched = true; @@ -60,9 +60,7 @@ pub fn eval(ast: &parser::Stmt, env: &mut Env) { } if !matched { if let Some(block) = default_block { - for expr in default_block { - eval_expr(expr, env); - } + eval_expr(block, env); } } } diff --git a/rust/src/parser.rs b/rust/src/parser.rs index 5c9173e..c35416a 100644 --- a/rust/src/parser.rs +++ b/rust/src/parser.rs @@ -35,7 +35,7 @@ impl fmt::Display for Atom { match self { Atom::String(a) => write!(f, "\"{}\"", a), Atom::Num(a) => write!(f, "{}", a), - Atom::Bool(a) => write!(f, "{}", a), + Atom::Bool(a) => write!(f, "{}", a), } } } @@ -68,21 +68,21 @@ peg::parser! { ['"'] s:$((!['"'] [_] / r#"\""#)*) ['"'] { Expr::Atom(Atom::String(s.to_string())) } f:funcall() { f } f:funcdef() { f } + b:boolean() _ { Expr::Atom(Atom::Bool(b)) } o:object() { o } - b:_bool() _ { Expr::Atom(Atom::Bool(b)) } - i:id() { Expr::Id(i) } + i:id() _ { Expr::Id(i) } n:num() _ { Expr::Atom(Atom::Num(n)) } } + rule boolean() -> bool + = b:$("true" / "false") { b.parse().unwrap() } rule id() -> String = i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i.to_string() } rule assignment() -> Stmt = i:id() "<-" _ e:expr() stop() { Stmt::Assignment(i, e) } rule num() -> f64 = n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() } - rule _bool() -> bool - = "true" _ { true } / "false" _ { false } rule funcall() -> Expr = i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) } rule funcdef() -> Expr @@ -331,14 +331,14 @@ else <<<"; let expected = vec![Stmt::Conditional( vec![ - Expr::GuardedBlock(Box::new(GuardedBlock { + GuardedBlock { guard: Expr::Id("foo".to_string()), block: vec![Stmt::Funcall(Expr::Funcall("bar".to_string(), vec![]))] - })), - Expr::GuardedBlock(Box::new(GuardedBlock { + }, + GuardedBlock { guard: Expr::Id("baz".to_string()), block: vec![Stmt::Funcall(Expr::Funcall("foobar".to_string(), vec![]))] - })), + }, ], Some(Expr::Block(vec![Stmt::Funcall(Expr::Funcall("quux".to_string(), vec![]))])), )];