Starting builtins

This commit is contained in:
greg 2017-12-20 22:56:24 -08:00
parent 1217f6e143
commit c101610cde

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) => {
Some(ref sexp) => { if is_builtin(sym) {
let q: &Sexp = sexp; //WTF? if I delete this line, the copy doesn't work?? Builtin(sym.clone())
q.clone() //TODO make this not involve a clone } else {
}, match self.get_var(sym) {
None => return Err(format!("Variable {} not bound", sym)), Some(ref sexp) => {
let q: &Sexp = sexp; //WTF? if I delete this line, the copy doesn't work??
q.clone() //TODO make this not involve a clone
},
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),
} }
} }