From 5ecd298e6a422a40bc1924368533bf2430a84abd Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 13 Jul 2018 21:21:27 -0700 Subject: [PATCH] Record Pattern --- schala-lang/src/parsing.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/schala-lang/src/parsing.rs b/schala-lang/src/parsing.rs index f68fe4e..096d002 100644 --- a/schala-lang/src/parsing.rs +++ b/schala-lang/src/parsing.rs @@ -114,7 +114,7 @@ pattern := '(' (pattern, ',')* ')' | simple_pattern simple_pattern := pattern_literal | record_pattern | tuple_struct_pattern pattern_literal := 'true' | 'false' | number_literal | STR_LITERAL | IDENTIFIER record_pattern := IDENTIFIER '{' (record_pattern_entry, ',')* '}' -record_pattern_entry := ??? +record_pattern_entry := IDENTIFIER | IDENTIFIER ':' Pattern tuple_struct_pattern := IDENTIFIER '(' (pattern, ',')* ')' /* Expression - If */ @@ -751,7 +751,15 @@ impl Parser { }); parse_method!(record_pattern_entry(&mut self) -> ParseResult<(Rc, Pattern)> { - unimplemented!() + let name = self.identifier()?; + Ok(match self.peek() { + Colon => { + expect!(self, Colon); + let pat = self.pattern()?; + (name, pat) + }, + _ => (name.clone(), Pattern::Literal(PatternLiteral::StringPattern(name.clone()))) + }) }); parse_method!(block(&mut self) -> ParseResult { @@ -1420,5 +1428,21 @@ fn a(x) { ) ]) } + + parse_test! { + "if x is Something { a, b: x } then { 4 } else { 9 }", AST(vec![ + exprstatement!( + IfExpression { + discriminator: bx!(Discriminator::Simple(ex!(Value(rc!(x))))), + body: bx!(IfExpressionBody::SimplePatternMatch( + Pattern::Record(rc!(Something), vec![ + (rc!(a),Pattern::Literal(PatternLiteral::StringPattern(rc!(a)))), + (rc!(b),Pattern::Literal(PatternLiteral::VarPattern(rc!(x)))) + ]), + vec![exprstatement!(NatLiteral(4))], Some(vec![exprstatement!(NatLiteral(9))]))) + } + ) + ]) + } } }