From: Mike D. Lowis Date: Thu, 8 Mar 2012 14:33:36 +0000 (-0500) Subject: Macro expansion is now fully functional! X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=5d915ebec39f813ccd63066b7de2a86e56c7160e;p=archive%2Fdlang.git Macro expansion is now fully functional! --- diff --git a/example.dl b/example.dl index 4254f91..e684e12 100644 --- a/example.dl +++ b/example.dl @@ -3,64 +3,64 @@ #------------------------------------------------------------------------------ # VectorLiteral -foo = [] -foo = [1] -foo = [1,2,3] -foo = foo[1] -foo = [1,2,3,4,5][2] +#foo = [] +#foo = [1] +#foo = [1,2,3] +#foo = foo[1] +#foo = [1,2,3,4,5][2] # ListLiteral -foo = () -foo = (1,2,3) -foo = foo[1] -foo = (1,2,3,4,5)[2] +#foo = () +#foo = (1,2,3) +#foo = foo[1] +#foo = (1,2,3,4,5)[2] # FuncLiteral foo = { 1 + 1 } -foo = {|a| a + 1} -foo = {|a,b| a + b } -foo = foo(1,2) -foo = ({|a,b| a + b })(1,2) +#foo = {|a| a + 1} +#foo = {|a,b| a + b } +#foo = foo(1,2) +#foo = ({|a,b| a + b })(1,2) # ID -foo = bar -foo.bar = bar +#foo = bar +#foo.bar = bar # NUM -foo = 1 -foo = 1.0 +#foo = 1 +#foo = 1.0 # CHAR -foo = 'a' +#foo = 'a' # STRING -foo = "some string" -foo = "12345"[2] +#foo = "some string" +#foo = "12345"[2] # SYMBOL -foo = $some_symbol +#foo = $some_symbol # MAP -foo = { - $foo : 1 + 1, - $bar : 2 + 2, - $stuff : 3 + 3 -} +#foo = { +# $foo : 1 + 1, +# $bar : 2 + 2, +# $stuff : 3 + 3 +#} # Macro -% if [ - (Expression Block Block) : exec_if($1, $2, $3), - (Expression Block) : exec_if($1, $2) -] +#% if [ +# (Expression Block Block) : exec_if($1, $2, $3), +# (Expression Block) : exec_if($1, $2) +#] -if (1==1) -{ - 1 + 1 -}{ - -} - -if (1 == 1) -{ - -} +#if (1==1) +#{ +# 1 + 1 +#}{ +# +#} +# +#if (1 == 1) +#{ +# +#} diff --git a/source/dlparser/dlparser.cpp b/source/dlparser/dlparser.cpp index 247002e..de4f912 100644 --- a/source/dlparser/dlparser.cpp +++ b/source/dlparser/dlparser.cpp @@ -80,8 +80,10 @@ bool DLParser::speculate_MacroPatternMatch(Pattern patt) AST* DLParser::MacroPatternMatch(Pattern patt) { + AST* ret = NULL; std::vector params; std::list::iterator patt_it; + for(patt_it = patt.begin(); patt_it != patt.end(); patt_it++) { AST* param = NULL; @@ -137,10 +139,20 @@ AST* DLParser::MacroPatternMatch(Pattern patt) throw Exception(lookaheadToken(1).line(), lookaheadToken(1).column()); break; } - params.push_back(param); + + if( !isSpeculating() ) + { + params.push_back(param); + } + else + { + delete param; + } } - return patt.accept( params ); + ret = (isSpeculating()) ? _new AST(MACRO) : patt.accept( params ); + + return ret; } bool DLParser::speculate_GroupExpr(void) @@ -511,7 +523,6 @@ Pattern DLParser::MacroPattern(void) { std::string text = lookaheadToken(1).text(); match(ID); - //pattern.push_back( str_to_patt_type(text) ); if( pattern_types.find(text) != pattern_types.end() ) { pattern.push_back( pattern_types[ text ] ); diff --git a/source/dlparser/macro/pattern.cpp b/source/dlparser/macro/pattern.cpp index fbf5387..0fcfb5b 100644 --- a/source/dlparser/macro/pattern.cpp +++ b/source/dlparser/macro/pattern.cpp @@ -1,4 +1,5 @@ #include "pattern.h" +#include "dllexer.h" Pattern::Pattern(const std::list& patt, const AST* ast) : pattern(patt), expr_ast(ast) { @@ -18,16 +19,44 @@ std::list::iterator Pattern::end() return pattern.end(); } +void Pattern::apply(AST* cur,std::vector& params) +{ + if (cur != NULL) + { + list* children = cur->children(); + list::iterator it = children->begin(); + + // Visit the tree + for(; it != children->end(); it++) + { + if ((*it)->type() == SYMBOL) + { + unsigned int arg; + istringstream((*it)->text()) >> arg; + + if (arg <= params.size()) + { + AST* temp = *it; + *it = params[ arg - 1 ]; + delete temp; + } + else + { + throw "Invalid parameter number"; + } + } + else + { + apply( *it, params ); + } + } + } +} + AST* Pattern::accept(std::vector& params) { AST* ret = expr_ast->clone(); - // TODO: Perform AST substitution - // if size == to expected - // clone exp tree - // replace symbols with params - // else - // Throw exception - // endif + apply( ret, params ); return ret; } diff --git a/source/dlparser/macro/pattern.h b/source/dlparser/macro/pattern.h index b8591ba..225ec02 100644 --- a/source/dlparser/macro/pattern.h +++ b/source/dlparser/macro/pattern.h @@ -19,9 +19,11 @@ typedef enum { } PatternType_T; class Pattern { - private: + protected: std::list pattern; const AST* expr_ast; + private: + void apply(AST* cur,std::vector& params); public: Pattern(const std::list& patt, const AST* ast); ~Pattern();