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-02-27 14:18:05 +02:00
|
|
|
mod compiler;
|
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-27 15:45:44 +02:00
|
|
|
fn interpret(
|
|
|
|
&mut self,
|
|
|
|
code: String,
|
|
|
|
) -> Result<Self::Value, Vec<Self::Error>> {
|
2021-02-27 14:18:05 +02:00
|
|
|
let chunk = compiler::compile(&code)?;
|
2021-01-18 03:21:52 +03:00
|
|
|
vm::interpret(chunk).map_err(|e| vec![e])
|
|
|
|
}
|
2021-01-17 20:31:03 +03:00
|
|
|
}
|