Interpreter - Initial environment implementation
This commit is contained in:
parent
7f94906412
commit
8b44af08ba
1 changed files with 29 additions and 0 deletions
|
@ -0,0 +1,29 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use crate::{errors::InterpreterError, tokens::Token};
|
||||
|
||||
use super::Value;
|
||||
|
||||
/// The execution environment.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Environment {
|
||||
values: HashMap<String, Value>,
|
||||
}
|
||||
|
||||
impl Environment {
|
||||
/// Define a new variable.
|
||||
pub fn define(&mut self, name: String, value: Value) {
|
||||
self.values.insert(name, value);
|
||||
}
|
||||
|
||||
/// Get the value of a variable.
|
||||
pub fn get(&self, name: &Token) -> Result<Value, InterpreterError> {
|
||||
match self.values.get(&name.lexeme as &str) {
|
||||
None => Err(InterpreterError::new(
|
||||
name,
|
||||
&format!("undefined variable '{}'", name.lexeme),
|
||||
)),
|
||||
Some(value) => Ok(value.clone()),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue