about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/treewalk/interpreter.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-18T00·21+0300
committertazjin <mail@tazj.in>2021-01-18T00·24+0000
commit1ff7a2686c2d7e405e597f9ac8a96189ec161d58 (patch)
tree1e6613d559744b9722329fa927dd78ce670581ca /users/tazjin/rlox/src/treewalk/interpreter.rs
parentd6d3c12efbcec61b3d868bc7d3f861fdb91835a5 (diff)
refactor(tazjin/rlox): Add Interpreter trait for switching impls r/2129
Change-Id: Iae28d64ce879014c5e5d7e145c536c1f16ad307d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2418
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/treewalk/interpreter.rs')
-rw-r--r--users/tazjin/rlox/src/treewalk/interpreter.rs46
1 files changed, 28 insertions, 18 deletions
diff --git a/users/tazjin/rlox/src/treewalk/interpreter.rs b/users/tazjin/rlox/src/treewalk/interpreter.rs
index a096716d9155..1263e6cb810b 100644
--- a/users/tazjin/rlox/src/treewalk/interpreter.rs
+++ b/users/tazjin/rlox/src/treewalk/interpreter.rs
@@ -1,7 +1,8 @@
 use crate::treewalk::errors::{Error, ErrorKind};
 use crate::treewalk::parser::{self, Block, Expr, Literal, Statement};
+use crate::treewalk::resolver;
 use crate::treewalk::scanner::{self, TokenKind};
-use crate::treewalk::treewalk::resolver;
+use crate::Lox;
 use std::collections::HashMap;
 use std::rc::Rc;
 use std::sync::RwLock;
@@ -174,10 +175,13 @@ pub struct Interpreter {
     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
     /// variable set.
-    pub fn create() -> Self {
+    fn create() -> Self {
         let mut globals = HashMap::new();
 
         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
     fn define_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> {
         self.env
@@ -221,21 +246,6 @@ impl Interpreter {
             .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
     /// environment is supplied, a new one is created using the
     /// current one as its parent.