Starting work on patterns

This commit is contained in:
greg 2018-06-20 02:07:11 -07:00
parent 005aba7a10
commit 927f427a86
3 changed files with 28 additions and 9 deletions

View File

@ -120,6 +120,7 @@ pub enum Discriminator {
#[derive(Debug, PartialEq, Clone)]
pub enum IfExpressionBody {
SimpleConditional(Block, Option<Block>),
SimplePatternMatch(Pattern, Block, Option<Block>),
GuardList(Vec<Guard>)
}

View File

@ -112,7 +112,7 @@ pattern := identifier //TODO NOT DONE
/* NEW GOOD */
/* Expression - If */
if_expr := 'if' discriminator ('then' condititional | guard_block)
if_expr := 'if' discriminator ('then' condititional | 'is' pattern 'then' conditional | guard_block)
discriminator := modified_precedence_expression
conditional := block else_clause
else_clause := ε | 'else' block
@ -634,10 +634,10 @@ impl Parser {
x?
});
let body = Box::new(if let Keyword(Kw::Then) = self.peek() {
self.conditional()?
} else {
self.guard_block()?
let body = Box::new(match self.peek() {
Keyword(Kw::Then) => self.conditional()?,
Keyword(Kw::Is) => self.simple_pattern_match()? ,
_ => self.guard_block()?
});
Ok(Expression(ExpressionType::IfExpression { discriminator, body }, None))
@ -654,6 +654,15 @@ impl Parser {
Ok(IfExpressionBody::SimpleConditional(then_clause, else_clause))
});
parse_method!(simple_pattern_match(&mut self) -> ParseResult<IfExpressionBody> {
expect!(self, Keyword(Kw::Is));
let pat = self.pattern()?;
expect!(self, Keyword(Kw::Then));
let then_clause = self.block()?; //TODO should be block_or_expr
let else_clause = self.else_clause()?;
Ok(IfExpressionBody::SimplePatternMatch(pat, then_clause, else_clause))
});
parse_method!(guard_block(&mut self) -> ParseResult<IfExpressionBody> {
ParseError::new("Rest of if not done")
});
@ -667,6 +676,11 @@ impl Parser {
})
});
parse_method!(pattern(&mut self) -> ParseResult<Pattern> {
let identifier = self.identifier()?;
Ok(Pattern { })
});
parse_method!(block(&mut self) -> ParseResult<Block> {
Ok(delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict))
});
@ -695,10 +709,6 @@ impl Parser {
Ok(MatchArm { pat, expr })
});
parse_method!(pattern(&mut self) -> ParseResult<Pattern> {
let identifier = self.identifier()?;
Ok(Pattern(identifier))
});
*/
parse_method!(while_expr(&mut self) -> ParseResult<Expression> {

View File

@ -126,6 +126,14 @@ impl Expression {
};
Expr::Conditional { cond, then_clause, else_clause }
},
IfExpressionBody::SimplePatternMatch(ref pat, ref then_clause, ref else_clause) => {
let then_clause = then_clause.iter().map(|expr| expr.reduce(symbol_table)).collect();
let else_clause = match else_clause {
None => vec![],
Some(stmts) => stmts.iter().map(|expr| expr.reduce(symbol_table)).collect(),
};
Expr::Conditional { cond, then_clause, else_clause }
},
_ => panic!(),
}
},