Simplify Alternative data structure
Most of the subfields are duplicated on Subpattern so just use that directly
This commit is contained in:
parent
37c77d93d7
commit
ee87695626
@ -425,15 +425,15 @@ impl<'a> State<'a> {
|
|||||||
let cond = self.expression(Node::Expr(cond))?;
|
let cond = self.expression(Node::Expr(cond))?;
|
||||||
for alt in alternatives {
|
for alt in alternatives {
|
||||||
// no matter what type of condition we have, ignore alternative if the guard evaluates false
|
// no matter what type of condition we have, ignore alternative if the guard evaluates false
|
||||||
if !self.guard_passes(&alt.guard, &cond)? {
|
if !self.guard_passes(&alt.matchable.guard, &cond)? {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
match cond {
|
match cond {
|
||||||
Node::PrimObject { ref tag, ref items, .. } => {
|
Node::PrimObject { ref tag, ref items, .. } => {
|
||||||
if alt.tag.map(|t| t == *tag).unwrap_or(true) {
|
if alt.matchable.tag.map(|t| t == *tag).unwrap_or(true) {
|
||||||
let mut inner_state = self.new_frame(items, &alt.bound_vars);
|
let mut inner_state = self.new_frame(items, &alt.matchable.bound_vars);
|
||||||
if all_subpatterns_pass(&mut inner_state, &alt.subpatterns, items)? {
|
if all_subpatterns_pass(&mut inner_state, &alt.matchable.subpatterns, items)? {
|
||||||
return inner_state.block(alt.item);
|
return inner_state.block(alt.item);
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
@ -441,15 +441,15 @@ impl<'a> State<'a> {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Node::PrimTuple { ref items } => {
|
Node::PrimTuple { ref items } => {
|
||||||
let mut inner_state = self.new_frame(items, &alt.bound_vars);
|
let mut inner_state = self.new_frame(items, &alt.matchable.bound_vars);
|
||||||
if all_subpatterns_pass(&mut inner_state, &alt.subpatterns, items)? {
|
if all_subpatterns_pass(&mut inner_state, &alt.matchable.subpatterns, items)? {
|
||||||
return inner_state.block(alt.item);
|
return inner_state.block(alt.item);
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Node::Expr(ref _e) => {
|
Node::Expr(ref _e) => {
|
||||||
if let None = alt.tag {
|
if let None = alt.matchable.tag {
|
||||||
return self.block(alt.item)
|
return self.block(alt.item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,10 +60,7 @@ pub type BoundVars = Vec<Option<Rc<String>>>; //remember that order matters here
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Alternative {
|
pub struct Alternative {
|
||||||
pub tag: Option<usize>,
|
pub matchable: Subpattern,
|
||||||
pub subpatterns: Vec<Option<Subpattern>>,
|
|
||||||
pub guard: Option<Expr>,
|
|
||||||
pub bound_vars: BoundVars,
|
|
||||||
pub item: Vec<Stmt>,
|
pub item: Vec<Stmt>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,10 +198,12 @@ fn reduce_if_expression(discriminator: &Discriminator, body: &IfExpressionBody,
|
|||||||
let alternatives = vec![
|
let alternatives = vec![
|
||||||
pat.to_alternative(then_clause, symbol_table),
|
pat.to_alternative(then_clause, symbol_table),
|
||||||
Alternative {
|
Alternative {
|
||||||
|
matchable: Subpattern {
|
||||||
tag: None,
|
tag: None,
|
||||||
subpatterns: vec![],
|
subpatterns: vec![],
|
||||||
bound_vars: vec![],
|
bound_vars: vec![],
|
||||||
guard: None,
|
guard: None,
|
||||||
|
},
|
||||||
item: else_clause
|
item: else_clause
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
@ -278,10 +277,12 @@ impl Pattern {
|
|||||||
fn to_alternative(&self, item: Vec<Stmt>, symbol_table: &SymbolTable) -> Alternative {
|
fn to_alternative(&self, item: Vec<Stmt>, symbol_table: &SymbolTable) -> Alternative {
|
||||||
let s = self.to_subpattern(symbol_table);
|
let s = self.to_subpattern(symbol_table);
|
||||||
Alternative {
|
Alternative {
|
||||||
|
matchable: Subpattern {
|
||||||
tag: s.tag,
|
tag: s.tag,
|
||||||
subpatterns: s.subpatterns,
|
subpatterns: s.subpatterns,
|
||||||
bound_vars: s.bound_vars,
|
bound_vars: s.bound_vars,
|
||||||
guard: s.guard,
|
guard: s.guard,
|
||||||
|
},
|
||||||
item
|
item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user