[rlox] Add repl
This commit is contained in:
@@ -3,28 +3,43 @@ mod lc;
|
||||
mod vm;
|
||||
|
||||
use std::env;
|
||||
use std::io;
|
||||
|
||||
use bc::Chunk;
|
||||
use vm::VM;
|
||||
use crate::vm::VMError;
|
||||
|
||||
fn repl() {
|
||||
let mut buffer = String::new();
|
||||
|
||||
loop {
|
||||
match io::stdin().read_line(&mut buffer) {
|
||||
Ok(n) => {
|
||||
let mut chunk = Chunk::new();
|
||||
lc::compile(buffer.as_str(), &mut chunk);
|
||||
let mut vm = VM::new();
|
||||
let result = vm.run(&chunk);
|
||||
println!("{:?}", result);
|
||||
buffer.clear();
|
||||
},
|
||||
Err(error) => todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_file() {
|
||||
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let num_args = env::args().len();
|
||||
let mut chunk = bc::Chunk::new();
|
||||
|
||||
lc::compile("print(1+2*3)", &mut chunk);
|
||||
|
||||
if num_args == 1 {
|
||||
repl();
|
||||
repl()
|
||||
} else if num_args == 2 {
|
||||
run_file();
|
||||
run_file()
|
||||
} else {
|
||||
println!("Usage: rlox [path]");
|
||||
println!("Usage: rlox [path]")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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]))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user