Skip to content

Commit

Permalink
supports testing for vm
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 18, 2023
1 parent bad6642 commit 9cb1499
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ lox-ir = { path="components/lox-ir" }
lox-parse = { path="components/lox-parse" }
lox-lex = { path="components/lox-lex" }
lox-compile = { path="components/lox-compile" }
lox-execute = { path="components/lox-execute" }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
1 change: 1 addition & 0 deletions components/lox-execute/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ edition = "2021"
salsa = { path="../salsa" }
lox-ir = { path="../lox-ir" }
lox-compile = { path="../lox-compile" }
tracing = "0.1.37"
12 changes: 12 additions & 0 deletions components/lox-execute/src/execute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use lox_compile::compile;
use lox_ir::input_file::InputFile;

use crate::vm::VM;


// FIXME: This should not return a `f64`, but for now it's convenient as our compiler is more or less a calculator for now.
pub fn execute_file(db: &impl crate::Db, input_file: InputFile) -> f64 {
let chunk = compile::compile_file(db, input_file);
let mut vm = VM::new(chunk);
vm.interpret()
}
3 changes: 3 additions & 0 deletions components/lox-execute/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![feature(trait_upcasting)]

pub mod vm;
pub mod execute;

pub use execute::execute_file;


#[salsa::jar(db = Db)]
Expand Down
14 changes: 12 additions & 2 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ pub struct VM {
}

impl VM {
pub fn interpret(&mut self) {
pub fn new(chunk: bytecode::Chunk) -> Self { Self { chunk, ip: 0, stack: Vec::new() } }

// FIXME: `interpret` should not return a `f64`, but for now it's convenient as
// our compiler is more or less a calculator.
pub fn interpret(&mut self) -> f64 {
loop {
if self.chunk.len() <= self.ip {
break;
}
tracing::debug!("ip: {}", self.ip);
tracing::debug!("stack: {:?}", self.stack);
let instruction = self.read_byte();
match instruction {
bytecode::Code::Return => return,
bytecode::Code::Return => break,
bytecode::Code::Constant(value) => {
self.push(value.into())
},
Expand All @@ -40,6 +49,7 @@ impl VM {
},
}
}
self.stack.pop().unwrap()
}

fn read_byte(&mut self) -> bytecode::Code {
Expand Down
4 changes: 4 additions & 0 deletions components/lox-ir/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ impl Chunk {
pub fn read_byte(&self, ip: usize) -> Code {
self.code[ip].clone()
}

pub fn len(&self) -> usize {
self.code.len()
}
}
2 changes: 1 addition & 1 deletion lox_tests/arithmetic.lox
Original file line number Diff line number Diff line change
@@ -1 +1 @@
print 1 + 2
1 + 2
1 change: 1 addition & 0 deletions lox_tests/arithmetic.output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ struct TestCase {
lox: PathBuf,
syntax: PathBuf,
bytecode: PathBuf,
output: PathBuf,
text: String,
}

Expand All @@ -27,8 +28,9 @@ impl TestCase {
let lox = path;
let syntax = lox.with_extension("syntax");
let bytecode = lox.with_extension("bytecode");
let output = lox.with_extension("output");
let text = fs::read_to_string(&lox).unwrap();
res.push(TestCase { lox, syntax, bytecode, text });
res.push(TestCase { lox, syntax, bytecode, output, text });
}
}
res.sort();
Expand All @@ -39,7 +41,7 @@ impl TestCase {
use expect_test::expect_file;
use lox_ir::{word::Word, input_file::InputFile, diagnostic::Diagnostics};

#[salsa::db(lox_parse::Jar, lox_ir::Jar, lox_lex::Jar, lox_compile::Jar)]
#[salsa::db(lox_parse::Jar, lox_ir::Jar, lox_lex::Jar, lox_compile::Jar, lox_execute::Jar)]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
Expand Down Expand Up @@ -93,5 +95,10 @@ fn main() {
// test bytecode
let chunk = lox_compile::compile_file(&db, input_file);
expect_file![case.bytecode].assert_eq(&format!("{:#?}", chunk));

// test execute
let output = lox_execute::execute_file(&db, input_file);
expect_file![case.output].assert_eq(&format!("{:#?}", output));

}
}

0 comments on commit 9cb1499

Please sign in to comment.