2021-01-17 20:31:03 +03:00
|
|
|
//! Bytecode interpreter for Lox.
|
|
|
|
//!
|
|
|
|
//! https://craftinginterpreters.com/chunks-of-bytecode.html
|
|
|
|
|
2021-01-17 20:33:39 +03:00
|
|
|
mod chunk;
|
2021-01-17 23:03:41 +03:00
|
|
|
mod errors;
|
2021-01-17 20:33:39 +03:00
|
|
|
mod opcode;
|
|
|
|
mod value;
|
2021-01-17 23:03:41 +03:00
|
|
|
mod vm;
|
2021-01-17 20:33:39 +03:00
|
|
|
|
|
|
|
use chunk::Chunk;
|
2021-01-18 03:21:52 +03:00
|
|
|
pub struct Interpreter {}
|
2021-01-17 20:33:39 +03:00
|
|
|
|
2021-01-18 03:21:52 +03:00
|
|
|
impl crate::Lox for Interpreter {
|
|
|
|
type Error = errors::Error;
|
|
|
|
type Value = value::Value;
|
2021-01-17 20:33:39 +03:00
|
|
|
|
2021-01-18 03:21:52 +03:00
|
|
|
fn create() -> Self {
|
|
|
|
Interpreter {}
|
|
|
|
}
|
2021-01-18 00:08:30 +03:00
|
|
|
|
2021-02-26 22:29:33 +02:00
|
|
|
fn interpret(&mut self, code: String) -> Result<Self::Value, Vec<Self::Error>> {
|
2021-01-18 03:21:52 +03:00
|
|
|
let chunk: Chunk = Default::default();
|
|
|
|
vm::interpret(chunk).map_err(|e| vec![e])
|
|
|
|
}
|
2021-01-17 20:31:03 +03:00
|
|
|
}
|
2021-01-18 03:21:52 +03:00
|
|
|
|
|
|
|
// pub fn main() {
|
|
|
|
// let mut chunk: Chunk = Default::default();
|
|
|
|
// let constant = chunk.add_constant(1.2);
|
|
|
|
// chunk.add_op(OpCode::OpConstant(constant), 1);
|
|
|
|
// let constant = chunk.add_constant(2.0);
|
|
|
|
// chunk.add_op(OpCode::OpConstant(constant), 2);
|
|
|
|
// chunk.add_op(OpCode::OpAdd, 3);
|
|
|
|
// chunk.add_op(OpCode::OpReturn, 4);
|
|
|
|
// vm::interpret(chunk).expect("it should work");
|
|
|
|
// }
|