From fe97398fd9d1e20ad4a953e27d080721b949865a Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 14 Jan 2021 17:44:53 +0300 Subject: refactor(tazjin/rlox): Thread lifetimes through interpreter In order to store a function in the interpreter's representation of a callable, the lifetimes used throughout rlox need to be threaded through properly. This is currently not optimal, for two reasons: * following the design of the book's scanner, the source code slice needs to still be available at runtime. Rust makes this explicit, but it seems unnecessary. * the interpreter's lifetime is now bounded to be smaller than the source's, which means that the REPL no longer persists state between evaluations Both of these can be fixed eventually by diverging the scanner from the book slightly, but right now that's not my priority. Change-Id: Id0bf694541ff59795cfdea3c64a965384a49bfe2 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2391 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/interpreter.rs | 73 ++++++++++++++++-------------- users/tazjin/rlox/src/interpreter/tests.rs | 25 ++++++---- users/tazjin/rlox/src/main.rs | 9 ++-- 3 files changed, 59 insertions(+), 48 deletions(-) (limited to 'users/tazjin/rlox') diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index 6aab389369ea..bd907542fb5e 100644 --- a/users/tazjin/rlox/src/interpreter.rs +++ b/users/tazjin/rlox/src/interpreter.rs @@ -16,32 +16,35 @@ mod tests; // Representation of all callables, including builtins & user-defined // functions. #[derive(Clone, Debug)] -pub enum Callable { +pub enum Callable<'a> { Builtin(&'static dyn builtins::Builtin), + Function(Rc>), } -impl Callable { +impl<'a> Callable<'a> { fn arity(&self) -> usize { match self { Callable::Builtin(builtin) => builtin.arity(), + _ => unimplemented!(), } } - fn call(&self, args: Vec) -> Result { + fn call(&self, args: Vec) -> Result, Error> { match self { Callable::Builtin(builtin) => builtin.call(args), + _ => unimplemented!(), } } } // Representation of an in-language value. #[derive(Clone, Debug)] -pub enum Value { +pub enum Value<'a> { Literal(Literal), - Callable(Callable), + Callable(Callable<'a>), } -impl PartialEq for Value { +impl<'a> PartialEq for Value<'a> { fn eq(&self, other: &Self) -> bool { match (self, other) { (Value::Literal(lhs), Value::Literal(rhs)) => lhs == rhs, @@ -51,13 +54,13 @@ impl PartialEq for Value { } } -impl From for Value { - fn from(lit: Literal) -> Value { +impl<'a> From for Value<'a> { + fn from(lit: Literal) -> Value<'a> { Value::Literal(lit) } } -impl Value { +impl<'a> Value<'a> { fn expect_literal(self) -> Result { match self { Value::Literal(lit) => Ok(lit), @@ -67,19 +70,19 @@ impl Value { } #[derive(Debug, Default)] -struct Environment { - enclosing: Option>>, - values: HashMap, +struct Environment<'a> { + enclosing: Option>>>, + values: HashMap>, } -impl Environment { - fn define(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { +impl<'a> Environment<'a> { + fn define(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { let ident = identifier_str(name)?; self.values.insert(ident.into(), value); Ok(()) } - fn get(&self, name: &parser::Variable) -> Result { + fn get(&self, name: &parser::Variable) -> Result, Error> { let ident = identifier_str(&name.0)?; self.values @@ -98,7 +101,7 @@ impl Environment { }) } - fn assign(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { + fn assign(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { let ident = identifier_str(name)?; match self.values.get_mut(ident) { @@ -132,11 +135,11 @@ fn identifier_str<'a>(name: &'a scanner::Token) -> Result<&'a str, Error> { } #[derive(Debug)] -pub struct Interpreter { - env: Rc>, +pub struct Interpreter<'a> { + env: Rc>>, } -impl Interpreter { +impl<'a> Interpreter<'a> { /// Create a new interpreter and configure the initial global /// variable set. pub fn create() -> Self { @@ -156,28 +159,28 @@ impl Interpreter { } // 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<'a>) -> Result<(), Error> { self.env .write() .expect("environment lock is poisoned") .define(name, value) } - fn assign_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { + fn assign_var(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { self.env .write() .expect("environment lock is poisoned") .assign(name, value) } - fn get_var(&mut self, var: &parser::Variable) -> Result { + fn get_var(&mut self, var: &parser::Variable) -> Result, Error> { self.env .read() .expect("environment lock is poisoned") .get(var) } - fn set_enclosing(&mut self, parent: Rc>) { + fn set_enclosing(&mut self, parent: Rc>>) { self.env .write() .expect("environment lock is poisoned") @@ -185,7 +188,7 @@ impl Interpreter { } // Interpreter itself - pub fn interpret<'a>(&mut self, program: &Block<'a>) -> Result { + pub fn interpret(&mut self, program: &Block<'a>) -> Result, Error> { let mut value = Value::Literal(Literal::Nil); for stmt in program { @@ -195,7 +198,7 @@ impl Interpreter { Ok(value) } - fn interpret_stmt<'a>(&mut self, stmt: &Statement<'a>) -> Result { + fn interpret_stmt(&mut self, stmt: &Statement<'a>) -> Result, Error> { let value = match stmt { Statement::Expr(expr) => self.eval(expr)?, Statement::Print(expr) => { @@ -214,7 +217,7 @@ impl Interpreter { Ok(value) } - fn interpret_var<'a>(&mut self, var: &parser::Var<'a>) -> Result { + fn interpret_var(&mut self, var: &parser::Var<'a>) -> Result, Error> { let init = var.initialiser.as_ref().ok_or_else(|| Error { line: var.name.line, kind: ErrorKind::InternalError("missing variable initialiser".into()), @@ -224,7 +227,7 @@ impl Interpreter { Ok(value) } - fn interpret_block<'a>(&mut self, block: &parser::Block<'a>) -> Result { + fn interpret_block(&mut self, block: &parser::Block<'a>) -> Result, Error> { // Initialise a new environment and point it at the parent // (this is a bit tedious because we need to wrap it in and // out of the Rc). @@ -241,7 +244,7 @@ impl Interpreter { return result; } - fn interpret_if<'a>(&mut self, if_stmt: &parser::If<'a>) -> Result { + fn interpret_if(&mut self, if_stmt: &parser::If<'a>) -> Result, Error> { let condition = self.eval(&if_stmt.condition)?; if eval_truthy(&condition) { @@ -253,7 +256,7 @@ impl Interpreter { } } - fn interpret_while<'a>(&mut self, stmt: &parser::While<'a>) -> Result { + fn interpret_while(&mut self, stmt: &parser::While<'a>) -> Result, Error> { let mut value = Value::Literal(Literal::Nil); while eval_truthy(&self.eval(&stmt.condition)?) { value = self.interpret_stmt(&stmt.body)?; @@ -262,7 +265,7 @@ impl Interpreter { Ok(value) } - fn eval<'a>(&mut self, expr: &Expr<'a>) -> Result { + fn eval(&mut self, expr: &Expr<'a>) -> Result, Error> { match expr { Expr::Assign(assign) => self.eval_assign(assign), Expr::Literal(lit) => Ok(lit.clone().into()), @@ -275,7 +278,7 @@ impl Interpreter { } } - fn eval_unary<'a>(&mut self, expr: &parser::Unary<'a>) -> Result { + fn eval_unary(&mut self, expr: &parser::Unary<'a>) -> Result, Error> { let right = self.eval(&*expr.right)?; match (&expr.operator.kind, right) { @@ -294,7 +297,7 @@ impl Interpreter { } } - fn eval_binary<'a>(&mut self, expr: &parser::Binary<'a>) -> Result { + fn eval_binary(&mut self, expr: &parser::Binary<'a>) -> Result, Error> { let left = self.eval(&*expr.left)?.expect_literal()?; let right = self.eval(&*expr.right)?.expect_literal()?; @@ -338,13 +341,13 @@ impl Interpreter { Ok(result.into()) } - fn eval_assign<'a>(&mut self, assign: &parser::Assign<'a>) -> Result { + fn eval_assign(&mut self, assign: &parser::Assign<'a>) -> Result, Error> { let value = self.eval(&assign.value)?; self.assign_var(&assign.name, value.clone())?; Ok(value) } - fn eval_logical<'a>(&mut self, logical: &parser::Logical<'a>) -> Result { + fn eval_logical(&mut self, logical: &parser::Logical<'a>) -> Result, Error> { let left = eval_truthy(&self.eval(&logical.left)?); let right = eval_truthy(&self.eval(&logical.right)?); @@ -358,7 +361,7 @@ impl Interpreter { } } - fn eval_call<'a>(&mut self, call: &parser::Call<'a>) -> Result { + fn eval_call(&mut self, call: &parser::Call<'a>) -> Result, Error> { let callable = match self.eval(&call.callee)? { Value::Callable(c) => c, Value::Literal(v) => { diff --git a/users/tazjin/rlox/src/interpreter/tests.rs b/users/tazjin/rlox/src/interpreter/tests.rs index 3aaea707d64b..93a025c5e832 100644 --- a/users/tazjin/rlox/src/interpreter/tests.rs +++ b/users/tazjin/rlox/src/interpreter/tests.rs @@ -1,8 +1,11 @@ use super::*; +fn code(code: &str) -> Vec { + code.chars().collect() +} + /// Evaluate a code snippet, returning a value. -fn parse_eval(code: &str) -> Value { - let chars: Vec = code.chars().collect(); +fn parse_eval<'a>(chars: &'a [char]) -> Value<'a> { let tokens = scanner::scan(&chars).expect("could not scan code"); let program = parser::parse(tokens).expect("could not parse code"); Interpreter::create() @@ -12,7 +15,7 @@ fn parse_eval(code: &str) -> Value { #[test] fn test_if() { - let result = parse_eval( + let code = code( r#" if (42 > 23) "pass"; @@ -21,12 +24,15 @@ else "#, ); - assert_eq!(Value::Literal(Literal::String("pass".into())), result); + assert_eq!( + Value::Literal(Literal::String("pass".into())), + parse_eval(&code) + ); } #[test] fn test_scope() { - let result = parse_eval( + let code = code( r#" var result = ""; @@ -48,16 +54,19 @@ var c = "global c"; assert_eq!( Value::Literal(Literal::String("inner a, outer b, global c".into())), - result + parse_eval(&code), ); } #[test] fn test_binary_operators() { - assert_eq!(Value::Literal(Literal::Number(42.0)), parse_eval("40 + 2;")); + assert_eq!( + Value::Literal(Literal::Number(42.0)), + parse_eval(&code("40 + 2;")) + ); assert_eq!( Value::Literal(Literal::String("foobar".into())), - parse_eval("\"foo\" + \"bar\";") + parse_eval(&code("\"foo\" + \"bar\";")) ); } diff --git a/users/tazjin/rlox/src/main.rs b/users/tazjin/rlox/src/main.rs index 8970349bfa69..222eb4cd534f 100644 --- a/users/tazjin/rlox/src/main.rs +++ b/users/tazjin/rlox/src/main.rs @@ -25,14 +25,12 @@ fn main() { // 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 = interpreter::Interpreter::create(); - run(&mut lox, &contents); + run(&contents); } // Evaluate Lox code interactively in a shitty REPL. fn run_prompt() { let mut line = String::new(); - let mut lox = interpreter::Interpreter::create(); loop { print!("> "); @@ -40,13 +38,14 @@ fn run_prompt() { io::stdin() .read_line(&mut line) .expect("failed to read user input"); - run(&mut lox, &line); + run(&line); line.clear(); } } -fn run(lox: &mut interpreter::Interpreter, code: &str) { +fn run(code: &str) { let chars: Vec = code.chars().collect(); + let mut lox = interpreter::Interpreter::create(); match scanner::scan(&chars) { Ok(tokens) => match parser::parse(tokens) { -- cgit 1.4.1