if-else and more tests
This commit is contained in:
parent
1a8f4660b2
commit
db183e95c4
2 changed files with 54 additions and 1 deletions
|
|
@ -339,7 +339,7 @@ a=2"#),
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn if_expr() {
|
||||
fn simple_if_expr() {
|
||||
assert_eq!(
|
||||
run(r#"
|
||||
if true:
|
||||
|
|
@ -349,6 +349,43 @@ if true:
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_expr() {
|
||||
assert_eq!(
|
||||
run(r#"
|
||||
if 1==1:
|
||||
2
|
||||
"#),
|
||||
Ok(Value::I64(2))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_var_expr() {
|
||||
assert_eq!(
|
||||
run(r#"
|
||||
let a=1
|
||||
if a==1:
|
||||
2
|
||||
"#),
|
||||
Ok(Value::I64(2))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_var_expr_else() {
|
||||
assert_eq!(
|
||||
run(r#"
|
||||
let a=1
|
||||
if a==2:
|
||||
1
|
||||
else:
|
||||
2
|
||||
"#),
|
||||
Ok(Value::I64(2))
|
||||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn package() {
|
||||
// assert_eq!(run(r#"a.b.c()"#), Ok(Value::U32(48)));
|
||||
|
|
|
|||
16
src/vm.rs
16
src/vm.rs
|
|
@ -260,6 +260,22 @@ impl Vm {
|
|||
}
|
||||
}
|
||||
}
|
||||
OP_IF_ELSE => {
|
||||
let condition = self.pop();
|
||||
self.push(if condition == Value::Bool(true) {
|
||||
if let Some(then) = self.registry.get(&format!("{}.?",chunk.name)){
|
||||
interpret_function(then, vec![])?
|
||||
} else {
|
||||
return Err(Something);
|
||||
}
|
||||
} else {
|
||||
if let Some(then) = self.registry.get(&format!("{}.:",chunk.name)){
|
||||
interpret_function(then, vec![])?
|
||||
} else {
|
||||
return Err(Something);
|
||||
}
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue