full if matching working with basic patterns
This commit is contained in:
parent
a99a5f10a4
commit
abe2db25b2
@ -468,7 +468,7 @@ mod eval_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_patterns() {
|
||||
fn if_is_patterns() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let x = Some(9); if x is Some(q) then { q } else { 0 }"#;
|
||||
@ -479,4 +479,21 @@ type Option<T> = Some(T) | None
|
||||
let x = None; if x is Some(q) then { q } else { 0 }"#;
|
||||
fresh_env!(source, "0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_if_matching() {
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let a = None
|
||||
if a { is None -> 4, is Some(x) -> x }
|
||||
"#;
|
||||
fresh_env!(source, "4");
|
||||
|
||||
let source = r#"
|
||||
type Option<T> = Some(T) | None
|
||||
let a = Some(99)
|
||||
if a { is None -> 4, is Some(x) -> x }
|
||||
"#;
|
||||
fresh_env!(source, "99");
|
||||
}
|
||||
}
|
||||
|
@ -189,12 +189,11 @@ fn reduce_if_expression(discriminator: &Discriminator, body: &IfExpressionBody,
|
||||
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.values.get(name).expect(&format!("Symbol {} not found", name));
|
||||
|
||||
fn handle_symbol(symbol: &Symbol, subpatterns: &Vec<Pattern>, item: Vec<Stmt>) -> Alternative {
|
||||
let tag = match symbol.spec {
|
||||
SymbolSpec::DataConstructor { index, .. } => index.clone(),
|
||||
_ => panic!("Bad symbol"),
|
||||
_ => 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()),
|
||||
@ -207,6 +206,12 @@ impl Pattern {
|
||||
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)
|
||||
},
|
||||
TuplePattern(_items) => {
|
||||
unimplemented!()
|
||||
@ -219,7 +224,12 @@ impl Pattern {
|
||||
PatternLiteral::NumPattern { neg, num } => unimplemented!(),
|
||||
PatternLiteral::StringPattern(_s) => unimplemented!(),
|
||||
PatternLiteral::BoolPattern(_b) => unimplemented!(),
|
||||
PatternLiteral::VarPattern(_var) => unimplemented!(),
|
||||
PatternLiteral::VarPattern(var) => match symbol_table.lookup_by_name(var) {
|
||||
Some(symbol) => handle_symbol(symbol, &vec![], item),
|
||||
None => {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user