[rlox] Add repl

This commit is contained in:
ctsk
2023-10-08 12:25:21 +02:00
parent e7e7ef3b10
commit 45014fbe77
2 changed files with 26 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
use std::ops::Not;
use crate::bc::{Chunk, Op, TraceInfo, Value};
pub struct VM {
@@ -35,7 +36,7 @@ impl VM {
.ok_or_else(|| self.runtime_err("Attempt to pop of empty stack."))
}
pub fn run(&mut self, chunk: &Chunk) -> Result<(), VMError> {
pub fn run(&mut self, chunk: &Chunk) -> Result<Option<Value>, VMError> {
while self.pc < chunk.code.len() {
let instr = chunk.code[self.pc];
self.pc += 1;
@@ -79,7 +80,7 @@ impl VM {
}
}
Ok(())
Ok(self.stack.is_empty().not().then_some(self.stack[self.stack.len() - 1]))
}
}