Interpreter - Variable assignment
This commit is contained in:
parent
8d6191c7ee
commit
f4111577ca
2 changed files with 7 additions and 3 deletions
|
@ -28,10 +28,10 @@ impl Environment {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Assign a value to an existing variable.
|
/// Assign a value to an existing variable.
|
||||||
pub fn assign(&mut self, name: &Token, value: &Value) -> Result<(), InterpreterError> {
|
pub fn assign(&mut self, name: &Token, value: Value) -> InterpreterResult {
|
||||||
if self.values.contains_key(&name.lexeme as &str) {
|
if self.values.contains_key(&name.lexeme as &str) {
|
||||||
self.values.insert(name.lexeme.clone(), value.clone());
|
self.values.insert(name.lexeme.clone(), value);
|
||||||
Ok(())
|
Ok(Value::Nil)
|
||||||
} else {
|
} else {
|
||||||
Err(InterpreterError::new(
|
Err(InterpreterError::new(
|
||||||
name,
|
name,
|
||||||
|
|
|
@ -92,6 +92,10 @@ impl ast::StmtNode {
|
||||||
impl Interpretable for ast::ExprNode {
|
impl Interpretable for ast::ExprNode {
|
||||||
fn interprete(&self, environment: &mut Environment) -> InterpreterResult {
|
fn interprete(&self, environment: &mut Environment) -> InterpreterResult {
|
||||||
match self {
|
match self {
|
||||||
|
ast::ExprNode::Assignment{ name, value} => {
|
||||||
|
let value = value.interprete(environment)?;
|
||||||
|
environment.assign(name, value)
|
||||||
|
}
|
||||||
ast::ExprNode::Binary {
|
ast::ExprNode::Binary {
|
||||||
left,
|
left,
|
||||||
operator,
|
operator,
|
||||||
|
|
Loading…
Reference in a new issue