diff options
author | Dominick Allen <dominick.allen1989@gmail.com> | 2020-06-20 13:38:03 -0500 |
---|---|---|
committer | Dominick Allen <dominick.allen1989@gmail.com> | 2020-06-20 13:38:03 -0500 |
commit | 36e1bf722a3d366cea20ab7315d63d588d23dc48 (patch) | |
tree | 84ef440e398978bfd4467a99d375d7710f016031 /src/lib/eval/mod.rs |
Working on a simple LISP/scheme interpreter.
Diffstat (limited to 'src/lib/eval/mod.rs')
-rw-r--r-- | src/lib/eval/mod.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/lib/eval/mod.rs b/src/lib/eval/mod.rs new file mode 100644 index 0000000..b582397 --- /dev/null +++ b/src/lib/eval/mod.rs @@ -0,0 +1,54 @@ +use std::collections::HashMap; +use super::types::Op; +use super::types::Op::*; +use super::types::SEXP; +use super::types::SEXP::*; +use super::types::Type::*; +use super::types::Number; + +pub mod arith; + +pub type Env = HashMap<String, SEXP>; + +pub fn eval(expr: &SEXP, env: &mut Env) -> Result<SEXP, String> { + match expr { + Atom(ref x) => Ok(Atom((*x).clone())), + Sexpr(ref s) => seval(s, env) + } +} + +fn seval(sexpr: &[SEXP], env: &mut Env) -> Result<SEXP, String> { + if sexpr.is_empty() { + return Err("Empty S Expression".to_string()) + } + + let op = match &sexpr[0] { + Atom(Operator(x)) => *x, + Atom(Symbol(_s)) => return Err("Not yet implemented".to_string()), + x => return Err(format!("{:?} is not a procedure", x)) + }; + + op_eval(op, &sexpr[1..], env) +} + +fn op_eval(op: Op, expr: &[SEXP], env: &mut Env) -> Result<SEXP, String> { + match op { + Add => op_add(expr, env), + //Define => define(expr, env), + _ => Err("Not yet implemented".to_string()) + } +} + +fn op_add(expr: &[SEXP], env: &mut Env) -> Result<SEXP, String> { + let mut acc = Number(Number::Int(0)); + let mut i = 0; + while i < expr.len() { + let b = match eval(&expr[i], env)? { + Atom(x) => x, + Sexpr(_) => panic!("This should have evaluated to an atom") + }; + acc = (acc + b)?; + i += 1; + } + Ok(SEXP::Atom(acc)) +} |