diff --git a/src/interpreter/environment.rs b/src/interpreter/environment.rs index 02b552c..602ee1e 100644 --- a/src/interpreter/environment.rs +++ b/src/interpreter/environment.rs @@ -2,7 +2,7 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc}; use crate::{errors::InterpreterError, tokens::Token}; -use super::Value; +use super::{native_fn, CallableRef, Value}; /// A mutable reference to an environment. pub type EnvironmentRef = Rc>; @@ -20,10 +20,11 @@ pub struct Environment { impl Default for Environment { /// Create the default global environment. This includes native functions. fn default() -> Self { - let env = Self { + let mut env = Self { enclosing: None, values: HashMap::new(), }; + env.add_default_fun("clock", native_fn::clock()); env } } @@ -37,6 +38,12 @@ impl Environment { })) } + /// Add a default function to the environment. + fn add_default_fun(&mut self, name: &str, fun: CallableRef) { + let value = Some(Value::Callable(fun)); + self.values.insert(name.to_owned(), value); + } + /// Define a new variable. pub fn define(&mut self, name: &Token, value: Variable) -> Result<(), InterpreterError> { if self.values.contains_key(&name.lexeme as &str) {