From 230f2dd7ff731e40299e53228dcf725ebb4b8bd8 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 21 Sep 2017 16:00:14 -0700 Subject: [PATCH] More match expr work --- src/schala_lang/parsing.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index a6dc304..406ba0b 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -308,6 +308,7 @@ else_clause := ε | 'else' block match_expr := 'match' expression '{' match_body '}' match_body := pattern '=>' expression +pattern := identifier //TODO NOT DONE block := '{' (statement)* '}' @@ -445,7 +446,7 @@ pub struct MatchArm { } #[derive(Debug, PartialEq)] -pub struct Pattern(String); +pub struct Pattern(Rc); #[derive(Debug, PartialEq)] pub struct Operation(Rc); @@ -731,7 +732,26 @@ impl Parser { }); parse_method!(match_body(&mut self) -> ParseResult> { - Ok(vec!()) + let mut arms = Vec::new(); + loop { + if let RCurlyBrace = self.peek() { + break; + } + let pat = self.pattern()?; + expect!(self, Operator(ref c) if **c == "=>", "Expected '=>'"); + let expr = self.expression()?; + arms.push(MatchArm {pat, expr}); + match self.peek() { + Comma => { self.next(); }, + _ => break + } + } + Ok(arms) + }); + + parse_method!(pattern(&mut self) -> ParseResult { + let identifier = self.identifier()?; + Ok(Pattern(identifier)) }); parse_method!(identifier(&mut self) -> ParseResult> {