More work here

This commit is contained in:
greg 2018-10-30 18:53:34 -07:00
parent f4ff92302f
commit 2d6c9010b9

View File

@ -202,12 +202,8 @@ fn reduce_if_expression(discriminator: &Discriminator, body: &IfExpressionBody,
* x is SomeBigOldEnum(_, x, Some(t))
*/
impl Pattern {
fn to_alternative(&self, item: Vec<Stmt>, symbol_table: &SymbolTable) -> Alternative {
fn handle_symbol(symbol: &Symbol, subpatterns: &Vec<Pattern>) -> Subpattern {
use self::Pattern::*;
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"),
@ -235,8 +231,11 @@ impl Pattern {
guard,
bound_vars,
}
}
}
impl Pattern {
fn to_alternative(&self, item: Vec<Stmt>, symbol_table: &SymbolTable) -> Alternative {
use self::Pattern::*;
match self {
TupleStruct(name, subpatterns) => {
let symbol = symbol_table.lookup_by_name(name).expect(&format!("Symbol {} not found", name));
@ -256,8 +255,25 @@ impl Pattern {
unimplemented!()
},
Ignored => Alternative::default(item),
Literal(lit) => match lit {
PatternLiteral::NumPattern { neg, num } => {
Literal(lit) => {
let s = lit.to_subpattern(symbol_table);
Alternative {
tag: s.tag,
subpatterns: s.subpatterns,
bound_vars: s.bound_vars,
guard: s.guard,
item
}
},
}
}
}
impl PatternLiteral {
fn to_subpattern(&self, symbol_table: &SymbolTable) -> Subpattern {
use self::PatternLiteral::*;
match self {
NumPattern { neg, num } => {
let comparison = Expr::Lit(match (neg, num) {
(false, ExpressionType::NatLiteral(n)) => Lit::Nat(*n),
(false, ExpressionType::FloatLiteral(f)) => Lit::Float(*f),
@ -269,16 +285,15 @@ impl Pattern {
f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("==".to_string())))),
args: vec![comparison, Expr::ConditionalTargetSigilValue],
});
Alternative {
Subpattern {
tag: None,
subpatterns: vec![],
guard,
bound_vars: vec![],
item
}
},
PatternLiteral::StringPattern(_s) => unimplemented!(),
PatternLiteral::BoolPattern(b) => {
StringPattern(_s) => unimplemented!(),
BoolPattern(b) => {
let guard = Some(if *b {
Expr::ConditionalTargetSigilValue
} else {
@ -287,34 +302,22 @@ impl Pattern {
args: vec![Expr::ConditionalTargetSigilValue]
}
});
Alternative {
Subpattern {
tag: None,
subpatterns: vec![],
guard,
bound_vars: vec![],
item
}
},
PatternLiteral::VarPattern(var) => match symbol_table.lookup_by_name(var) {
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 {
VarPattern(var) => match symbol_table.lookup_by_name(var) {
Some(symbol) => handle_symbol(symbol, &vec![]),
None => Subpattern {
tag: None,
subpatterns: vec![],
guard: None,
bound_vars: vec![Some(var.clone())],
item
}
}
},
}
}
}