Initial version of environment model

This commit is contained in:
2021-05-27 21:02:18 -04:00
parent 56f5426426
commit 794cad2219
4 changed files with 74 additions and 36 deletions

31
src/environment.rs Normal file
View File

@@ -0,0 +1,31 @@
use crate::parser::Datum;
use crate::parser::Datum::{Procedure, Symbol};
use crate::parser::make_symbol;
use crate::primitives;
type Mapping = (Datum, Datum);
type Frame = Vec<Mapping>;
pub type Env = Vec<Frame>;
pub fn get_global_environment() -> Env {
vec![vec![(make_symbol("+"), Procedure(primitives::add)),
(make_symbol("*"), Procedure(primitives::mul)),
(make_symbol("-"), Procedure(primitives::sub))]]
}
pub fn lookup_variable_value(exp: &Datum, env: &Env) -> Datum {
if let Symbol(symbol_name) = exp {
for frame in env {
for mapping in frame {
if let (Symbol(current_name), datum) = mapping {
if symbol_name == current_name {
return datum.clone();
}
}
}
}
} else {
panic!("LOOKUP-VARIABLE-VALUE -- not-a-symbol {:?}", exp)
}
panic!("LOOKUP-VARIABLE-VALUE -- unbound-variable-error {:?}", exp)
}