Use subpattern abstraction

This commit is contained in:
greg 2018-10-30 18:46:06 -07:00
parent e88ed97b06
commit f4ff92302f
1 changed files with 21 additions and 6 deletions

View File

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