From d69970a80603933ae4148c47823110674cd923a6 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 26 Feb 2018 18:12:37 -0800 Subject: [PATCH] Separate Value and NamedStruct syntactic categories --- src/schala_lang/eval.rs | 4 ++-- src/schala_lang/parsing.rs | 25 +++++++++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/schala_lang/eval.rs b/src/schala_lang/eval.rs index 1f12e2f..459902d 100644 --- a/src/schala_lang/eval.rs +++ b/src/schala_lang/eval.rs @@ -121,7 +121,7 @@ impl<'a> State<'a> { BoolLiteral(b) => Ok(Bool(b)), PrefixExp(op, expr) => self.eval_prefix_exp(op, expr), BinExp(op, lhs, rhs) => self.eval_binexp(op, lhs, rhs), - Value(name, _) => self.eval_value(name), + Value(name) => self.eval_value(name), TupleLiteral(expressions) => { let mut evals = Vec::new(); for expr in expressions { @@ -140,7 +140,7 @@ impl<'a> State<'a> { fn eval_application(&mut self, f: Expression, _arguments: Vec) -> EvalResult { use self::ExpressionType::*; match f { - Expression(Value(identifier, _), _) => { + Expression(Value(identifier), _) => { match self.values.get(&identifier) { Some(&ValueEntry::Function { ref body }) => { let mut new_state = State::new_with_parent(self); diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 54e4771..48497ad 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -229,7 +229,11 @@ pub enum ExpressionType { BinExp(BinOp, Box, Box), PrefixExp(PrefixOp, Box), TupleLiteral(Vec), - Value(Rc, Vec<(Rc, Expression)>), + Value(Rc), + NamedStruct { + name: Rc, + fields: Vec<(Rc, Expression)>, + }, Call { f: Box, arguments: Vec, @@ -603,22 +607,22 @@ impl Parser { Ok(match self.peek() { LCurlyBrace if !self.restrictions.no_struct_literal => { let fields = self.record_block()?; - Expression(Value(identifier, fields), None) + Expression(NamedStruct { name: identifier, fields }, None) }, LParen => { let arguments = self.call_expr()?; //TODO make this be more general - let f = bx!(Expression(Value(identifier, vec![]), None)); + let f = bx!(Expression(Value(identifier), None)); Expression(Call { f, arguments }, None) }, LSquareBracket => { let indexers = self.index_expr()?; Expression(Index { - indexee: bx!(Expression(Value(identifier, vec![]), None)), + indexee: bx!(Expression(Value(identifier), None)), indexers, }, None) } - _ => Expression(Value(identifier, vec![]), None) + _ => Expression(Value(identifier), None) }) }); @@ -857,7 +861,7 @@ mod parse_tests { ($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression($lhs, None))) } } macro_rules! val { - ($var:expr) => { Value(Rc::new($var.to_string()), vec![]) } + ($var:expr) => { Value(Rc::new($var.to_string())) } } macro_rules! exprstatement { ($expr_type:expr) => { Statement::ExpressionStatement(Expression($expr_type, None)) }; @@ -938,7 +942,8 @@ mod parse_tests { parse_test!("None", AST(vec![exprstatement!(val!("None"))])); parse_test!("Pandas { a: x + y }", AST(vec![ - exprstatement!(Value(rc!(Pandas), vec![(rc!(a), ex!(binexp!("+", val!("x"), val!("y"))))]))])); + exprstatement!(NamedStruct { name: rc!(Pandas), fields: vec![(rc!(a), ex!(binexp!("+", val!("x"), val!("y"))))]}) + ])); } #[test] @@ -1035,8 +1040,8 @@ mod parse_tests { }"#, AST(vec![exprstatement!(IfExpression(bx!(ex!(BoolLiteral(true))), vec![Declaration(Binding { name: rc!(a), constant: true, expr: ex!(IntLiteral(10)) }), - exprstatement!(Value(rc!(b), vec![]))], - Some(vec![exprstatement!(Value(rc!(c), vec![]))])))]) + exprstatement!(val!(rc!(b)))], + Some(vec![exprstatement!(val!(rc!(c)))])))]) ); parse_test!("if a { b } else { c }", AST(vec![exprstatement!( @@ -1045,7 +1050,7 @@ mod parse_tests { Some(vec![exprstatement!(val!("c"))])))])); parse_test!("if (A {a: 1}) { b } else { c }", AST(vec![exprstatement!( - IfExpression(bx!(ex!(Value(rc!(A), vec![(rc!(a), ex!(IntLiteral(1)))]))), + IfExpression(bx!(ex!(NamedStruct { name: rc!(A), fields: vec![(rc!(a), ex!(IntLiteral(1)))]})), vec![exprstatement!(val!("b"))], Some(vec![exprstatement!(val!("c"))])))]));