aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/parse.cpp98
-rw-r--r--src/resp.hpp9
2 files changed, 40 insertions, 67 deletions
diff --git a/src/parse.cpp b/src/parse.cpp
index 8a7e3e0..e26ef1f 100644
--- a/src/parse.cpp
+++ b/src/parse.cpp
@@ -40,18 +40,6 @@ PEnv::parse(const AST* exp)
THROW_IF(tup->empty(), exp->loc, "Call to empty list");
const ALexeme* form = tup->head()->to_lexeme();
if (form) {
- MF mf = mac(*form);
- if (mf) {
- exp = mf(*this, exp)->as_tuple(); // Apply macro
- tup = exp->to_tuple();
- }
- }
- }
-
- if (tup) {
- 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);
if (h)
return h->func(*this, exp, h->arg); // Parse special form
@@ -83,48 +71,6 @@ PEnv::parse(const AST* exp)
/***************************************************************************
- * Macro Functions *
- ***************************************************************************/
-
-inline const AST*
-macDef(PEnv& penv, const AST* exp)
-{
- const ATuple* tup = exp->to_tuple();
- ATuple::const_iterator i = tup->begin();
- THROW_IF(i == tup->end(), tup->loc, "Unexpected end of `def' macro call");
- const AST* arg1 = *(++i);
- THROW_IF(i == tup->end(), arg1->loc, "Unexpected end of `def' macro call");
- if (arg1->to_lexeme()) {
- return exp;
- } else {
- // (def (f x) y) => (def f (fn (x) y))
- const ATuple* pat = arg1->to_tuple();
-
- List<ATuple, const AST> argsExp;
- ATuple::const_iterator j = pat->begin();
- for (++j; j != pat->end(); ++j)
- argsExp.push_back(*j);
- argsExp.head->loc = exp->loc;
- const AST* body = *(++i);
-
- List<ATuple, const AST> fnExp;
- fnExp.push_back(new ALexeme(exp->loc, "fn"));
- fnExp.push_back(argsExp.head);
- for (; i != tup->end(); ++i)
- fnExp.push_back(*i);
- fnExp.head->loc = body->loc;
-
- List<ATuple, const AST> ret;
- ret.push_back(tup->head());
- ret.push_back(pat->head());
- ret.push_back(fnExp.head);
- ret.head->loc = exp->loc;
- return ret.head;
- }
-}
-
-
-/***************************************************************************
* Parser Functions *
***************************************************************************/
@@ -166,6 +112,45 @@ parseQuote(PEnv& penv, const AST* exp, void* arg)
return ret;
}
+inline const AST*
+parseDef(PEnv& penv, const AST* exp, void* arg)
+{
+ const ATuple* tup = exp->as_tuple();
+
+ ATuple::const_iterator i = tup->begin();
+ THROW_IF(i == tup->end(), tup->loc, "Unexpected end of `def' form");
+ const AST* arg1 = *(++i);
+ THROW_IF(i == tup->end(), arg1->loc, "Unexpected end of `def' form");
+ if (arg1->to_lexeme()) {
+ return parseCall(penv, exp, arg);
+ } else {
+ // (def (f x) y) => (def f (fn (x) y))
+ const ATuple* pat = arg1->to_tuple();
+
+ List<ATuple, const AST> argsExp;
+ ATuple::const_iterator j = pat->begin();
+ for (++j; j != pat->end(); ++j)
+ argsExp.push_back(*j);
+ argsExp.head->loc = exp->loc;
+ const AST* body = *(++i);
+
+ List<ATuple, const AST> fnExp;
+ fnExp.push_back(new ALexeme(exp->loc, "fn"));
+ fnExp.push_back(argsExp.head);
+ for (; i != tup->end(); ++i)
+ fnExp.push_back(*i);
+ fnExp.head->loc = body->loc;
+
+ List<ATuple, const AST> ret;
+ ret.push_back(tup->head());
+ ret.push_back(pat->head());
+ ret.push_back(fnExp.head);
+ ret.head->loc = exp->loc;
+
+ return parseCall(penv, ret.head, arg);
+ }
+}
+
/***************************************************************************
* Language Definition *
@@ -189,15 +174,12 @@ initLang(PEnv& penv, TEnv& tenv)
penv.reg(false, "#t", PEnv::Handler(parseBool, &trueVal));
penv.reg(false, "#f", PEnv::Handler(parseBool, &falseVal));
- // Macros
- penv.defmac("def", macDef);
-
// 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(parseCall));
+ penv.reg(true, "def", PEnv::Handler(parseDef));
penv.reg(true, "def-type", PEnv::Handler(parseCall));
penv.reg(true, "match", PEnv::Handler(parseCall));
diff --git a/src/resp.hpp b/src/resp.hpp
index 35edf31..d524aae 100644
--- a/src/resp.hpp
+++ b/src/resp.hpp
@@ -572,11 +572,9 @@ AST::operator==(const AST& rhs) const
struct PEnv : private map<const string, ASymbol*> {
PEnv() : symID(0) {}
typedef const AST* (*PF)(PEnv&, const AST*, void*); ///< Parse Function
- typedef const AST* (*MF)(PEnv&, const AST*); ///< Macro Function
struct Handler { Handler(PF f, void* a=0) : func(f), arg(a) {} PF func; void* arg; };
map<const string, Handler> aHandlers; ///< Atom parse functions
map<const string, Handler> lHandlers; ///< List parse functions
- map<const string, MF> macros; ///< Macro functions
void reg(bool list, const string& s, const Handler& h) {
(list ? lHandlers : aHandlers).insert(make_pair(sym(s)->str(), h));
}
@@ -585,13 +583,6 @@ struct PEnv : private map<const string, ASymbol*> {
map<string, Handler>::const_iterator i = handlers.find(s);
return (i != handlers.end()) ? &i->second : NULL;
}
- void defmac(const string& s, const MF f) {
- macros.insert(make_pair(s, f));
- }
- MF mac(const ALexeme& s) const {
- map<string, MF>::const_iterator i = macros.find(s.cppstr);
- return (i != macros.end()) ? i->second : NULL;
- }
string gensymstr(const char* s="_") { return (format("%s_%d") % s % symID++).str(); }
ASymbol* gensym(const char* s="_") { return sym(gensymstr(s)); }
ASymbol* sym(const string& s, Cursor c=Cursor()) {