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