about summary refs log tree commit diff
path: root/users/tazjin
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-17T08·59+0300
committertazjin <mail@tazj.in>2021-01-17T09·34+0000
commit06a6aa5dc08ffff2ba82af2254256ea60c6a5bec (patch)
tree255922aa98b3dda00a2f941159bb71400cac27aa /users/tazjin
parent9ea76fdf1ac08cd9b594ad37f6b963e78f818efc (diff)
refactor(tazjin/rlox): Call resolver from interpreter r/2118
This makes the interpreter API a bit cleaner and allows for tighter
integration between the two parts (e.g. for static globals, which are
unhandled in the resolver right now).

Change-Id: I363714dc2e13cefa7731b54326573e0b871295d6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2407
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin')
-rw-r--r--users/tazjin/rlox/src/interpreter.rs18
-rw-r--r--users/tazjin/rlox/src/interpreter/tests.rs6
-rw-r--r--users/tazjin/rlox/src/main.rs3
-rw-r--r--users/tazjin/rlox/src/resolver.rs5
4 files changed, 17 insertions, 15 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs
index 01ea217f8201..e7f87577bb7b 100644
--- a/users/tazjin/rlox/src/interpreter.rs
+++ b/users/tazjin/rlox/src/interpreter.rs
@@ -1,5 +1,6 @@
 use crate::errors::{Error, ErrorKind};
 use crate::parser::{self, Block, Expr, Literal, Statement};
+use crate::resolver;
 use crate::scanner::{self, TokenKind};
 use std::collections::HashMap;
 use std::rc::Rc;
@@ -44,7 +45,8 @@ impl Callable {
                     fn_env.define(param, value)?;
                 }
 
-                let result = lox.interpret_block(Some(Rc::new(RwLock::new(fn_env))), &func.body);
+                let result =
+                    lox.interpret_block_with_env(Some(Rc::new(RwLock::new(fn_env))), &func.body);
 
                 match result {
                     // extract returned values if applicable
@@ -107,7 +109,6 @@ impl Environment {
     }
 
     fn get(&self, ident: &str, line: usize, depth: usize) -> Result<Value, Error> {
-        println!("looking up {} at depth {}", ident, depth);
         if depth > 0 {
             match &self.enclosing {
                 None => {
@@ -221,7 +222,12 @@ impl Interpreter {
     }
 
     // Interpreter itself
-    pub fn interpret(&mut self, program: &Block) -> Result<Value, Error> {
+    pub fn interpret(&mut self, mut program: Block) -> Result<Value, Error> {
+        resolver::resolve(&mut program)?;
+        self.interpret_block(&program)
+    }
+
+    fn interpret_block(&mut self, program: &Block) -> Result<Value, Error> {
         let mut value = Value::Literal(Literal::Nil);
 
         for stmt in program {
@@ -241,7 +247,7 @@ impl Interpreter {
                 Value::Literal(Literal::String(output))
             }
             Statement::Var(var) => return self.interpret_var(var),
-            Statement::Block(block) => return self.interpret_block(None, block),
+            Statement::Block(block) => return self.interpret_block_with_env(None, block),
             Statement::If(if_stmt) => return self.interpret_if(if_stmt),
             Statement::While(while_stmt) => return self.interpret_while(while_stmt),
             Statement::Function(func) => return self.interpret_function(func.clone()),
@@ -269,7 +275,7 @@ impl Interpreter {
     /// Interpret the block in the supplied environment. If no
     /// environment is supplied, a new one is created using the
     /// current one as its parent.
-    fn interpret_block(
+    fn interpret_block_with_env(
         &mut self,
         env: Option<Rc<RwLock<Environment>>>,
         block: &parser::Block,
@@ -284,7 +290,7 @@ impl Interpreter {
         };
 
         let previous = std::mem::replace(&mut self.env, env);
-        let result = self.interpret(block);
+        let result = self.interpret_block(block);
 
         // Swap it back, discarding the child env.
         self.env = previous;
diff --git a/users/tazjin/rlox/src/interpreter/tests.rs b/users/tazjin/rlox/src/interpreter/tests.rs
index 4698583dfc11..34b1df34b0b6 100644
--- a/users/tazjin/rlox/src/interpreter/tests.rs
+++ b/users/tazjin/rlox/src/interpreter/tests.rs
@@ -1,15 +1,13 @@
 use super::*;
-use crate::resolver;
 
 /// Evaluate a code snippet, returning a 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 mut program = parser::parse(tokens).expect("could not parse code");
-    program = resolver::resolve(program).expect("could not resolve code");
+    let program = parser::parse(tokens).expect("could not parse code");
 
     Interpreter::create()
-        .interpret(&program)
+        .interpret(program)
         .expect("could not eval code")
 }
 
diff --git a/users/tazjin/rlox/src/main.rs b/users/tazjin/rlox/src/main.rs
index 24ebe503b692..76e4ae8ae78f 100644
--- a/users/tazjin/rlox/src/main.rs
+++ b/users/tazjin/rlox/src/main.rs
@@ -51,8 +51,7 @@ fn run(lox: &mut interpreter::Interpreter, code: &str) {
 
     let result = scanner::scan(&chars)
         .and_then(|tokens| parser::parse(tokens))
-        .and_then(|program| resolver::resolve(program).map_err(|e| vec![e]))
-        .and_then(|program| lox.interpret(&program).map_err(|e| vec![e]));
+        .and_then(|program| lox.interpret(program).map_err(|e| vec![e]));
 
     if let Err(errors) = result {
         report_errors(errors);
diff --git a/users/tazjin/rlox/src/resolver.rs b/users/tazjin/rlox/src/resolver.rs
index f5ef91b99a19..03ac3c8e6794 100644
--- a/users/tazjin/rlox/src/resolver.rs
+++ b/users/tazjin/rlox/src/resolver.rs
@@ -186,8 +186,7 @@ impl<'a> Resolver<'a> {
     }
 }
 
-pub fn resolve(mut block: parser::Block) -> Result<parser::Block, Error> {
+pub fn resolve(block: &mut parser::Block) -> Result<(), Error> {
     let mut resolver: Resolver = Default::default();
-    resolver.resolve(&mut block)?;
-    Ok(block)
+    resolver.resolve(block)
 }