parent
934cebc598
commit
a5533ea58b
@ -1,13 +1,22 @@
|
||||
use chunk::{Chunk, OpCode};
|
||||
use vm::VM;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
mod chunk;
|
||||
mod value;
|
||||
mod vm;
|
||||
|
||||
fn main() {
|
||||
fn main() -> Result<()> {
|
||||
let mut chunk = Chunk::default();
|
||||
|
||||
chunk.push_constant(1.2, 123);
|
||||
chunk.push(OpCode::Return, 123);
|
||||
|
||||
chunk.disassemble("test chunk");
|
||||
|
||||
let mut vm = VM::new(chunk);
|
||||
vm.interpret()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -0,0 +1,37 @@
|
||||
use anyhow::Result;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::chunk::{Chunk, OpCode};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum InterpretError {
|
||||
#[error("compile error")]
|
||||
Compile,
|
||||
#[error("runtime error")]
|
||||
Runtime,
|
||||
}
|
||||
|
||||
pub struct VM {
|
||||
chunk: Chunk,
|
||||
ip: usize,
|
||||
}
|
||||
|
||||
impl VM {
|
||||
pub fn new(chunk: Chunk) -> Self {
|
||||
Self { chunk, ip: 0 }
|
||||
}
|
||||
|
||||
pub fn interpret(&mut self) -> Result<()> {
|
||||
loop {
|
||||
match self.chunk.code[self.ip] {
|
||||
OpCode::Constant(constant) => {
|
||||
let value = self.chunk.constants[constant];
|
||||
println!("{}", value);
|
||||
},
|
||||
OpCode::Return => return Ok(()),
|
||||
}
|
||||
|
||||
self.ip += 1;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue