Refactoring matching - WIP

doesn't work yet
This commit is contained in:
greg 2018-11-01 02:43:47 -07:00
parent ec5a9d457e
commit cab4702bd6
2 changed files with 48 additions and 42 deletions

View File

@ -369,53 +369,52 @@ impl<'a> State<'a> {
Ok(Node::Expr(Expr::Unit)) Ok(Node::Expr(Expr::Unit))
} }
fn case_match_expression(&mut self, cond: Expr, alternatives: Vec<Alternative>) -> EvalResult<Node> { fn case_match_expression(&mut self, cond: Expr, alternatives: Vec<Alternative>) -> EvalResult<Node> {
match self.expression(Node::Expr(cond))? { let cond = self.expression(Node::Expr(cond))?;
Node::PrimObject { tag, items, .. } => { for alt in alternatives {
for alt in alternatives { // no matter what type of condition we have, ignore alternative if the guard evaluates false
if alt.tag.map(|t| t == tag).unwrap_or(true) { if let Some(guard_expr) = alt.guard {
let mut inner_state = State { let guard_expr = guard_expr.clone().replace_conditional_target_sigil(&cond);
values: self.values.new_scope(None), match self.expression(guard_expr.to_node())? {
symbol_table_handle: self.symbol_table_handle.clone(), Node::Expr(Expr::Lit(::reduced_ast::Lit::Bool(true))) => (),
}; _ => continue,
for (bound_var, val) in alt.bound_vars.iter().zip(items.iter()) {
if let Some(bv) = bound_var.as_ref() {
inner_state.values.insert(bv.clone(), ValueEntry::Binding { constant: true, val: val.clone() });
}
}
if let Some(guard_expr) = alt.guard {
let evaled_guard = inner_state.expression(guard_expr.to_node());
println!("EVALED GUARD: {:?}", evaled_guard);
//continue
}
return inner_state.block(alt.item)
} }
} }
return Err(format!("PrimObject failed pattern match"));
}, match cond {
Node::PrimTuple { .. } => Err(format!("Tuples not implemented")), //TODO make a distinction between not yet implemented and an actual runtime error Node::PrimObject { tag, items, .. } => {
Node::Expr(e) => { if alt.tag.map(|t| t == tag).unwrap_or(true) {
for alt in alternatives { let mut inner_state = State {
match (alt.guard, alt.tag) { values: self.values.new_scope(None),
(Some(ref guard_expr), None) => { symbol_table_handle: self.symbol_table_handle.clone(),
let guard_expr = guard_expr.clone().replace_conditional_target_sigil(&e); };
match self.expression(guard_expr.to_node())? { for (bound_var, val) in alt.bound_vars.iter().zip(items.iter()) {
Node::Expr(Expr::Lit(::reduced_ast::Lit::Bool(true))) => if let Some(bv) = bound_var.as_ref() {
return self.block(alt.item), inner_state.values.insert(bv.clone(), ValueEntry::Binding { constant: true, val: val.clone() });
_ => continue, }
} }
},
(None, None) => return self.block(alt.item), if let Some(guard_expr) = alt.guard {
_ => return Err(format!("Shouldn't match an expr against a pattern")) let evaled_guard = inner_state.expression(guard_expr.to_node());
println!("EVALED GUARD: {:?}", evaled_guard);
//continue
}
return inner_state.block(alt.item)
}
},
Node::PrimTuple { .. } => {
return Err(format!("Prim tuple not done"))
},
Node::Expr(e) => {
if let None = alt.tag {
return self.block(alt.item)
}
} }
} }
return Err(format!("Expr Failed pattern match"));
} }
Err(format!("{:?} failed pattern match", cond))
} }
}
fn value(&mut self, name: Rc<String>) -> EvalResult<Node> { fn value(&mut self, name: Rc<String>) -> EvalResult<Node> {
use self::ValueEntry::*; use self::ValueEntry::*;

View File

@ -248,8 +248,15 @@ impl Pattern {
item item
} }
}, },
TuplePattern(_items) => { TuplePattern(subpatterns) => {
unimplemented!() let s = handle_symbol(None, subpatterns);
Alternative {
tag: s.tag,
subpatterns: s.subpatterns,
guard: s.guard,
bound_vars: s.bound_vars,
item
}
}, },
Record(_name, _pairs) => { Record(_name, _pairs) => {
unimplemented!() unimplemented!()