Builtins - +
This commit is contained in:
parent
85aabed344
commit
c20d75faf1
@ -11,13 +11,18 @@ pub struct EvaluatorState {
|
|||||||
|
|
||||||
impl EvaluatorState {
|
impl EvaluatorState {
|
||||||
fn new() -> EvaluatorState {
|
fn new() -> EvaluatorState {
|
||||||
use self::Sexp::*;
|
use self::Sexp::Builtin;
|
||||||
|
use self::BuiltinFn::*;
|
||||||
let mut default_map = HashMap::new();
|
let mut default_map = HashMap::new();
|
||||||
default_map.insert(format!("+"), Builtin("+".to_string()));
|
default_map.insert(format!("+"), Builtin(Plus));
|
||||||
default_map.insert(format!("-"), Builtin("-".to_string()));
|
default_map.insert(format!("-"), Builtin(Minus));
|
||||||
default_map.insert(format!("*"), Builtin("*".to_string()));
|
default_map.insert(format!("*"), Builtin(Mult));
|
||||||
default_map.insert(format!("/"), Builtin("/".to_string()));
|
default_map.insert(format!("/"), Builtin(Div));
|
||||||
default_map.insert(format!("%"), Builtin("%".to_string()));
|
default_map.insert(format!("%"), Builtin(Mod));
|
||||||
|
default_map.insert(format!(">"), Builtin(Greater));
|
||||||
|
default_map.insert(format!("<"), Builtin(Less));
|
||||||
|
default_map.insert(format!("<="), Builtin(LessThanOrEqual));
|
||||||
|
default_map.insert(format!(">="), Builtin(GreaterThanOrEqual));
|
||||||
|
|
||||||
EvaluatorState {
|
EvaluatorState {
|
||||||
binding_stack: vec![default_map],
|
binding_stack: vec![default_map],
|
||||||
@ -82,8 +87,8 @@ impl EvaluatorState {
|
|||||||
println!("Evaling {:?}", expr);
|
println!("Evaling {:?}", expr);
|
||||||
Ok(match expr {
|
Ok(match expr {
|
||||||
SymbolAtom(ref sym) => {
|
SymbolAtom(ref sym) => {
|
||||||
if is_builtin(sym) {
|
if let Some(op) = get_builtin(sym) {
|
||||||
Builtin(sym.clone())
|
Builtin(op)
|
||||||
} else {
|
} else {
|
||||||
match self.get_var(sym) {
|
match self.get_var(sym) {
|
||||||
Some(ref sexp) => {
|
Some(ref sexp) => {
|
||||||
@ -197,18 +202,28 @@ impl EvaluatorState {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply(&mut self, function: Sexp, _operands: Sexp) -> Result<Sexp, String> {
|
fn apply(&mut self, function: Sexp, operands: Sexp) -> Result<Sexp, String> {
|
||||||
use self::Sexp::*;
|
use self::Sexp::*;
|
||||||
match function {
|
match function {
|
||||||
FnLiteral { formal_params, body } => {
|
FnLiteral { formal_params, body } => {
|
||||||
Err(format!("unimplementd"))
|
Err(format!("unimplementd"))
|
||||||
},
|
},
|
||||||
Builtin(sym) => {
|
Builtin(builtin) => self.apply_builtin(builtin, operands),
|
||||||
Err(format!("unimplementd"))
|
|
||||||
},
|
|
||||||
_ => return Err(format!("Bad type to apply")),
|
_ => return Err(format!("Bad type to apply")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn apply_builtin(&mut self, op: BuiltinFn, operands: Sexp) -> Result<Sexp, String> {
|
||||||
|
use self::Sexp::*;
|
||||||
|
use self::BuiltinFn::*;
|
||||||
|
Ok(match op {
|
||||||
|
Plus => match operands {
|
||||||
|
Cons(box NumberAtom(l), box Cons(box NumberAtom(r), box Nil)) => NumberAtom(l + r),
|
||||||
|
_ => return Err(format!("Bad arguments for +")),
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Not implemented")),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read(input: &str) -> Result<Vec<Sexp>, String> {
|
fn read(input: &str) -> Result<Vec<Sexp>, String> {
|
||||||
@ -245,14 +260,21 @@ enum Sexp {
|
|||||||
formal_params: Vec<String>,
|
formal_params: Vec<String>,
|
||||||
body: Box<Sexp>
|
body: Box<Sexp>
|
||||||
},
|
},
|
||||||
Builtin(String)
|
Builtin(BuiltinFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_builtin(sym: &String) -> bool {
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
match &sym[..] {
|
enum BuiltinFn {
|
||||||
"+" | "-" | "*" | "/" | "%" => true,
|
Plus, Minus, Mult, Div, Mod, Greater, Less, GreaterThanOrEqual, LessThanOrEqual
|
||||||
_ => false
|
}
|
||||||
}
|
|
||||||
|
fn get_builtin(sym: &String) -> Option<BuiltinFn> {
|
||||||
|
use self::BuiltinFn::*;
|
||||||
|
Some(match &sym[..] {
|
||||||
|
"+" => Plus, "-" => Minus, "*" => Mult, "/" => Div, "%" => Mod,
|
||||||
|
">" => Greater, "<" => Less, ">=" => GreaterThanOrEqual, "<=" => LessThanOrEqual,
|
||||||
|
_ => return None
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sexp {
|
impl Sexp {
|
||||||
@ -267,7 +289,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),
|
&Builtin(ref sym) => format!("<builtin \"{:?}\">", sym),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user