Switch out types for evaluator
This commit is contained in:
parent
f9c2fc3f9d
commit
8067c862f3
@ -16,7 +16,7 @@ pub struct State<'a> {
|
|||||||
|
|
||||||
macro_rules! builtin_binding {
|
macro_rules! builtin_binding {
|
||||||
($name:expr, $values:expr) => {
|
($name:expr, $values:expr) => {
|
||||||
$values.insert(Rc::new(format!($name)), ValueEntry::Binding { constant: true, val: Expr::Func(Func::BuiltIn(Rc::new(format!($name)))) });
|
$values.insert(Rc::new(format!($name)), ValueEntry::Binding { constant: true, val: Node::Expr(Expr::Func(Func::BuiltIn(Rc::new(format!($name))))) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,27 +34,22 @@ impl<'a> State<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
enum ValueEntry {
|
enum Node {
|
||||||
Binding {
|
Expr(Expr),
|
||||||
constant: bool,
|
PrimObject {
|
||||||
val: /*FullyEvaluatedExpr*/ Expr,
|
name: Rc<String>,
|
||||||
|
tag: usize,
|
||||||
|
items: Vec<Node>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type EvalResult<T> = Result<T, String>;
|
fn paren_wrapped_vec(terms: impl Iterator<Item=String>) -> String {
|
||||||
|
|
||||||
|
|
||||||
impl Expr {
|
|
||||||
fn to_repl(&self) -> String {
|
|
||||||
use self::Lit::*;
|
|
||||||
use self::Func::*;
|
|
||||||
fn paren_wrapped_vec(exprs: &Vec<Expr>) -> String {
|
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
write!(buf, "(").unwrap();
|
write!(buf, "(").unwrap();
|
||||||
for term in exprs.iter().map(|e| Some(e)).intersperse(None) {
|
for term in terms.map(|e| Some(e)).intersperse(None) {
|
||||||
match term {
|
match term {
|
||||||
Some(e) => write!(buf, "{}", e.to_repl()).unwrap(),
|
Some(e) => write!(buf, "{}", e).unwrap(),
|
||||||
None => write!(buf, ", ").unwrap(),
|
None => write!(buf, ", ").unwrap(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -62,6 +57,35 @@ impl Expr {
|
|||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Node {
|
||||||
|
fn to_repl(&self) -> String {
|
||||||
|
match self {
|
||||||
|
Node::Expr(e) => e.to_repl(),
|
||||||
|
Node::PrimObject { name, items, tag } if items.len() == 0 => format!("{}", name),
|
||||||
|
Node::PrimObject { name, items, tag } => format!("{}{}", name, paren_wrapped_vec(items.iter().map(|x| x.to_repl()))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum ValueEntry {
|
||||||
|
Binding {
|
||||||
|
constant: bool,
|
||||||
|
val: /*FullyEvaluatedExpr*/ Node, //TODO make this use a subtype to represent fully evaluatedness
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type EvalResult<T> = Result<T, String>;
|
||||||
|
|
||||||
|
impl Expr {
|
||||||
|
fn to_node(self) -> Node {
|
||||||
|
Node::Expr(self)
|
||||||
|
}
|
||||||
|
fn to_repl(&self) -> String {
|
||||||
|
use self::Lit::*;
|
||||||
|
use self::Func::*;
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
Expr::Lit(ref l) => match l {
|
Expr::Lit(ref l) => match l {
|
||||||
Nat(n) => format!("{}", n),
|
Nat(n) => format!("{}", n),
|
||||||
@ -69,8 +93,6 @@ impl Expr {
|
|||||||
Float(f) => format!("{}", f),
|
Float(f) => format!("{}", f),
|
||||||
Bool(b) => format!("{}", b),
|
Bool(b) => format!("{}", b),
|
||||||
StringLit(s) => format!("\"{}\"", s),
|
StringLit(s) => format!("\"{}\"", s),
|
||||||
PrimObject { name, items } if items.len() == 0 => format!("{}", name),
|
|
||||||
PrimObject { name, items } => format!("{}{}", name, paren_wrapped_vec(items)),
|
|
||||||
},
|
},
|
||||||
Expr::Func(f) => match f {
|
Expr::Func(f) => match f {
|
||||||
BuiltIn(name) => format!("<built-in function '{}'>", name),
|
BuiltIn(name) => format!("<built-in function '{}'>", name),
|
||||||
@ -84,7 +106,7 @@ impl Expr {
|
|||||||
} else {
|
} else {
|
||||||
format!("<data constructor '{}'>", name)
|
format!("<data constructor '{}'>", name)
|
||||||
},
|
},
|
||||||
Expr::Tuple(exprs) => paren_wrapped_vec(exprs),
|
Expr::Tuple(exprs) => paren_wrapped_vec(exprs.iter().map(|x| x.to_repl())),
|
||||||
_ => format!("{:?}", self),
|
_ => format!("{:?}", self),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -115,7 +137,7 @@ impl<'a> State<'a> {
|
|||||||
fn prebinding(&mut self, stmt: &Stmt) {
|
fn prebinding(&mut self, stmt: &Stmt) {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::PreBinding { name, func } => {
|
Stmt::PreBinding { name, func } => {
|
||||||
let v_entry = ValueEntry::Binding { constant: true, val: Expr::Func(func.clone()) };
|
let v_entry = ValueEntry::Binding { constant: true, val: Node::Expr(Expr::Func(func.clone())) };
|
||||||
self.values.insert(name.clone(), v_entry);
|
self.values.insert(name.clone(), v_entry);
|
||||||
},
|
},
|
||||||
Stmt::Expr(_expr) => {
|
Stmt::Expr(_expr) => {
|
||||||
@ -126,41 +148,46 @@ impl<'a> State<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn statement(&mut self, stmt: Stmt) -> EvalResult<Option<Expr>> {
|
fn statement(&mut self, stmt: Stmt) -> EvalResult<Option<Node>> {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::Binding { name, constant, expr } => {
|
Stmt::Binding { name, constant, expr } => {
|
||||||
let val = self.expression(expr)?;
|
let val = self.expression(Node::Expr(expr))?;
|
||||||
self.values.insert(name.clone(), ValueEntry::Binding { constant, val });
|
self.values.insert(name.clone(), ValueEntry::Binding { constant, val });
|
||||||
Ok(None)
|
Ok(None)
|
||||||
},
|
},
|
||||||
Stmt::Expr(expr) => Ok(Some(self.expression(expr)?)),
|
Stmt::Expr(expr) => Ok(Some(self.expression(expr.to_node())?)),
|
||||||
Stmt::PreBinding {..} | Stmt::Noop => Ok(None),
|
Stmt::PreBinding {..} | Stmt::Noop => Ok(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn block(&mut self, stmts: Vec<Stmt>) -> EvalResult<Expr> {
|
fn block(&mut self, stmts: Vec<Stmt>) -> EvalResult<Node> {
|
||||||
let mut ret = None;
|
let mut ret = None;
|
||||||
for stmt in stmts {
|
for stmt in stmts {
|
||||||
ret = self.statement(stmt)?;
|
ret = self.statement(stmt)?;
|
||||||
}
|
}
|
||||||
Ok(ret.unwrap_or(Expr::Unit))
|
Ok(ret.unwrap_or(Node::Expr(Expr::Unit)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expression(&mut self, expr: Expr) -> EvalResult<Expr> {
|
fn expression(&mut self, node: Node) -> EvalResult<Node> {
|
||||||
use self::Expr::*;
|
use self::Expr::*;
|
||||||
match expr {
|
match node {
|
||||||
literal @ Lit(_) => Ok(literal),
|
obj @ Node::PrimObject { .. } => Ok(obj),
|
||||||
|
Node::Expr(expr) => match expr {
|
||||||
|
literal @ Lit(_) => Ok(Node::Expr(literal)),
|
||||||
Call { box f, args } => {
|
Call { box f, args } => {
|
||||||
match self.expression(f)? {
|
match self.expression(Node::Expr(f))? {
|
||||||
Constructor { type_name, name, tag, arity} => self.apply_data_constructor(type_name, name, tag, arity, args),
|
Node::Expr(Constructor { type_name, name, tag, arity }) => self.apply_data_constructor(type_name, name, tag, arity, args),
|
||||||
Func(f) => self.apply_function(f, args),
|
Node::Expr(Func(f)) => self.apply_function(f, args),
|
||||||
other => return Err(format!("Tried to call {:?} which is not a function or data constructor", other)),
|
other => return Err(format!("Tried to call {:?} which is not a function or data constructor", other)),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Val(v) => self.value(v),
|
Val(v) => self.value(v),
|
||||||
constructor @ Constructor { .. } => Ok(constructor),
|
constructor @ Constructor { .. } => Ok(Node::Expr(constructor)),
|
||||||
func @ Func(_) => Ok(func),
|
func @ Func(_) => Ok(Node::Expr(func)),
|
||||||
Tuple(exprs) => Ok(Tuple(exprs.into_iter().map(|expr| self.expression(expr)).collect::<Result<Vec<Expr>,_>>()?)),
|
Tuple(exprs) => {
|
||||||
|
unimplemented!()
|
||||||
|
},
|
||||||
|
//Tuple(exprs) => Ok(Tuple(exprs.into_iter().map(|expr| self.expression(expr)).collect::<Result<Vec<Expr>,_>>()?)),
|
||||||
Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause),
|
Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause),
|
||||||
Assign { box val, box expr } => {
|
Assign { box val, box expr } => {
|
||||||
let name = match val {
|
let name = match val {
|
||||||
@ -175,30 +202,32 @@ impl<'a> State<'a> {
|
|||||||
if constant {
|
if constant {
|
||||||
return Err(format!("trying to update {}, a non-mutable binding", name));
|
return Err(format!("trying to update {}, a non-mutable binding", name));
|
||||||
}
|
}
|
||||||
let val = self.expression(expr)?;
|
let val = self.expression(Node::Expr(expr))?;
|
||||||
self.values.insert(name.clone(), ValueEntry::Binding { constant: false, val });
|
self.values.insert(name.clone(), ValueEntry::Binding { constant: false, val });
|
||||||
Ok(Expr::Unit)
|
Ok(Node::Expr(Expr::Unit))
|
||||||
},
|
},
|
||||||
e => Err(format!("Expr {:?} eval not implemented", e))
|
e => Err(format!("Expr {:?} eval not implemented", e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn apply_data_constructor(&mut self, type_name: Rc<String>, name: Rc<String>, tag: usize, arity: usize, args: Vec<Expr>) -> EvalResult<Expr> {
|
fn apply_data_constructor(&mut self, type_name: Rc<String>, name: Rc<String>, tag: usize, arity: usize, args: Vec<Expr>) -> EvalResult<Node> {
|
||||||
if arity != args.len() {
|
if arity != args.len() {
|
||||||
return Err(format!("Data constructor {} requires {} args", name, arity));
|
return Err(format!("Data constructor {} requires {} args", name, arity));
|
||||||
}
|
}
|
||||||
|
|
||||||
let evaled_args = args.into_iter().map(|expr| self.expression(expr)).collect::<Result<Vec<Expr>,_>>()?;
|
let evaled_args = args.into_iter().map(|expr| self.expression(Node::Expr(expr))).collect::<Result<Vec<Node>,_>>()?;
|
||||||
//let evaled_args = vec![];
|
//let evaled_args = vec![];
|
||||||
Ok(Expr::Lit(self::Lit::PrimObject {
|
Ok(Node::PrimObject {
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
items: evaled_args,
|
items: evaled_args,
|
||||||
}))
|
tag: 0,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_function(&mut self, f: Func, args: Vec<Expr>) -> EvalResult<Expr> {
|
fn apply_function(&mut self, f: Func, args: Vec<Expr>) -> EvalResult<Node> {
|
||||||
match f {
|
match f {
|
||||||
Func::BuiltIn(sigil) => self.apply_builtin(sigil, args),
|
Func::BuiltIn(sigil) => Ok(Node::Expr(self.apply_builtin(sigil, args)?)),
|
||||||
Func::UserDefined { params, body, name } => {
|
Func::UserDefined { params, body, name } => {
|
||||||
|
|
||||||
if params.len() != args.len() {
|
if params.len() != args.len() {
|
||||||
@ -209,7 +238,7 @@ impl<'a> State<'a> {
|
|||||||
symbol_table_handle: self.symbol_table_handle.clone(),
|
symbol_table_handle: self.symbol_table_handle.clone(),
|
||||||
};
|
};
|
||||||
for (param, val) in params.into_iter().zip(args.into_iter()) {
|
for (param, val) in params.into_iter().zip(args.into_iter()) {
|
||||||
let val = func_state.expression(val)?;
|
let val = func_state.expression(Node::Expr(val))?;
|
||||||
func_state.values.insert(param, ValueEntry::Binding { constant: true, val });
|
func_state.values.insert(param, ValueEntry::Binding { constant: true, val });
|
||||||
}
|
}
|
||||||
// TODO figure out function return semantics
|
// TODO figure out function return semantics
|
||||||
@ -221,7 +250,13 @@ impl<'a> State<'a> {
|
|||||||
fn apply_builtin(&mut self, name: Rc<String>, args: Vec<Expr>) -> EvalResult<Expr> {
|
fn apply_builtin(&mut self, name: Rc<String>, args: Vec<Expr>) -> EvalResult<Expr> {
|
||||||
use self::Expr::*;
|
use self::Expr::*;
|
||||||
use self::Lit::*;
|
use self::Lit::*;
|
||||||
let evaled_args: Result<Vec<Expr>, String> = args.into_iter().map(|arg| self.expression(arg)).collect();
|
let evaled_args: Result<Vec<Expr>, String> = args.into_iter().map(|arg| {
|
||||||
|
match self.expression(Node::Expr(arg)) {
|
||||||
|
Ok(Node::Expr(e)) => Ok(e),
|
||||||
|
Ok(Node::PrimObject { .. }) => Err(format!("Trying to apply a builtin to a primitive object")),
|
||||||
|
Err(e) => Err(e)
|
||||||
|
}
|
||||||
|
}).collect();
|
||||||
let evaled_args = evaled_args?;
|
let evaled_args = evaled_args?;
|
||||||
|
|
||||||
Ok(match (name.as_str(), evaled_args.as_slice()) {
|
Ok(match (name.as_str(), evaled_args.as_slice()) {
|
||||||
@ -274,16 +309,16 @@ impl<'a> State<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn conditional(&mut self, cond: Expr, then_clause: Vec<Stmt>, else_clause: Vec<Stmt>) -> EvalResult<Expr> {
|
fn conditional(&mut self, cond: Expr, then_clause: Vec<Stmt>, else_clause: Vec<Stmt>) -> EvalResult<Node> {
|
||||||
let cond = self.expression(cond)?;
|
let cond = self.expression(Node::Expr(cond))?;
|
||||||
Ok(match cond {
|
Ok(match cond {
|
||||||
Expr::Lit(Lit::Bool(true)) => self.block(then_clause)?,
|
Node::Expr(Expr::Lit(Lit::Bool(true))) => self.block(then_clause)?,
|
||||||
Expr::Lit(Lit::Bool(false)) => self.block(else_clause)?,
|
Node::Expr(Expr::Lit(Lit::Bool(false))) => self.block(else_clause)?,
|
||||||
_ => return Err(format!("Conditional with non-boolean condition"))
|
_ => return Err(format!("Conditional with non-boolean condition"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn value(&mut self, name: Rc<String>) -> EvalResult<Expr> {
|
fn value(&mut self, name: Rc<String>) -> EvalResult<Node> {
|
||||||
use self::ValueEntry::*;
|
use self::ValueEntry::*;
|
||||||
use self::Func::*;
|
use self::Func::*;
|
||||||
//TODO add a layer of indirection here to talk to the symbol table first, and only then look up
|
//TODO add a layer of indirection here to talk to the symbol table first, and only then look up
|
||||||
@ -295,14 +330,14 @@ impl<'a> State<'a> {
|
|||||||
Some(Symbol { name, spec }) => match spec {
|
Some(Symbol { name, spec }) => match spec {
|
||||||
SymbolSpec::DataConstructor { type_name, type_args, .. } => {
|
SymbolSpec::DataConstructor { type_name, type_args, .. } => {
|
||||||
if type_args.len() == 0 {
|
if type_args.len() == 0 {
|
||||||
Expr::Lit(Lit::PrimObject { name: name.clone(), items: vec![] })
|
Node::PrimObject { name: name.clone(), tag: 0, items: vec![] }
|
||||||
} else {
|
} else {
|
||||||
return Err(format!("This data constructor thing not done"))
|
return Err(format!("This data constructor thing not done"))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
SymbolSpec::Func(_) => match self.values.lookup(&name) {
|
SymbolSpec::Func(_) => match self.values.lookup(&name) {
|
||||||
Some(Binding { val: Expr::Func(UserDefined { name, params, body }), .. }) => {
|
Some(Binding { val: Node::Expr(Expr::Func(UserDefined { name, params, body })), .. }) => {
|
||||||
Expr::Func(UserDefined { name: name.clone(), params: params.clone(), body: body.clone() })
|
Node::Expr(Expr::Func(UserDefined { name: name.clone(), params: params.clone(), body: body.clone() }))
|
||||||
},
|
},
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
},
|
},
|
||||||
|
@ -69,10 +69,6 @@ pub enum Lit {
|
|||||||
Float(f64),
|
Float(f64),
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
StringLit(Rc<String>),
|
StringLit(Rc<String>),
|
||||||
PrimObject { //TODO rethink placement in type heierarchy
|
|
||||||
name: Rc<String>,
|
|
||||||
items: Vec<Expr>,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
Loading…
Reference in New Issue
Block a user