Starting builtins
This commit is contained in:
parent
1217f6e143
commit
c101610cde
@ -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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user