about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/interpreter.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-14T00·15+0300
committertazjin <mail@tazj.in>2021-01-14T00·31+0000
commita03b509fb85ee4fb3b397b3e279ca77d625bc5f6 (patch)
tree261d6515c9caa40ac42677ba6a805a465654cfb1 /users/tazjin/rlox/src/interpreter.rs
parent26544aa5f05021ed95eca77966738a84d8be902e (diff)
refactor(tazjin/rlox): Constructor for interpreter with globals r/2097
Change-Id: Id8242c22500c8e2781cc656d3faabb28d9bdf091
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2383
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/interpreter.rs')
-rw-r--r--users/tazjin/rlox/src/interpreter.rs20
1 files changed, 19 insertions, 1 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs
index 2ddbab7e1f..5944bdfec8 100644
--- a/users/tazjin/rlox/src/interpreter.rs
+++ b/users/tazjin/rlox/src/interpreter.rs
@@ -118,12 +118,30 @@ fn identifier_str<'a>(name: &'a scanner::Token) -> Result<&'a str, Error> {
     }
 }
 
-#[derive(Debug, Default)]
+#[derive(Debug)]
 pub struct Interpreter {
     env: Rc<RwLock<Environment>>,
 }
 
 impl Interpreter {
+    /// Create a new interpreter and configure the initial global
+    /// variable set.
+    pub fn create() -> Self {
+        let mut globals = HashMap::new();
+
+        globals.insert(
+            "clock".into(),
+            Value::Callable(Callable::Builtin(&builtins::Clock {})),
+        );
+
+        Interpreter {
+            env: Rc::new(RwLock::new(Environment {
+                enclosing: None,
+                values: globals,
+            })),
+        }
+    }
+
     // Environment modification helpers
     fn define_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> {
         self.env