From 60f4383ee1df7c326ac887b7c1750575c3becbb8 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 19 Aug 2010 18:23:34 +0000 Subject: Move PEnv::parse implementation to parse.cpp and remove PEnv::parseTuple. git-svn-id: http://svn.drobilla.net/resp/resp@263 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/parse.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- src/resp.hpp | 39 +-------------------------------------- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/parse.cpp b/src/parse.cpp index 2c59c56..3184f8a 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -23,6 +23,50 @@ using namespace std; +ATuple* +parseTuple(PEnv& penv, const ATuple* e) +{ + ATuple* ret = new ATuple(e->loc); + FOREACHP(ATuple::const_iterator, i, e) + ret->push_back(penv.parse(*i)); + return ret; +} + +AST* +PEnv::parse(const AST* exp) +{ + const ATuple* tup = exp->to(); + if (tup) { + if (tup->empty()) throw Error(exp->loc, "call to empty list"); + if (!tup->head()->to()) { + MF mf = mac(*tup->head()->to()); + const AST* expanded = (mf ? mf(*this, exp) : exp); + const ATuple* expanded_tup = expanded->to(); + const PEnv::Handler* h = handler(true, *expanded_tup->head()->to()); + if (h) + return h->func(*this, expanded, h->arg); + } + ATuple* parsed_tup = parseTuple(*this, tup); + return new ACall(parsed_tup); // Parse as regular call + } + const ALexeme* lex = exp->to(); + assert(lex); + if (isdigit((*lex)[0])) { + const std::string& s = *lex; + if (s.find('.') == string::npos) + return new ALiteral(strtol(s.c_str(), NULL, 10), exp->loc); + else + return new ALiteral(strtod(s.c_str(), NULL), exp->loc); + } else if ((*lex)[0] == '\"') { + return new AString(exp->loc, lex->substr(1, lex->length() - 2)); + } else { + const PEnv::Handler* h = handler(false, *lex); + if (h) + return h->func(*this, exp, h->arg); + } + return sym(*lex, exp->loc); +} + /*************************************************************************** * Macro Functions * @@ -69,7 +113,7 @@ template inline AST* parseCall(PEnv& penv, const AST* exp, void* arg) { - return new C(penv.parseTuple(exp->to())); + return new C(parseTuple(penv, exp->to())); } template @@ -85,7 +129,7 @@ parseFn(PEnv& penv, const AST* exp, void* arg) const ATuple* texp = exp->to(); ATuple::const_iterator a = texp->begin(); THROW_IF(++a == texp->end(), exp->loc, "Unexpected end of `fn' form"); - ATuple* prot = penv.parseTuple((*a++)->to()); + ATuple* prot = parseTuple(penv, (*a++)->to()); AFn* ret = tup(exp->loc, penv.sym("fn"), prot, 0); while (a != texp->end()) ret->push_back(penv.parse(*a++)); diff --git a/src/resp.hpp b/src/resp.hpp index 4be28bd..e551c0c 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -521,44 +521,7 @@ struct PEnv : private map { return sym; } } - ATuple* parseTuple(const ATuple* e) { - ATuple* ret = new ATuple(e->loc); - FOREACHP(ATuple::const_iterator, i, e) - ret->push_back(parse(*i)); - return ret; - } - AST* parse(const AST* exp) { - const ATuple* tup = exp->to(); - if (tup) { - if (tup->empty()) throw Error(exp->loc, "call to empty list"); - if (!tup->head()->to()) { - MF mf = mac(*tup->head()->to()); - const AST* expanded = (mf ? mf(*this, exp) : exp); - const ATuple* expanded_tup = expanded->to(); - const PEnv::Handler* h = handler(true, *expanded_tup->head()->to()); - if (h) - return h->func(*this, expanded, h->arg); - } - ATuple* parsed_tup = parseTuple(tup); - return new ACall(parsed_tup); // Parse as regular call - } - const ALexeme* lex = exp->to(); - assert(lex); - if (isdigit((*lex)[0])) { - const std::string& s = *lex; - if (s.find('.') == string::npos) - return new ALiteral(strtol(s.c_str(), NULL, 10), exp->loc); - else - return new ALiteral(strtod(s.c_str(), NULL), exp->loc); - } else if ((*lex)[0] == '\"') { - return new AString(exp->loc, lex->substr(1, lex->length() - 2)); - } else { - const PEnv::Handler* h = handler(false, *lex); - if (h) - return h->func(*this, exp, h->arg); - } - return sym(*lex, exp->loc); - } + AST* parse(const AST* exp); unsigned symID; }; -- cgit v1.2.1