-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8284942
commit 348de4b
Showing
8 changed files
with
232 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod compiler; | ||
mod vm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::rc::Rc; | ||
|
||
use crate::{compiler::Compiler, object::Object, vm::VirtualMachine}; | ||
|
||
fn run_test(input: &str, expected: Object) { | ||
let program = parser::parse(input).unwrap(); | ||
|
||
let compiler = Compiler::new(); | ||
let bytecode = compiler.compile(&program); | ||
|
||
let vm = VirtualMachine::new(); | ||
let obj = vm.run(&bytecode); | ||
|
||
assert_eq!(obj, expected); | ||
} | ||
|
||
#[test] | ||
fn constants() { | ||
let tests = [ | ||
("10", Object::Integer(10)), | ||
("6.9", Object::Float(6.9)), | ||
("\"foo\"", Object::String(Rc::new("foo".to_string()))), | ||
("true", Object::Boolean(true)), | ||
]; | ||
|
||
for (input, expected) in tests { | ||
run_test(input, expected); | ||
} | ||
} | ||
|
||
#[test] | ||
fn array() { | ||
let tests = [ | ||
("[]", vec![]), | ||
("[1]", vec![Object::Integer(1)]), | ||
( | ||
"[1, \"foo\"]", | ||
vec![ | ||
Object::Integer(1), | ||
Object::String(Rc::new("foo".to_string())), | ||
], | ||
), | ||
( | ||
"[1, [2, 3], 4]", | ||
vec![ | ||
Object::Integer(1), | ||
Object::Array(Rc::new(vec![Object::Integer(2), Object::Integer(3)])), | ||
Object::Integer(4), | ||
], | ||
), | ||
]; | ||
|
||
for (input, expected) in tests { | ||
run_test(input, Object::Array(Rc::new(expected))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters