Match expressions

not done yet
This commit is contained in:
greg 2017-09-20 20:30:30 -07:00
parent 18fa160fed
commit 6bff7aac0d
1 changed files with 15 additions and 2 deletions

View File

@ -297,15 +297,18 @@ expression := precedence_expr
precedence_expr := prefix_expr
prefix_expr := prefix_op primary
prefix_op := '+' | '-' | '!' | '~'
primary := literal | paren_expr | identifier_expr
primary := literal | paren_expr | if_expr | match_expr | identifier_expr
paren_expr := LParen expression RParen
identifier_expr := call_expr | index_expr | if_expr | IDENTIFIER
identifier_expr := call_expr | index_expr | IDENTIFIER
literal := 'true' | 'false' | number_literal | STR_LITERAL
if_expr := 'if' expression block else_clause
else_clause := ε | 'else' block
match_expr := 'match' expression '{' match_body '}'
match_body := pattern '=>' expression
block := '{' (statement)* '}'
call_expr := IDENTIFIER '(' expr_list ')' //TODO maybe make this optional? or no, have a bare identifier meant to be used as method taken care of in eval
@ -606,6 +609,7 @@ impl Parser {
match self.peek() {
LParen => self.paren_expr(),
Keyword(Kw::If) => self.if_expr(),
Keyword(Kw::Match) => self.match_expr(),
Identifier(_) => self.identifier_expr(),
_ => self.literal(),
}
@ -707,6 +711,15 @@ impl Parser {
Ok(statements)
});
parse_method!(match_expr(&mut self) -> ParseResult<Expression> {
expect!(self, Keyword(Kw::Match), "Expected 'match'");
let expr = self.expression()?;
expect!(self, LCurlyBrace, "Expected '{'");
let body = self.match_body()?;
expect!(self, RCurlyBrace, "Expected '}'");
unimplementd!()
});
parse_method!(identifier(&mut self) -> ParseResult<Rc<String>> {
match self.next() {
Identifier(s) => Ok(s),