diff --git a/src/interpreter.rs b/src/interpreter.rs new file mode 100644 index 0000000..237a694 --- /dev/null +++ b/src/interpreter.rs @@ -0,0 +1,34 @@ +use crate::parser::Datum; +use crate::parser::Datum::*; + +fn add(args: Vec) -> 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) + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index dce5c80..1bb50aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,13 @@ mod lexer; mod parser; +mod interpreter; + fn main() { - let scm_code = "(+ a (* 32 b) c #t #f)"; + // let scm_code = "(+ a (* 32 b) c #t #f)"; + let scm_code = "+"; let tokens = lexer::read(scm_code); let datum = parser::parse(tokens); - println!("{:?}", datum); + let result = interpreter::interpret(datum); + println!("{:?}", result); } diff --git a/src/parser.rs b/src/parser.rs index a73c59b..437b47c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,12 +2,14 @@ use crate::lexer::Token; use crate::lexer::Tokens; + #[derive(Debug)] pub enum Datum { Boolean(bool), Number(i64), Symbol(String), List(Vec), + Procedure(fn(Vec) -> Datum), } pub fn parse(tokens: Tokens) -> Datum {