refactor(tazjin/rlox): Add Interpreter trait for switching impls

Change-Id: Iae28d64ce879014c5e5d7e145c536c1f16ad307d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2418
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2021-01-18 03:21:52 +03:00 committed by tazjin
parent d6d3c12efb
commit 1ff7a2686c
8 changed files with 125 additions and 102 deletions

View file

@ -1,3 +1,5 @@
use std::fmt;
#[derive(Debug)] #[derive(Debug)]
pub enum ErrorKind { pub enum ErrorKind {
// CompileError, // CompileError,
@ -10,4 +12,10 @@ pub struct Error {
pub kind: ErrorKind, pub kind: ErrorKind,
} }
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[line NYI] Error: {:?}", self.kind)
}
}
pub type LoxResult<T> = Result<T, Error>; pub type LoxResult<T> = Result<T, Error>;

View file

@ -9,19 +9,29 @@ mod value;
mod vm; mod vm;
use chunk::Chunk; use chunk::Chunk;
use opcode::OpCode; pub struct Interpreter {}
pub fn main() { impl crate::Lox for Interpreter {
let mut chunk: Chunk = Default::default(); type Error = errors::Error;
type Value = value::Value;
let constant = chunk.add_constant(1.2); fn create() -> Self {
chunk.add_op(OpCode::OpConstant(constant), 1); Interpreter {}
}
let constant = chunk.add_constant(2.0); fn interpret(&mut self, _: String) -> Result<Self::Value, Vec<Self::Error>> {
chunk.add_op(OpCode::OpConstant(constant), 2); let chunk: Chunk = Default::default();
vm::interpret(chunk).map_err(|e| vec![e])
chunk.add_op(OpCode::OpAdd, 3); }
chunk.add_op(OpCode::OpReturn, 4);
vm::interpret(chunk).expect("it should work");
} }
// 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");
// }

View file

