Starting builtins

This commit is contained in:
greg 2017-12-20 22:56:24 -08:00
parent 6766791627
commit ffcc0ef379

View File

@ -66,13 +66,20 @@ impl EvaluatorState {
use self::Sexp::*; use self::Sexp::*;
println!("Evaling {:?}", expr); println!("Evaling {:?}", expr);
Ok(match expr { Ok(match expr {
SymbolAtom(ref sym) => match self.get_var(sym) { SymbolAtom(ref sym) => {
if is_builtin(sym) {
Builtin(sym.clone())
} else {
match self.get_var(sym) {
Some(ref sexp) => { Some(ref sexp) => {
let q: &Sexp = sexp; //WTF? if I delete this line, the copy doesn't work?? let q: &Sexp = sexp; //WTF? if I delete this line, the copy doesn't work??
q.clone() //TODO make this not involve a clone q.clone() //TODO make this not involve a clone
}, },
None => return Err(format!("Variable {} not bound", sym)), None => return Err(format!("Variable {} not bound", sym)),
}
}
}, },
expr @ Builtin(_) => expr,
expr @ FnLiteral { .. } => expr, expr @ FnLiteral { .. } => expr,
expr @ StringAtom(_) => expr, expr @ StringAtom(_) => expr,
expr @ NumberAtom(_) => expr, expr @ NumberAtom(_) => expr,
@ -210,6 +217,14 @@ enum Sexp {
FnLiteral { FnLiteral {
formal_params: Vec<String>, formal_params: Vec<String>,
body: Box<Sexp> body: Box<Sexp>
},
Builtin(String)
}
fn is_builtin(sym: &String) -> bool {
match &sym[..] {
"+" | "-" | "*" | "/" | "%" => true,
_ => false
} }
} }
@ -225,6 +240,7 @@ impl Sexp {
&Cons(ref car, ref cdr) => format!("({} . {})", car.print(), cdr.print()), &Cons(ref car, ref cdr) => format!("({} . {})", car.print(), cdr.print()),
&Nil => format!("()"), &Nil => format!("()"),
&FnLiteral { ref formal_params, .. } => format!("<lambda {:?}>", formal_params), &FnLiteral { ref formal_params, .. } => format!("<lambda {:?}>", formal_params),
&Builtin(ref sym) => format!("<builtin \"{}\">", sym),
} }
} }