Initial version of environment model
This commit is contained in:
31
src/environment.rs
Normal file
31
src/environment.rs
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user