From f4ff92302f180099eca6102830827039183b8246 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 30 Oct 2018 18:46:06 -0700 Subject: [PATCH] Use subpattern abstraction --- schala-lang/language/src/reduced_ast.rs | 27 +++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/schala-lang/language/src/reduced_ast.rs b/schala-lang/language/src/reduced_ast.rs index f6e4d26..7fbd7a8 100644 --- a/schala-lang/language/src/reduced_ast.rs +++ b/schala-lang/language/src/reduced_ast.rs @@ -74,7 +74,7 @@ impl Alternative { #[derive(Debug, Clone)] pub struct Subpattern { pub tag: Option, - pub subpatterns: Vec, + pub subpatterns: Vec>, pub bound_vars: Vec>>, pub guard: Option, } @@ -207,7 +207,7 @@ impl Pattern { fn to_alternative(&self, item: Vec, symbol_table: &SymbolTable) -> Alternative { use self::Pattern::*; - fn handle_symbol(symbol: &Symbol, subpatterns: &Vec, item: Vec) -> Alternative { + fn handle_symbol(symbol: &Symbol, subpatterns: &Vec) -> 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![],