2023-04-14 15:27:24 +02:00
|
|
|
use crate::bc::{Chunk, Op, TraceInfo, Value};
|
2023-03-30 11:49:38 +02:00
|
|
|
|
2023-03-30 20:17:00 +02:00
|
|
|
pub struct VM {
|
|
|
|
|
pub trace: bool,
|
2023-03-30 11:49:38 +02:00
|
|
|
stack: Vec<Value>,
|
2023-03-30 20:17:00 +02:00
|
|
|
pc: usize,
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-30 20:17:00 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum VMError {
|
2023-03-30 11:49:38 +02:00
|
|
|
Compile,
|
2023-06-04 19:19:48 +02:00
|
|
|
Runtime(&'static str, usize),
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl VM {
|
2023-03-30 20:17:00 +02:00
|
|
|
pub fn new() -> VM {
|
|
|
|
|
VM {
|
|
|
|
|
trace: false,
|
|
|
|
|
stack: Vec::new(),
|
|
|
|
|
pc: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 19:19:48 +02:00
|
|
|
fn runtime_err(&self, msg: &'static str) -> VMError {
|
|
|
|
|
return VMError::Runtime(msg, self.pc);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 11:49:38 +02:00
|
|
|
fn push(&mut self, value: Value) {
|
|
|
|
|
self.stack.push(value);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 20:17:00 +02:00
|
|
|
fn pop(&mut self) -> Result<Value, VMError> {
|
2023-06-04 19:19:48 +02:00
|
|
|
self.stack.pop().ok_or_else(|| self.runtime_err("Attempt to pop of empty stack."))
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 12:46:24 +02:00
|
|
|
pub fn run(&mut self, chunk: &Chunk) -> Result<(), VMError> {
|
2023-03-30 20:17:00 +02:00
|
|
|
while self.pc < chunk.code.len() {
|
|
|
|
|
let instr = chunk.code[self.pc];
|
|
|
|
|
self.pc += 1;
|
|
|
|
|
|
2023-03-30 11:49:38 +02:00
|
|
|
if self.trace {
|
2023-03-30 20:17:00 +02:00
|
|
|
print!(" [ ");
|
2023-03-30 11:49:38 +02:00
|
|
|
for value in self.stack.iter() {
|
2023-03-30 20:17:00 +02:00
|
|
|
print!("{:?} | ", value);
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
2023-03-30 20:17:00 +02:00
|
|
|
println!("_ ]\n");
|
|
|
|
|
|
2023-04-04 19:03:57 +02:00
|
|
|
println!(
|
|
|
|
|
"{:?}\n",
|
|
|
|
|
TraceInfo {
|
|
|
|
|
offset: self.pc - 1,
|
|
|
|
|
op: instr,
|
|
|
|
|
chunk: chunk
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match instr {
|
2023-06-04 19:19:48 +02:00
|
|
|
Op::Return => print!("{:?}", self.pop()?),
|
2023-04-04 19:03:57 +02:00
|
|
|
Op::Constant { offset } => self.push(chunk.constants[offset]),
|
2023-03-30 20:17:00 +02:00
|
|
|
Op::Negate => {
|
|
|
|
|
let new_val = -self.pop()?.val;
|
|
|
|
|
self.push(Value::from(new_val));
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
2023-04-04 19:03:57 +02:00
|
|
|
Op::Add | Op::Subtract | Op::Multiply | Op::Divide => {
|
|
|
|
|
let b = self.pop()?;
|
|
|
|
|
let a = self.pop()?;
|
|
|
|
|
let r = match instr {
|
2023-06-04 19:19:48 +02:00
|
|
|
Op::Add => Ok(a.val + b.val),
|
|
|
|
|
Op::Subtract => Ok(a.val - b.val),
|
|
|
|
|
Op::Multiply => Ok(a.val * b.val),
|
|
|
|
|
Op::Divide => Ok(a.val / b.val),
|
|
|
|
|
_ => Err(self.runtime_err("Op not implemented"))
|
|
|
|
|
}?;
|
|
|
|
|
self.push(r.into())
|
2023-04-04 19:03:57 +02:00
|
|
|
}
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 19:03:57 +02:00
|
|
|
return Ok(());
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-04 20:34:36 +02:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::{Chunk, Op, Value, VM};
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn simple_arithmetic() {
|
2023-06-04 19:19:48 +02:00
|
|
|
let mut chunk = Chunk::new();
|
2023-04-04 20:34:36 +02:00
|
|
|
chunk.add_constant(Value::from(3.));
|
|
|
|
|
chunk.add_constant(Value::from(7.));
|
|
|
|
|
chunk.add_constant(Value::from(11.));
|
|
|
|
|
chunk.add_constant(Value::from(17.));
|
|
|
|
|
chunk.add_constant(Value::from(500.));
|
|
|
|
|
chunk.add_constant(Value::from(1000.));
|
|
|
|
|
chunk.add_constant(Value::from(250.));
|
|
|
|
|
|
|
|
|
|
chunk.add_op(Op::Constant { offset: 0 }, 1);
|
|
|
|
|
chunk.add_op(Op::Constant { offset: 1 }, 1);
|
|
|
|
|
chunk.add_op(Op::Multiply, 1);
|
|
|
|
|
chunk.add_op(Op::Constant { offset: 2 }, 1);
|
|
|
|
|
chunk.add_op(Op::Constant { offset: 3 }, 1);
|
|
|
|
|
chunk.add_op(Op::Multiply, 1);
|
|
|
|
|
chunk.add_op(Op::Multiply, 1);
|
|
|
|
|
chunk.add_op(Op::Negate, 1);
|
|
|
|
|
chunk.add_op(Op::Constant { offset: 4 }, 2);
|
|
|
|
|
chunk.add_op(Op::Constant { offset: 5 }, 2);
|
|
|
|
|
chunk.add_op(Op::Add, 2);
|
|
|
|
|
chunk.add_op(Op::Constant { offset: 6 }, 2);
|
|
|
|
|
chunk.add_op(Op::Subtract, 2);
|
|
|
|
|
chunk.add_op(Op::Negate, 2);
|
|
|
|
|
chunk.add_op(Op::Divide, 2);
|
|
|
|
|
|
2023-04-12 12:46:24 +02:00
|
|
|
let mut vm = VM::new();
|
|
|
|
|
vm.run(&chunk).unwrap();
|
2023-04-04 20:34:36 +02:00
|
|
|
|
2023-04-12 12:46:24 +02:00
|
|
|
assert_eq!(vm.stack[0], Value::from(3.1416));
|
2023-04-04 20:34:36 +02:00
|
|
|
}
|
|
|
|
|
}
|