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))
}
fn case_match_expression(&mut self, cond: Expr, alternatives: Vec<Alternative>) -> EvalResult<Node> {
match self.expression(Node::Expr(cond))? {
Node::PrimObject { tag, items, .. } => {
for alt in alternatives {
if alt.tag.map(|t| t == tag).unwrap_or(true) {
let mut inner_state = State {
values: self.values.new_scope(None),
symbol_table_handle: self.symbol_table_handle.clone(),
};
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)
fn case_match_expression(&mut self, cond: Expr, alternatives: Vec<Alternative>) -> EvalResult<Node> {
let cond = self.expression(Node::Expr(cond))?;
for alt in alternatives {
// no matter what type of condition we have, ignore alternative if the guard evaluates false
if let Some(guard_expr) = alt.guard {
let guard_expr = guard_expr.clone().replace_conditional_target_sigil(&cond);
match self.expression(guard_expr.to_node())? {
Node::Expr(Expr::Lit(::reduced_ast::Lit::Bool(true))) => (),
_ => continue,
}
}
return Err(format!("PrimObject failed pattern match"));
},
Node::PrimTuple { .. } => Err(format!("Tuples not implemented")), //TODO make a distinction between not yet implemented and an actual runtime error
Node::Expr(e) => {
for alt in alternatives {
match (alt.guard, alt.tag) {
(Some(ref guard_expr), None) => {
let guard_expr = guard_expr.clone().replace_conditional_target_sigil(&e);
match self.expression(guard_expr.to_node())? {
Node::Expr(Expr::Lit(::reduced_ast::Lit::Bool(true))) =>
return self.block(alt.item),
_ => continue,
match cond {
Node::PrimObject { tag, items, .. } => {
if alt.tag.map(|t| t == tag).unwrap_or(true) {
let mut inner_state = State {
values: self.values.new_scope(None),
symbol_table_handle: self.symbol_table_handle.clone(),
};
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() });
}
}
},
(None, None) => return self.block(alt.item),
_ => return Err(format!("Shouldn't match an expr against a pattern"))
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)
}
},
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> {
use self::ValueEntry::*;

View File

@ -248,8 +248,15 @@ impl Pattern {
item
}
},
TuplePattern(_items) => {
unimplemented!()
TuplePattern(subpatterns) => {
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) => {
unimplemented!()