From 19ec222ce04979ccd4125ecb6c95a86302ba937c Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 5 Dec 2010 01:45:37 +0000 Subject: Simplify parser. git-svn-id: http://svn.drobilla.net/resp/resp@300 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/lift.cpp | 4 +--- src/parse.cpp | 38 ++++++++++++++++---------------------- src/resp.hpp | 10 ++++------ 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/lift.cpp b/src/lift.cpp index 10c4707..fe901f0 100644 --- a/src/lift.cpp +++ b/src/lift.cpp @@ -33,9 +33,7 @@ lift_symbol(CEnv& cenv, Code& code, const ASymbol* sym) throw() const std::string& cppstr = sym->cppstr; if (!cenv.liftStack.empty() && cppstr == cenv.name(cenv.liftStack.top().fn)) { return cenv.penv.sym("_me"); // Reference to innermost function - } else if (!cenv.penv.handler(true, cppstr) - && !cenv.penv.handler(false, cppstr) - && !cenv.code.innermost(sym)) { + } else if (!cenv.penv.handler(cppstr) && !cenv.code.innermost(sym)) { const int32_t index = cenv.liftStack.top().index(sym); diff --git a/src/parse.cpp b/src/parse.cpp index e26ef1f..21a9963 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -40,7 +40,7 @@ PEnv::parse(const AST* exp) THROW_IF(tup->empty(), exp->loc, "Call to empty list"); const ALexeme* form = tup->head()->to_lexeme(); if (form) { - const PEnv::Handler* h = handler(true, form->cppstr); + const PEnv::Handler* h = handler(form->cppstr); if (h) return h->func(*this, exp, h->arg); // Parse special form @@ -61,10 +61,10 @@ PEnv::parse(const AST* exp) return new ALiteral(T_FLOAT, strtod(s.c_str(), NULL), exp->loc); } else if (lex->cppstr[0] == '\"') { return new AString(exp->loc, lex->cppstr.substr(1, lex->cppstr.length() - 2)); - } else { - const PEnv::Handler* h = handler(false, lex->cppstr); - if (h) - return h->func(*this, exp, h->arg); + } else if (lex->cppstr == "#t") { + return new ALiteral(T_BOOL, true, exp->loc); + } else if (lex->cppstr == "#f") { + return new ALiteral(T_BOOL, false, exp->loc); } return sym(lex->cppstr, exp->loc); } @@ -160,28 +160,22 @@ void initLang(PEnv& penv, TEnv& tenv) { // Types - tenv.def(penv.sym("Nothing"), new AType(penv.sym("Nothing"), AType::PRIM)); tenv.def(penv.sym("Bool"), new AType(penv.sym("Bool"), AType::PRIM)); - tenv.def(penv.sym("Int"), new AType(penv.sym("Int"), AType::PRIM)); tenv.def(penv.sym("Float"), new AType(penv.sym("Float"), AType::PRIM)); - tenv.def(penv.sym("String"), new AType(penv.sym("String"), AType::PRIM)); + tenv.def(penv.sym("Int"), new AType(penv.sym("Int"), AType::PRIM)); tenv.def(penv.sym("Lexeme"), new AType(penv.sym("Lexeme"), AType::PRIM)); + tenv.def(penv.sym("Nothing"), new AType(penv.sym("Nothing"), AType::PRIM)); tenv.def(penv.sym("Quote"), new AType(penv.sym("Quote"), AType::PRIM)); + tenv.def(penv.sym("String"), new AType(penv.sym("String"), AType::PRIM)); - // Literals - static bool trueVal = true; - static bool falseVal = false; - penv.reg(false, "#t", PEnv::Handler(parseBool, &trueVal)); - penv.reg(false, "#f", PEnv::Handler(parseBool, &falseVal)); - // Special forms - penv.reg(true, "fn", PEnv::Handler(parseFn)); - penv.reg(true, "quote", PEnv::Handler(parseQuote)); - penv.reg(true, "if", PEnv::Handler(parseCall)); - penv.reg(true, ".", PEnv::Handler(parseCall)); - penv.reg(true, "def", PEnv::Handler(parseDef)); - penv.reg(true, "def-type", PEnv::Handler(parseCall)); - penv.reg(true, "match", PEnv::Handler(parseCall)); + penv.reg(".", PEnv::Handler(parseCall)); + penv.reg("def", PEnv::Handler(parseDef)); + penv.reg("def-type", PEnv::Handler(parseCall)); + penv.reg("fn", PEnv::Handler(parseFn)); + penv.reg("if", PEnv::Handler(parseCall)); + penv.reg("match", PEnv::Handler(parseCall)); + penv.reg("quote", PEnv::Handler(parseQuote)); // Numeric primitives penv.primitives.insert("+"); @@ -199,5 +193,5 @@ initLang(PEnv& penv, TEnv& tenv) penv.primitives.insert("<"); penv.primitives.insert("<="); FOREACH (PEnv::Primitives::const_iterator, i, penv.primitives) - penv.reg(true, *i, PEnv::Handler(parseCall)); + penv.reg(*i, PEnv::Handler(parseCall)); } diff --git a/src/resp.hpp b/src/resp.hpp index d524aae..899f4fb 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -573,13 +573,11 @@ struct PEnv : private map { PEnv() : symID(0) {} typedef const AST* (*PF)(PEnv&, const AST*, void*); ///< Parse Function struct Handler { Handler(PF f, void* a=0) : func(f), arg(a) {} PF func; void* arg; }; - map aHandlers; ///< Atom parse functions - map lHandlers; ///< List parse functions - void reg(bool list, const string& s, const Handler& h) { - (list ? lHandlers : aHandlers).insert(make_pair(sym(s)->str(), h)); + map handlers; ///< List parse functions + void reg(const string& s, const Handler& h) { + handlers.insert(make_pair(sym(s)->str(), h)); } - const Handler* handler(bool list, const string& s) const { - const map& handlers = list ? lHandlers : aHandlers; + const Handler* handler(const string& s) const { map::const_iterator i = handlers.find(s); return (i != handlers.end()) ? &i->second : NULL; } -- cgit v1.2.1