refactor(tazjin/rlox): Constructor for interpreter with globals

Change-Id: Id8242c22500c8e2781cc656d3faabb28d9bdf091
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2383
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2021-01-14 03:15:25 +03:00 committed by tazjin
parent 26544aa5f0
commit a03b509fb8
2 changed files with 21 additions and 3 deletions

View file

@ -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