2023-03-29 21:15:41 +02:00
|
|
|
use std::convert::From;
|
2023-03-29 20:03:16 +02:00
|
|
|
use std::fmt;
|
|
|
|
|
|
2023-03-29 20:07:31 +02:00
|
|
|
#[repr(u8)]
|
2023-03-29 21:15:41 +02:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2023-03-29 20:07:31 +02:00
|
|
|
pub enum Op {
|
2023-03-29 21:15:41 +02:00
|
|
|
Return,
|
|
|
|
|
Constant { offset: usize },
|
2023-03-30 20:17:00 +02:00
|
|
|
Negate,
|
2023-03-29 21:15:41 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-30 11:49:38 +02:00
|
|
|
#[derive(Copy, Clone)]
|
2023-03-29 21:15:41 +02:00
|
|
|
pub struct Value {
|
|
|
|
|
val: f64,
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 11:49:38 +02:00
|
|
|
impl fmt::Debug for Value {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
|
|
|
|
write!(f, "{}", self.val)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 21:15:41 +02:00
|
|
|
impl From<f64> for Value {
|
|
|
|
|
fn from(value: f64) -> Self {
|
|
|
|
|
Value { val: value }
|
|
|
|
|
}
|
2023-03-29 20:07:31 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-29 20:03:16 +02:00
|
|
|
pub struct Chunk {
|
|
|
|
|
code: Vec<Op>,
|
2023-03-29 21:15:41 +02:00
|
|
|
name: String,
|
|
|
|
|
debug_info: Vec<usize>,
|
|
|
|
|
constants: Vec<Value>,
|
2023-03-29 20:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Chunk {
|
2023-03-29 21:15:41 +02:00
|
|
|
pub fn new(name: String) -> Self {
|
2023-03-29 20:03:16 +02:00
|
|
|
Chunk {
|
|
|
|
|
code: Vec::new(),
|
2023-03-29 21:15:41 +02:00
|
|
|
name: name,
|
|
|
|
|
debug_info: Vec::new(),
|
|
|
|
|
constants: Vec::new(),
|
2023-03-29 20:03:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 21:15:41 +02:00
|
|
|
pub fn add_op(&mut self, op: Op, line: usize) {
|
|
|
|
|
self.code.push(op);
|
|
|
|
|
self.debug_info.push(line);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn add_constant(&mut self, value: Value) {
|
|
|
|
|
self.constants.push(value);
|
2023-03-29 20:03:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for Chunk {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
|
|
|
|
writeln!(f, "-*-*- {} -*-*-", self.name)?;
|
2023-03-29 21:15:41 +02:00
|
|
|
for (idx, op) in self.code.iter().copied().enumerate() {
|
2023-03-30 20:17:00 +02:00
|
|
|
writeln!(f, "{:?}", TraceInfo {
|
|
|
|
|
offset: idx,
|
|
|
|
|
op: op,
|
|
|
|
|
chunk: &self
|
|
|
|
|
})?;
|
2023-03-29 20:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-30 11:49:38 +02:00
|
|
|
|
2023-03-30 20:17:00 +02:00
|
|
|
struct TraceInfo<'a> {
|
|
|
|
|
offset: usize,
|
|
|
|
|
op: Op,
|
|
|
|
|
chunk: &'a Chunk
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for TraceInfo<'_> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
|
|
|
|
let chunk = self.chunk;
|
|
|
|
|
let op = self.op;
|
|
|
|
|
let offset = self.offset;
|
|
|
|
|
|
|
|
|
|
write!(f, "{:04} ", offset)?;
|
|
|
|
|
|
|
|
|
|
let line = chunk.debug_info[offset];
|
|
|
|
|
|
|
|
|
|
if offset > 0 && chunk.debug_info[offset-1] == line {
|
|
|
|
|
write!(f, " | ")
|
|
|
|
|
} else {
|
|
|
|
|
write!(f, "{:4} ", line)
|
|
|
|
|
}?;
|
|
|
|
|
|
|
|
|
|
match op {
|
|
|
|
|
Op::Return | Op::Negate => write!(f, "{:?}", op),
|
|
|
|
|
Op::Constant { offset } => {
|
|
|
|
|
f.debug_struct("Constant")
|
|
|
|
|
.field("val", &chunk.constants[offset].val)
|
|
|
|
|
.finish()?;
|
|
|
|
|
write!(f, "")
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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,
|
|
|
|
|
Runtime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl VM {
|
2023-03-30 20:17:00 +02:00
|
|
|
pub fn new() -> VM {
|
|
|
|
|
VM {
|
|
|
|
|
trace: false,
|
|
|
|
|
stack: Vec::new(),
|
|
|
|
|
pc: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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> {
|
|
|
|
|
self.stack.pop().ok_or(VMError::Runtime)
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn interpret(&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-03-30 11:49:38 +02:00
|
|
|
|
2023-03-30 20:17:00 +02:00
|
|
|
println!("{:?}\n", TraceInfo {
|
|
|
|
|
offset: self.pc - 1,
|
|
|
|
|
op: instr,
|
|
|
|
|
chunk: chunk
|
|
|
|
|
});
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match instr {
|
|
|
|
|
Op::Return => {
|
2023-03-30 20:17:00 +02:00
|
|
|
print!("{:?}", self.pop()?);
|
2023-03-30 11:49:38 +02:00
|
|
|
return Ok(())
|
|
|
|
|
},
|
|
|
|
|
Op::Constant { offset } => {
|
2023-03-30 20:17:00 +02:00
|
|
|
self.push(chunk.constants[offset])
|
|
|
|
|
}
|
|
|
|
|
Op::Negate => {
|
|
|
|
|
let new_val = -self.pop()?.val;
|
|
|
|
|
self.push(Value::from(new_val));
|
2023-03-30 11:49:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|