Primitive tuple

This commit is contained in:
greg 2018-08-14 02:03:05 -07:00
parent 8067c862f3
commit 9debdd8d66

View File

@ -41,6 +41,9 @@ enum Node {
name: Rc<String>, name: Rc<String>,
tag: usize, tag: usize,
items: Vec<Node>, items: Vec<Node>,
},
PrimTuple {
items: Vec<Node>
} }
} }
@ -64,6 +67,7 @@ impl Node {
Node::Expr(e) => e.to_repl(), Node::Expr(e) => e.to_repl(),
Node::PrimObject { name, items, tag } if items.len() == 0 => format!("{}", name), 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()))), Node::PrimObject { name, items, tag } => format!("{}{}", name, paren_wrapped_vec(items.iter().map(|x| x.to_repl()))),
Node::PrimTuple { items } => format!("{}", paren_wrapped_vec(items.iter().map(|x| x.to_repl()))),
} }
} }
} }
@ -171,23 +175,22 @@ impl<'a> State<'a> {
fn expression(&mut self, node: Node) -> EvalResult<Node> { fn expression(&mut self, node: Node) -> EvalResult<Node> {
use self::Expr::*; use self::Expr::*;
match node { match node {
t @ Node::PrimTuple { .. } => Ok(t),
obj @ Node::PrimObject { .. } => Ok(obj), obj @ Node::PrimObject { .. } => Ok(obj),
Node::Expr(expr) => match expr { Node::Expr(expr) => match expr {
literal @ Lit(_) => Ok(Node::Expr(literal)), literal @ Lit(_) => Ok(Node::Expr(literal)),
Call { box f, args } => { Call { box f, args } => match self.expression(Node::Expr(f))? {
match self.expression(Node::Expr(f))? { Node::Expr(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), Node::Expr(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(Node::Expr(constructor)), constructor @ Constructor { .. } => Ok(Node::Expr(constructor)),
func @ Func(_) => Ok(Node::Expr(func)), func @ Func(_) => Ok(Node::Expr(func)),
Tuple(exprs) => { Tuple(exprs) => {
unimplemented!() let nodes = exprs.into_iter().map(|expr| self.expression(Node::Expr(expr))).collect::<Result<Vec<Node>,_>>()?;
Ok(Node::PrimTuple { items: nodes })
}, },
//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 {
@ -221,7 +224,7 @@ impl<'a> State<'a> {
Ok(Node::PrimObject { Ok(Node::PrimObject {
name: name.clone(), name: name.clone(),
items: evaled_args, items: evaled_args,
tag: 0, tag
}) })
} }
@ -253,6 +256,7 @@ impl<'a> State<'a> {
let evaled_args: Result<Vec<Expr>, String> = args.into_iter().map(|arg| { let evaled_args: Result<Vec<Expr>, String> = args.into_iter().map(|arg| {
match self.expression(Node::Expr(arg)) { match self.expression(Node::Expr(arg)) {
Ok(Node::Expr(e)) => Ok(e), Ok(Node::Expr(e)) => Ok(e),
Ok(Node::PrimTuple { items }) => Err(format!("Trying to apply a builtin to a tuple")),
Ok(Node::PrimObject { .. }) => Err(format!("Trying to apply a builtin to a primitive object")), Ok(Node::PrimObject { .. }) => Err(format!("Trying to apply a builtin to a primitive object")),
Err(e) => Err(e) Err(e) => Err(e)
} }