@ -32,7 +32,7 @@ macro_rules! binary_op {
} }
impl VM { impl VM {
fn run(&mut self) -> LoxResult<()> { fn run(&mut self) -> LoxResult<Value> {
loop { loop {
let op = &self.chunk.code[self.ip]; let op = &self.chunk.code[self.ip];
@ -42,10 +42,7 @@ impl VM {
self.ip += 1; self.ip += 1;
match op { match op {
OpCode::OpReturn => { OpCode::OpReturn => return Ok(self.pop()),
println!("{:?}", self.pop());
return Ok(());
}
OpCode::OpConstant(idx) => { OpCode::OpConstant(idx) => {
let c = *self.chunk.constant(*idx); let c = *self.chunk.constant(*idx);
@ -66,7 +63,7 @@ impl VM {
} }
} }
pub fn interpret(chunk: chunk::Chunk) -> LoxResult<()> { pub fn interpret(chunk: chunk::Chunk) -> LoxResult<Value> {
let mut vm = VM { let mut vm = VM {
chunk, chunk,
ip: 0, ip: 0,

View file

@ -7,9 +7,64 @@ use std::process;
mod bytecode; mod bytecode;
mod treewalk; mod treewalk;
/// Trait for making the different interpreters callable in the same
/// way.
pub trait Lox {
type Value;
type Error: std::fmt::Display;
fn create() -> Self;
fn interpret(&mut self, source: String) -> Result<Self::Value, Vec<Self::Error>>;
}
fn main() { fn main() {
let mut args = env::args();
if args.len() > 2 {
println!("Usage: rlox [script]");
process::exit(1);
}
match env::var("LOX_INTERPRETER").as_ref().map(String::as_str) { match env::var("LOX_INTERPRETER").as_ref().map(String::as_str) {
Ok("treewalk") => treewalk::main(), Ok("treewalk") => pick::<treewalk::interpreter::Interpreter>(args.nth(1)),
_ => bytecode::main(), _ => pick::<bytecode::Interpreter>(args.nth(1)),
}
}
fn pick<I: Lox>(file_arg: Option<String>) {
if let Some(file) = file_arg {
run_file::<I>(&file);
} else {
run_prompt::<I>();
}
}
// Run Lox code from a file and print results to stdout
fn run_file<I: Lox>(file: &str) {
let contents = fs::read_to_string(file).expect("failed to read the input file");
let mut lox = I::create();
run(&mut lox, contents);
}
// Evaluate Lox code interactively in a shitty REPL.
fn run_prompt<I: Lox>() {
let mut line = String::new();
let mut lox = I::create();
loop {
print!("> ");
io::stdout().flush().unwrap();
io::stdin()
.read_line(&mut line)
.expect("failed to read user input");
run(&mut lox, std::mem::take(&mut line));
line.clear();
}
}
fn run<I: Lox>(lox: &mut I, code: String) {
if let Err(errors) = lox.interpret(code) {
for error in errors {
eprintln!("{}", error);
}
} }
} }

View file

@ -1,4 +1,5 @@
use crate::treewalk::interpreter::Value; use crate::treewalk::interpreter::Value;
use std::fmt;
#[derive(Debug)] #[derive(Debug)]
pub enum ErrorKind { pub enum ErrorKind {
@ -33,6 +34,8 @@ pub struct Error {
pub kind: ErrorKind, pub kind: ErrorKind,
} }
pub fn report(err: &Error) { impl fmt::Display for Error {
eprintln!("[line {}] Error: {:?}", err.line, err.kind); fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[line {}] Error: {:?}", self.line, self.kind)
}
} }

View file

@ -1,7 +1,8 @@
use crate::treewalk::errors::{Error, ErrorKind}; use crate::treewalk::errors::{Error, ErrorKind};
use crate::treewalk::parser::{self, Block, Expr, Literal, Statement}; use crate::treewalk::parser::{self, Block, Expr, Literal, Statement};
use crate::treewalk::resolver;
use crate::treewalk::scanner::{self, TokenKind}; use crate::treewalk::scanner::{self, TokenKind};
use crate::treewalk::treewalk::resolver; use crate::Lox;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use std::sync::RwLock; use std::sync::RwLock;
@ -174,10 +175,13 @@ pub struct Interpreter {
env: Rc<RwLock<Environment>>, env: Rc<RwLock<Environment>>,
} }
impl Interpreter { impl Lox for Interpreter {
type Value = Value;
type Error = Error;
/// Create a new interpreter and configure the initial global /// Create a new interpreter and configure the initial global
/// variable set. /// variable set.
pub fn create() -> Self { fn create() -> Self {
let mut globals = HashMap::new(); let mut globals = HashMap::new();
globals.insert( globals.insert(
@ -193,6 +197,27 @@ impl Interpreter {
} }
} }
fn interpret(&mut self, code: String) -> Result<Value, Vec<Error>> {
let chars: Vec<char> = code.chars().collect();
let mut program = scanner::scan(&chars).and_then(|tokens| parser::parse(tokens))?;
let globals = self
.env
.read()
.expect("static globals lock poisoned")
.values
.keys()
.map(Clone::clone)
.collect::<Vec<String>>();
resolver::resolve(&globals, &mut program).map_err(|e| vec![e])?;
self.interpret_block_with_env(None, &program)
.map_err(|e| vec![e])
}
}
impl Interpreter {
// Environment modification helpers // Environment modification helpers
fn define_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { fn define_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> {
self.env self.env
@ -221,21 +246,6 @@ impl Interpreter {
.get(ident, var.name.line, depth) .get(ident, var.name.line, depth)
} }
// Interpreter itself
pub fn interpret(&mut self, mut program: Block) -> Result<Value, Error> {
let globals = self
.env
.read()
.expect("static globals lock poisoned")
.values
.keys()
.map(Clone::clone)
.collect::<Vec<String>>();
resolver::resolve(&globals, &mut program)?;
self.interpret_block_with_env(None, &program)
}
/// Interpret the block in the supplied environment. If no /// Interpret the block in the supplied environment. If no
/// environment is supplied, a new one is created using the /// environment is supplied, a new one is created using the
/// current one as its parent. /// current one as its parent.

View file

@ -2,13 +2,9 @@ use super::*;
/// Evaluate a code snippet, returning a value. /// Evaluate a code snippet, returning a value.
fn parse_eval(code: &str) -> Value { fn parse_eval(code: &str) -> Value {
let chars: Vec<char> = code.chars().collect();
let tokens = scanner::scan(&chars).expect("could not scan code");
let program = parser::parse(tokens).expect("could not parse code");
Interpreter::create() Interpreter::create()
.interpret(program) .interpret(code.into())
.expect("could not eval code") .expect("could not interpret code")
} }
#[test] #[test]

View file

@ -1,61 +1,5 @@
use crate::*;
mod errors; mod errors;
pub mod interpreter; pub mod interpreter;
mod parser; mod parser;
mod resolver; mod resolver;
mod scanner; mod scanner;
pub fn main() {
let mut args = env::args();
if args.len() > 2 {
println!("Usage: rlox [script]");
process::exit(1);
} else if let Some(file) = args.nth(1) {
run_file(&file);
} else {
run_prompt();
}
}
// Run Lox code from a file and print results to stdout
fn run_file(file: &str) {
let contents = fs::read_to_string(file).expect("failed to read the input file");
let mut lox = treewalk::interpreter::Interpreter::create();
run(&mut lox, &contents);
}
// Evaluate Lox code interactively in a shitty REPL.
fn run_prompt() {
let mut line = String::new();
let mut lox = treewalk::interpreter::Interpreter::create();
loop {
print!("> ");
io::stdout().flush().unwrap();
io::stdin()
.read_line(&mut line)
.expect("failed to read user input");
run(&mut lox, &line);
line.clear();
}
}
fn run(lox: &mut treewalk::interpreter::Interpreter, code: &str) {
let chars: Vec<char> = code.chars().collect();
let result = scanner::scan(&chars)
.and_then(|tokens| parser::parse(tokens))
.and_then(|program| lox.interpret(program).map_err(|e| vec![e]));
if let Err(errors) = result {
report_errors(errors);
}
}
fn report_errors(errors: Vec<errors::Error>) {
for error in errors {
errors::report(&error);
}
}