More match expr work

This commit is contained in:
greg 2017-09-21 16:00:14 -07:00
parent 1615269a7b
commit 230f2dd7ff

View File

@ -308,6 +308,7 @@ else_clause := ε | 'else' block
match_expr := 'match' expression '{' match_body '}' match_expr := 'match' expression '{' match_body '}'
match_body := pattern '=>' expression match_body := pattern '=>' expression
pattern := identifier //TODO NOT DONE
block := '{' (statement)* '}' block := '{' (statement)* '}'
@ -445,7 +446,7 @@ pub struct MatchArm {
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Pattern(String); pub struct Pattern(Rc<String>);
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Operation(Rc<String>); pub struct Operation(Rc<String>);
@ -731,7 +732,26 @@ impl Parser {
}); });
parse_method!(match_body(&mut self) -> ParseResult<Vec<MatchArm>> { parse_method!(match_body(&mut self) -> ParseResult<Vec<MatchArm>> {
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<Pattern> {
let identifier = self.identifier()?;
Ok(Pattern(identifier))
}); });
parse_method!(identifier(&mut self) -> ParseResult<Rc<String>> { parse_method!(identifier(&mut self) -> ParseResult<Rc<String>> {