More work here
This commit is contained in:
parent
f4ff92302f
commit
2d6c9010b9
@ -202,41 +202,40 @@ fn reduce_if_expression(discriminator: &Discriminator, body: &IfExpressionBody,
|
|||||||
* x is SomeBigOldEnum(_, x, Some(t))
|
* x is SomeBigOldEnum(_, x, Some(t))
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
fn handle_symbol(symbol: &Symbol, subpatterns: &Vec<Pattern>) -> Subpattern {
|
||||||
|
use self::Pattern::*;
|
||||||
|
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"),
|
||||||
|
};
|
||||||
|
let bound_vars = subpatterns.iter().map(|p| match p {
|
||||||
|
Literal(PatternLiteral::VarPattern(var)) => Some(var.clone()),
|
||||||
|
_ => None,
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
/*
|
||||||
|
let guard_equality_exprs: Vec<Expr> = subpatterns.iter().map(|p| match p {
|
||||||
|
Literal(lit) => match lit {
|
||||||
|
_ => unimplemented!()
|
||||||
|
},
|
||||||
|
_ => unimplemented!()
|
||||||
|
}).collect();
|
||||||
|
*/
|
||||||
|
|
||||||
|
let guard = None;
|
||||||
|
let subpatterns = vec![];
|
||||||
|
|
||||||
|
Subpattern {
|
||||||
|
tag: Some(tag),
|
||||||
|
subpatterns,
|
||||||
|
guard,
|
||||||
|
bound_vars,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Pattern {
|
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>) -> 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"),
|
|
||||||
};
|
|
||||||
let bound_vars = subpatterns.iter().map(|p| match p {
|
|
||||||
Literal(PatternLiteral::VarPattern(var)) => Some(var.clone()),
|
|
||||||
_ => None,
|
|
||||||
}).collect();
|
|
||||||
|
|
||||||
/*
|
|
||||||
let guard_equality_exprs: Vec<Expr> = subpatterns.iter().map(|p| match p {
|
|
||||||
Literal(lit) => match lit {
|
|
||||||
_ => unimplemented!()
|
|
||||||
},
|
|
||||||
_ => unimplemented!()
|
|
||||||
}).collect();
|
|
||||||
*/
|
|
||||||
|
|
||||||
let guard = None;
|
|
||||||
let subpatterns = vec![];
|
|
||||||
|
|
||||||
Subpattern {
|
|
||||||
tag: Some(tag),
|
|
||||||
subpatterns,
|
|
||||||
guard,
|
|
||||||
bound_vars,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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,69 +255,73 @@ 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);
|
||||||
let comparison = Expr::Lit(match (neg, num) {
|
Alternative {
|
||||||
(false, ExpressionType::NatLiteral(n)) => Lit::Nat(*n),
|
tag: s.tag,
|
||||||
(false, ExpressionType::FloatLiteral(f)) => Lit::Float(*f),
|
subpatterns: s.subpatterns,
|
||||||
(true, ExpressionType::NatLiteral(n)) => Lit::Int(-1*(*n as i64)),
|
bound_vars: s.bound_vars,
|
||||||
(true, ExpressionType::FloatLiteral(f)) => Lit::Float(-1.0*f),
|
guard: s.guard,
|
||||||
_ => panic!("This should never happen")
|
item
|
||||||
});
|
|
||||||
let guard = Some(Expr::Call {
|
|
||||||
f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("==".to_string())))),
|
|
||||||
args: vec![comparison, Expr::ConditionalTargetSigilValue],
|
|
||||||
});
|
|
||||||
Alternative {
|
|
||||||
tag: None,
|
|
||||||
subpatterns: vec![],
|
|
||||||
guard,
|
|
||||||
bound_vars: vec![],
|
|
||||||
item
|
|
||||||
}
|
|
||||||
},
|
|
||||||
PatternLiteral::StringPattern(_s) => unimplemented!(),
|
|
||||||
PatternLiteral::BoolPattern(b) => {
|
|
||||||
let guard = Some(if *b {
|
|
||||||
Expr::ConditionalTargetSigilValue
|
|
||||||
} else {
|
|
||||||
Expr::Call {
|
|
||||||
f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("!".to_string())))),
|
|
||||||
args: vec![Expr::ConditionalTargetSigilValue]
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Alternative {
|
|
||||||
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 {
|
|
||||||
tag: None,
|
|
||||||
subpatterns: vec![],
|
|
||||||
guard: None,
|
|
||||||
bound_vars: vec![Some(var.clone())],
|
|
||||||
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),
|
||||||
|
(true, ExpressionType::NatLiteral(n)) => Lit::Int(-1*(*n as i64)),
|
||||||
|
(true, ExpressionType::FloatLiteral(f)) => Lit::Float(-1.0*f),
|
||||||
|
_ => panic!("This should never happen")
|
||||||
|
});
|
||||||
|
let guard = Some(Expr::Call {
|
||||||
|
f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("==".to_string())))),
|
||||||
|
args: vec![comparison, Expr::ConditionalTargetSigilValue],
|
||||||
|
});
|
||||||
|
Subpattern {
|
||||||
|
tag: None,
|
||||||
|
subpatterns: vec![],
|
||||||
|
guard,
|
||||||
|
bound_vars: vec![],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
StringPattern(_s) => unimplemented!(),
|
||||||
|
BoolPattern(b) => {
|
||||||
|
let guard = Some(if *b {
|
||||||
|
Expr::ConditionalTargetSigilValue
|
||||||
|
} else {
|
||||||
|
Expr::Call {
|
||||||
|
f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("!".to_string())))),
|
||||||
|
args: vec![Expr::ConditionalTargetSigilValue]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Subpattern {
|
||||||
|
tag: None,
|
||||||
|
subpatterns: vec![],
|
||||||
|
guard,
|
||||||
|
bound_vars: vec![],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
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())],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Declaration {
|
impl Declaration {
|
||||||
fn reduce(&self, symbol_table: &SymbolTable) -> Stmt {
|
fn reduce(&self, symbol_table: &SymbolTable) -> Stmt {
|
||||||
use self::Declaration::*;
|
use self::Declaration::*;
|
||||||
|
Loading…
Reference in New Issue
Block a user