Use subpattern abstraction

This commit is contained in:
greg 2018-10-30 18:46:06 -07:00
parent e88ed97b06
commit f4ff92302f

View File

@ -74,7 +74,7 @@ impl Alternative {
#[derive(Debug, Clone)]
pub struct Subpattern {
pub tag: Option<usize>,
pub subpatterns: Vec<Subpattern>,
pub subpatterns: Vec<Option<Subpattern>>,
pub bound_vars: Vec<Option<Rc<String>>>,
pub guard: Option<Expr>,
}
@ -207,7 +207,7 @@ impl Pattern {
fn to_alternative(&self, item: Vec<Stmt>, symbol_table: &SymbolTable) -> Alternative {
use self::Pattern::*;
fn handle_symbol(symbol: &Symbol, subpatterns: &Vec<Pattern>, item: Vec<Stmt>) -> Alternative {
fn handle_symbol(symbol: &Symbol, subpatterns: &Vec<Pattern>) -> Subpattern {
let tag = match symbol.spec {
SymbolSpec::DataConstructor { index, .. } => index.clone(),
_ => panic!("Symbol is not a data constructor - this should've been caught in type-checking"),
@ -229,19 +229,25 @@ impl Pattern {
let guard = None;
let subpatterns = vec![];
Alternative {
Subpattern {
tag: Some(tag),
subpatterns,
guard,
bound_vars,
item,
}
}
match self {
TupleStruct(name, subpatterns) => {
let symbol = symbol_table.lookup_by_name(name).expect(&format!("Symbol {} not found", name));
handle_symbol(symbol, subpatterns, item)
let s = handle_symbol(symbol, subpatterns);
Alternative {
tag: s.tag,
subpatterns: s.subpatterns,
guard: s.guard,
bound_vars: s.bound_vars,
item
}
},
TuplePattern(_items) => {
unimplemented!()
@ -290,7 +296,16 @@ impl Pattern {
}
},
PatternLiteral::VarPattern(var) => match symbol_table.lookup_by_name(var) {
Some(symbol) => handle_symbol(symbol, &vec![], item),
Some(symbol) => {
let s = handle_symbol(symbol, &vec![]);
Alternative {
tag: s.tag,
subpatterns: s.subpatterns,
guard: s.guard,
bound_vars: s.bound_vars,
item
}
},
None => Alternative {
tag: None,
subpatterns: vec![],