diff --git a/src/compiler_tests.rs b/src/compiler_tests.rs index d43697a..14ad19a 100644 --- a/src/compiler_tests.rs +++ b/src/compiler_tests.rs @@ -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))); diff --git a/src/vm.rs b/src/vm.rs index f7eb18a..ec8e0d2 100644 --- a/src/vm.rs +++ b/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); + } + }); + } _ => {} } }