Add guard to Alternative

The semantics are:
    -if tag is Some(_), assume the condition is a constructor,
    and compare tags
    - if guard is Some(_), evaluate true/false *after* having
    applied any bound variables

With this, I can technically get rid of bare conditionals now, since
they are the same as an Alternative with a None tag
This commit is contained in:
greg 2018-08-27 12:45:08 -07:00
parent 065bdd6bda
commit 1abbe2e448

View File

@ -58,6 +58,7 @@ pub enum Expr {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Alternative { pub struct Alternative {
pub tag: Option<usize>, pub tag: Option<usize>,
pub guard: Option<Expr>,
pub bound_vars: Vec<Option<Rc<String>>>, //remember that order matters here pub bound_vars: Vec<Option<Rc<String>>>, //remember that order matters here
pub item: Vec<Stmt>, pub item: Vec<Stmt>,
} }
@ -159,6 +160,7 @@ fn reduce_if_expression(discriminator: &Discriminator, body: &IfExpressionBody,
pat.to_alternative(then_clause, symbol_table), pat.to_alternative(then_clause, symbol_table),
Alternative { Alternative {
tag: None, tag: None,
guard: None,
bound_vars: vec![], bound_vars: vec![],
item: else_clause, item: else_clause,
}, },
@ -201,6 +203,7 @@ impl Pattern {
}).collect(); }).collect();
Alternative { Alternative {
tag: Some(tag), tag: Some(tag),
guard: None,
bound_vars, bound_vars,
item, item,
} }