Fix up guarded block stuff (it's a statement)

This commit is contained in:
Dane Johnson 2024-11-04 12:27:11 -06:00
parent 3d8e76c9b0
commit dea529b42b
2 changed files with 12 additions and 14 deletions

View File

@ -45,7 +45,7 @@ pub fn eval(ast: &parser::Stmt, env: &mut Env) {
parser::Stmt::Conditional(guarded_blocks, default_block) => { parser::Stmt::Conditional(guarded_blocks, default_block) => {
let mut matched = false; let mut matched = false;
for GuardedBlock { guard, block } in guarded_blocks { for GuardedBlock { guard, block } in guarded_blocks {
let res = eval_expr(&guard, env); let res = eval_expr(guard, env);
match res { match res {
Atom::Bool(true) => { Atom::Bool(true) => {
matched = true; matched = true;
@ -60,9 +60,7 @@ pub fn eval(ast: &parser::Stmt, env: &mut Env) {
} }
if !matched { if !matched {
if let Some(block) = default_block { if let Some(block) = default_block {
for expr in default_block { eval_expr(block, env);
eval_expr(expr, env);
}
} }
} }
} }

View File

@ -68,21 +68,21 @@ peg::parser! {
['"'] s:$((!['"'] [_] / r#"\""#)*) ['"'] { Expr::Atom(Atom::String(s.to_string())) } ['"'] s:$((!['"'] [_] / r#"\""#)*) ['"'] { Expr::Atom(Atom::String(s.to_string())) }
f:funcall() { f } f:funcall() { f }
f:funcdef() { f } f:funcdef() { f }
b:boolean() _ { Expr::Atom(Atom::Bool(b)) }
o:object() { o } 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)) } n:num() _ { Expr::Atom(Atom::Num(n)) }
} }
rule boolean() -> bool
= b:$("true" / "false") { b.parse().unwrap() }
rule id() -> String rule id() -> String
= i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i.to_string() } = i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i.to_string() }
rule assignment() -> Stmt rule assignment() -> Stmt
= i:id() "<-" _ e:expr() stop() { Stmt::Assignment(i, e) } = i:id() "<-" _ e:expr() stop() { Stmt::Assignment(i, e) }
rule num() -> f64 rule num() -> f64
= n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() } = n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() }
rule _bool() -> bool
= "true" _ { true } / "false" _ { false }
rule funcall() -> Expr rule funcall() -> Expr
= i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) } = i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) }
rule funcdef() -> Expr rule funcdef() -> Expr
@ -331,14 +331,14 @@ else
<<<"; <<<";
let expected = vec![Stmt::Conditional( let expected = vec![Stmt::Conditional(
vec![ vec![
Expr::GuardedBlock(Box::new(GuardedBlock { GuardedBlock {
guard: Expr::Id("foo".to_string()), guard: Expr::Id("foo".to_string()),
block: vec![Stmt::Funcall(Expr::Funcall("bar".to_string(), vec![]))] block: vec![Stmt::Funcall(Expr::Funcall("bar".to_string(), vec![]))]
})), },
Expr::GuardedBlock(Box::new(GuardedBlock { GuardedBlock {
guard: Expr::Id("baz".to_string()), guard: Expr::Id("baz".to_string()),
block: vec![Stmt::Funcall(Expr::Funcall("foobar".to_string(), vec![]))] block: vec![Stmt::Funcall(Expr::Funcall("foobar".to_string(), vec![]))]
})), },
], ],
Some(Expr::Block(vec![Stmt::Funcall(Expr::Funcall("quux".to_string(), vec![]))])), Some(Expr::Block(vec![Stmt::Funcall(Expr::Funcall("quux".to_string(), vec![]))])),
)]; )];