diff options
Diffstat (limited to 'users/tazjin/rlox/src/interpreter.rs')
-rw-r--r-- | users/tazjin/rlox/src/interpreter.rs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index 2ddbab7e1f29..5944bdfec8d9 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 |