-
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
Showing
10 changed files
with
278 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,2 +1,2 @@ | ||
[workspace] | ||
members = [ "opcode", "compiler", "lexer", "parser", "evaluator", "object", "interpreter"] | ||
members = [ "opcode", "compiler", "lexer", "parser", "evaluator", "object", "interpreter", "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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "vm" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1.0.75" | ||
compiler = { path = "../compiler" } | ||
env_logger = "0.10.0" | ||
lexer = { path = "../lexer" } | ||
object = { path = "../object" } | ||
opcode = { path = "../opcode" } | ||
parser = { path = "../parser" } | ||
log = "0.4.20" | ||
byteorder = "1.5.0" |
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,140 @@ | ||
use std::{rc::Rc, borrow::Borrow}; | ||
|
||
use anyhow::Error; | ||
use byteorder::{BigEndian, ByteOrder}; | ||
use compiler::Bytecode; | ||
use object::Object; | ||
use opcode::Opcode; | ||
|
||
const STACK_SIZE: usize = 2048; | ||
|
||
pub struct Vm { | ||
constants: Vec<Rc<Object>>, | ||
instructions: opcode::Instructions, | ||
|
||
stack: Vec<Rc<Object>>, | ||
stack_pointer: usize | ||
} | ||
|
||
impl Vm { | ||
pub fn new(bytecode: Bytecode) -> Self { | ||
Self { | ||
constants: bytecode.constants, | ||
instructions: bytecode.instructions, | ||
|
||
stack: vec![Rc::new(Object::Null); STACK_SIZE], | ||
stack_pointer: 0 | ||
} | ||
} | ||
|
||
pub fn run(&mut self) -> Result<(), Error> { | ||
let mut ip = 0; | ||
|
||
while ip < self.instructions.0.len() { | ||
let op = Opcode::from(self.instructions.0[ip]); | ||
ip += 1; | ||
|
||
match op { | ||
Opcode::OpConst => { | ||
let const_index = BigEndian::read_u16(&self.instructions.0[ip..ip + 2]) as usize; | ||
ip += 2; | ||
|
||
self.push(Rc::clone(&self.constants[const_index])); | ||
} | ||
Opcode::OpAdd => { | ||
let right = self.pop(); | ||
let left = self.pop(); | ||
|
||
let result = match (&* left, &* right) { | ||
(Object::Integer(l), Object::Integer(r)) => Object::Integer(l + r), | ||
_ => { | ||
return Err(Error::msg(format!( | ||
"unsupported types for addition: {} + {}", | ||
left, right | ||
))); | ||
} | ||
}; | ||
|
||
self.push(Rc::new(result)); | ||
} | ||
Opcode::OpDiv => { | ||
let right = self.stack[self.stack_pointer - 1].borrow(); | ||
let left = self.stack[self.stack_pointer - 2].borrow(); | ||
|
||
let result = match (left, right) { | ||
(Object::Integer(l), Object::Integer(r)) => Object::Integer(l / r), | ||
_ => { | ||
return Err(Error::msg(format!( | ||
"unsupported types for division: {} / {}", | ||
left, right | ||
))); | ||
} | ||
}; | ||
|
||
self.stack_pointer -= 1; | ||
self.stack[self.stack_pointer - 1] = Rc::new(result); | ||
} | ||
Opcode::OpMul => { | ||
let right = self.stack[self.stack_pointer - 1].borrow(); | ||
let left = self.stack[self.stack_pointer - 2].borrow(); | ||
|
||
let result = match (left, right) { | ||
(Object::Integer(l), Object::Integer(r)) => Object::Integer(l * r), | ||
_ => { | ||
return Err(Error::msg(format!( | ||
"unsupported types for multiplication: {} * {}", | ||
left, right | ||
))); | ||
} | ||
}; | ||
|
||
self.stack_pointer -= 1; | ||
self.stack[self.stack_pointer - 1] = Rc::new(result); | ||
} | ||
Opcode::OpSub => { | ||
let right = self.stack[self.stack_pointer - 1].borrow(); | ||
let left = self.stack[self.stack_pointer - 2].borrow(); | ||
|
||
let result = match (left, right) { | ||
(Object::Integer(l), Object::Integer(r)) => Object::Integer(l - r), | ||
_ => { | ||
return Err(Error::msg(format!( | ||
"unsupported types for subtraction: {} - {}", | ||
left, right | ||
))); | ||
} | ||
}; | ||
|
||
self.stack_pointer -= 1; | ||
self.stack[self.stack_pointer - 1] = Rc::new(result); | ||
} | ||
Opcode::OpPop => { | ||
self.pop(); | ||
} | ||
_ => { | ||
return Err(Error::msg(format!("unknown opcode: {}", op))); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn last_popped_stack_elem(&self) -> Rc<Object> { | ||
Rc::clone(&self.stack[self.stack_pointer]) | ||
} | ||
|
||
pub fn pop(&mut self) -> Rc<Object> { | ||
self.stack_pointer -= 1; | ||
Rc::clone(&self.stack[self.stack_pointer]) | ||
} | ||
|
||
pub fn push(&mut self, obj: Rc<Object>) { | ||
self.stack[self.stack_pointer] = obj; | ||
self.stack_pointer += 1; | ||
} | ||
|
||
pub fn stack_top(&self) -> Rc<Object> { | ||
Rc::clone(&self.stack[self.stack_pointer - 1]) | ||
} | ||
} |
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,69 @@ | ||
use anyhow::Error; | ||
use compiler::Compiler; | ||
use lexer::Lexer; | ||
use parser::{Parser, ast::Node}; | ||
use vm::Vm; | ||
|
||
struct VmTestCase { | ||
input: String, | ||
expected: String, | ||
} | ||
|
||
fn run_vm_tests(tests: Vec<VmTestCase>) -> Result<(), Error> { | ||
for test in tests { | ||
let mut parser = Parser::new(Lexer::new(&test.input)); | ||
|
||
let program = parser.parse_program()?; | ||
let mut compiler = Compiler::new(); | ||
|
||
let bytecode = compiler.compile(&Node::Program(program))?; | ||
|
||
let mut vm = Vm::new(bytecode); | ||
|
||
vm.run()?; | ||
|
||
let stack_elem = vm.last_popped_stack_elem(); | ||
|
||
assert_eq!(stack_elem.to_string(), test.expected); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_integer_arithmetic() -> Result<(), Error> { | ||
let tests = vec![ | ||
VmTestCase { | ||
input: "1".to_string(), | ||
expected: "1".to_string(), | ||
}, | ||
VmTestCase { | ||
input: "2".to_string(), | ||
expected: "2".to_string(), | ||
}, | ||
VmTestCase { | ||
input: "1 + 2".to_string(), | ||
expected: "3".to_string(), | ||
}, | ||
VmTestCase { | ||
input: "1 - 2".to_string(), | ||
expected: "-1".to_string(), | ||
}, | ||
VmTestCase { | ||
input: "1 * 2".to_string(), | ||
expected: "2".to_string(), | ||
}, | ||
VmTestCase { | ||
input: "4 / 2".to_string(), | ||
expected: "2".to_string(), | ||
}, | ||
VmTestCase { | ||
input: "50 / 2 * 2 + 10 - 5".to_string(), | ||
expected: "55".to_string(), | ||
}, | ||
]; | ||
|
||
run_vm_tests(tests)?; | ||
|
||
Ok(()) | ||
} |