if-else and more tests

This commit is contained in:
Shautvast 2025-11-15 13:34:05 +01:00
parent 1a8f4660b2
commit db183e95c4
2 changed files with 54 additions and 1 deletions

View file

@ -339,7 +339,7 @@ a=2"#),
} }
#[test] #[test]
fn if_expr() { fn simple_if_expr() {
assert_eq!( assert_eq!(
run(r#" run(r#"
if true: 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] // #[test]
// fn package() { // fn package() {
// assert_eq!(run(r#"a.b.c()"#), Ok(Value::U32(48))); // assert_eq!(run(r#"a.b.c()"#), Ok(Value::U32(48)));

View file

@ -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);
}
});
}
_ => {} _ => {}
} }
} }