From fd332f979c4216a560925639ae99f2155ab56044 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 10 Dec 2010 18:28:49 +0000 Subject: Simplify if into nested 2-branch (scheme style) ifs at simplify stage. git-svn-id: http://svn.drobilla.net/resp/resp@346 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/simplify.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src/simplify.cpp') diff --git a/src/simplify.cpp b/src/simplify.cpp index 31eb83a..35178ac 100644 --- a/src/simplify.cpp +++ b/src/simplify.cpp @@ -25,6 +25,34 @@ using namespace std; +static const AST* +simplify_if(CEnv& cenv, const ATuple* aif) throw() +{ + List copy(aif->loc, cenv.penv.sym("if"), NULL); + copy.push_back(aif->list_ref(1)); + copy.push_back(aif->list_ref(2)); + + ATuple* tail = copy.tail; + ATuple::const_iterator i = aif->iter_at(3); + for (; ; ++i) { + ATuple::const_iterator next = i; + if (++next == aif->end()) + break; + + List inner_if((*i)->loc, cenv.penv.sym("if"), *i, *next, NULL); + tail->last(new ATuple(inner_if.head, NULL, Cursor())); + tail = inner_if.tail; + + cenv.setTypeSameAs(inner_if, aif); + + i = next; // jump 2 elements (to the next predicate) + } + + tail->last(new ATuple(*i, NULL, Cursor())); + cenv.setTypeSameAs(copy, aif); + return copy; +} + static const AST* simplify_match(CEnv& cenv, const ATuple* match) throw() { @@ -57,9 +85,8 @@ simplify_match(CEnv& cenv, const ATuple* match) throw() copyIf.push_back(resp_simplify(cenv, body)); } copyIf.push_back(cenv.penv.sym("__unreachable")); - copy.push_back(copyIf); - cenv.setTypeSameAs(copyIf, match); + copy.push_back(simplify_if(cenv, copyIf)); cenv.setTypeSameAs(copy, match); return copy; @@ -89,6 +116,8 @@ resp_simplify(CEnv& cenv, const AST* ast) throw() if (form == "match") return simplify_match(cenv, list); + else if (form == "if") + return simplify_if(cenv, list); else return simplify_list(cenv, list); } -- cgit v1.2.1