Fix up guarded block stuff (it's a statement)
This commit is contained in:
parent
3d8e76c9b0
commit
dea529b42b
@ -44,8 +44,8 @@ pub fn eval(ast: &parser::Stmt, env: &mut Env) {
|
|||||||
env.set(id.clone(), eval_expr(expr, env)),
|
env.set(id.clone(), eval_expr(expr, 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ impl fmt::Display for Atom {
|
|||||||
match self {
|
match self {
|
||||||
Atom::String(a) => write!(f, "\"{}\"", a),
|
Atom::String(a) => write!(f, "\"{}\"", a),
|
||||||
Atom::Num(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())) }
|
['"'] 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![]))])),
|
||||||
)];
|
)];
|
||||||
|
Loading…
Reference in New Issue
Block a user