List evaluation technically working

This commit is contained in:
greg 2017-02-18 03:09:36 -08:00
parent dd93adf5b7
commit dbf5886aad
2 changed files with 26 additions and 1 deletions

View File

@ -104,6 +104,9 @@ impl Evaluable for Expression {
StringLiteral(_) => false,
Lambda(_) => false,
Number(_) => false,
ListLiteral(ref items) => {
items.iter().any(|x| x.is_reducible())
}
_ => true,
}
}
@ -327,6 +330,20 @@ impl<'a> Evaluator<'a> {
_ => (Null, None)
}
}
ListLiteral(mut exprs) => {
let mut side_effect = None;
for expr in exprs.iter_mut() {
if expr.is_reducible() {
take_mut::take(expr, |expr| {
let (a, b) = self.reduce_expr(expr);
side_effect = b;
a
});
break;
}
}
(ListLiteral(exprs), side_effect)
},
}
}

View File

@ -74,6 +74,7 @@ pub enum Expression {
Block(VecDeque<Expression>),
While(Box<Expression>, Vec<Expression>),
Index(Box<Expression>, Box<Expression>),
ListLiteral(VecDeque<Expression>),
}
#[derive(Clone, Debug)]
@ -92,6 +93,9 @@ impl fmt::Display for Expression {
Lambda(Function { prototype: Prototype { ref name, ref parameters, .. }, .. }) => {
write!(f, "«function: {}, {} arg(s)»", name, parameters.len())
}
ListLiteral(ref items) => {
write!(f, "[ <a list> ]")
}
_ => write!(f, "UNIMPLEMENTED"),
}
}
@ -444,7 +448,11 @@ impl Parser {
}
fn list_expr(&mut self) -> ParseResult<Expression> {
unimplemented!()
expect!(self, LSquareBracket);
let exprlist: Vec<Expression> = self.exprlist()?;
expect!(self, RSquareBracket);
Ok(Expression::ListLiteral(VecDeque::from(exprlist)))
}
fn number_expression(&mut self) -> ParseResult<Expression> {