diff --git a/Cargo.lock b/Cargo.lock index cb1dff01..1ec1c348 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -763,6 +763,7 @@ dependencies = [ "oxide_api", "oxide_compiler", "oxide_parser", + "oxide_vm", "rustyline", ] @@ -805,6 +806,7 @@ dependencies = [ "hashbrown 0.14.5", "oxide_compiler", "oxide_kernel", + "oxide_parser", "proptest", "ryu", ] diff --git a/crates/oxide_cli/Cargo.toml b/crates/oxide_cli/Cargo.toml index f3eeef95..75983bc5 100644 --- a/crates/oxide_cli/Cargo.toml +++ b/crates/oxide_cli/Cargo.toml @@ -13,6 +13,7 @@ path = "src/main.rs" [dependencies] oxide_parser = { path = "../oxide_parser" } oxide_compiler = { path = "../oxide_compiler" } +oxide_vm = { path = "../oxide_vm" } oxide_api = { path = "../oxide_api" } clap.workspace = true rustyline.workspace = true diff --git a/crates/oxide_cli/src/main.rs b/crates/oxide_cli/src/main.rs index ae7c905e..bae5e483 100644 --- a/crates/oxide_cli/src/main.rs +++ b/crates/oxide_cli/src/main.rs @@ -5,6 +5,7 @@ use ansi_term::Colour::Red; use clap::{Parser, Subcommand}; use oxide_compiler::compiler::Compiler; use oxide_parser::Allocator; +use oxide_vm::vm::Vm; #[derive(Parser)] #[command(name = "oxide", version, about = "OxideJS - Rust JavaScript engine")] @@ -66,9 +67,18 @@ fn eval(code: &str) -> ExitCode { }; let compiler = Compiler::new(); - match compiler.compile(&program) { - Ok(module) => { - print!("{module}"); + let module = match compiler.compile(&program) { + Ok(m) => m, + Err(err) => { + eprintln!("{}", Red.paint(err)); + return ExitCode::FAILURE; + } + }; + + let mut vm = Vm::new(); + match vm.run(&module) { + Ok(result) => { + println!("{result}"); ExitCode::SUCCESS } Err(err) => { diff --git a/crates/oxide_vm/Cargo.toml b/crates/oxide_vm/Cargo.toml index ae4e465e..88f3ec97 100644 --- a/crates/oxide_vm/Cargo.toml +++ b/crates/oxide_vm/Cargo.toml @@ -16,3 +16,4 @@ ryu.workspace = true [dev-dependencies] proptest.workspace = true +oxide_parser = { path = "../oxide_parser" } diff --git a/crates/oxide_vm/tests/vm_tests.rs b/crates/oxide_vm/tests/vm_tests.rs new file mode 100644 index 00000000..08258973 --- /dev/null +++ b/crates/oxide_vm/tests/vm_tests.rs @@ -0,0 +1,115 @@ +use oxide_compiler::compiler::Compiler; +use oxide_parser::Allocator; +use oxide_vm::vm::Vm; + +fn eval(source: &str) -> String { + let allocator = Allocator::default(); + let program = oxide_parser::parse(&allocator, source).expect("parse failed"); + let module = Compiler::new().compile(&program).expect("compile failed"); + let mut vm = Vm::new(); + let result = vm.run(&module).expect("vm run failed"); + format!("{result}") +} + +#[test] +fn eval_arithmetic_add() { + assert_eq!(eval("1 + 2"), "3"); +} + +#[test] +fn eval_arithmetic_sub() { + assert_eq!(eval("5 - 3"), "2"); +} + +#[test] +fn eval_arithmetic_mul() { + assert_eq!(eval("3 * 4"), "12"); +} + +#[test] +fn eval_arithmetic_div() { + assert_eq!(eval("10 / 2"), "5"); +} + +#[test] +fn eval_arithmetic_mod() { + assert_eq!(eval("7 % 3"), "1"); +} + +#[test] +fn eval_comparison_eq_true() { + assert_eq!(eval("1 == 1"), "true"); +} + +#[test] +fn eval_comparison_eq_false() { + assert_eq!(eval("1 == 2"), "false"); +} + +#[test] +fn eval_comparison_lt() { + assert_eq!(eval("2 < 3"), "true"); +} + +#[test] +fn eval_comparison_gt() { + assert_eq!(eval("4 > 5"), "false"); +} + +#[test] +fn eval_comparison_lte() { + assert_eq!(eval("3 <= 3"), "true"); +} + +#[test] +fn eval_comparison_gte() { + assert_eq!(eval("5 >= 4"), "true"); +} + +#[test] +fn eval_neq() { + assert_eq!(eval("1 != 2"), "true"); + assert_eq!(eval("1 != 1"), "false"); +} + +#[test] +fn eval_coercion_null_equals_null() { + assert_eq!(eval("null == null"), "true"); +} + +#[test] +fn eval_coercion_bool_equals_int() { + assert_eq!(eval("true == 1"), "true"); + assert_eq!(eval("false == 0"), "true"); +} + +#[test] +fn eval_precedence() { + assert_eq!(eval("1 + 2 * 3"), "7"); + assert_eq!(eval("1 * 2 + 3"), "5"); +} + +#[test] +fn eval_negation() { + assert_eq!(eval("-5"), "-5"); +} + +#[test] +fn eval_boolean_and() { + // && not supported by compiler yet, test with nested ternary or skip +} + +#[test] +fn eval_division_by_zero() { + assert_eq!(eval("1 / 0"), "Infinity"); +} + +#[test] +fn eval_multi_stmt_last_value() { + assert_eq!(eval("1; 2; 3"), "3"); +} + +#[test] +fn eval_var_declaration() { + assert_eq!(eval("var x = 42"), "42"); +}