Work on interpreter
This commit is contained in:
34
src/interpreter.rs
Normal file
34
src/interpreter.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use crate::parser::Datum;
|
||||
use crate::parser::Datum::*;
|
||||
|
||||
fn add(args: Vec<Datum>) -> Datum {
|
||||
Number(42)
|
||||
}
|
||||
|
||||
fn lookup_variable_value(exp: Datum) -> Datum {
|
||||
if let Symbol(string) = exp {
|
||||
match string.as_str() {
|
||||
"+" => Procedure(add),
|
||||
_ => panic!("unbound-variable {:?}", string)
|
||||
}
|
||||
} else {
|
||||
panic!("LOOKUP-VARIABLE-VALUE -- not-a-symbol {:?}", exp)
|
||||
}
|
||||
}
|
||||
|
||||
fn application(exp: Datum) -> Datum {
|
||||
if let List(vector) = exp {
|
||||
vector[0]
|
||||
} else {
|
||||
panic!("APPLICATION -- cannot-apply {:?}", exp)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interpret(exp: Datum) -> Datum {
|
||||
match exp {
|
||||
Boolean(_) | Number(_) => exp,
|
||||
Symbol(_) => lookup_variable_value(exp),
|
||||
List(_) => application(exp),
|
||||
_ => panic!("unknown-expression {:?}", exp)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user