diff options
Diffstat (limited to 'users/tazjin')
-rw-r--r-- | users/tazjin/rlox/src/interpreter.rs | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index 0855068b6387..8d968a1f20dd 100644 --- a/users/tazjin/rlox/src/interpreter.rs +++ b/users/tazjin/rlox/src/interpreter.rs @@ -28,6 +28,19 @@ impl Environment { kind: ErrorKind::UndefinedVariable(ident.into()), }) } + + fn assign(&mut self, name: &scanner::Token, value: Literal) -> Result<(), Error> { + let ident = identifier_str(name)?; + let target = self.values + .get_mut(ident) + .ok_or_else(|| Error { + line: name.line, + kind: ErrorKind::UndefinedVariable(ident.into()), + })?; + + *target = value; + Ok(()) + } } fn identifier_str<'a>(name: &'a scanner::Token) -> Result<&'a str, Error> { @@ -156,7 +169,7 @@ impl Interpreter { fn eval_assign<'a>(&mut self, assign: &parser::Assign<'a>) -> Result<Literal, Error> { let value = self.eval(&assign.value)?; - self.globals.define(&assign.name, value.clone())?; + self.globals.assign(&assign.name, value.clone())?; Ok(value) } } |