|
|
@ -34,14 +34,18 @@ impl VM {
|
|
|
|
|
|
|
|
|
|
|
|
pub fn interpret(&mut self) -> Result<()> {
|
|
|
|
pub fn interpret(&mut self) -> Result<()> {
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
let op_code = &self.chunk.code[self.ip];
|
|
|
|
debug!("{:?}", &self.stack[0..self.stack_top]);
|
|
|
|
debug!("{}", DisassembledInstruction::new(self.ip, &self.chunk,));
|
|
|
|
debug!("{}", DisassembledInstruction::new(self.ip, &self.chunk,));
|
|
|
|
match op_code {
|
|
|
|
|
|
|
|
|
|
|
|
match &self.chunk.code[self.ip] {
|
|
|
|
OpCode::Constant(constant) => {
|
|
|
|
OpCode::Constant(constant) => {
|
|
|
|
let value = self.chunk.constants[*constant];
|
|
|
|
let value = self.chunk.constants[*constant];
|
|
|
|
println!("{}", value);
|
|
|
|
self.push(value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
OpCode::Return => {
|
|
|
|
|
|
|
|
println!("{}", self.pop());
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OpCode::Return => return Ok(()),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.ip += 1;
|
|
|
|
self.ip += 1;
|
|
|
|