From bf757dcc9b66ebb3bf7e2df8e8c7d3a011ddd6dc Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 25 Dec 2012 00:09:34 +0000 Subject: Preliminary syntax-rules macro implementation. git-svn-id: http://svn.drobilla.net/resp/trunk@443 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/resp.hpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src/resp.hpp') diff --git a/src/resp.hpp b/src/resp.hpp index e572473..fd861aa 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -490,6 +490,17 @@ ostream& operator<<(ostream& out, const Env& env) { * Parser: S-Expressions (SExp) -> AST Nodes (AST) * ***************************************************************************/ +struct Macro { + struct Rule { + Rule(const ATuple* p, const ATuple* t) : pattern(p), templ(t) {} + const ATuple* pattern; + const ATuple* templ; + }; + + std::set keywords; + std::list rules; +}; + /// Parse Time Environment (really just a symbol table) struct PEnv : private map { PEnv() : symID(0) {} @@ -514,6 +525,9 @@ struct PEnv : private map { typedef std::set Primitives; Primitives primitives; + typedef std::map Macros; + Macros macros; + unsigned symID; }; @@ -528,6 +542,11 @@ struct Constraint : public pair { : pair(a, b) {} }; +static inline bool +is_dots(const AST* exp) { + return (exp->to_symbol() && exp->as_symbol()->str() == "..."); +} + /// Type substitution struct Subst : public list { Subst(const AST* s=0, const AST* t=0) { @@ -550,8 +569,18 @@ struct Subst : public list { if (in->as_tuple()->empty()) return in; List out; - for (ATuple::const_iterator i = in->as_tuple()->begin(); i != in->as_tuple()->end(); ++i) - out.push_back(apply(*i)); + for (auto i : *in->as_tuple()) { + if (is_dots(i)) { + const_iterator o = find(i); + if (o != end()) { + for (auto j : *o->second->as_tuple()) { + out.push_back(apply(j)); + } + } + } else { + out.push_back(apply(i)); + } + } if (out.head) out.head->loc = in->loc; return out.head; -- cgit v1.2.1