Some more structure in evaluator

This commit is contained in:
greg 2017-10-23 01:54:02 -07:00
parent e054c4b27f
commit 98e1a5235a
1 changed files with 22 additions and 7 deletions

View File

@ -24,6 +24,7 @@ enum FullyEvaluatedExpr {
Float(f64), Float(f64),
Str(String), Str(String),
Bool(bool), Bool(bool),
FuncLit(Rc<String>),
Custom { Custom {
string_rep: Rc<String>, string_rep: Rc<String>,
}, },
@ -68,6 +69,7 @@ impl ReplState {
Bool(b) => Some(format!("{}", b)), Bool(b) => Some(format!("{}", b)),
Custom { string_rep } => Some(format!("{}", string_rep)), Custom { string_rep } => Some(format!("{}", string_rep)),
Tuple(_items) => Some(format!("(tuple to be defined later)")), Tuple(_items) => Some(format!("(tuple to be defined later)")),
FuncLit(name) => Some(format!("<function {}>", name)),
} }
}) })
}, },
@ -91,8 +93,8 @@ impl ReplState {
match variant { match variant {
&UnitStruct(ref name) => self.values.insert(name.clone(), &UnitStruct(ref name) => self.values.insert(name.clone(),
ValueEntry::Binding { val: FullyEvaluatedExpr::Custom { string_rep: name.clone() } }), ValueEntry::Binding { val: FullyEvaluatedExpr::Custom { string_rep: name.clone() } }),
&TupleStruct(ref name, ref args) => unimplemented!(), &TupleStruct(ref _name, ref _args) => unimplemented!(),
&Record(ref name, ref fields) => unimplemented!(), &Record(ref _name, ref _fields) => unimplemented!(),
}; };
} }
}, },
@ -124,10 +126,27 @@ impl ReplState {
} }
Ok(Tuple(evals)) Ok(Tuple(evals))
} }
Call { f, arguments } => self.eval_application(*f, arguments),
x => Err(format!("Unimplemented thing {:?}", x)), x => Err(format!("Unimplemented thing {:?}", x)),
} }
} }
fn eval_application(&mut self, f: Expression, _arguments: Vec<Expression>) -> EvalResult<FullyEvaluatedExpr> {
use self::ExpressionType::*;
match f {
Expression(Value(identifier, _), _) => {
match self.values.get(&identifier) {
Some(&ValueEntry::Function { ref body }) => {
println!("LOL ALL FUNCTIONS EVALUATE TO 2!");
Ok(FullyEvaluatedExpr::UnsignedInt(2))
},
_ => Err(format!("Function {} not found", identifier)),
}
},
x => Err(format!("Trying to apply {:?} which is not a function", x)),
}
}
fn eval_value(&mut self, name: Rc<String>) -> EvalResult<FullyEvaluatedExpr> { fn eval_value(&mut self, name: Rc<String>) -> EvalResult<FullyEvaluatedExpr> {
use self::ValueEntry::*; use self::ValueEntry::*;
match self.values.get(&name) { match self.values.get(&name) {
@ -135,11 +154,7 @@ impl ReplState {
Some(lookup) => { Some(lookup) => {
match lookup { match lookup {
&Binding { ref val } => Ok(val.clone()), &Binding { ref val } => Ok(val.clone()),
&Function { ref body } => { &Function { .. } => Ok(FullyEvaluatedExpr::FuncLit(name.clone()))
Ok(FullyEvaluatedExpr::Custom {
string_rep: Rc::new(format!("<function {}>", *name))
})
}
} }
} }
} }