From 2eda11ca28589991471ff3251cccc2471424770e Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 29 Dec 2010 22:50:20 +0000 Subject: Destructuring (i.e. working `match'). git-svn-id: http://svn.drobilla.net/resp/resp@374 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/constrain.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/constrain.cpp') diff --git a/src/constrain.cpp b/src/constrain.cpp index a4a1ad3..78accf6 100644 --- a/src/constrain.cpp +++ b/src/constrain.cpp @@ -241,6 +241,8 @@ constrain_match(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) THROW_IF(!pattern, exp->loc, "pattern expression expected"); const ASymbol* name = (*pattern->begin())->to_symbol(); THROW_IF(!name, (*pattern->begin())->loc, "pattern does not start with a symbol"); + THROW_IF(!tenv.ref(name), name->loc, + (format("undefined constructor `%1%'") % name->sym()).str()); const AST* consT = *tenv.ref(name); @@ -251,8 +253,16 @@ constrain_match(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) THROW_IF(i == call->end(), pattern->loc, "missing pattern body"); const AST* body = *i++; + + TEnv::Frame frame; + ATuple::const_iterator ti = consT->as_tuple()->iter_at(2); + for (ATuple::const_iterator pi = pattern->iter_at(1); pi != pattern->end(); ++pi) + frame.push_back(make_pair((*pi)->as_symbol()->sym(), *ti++)); + + tenv.push(frame); resp_constrain(tenv, c, body); - c.constrain(tenv, body, retT); + c.constrain(tenv, body, retT); + tenv.pop(); } c.constrain(tenv, call, retT); c.constrain(tenv, matchee, matcheeT); -- cgit v1.2.1