[rlox] Implement VM Stack
This commit is contained in:
@@ -8,11 +8,17 @@ pub enum Op {
|
|||||||
Constant { offset: usize },
|
Constant { offset: usize },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct Value {
|
pub struct Value {
|
||||||
val: f64,
|
val: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for Value {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
|
write!(f, "{}", self.val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<f64> for Value {
|
impl From<f64> for Value {
|
||||||
fn from(value: f64) -> Self {
|
fn from(value: f64) -> Self {
|
||||||
Value { val: value }
|
Value { val: value }
|
||||||
@@ -56,10 +62,10 @@ impl fmt::Debug for Chunk {
|
|||||||
let line = self.debug_info[idx];
|
let line = self.debug_info[idx];
|
||||||
|
|
||||||
if idx > 0 && self.debug_info[idx-1] == line {
|
if idx > 0 && self.debug_info[idx-1] == line {
|
||||||
write!(f, " | ")?;
|
write!(f, " | ")
|
||||||
} else {
|
} else {
|
||||||
write!(f, "{:4} ", line)?;
|
write!(f, "{:4} ", line)
|
||||||
}
|
}?;
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Op::Return => writeln!(f, "{:?}", op),
|
Op::Return => writeln!(f, "{:?}", op),
|
||||||
@@ -73,3 +79,52 @@ impl fmt::Debug for Chunk {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const VM_STACK_SIZE: usize = 256;
|
||||||
|
|
||||||
|
struct VM {
|
||||||
|
trace: bool,
|
||||||
|
stack: Vec<Value>,
|
||||||
|
code: Chunk
|
||||||
|
}
|
||||||
|
|
||||||
|
enum VMError {
|
||||||
|
Compile,
|
||||||
|
Runtime
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VM {
|
||||||
|
fn push(&mut self, value: Value) {
|
||||||
|
self.stack.push(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pop(&mut self) -> Value {
|
||||||
|
self.stack.pop().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn interpret(&mut self, chunk: &Chunk) -> Result<(), VMError> {
|
||||||
|
for instr in chunk.code.iter().copied() {
|
||||||
|
if self.trace {
|
||||||
|
print!(" [");
|
||||||
|
for value in self.stack.iter() {
|
||||||
|
println!("{:?} | ", value);
|
||||||
|
}
|
||||||
|
println!("_ ]");
|
||||||
|
|
||||||
|
println!("{:?}", instr);
|
||||||
|
}
|
||||||
|
|
||||||
|
match instr {
|
||||||
|
Op::Return => {
|
||||||
|
print!("{:?}", self.pop());
|
||||||
|
return Ok(())
|
||||||
|
},
|
||||||
|
Op::Constant { offset } => {
|
||||||
|
self.push(self.code.constants[offset])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user