From: Mike D. Lowis Date: Tue, 6 Mar 2012 22:00:33 +0000 (-0500) Subject: Macro parameter parsing is in place. AST substitution still needs to be implemented X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=ddc792e1f53291ac277aaef166be511834c006ef;p=archive%2Fdlang.git Macro parameter parsing is in place. AST substitution still needs to be implemented --- diff --git a/example.dl b/example.dl index 7b4b898..fbe8218 100644 --- a/example.dl +++ b/example.dl @@ -52,3 +52,8 @@ foo = { (Ex Bk) : exec_if($1, $2), (Ex Bk Bk) : exec_if($1, $2, $3) ] + +#if (1==1) +#{ +# +#} diff --git a/source/dlparser/dlparser.cpp b/source/dlparser/dlparser.cpp index 6a1f668..868e048 100644 --- a/source/dlparser/dlparser.cpp +++ b/source/dlparser/dlparser.cpp @@ -18,13 +18,121 @@ AST* DLParser::parse(void) bool DLParser::isMacro( Token& token ) { bool ret = false; - if( (token.type() == ID) && (macros.find(token.text()) != macros.end()) ) + if( (token.type() == ID) + && (macros.find(token.text()) != macros.end()) ) { ret = true; } return ret; } +AST* DLParser::MacroExpansion() +{ + AST* ret = NULL; + Macro macro = macros[ lookaheadToken(1).text() ]; + std::list::iterator patt_it; + + for(patt_it = macro.begin(); patt_it != macro.end(); patt_it++) + { + if( speculate_MacroPatternMatch(*patt_it) ) + { + ret = MacroPatternMatch(*patt_it); + break; + } + } + + if (ret == NULL) + { + throw "Did not find a matching pattern for keyword "; + } + + return ret; +} + +bool DLParser::speculate_MacroPatternMatch(Pattern patt) +{ + bool success = false; + + mark(); + try + { + delete MacroPatternMatch(patt); + } + catch (Exception e) + { + success = false; + } + release(); + + return success; + +} + +AST* DLParser::MacroPatternMatch(Pattern patt) +{ + std::list params; + std::list::iterator patt_it; + + for(patt_it = patt.begin(); patt_it != patt.end(); patt_it++) + { + AST* param = NULL; + string text = lookaheadToken(1).text(); + switch( *patt_it ) + { + case MAP_TYP: + param = MapLiteral(); + break; + + case VECT_TYP: + param = VectorLiteral(); + break; + + case LIST_TYP: + break; + + case BLK_TYP: + param = FuncLiteral(); + break; + + case ID_TYP: + match(ID); + param = _new AST(ID,text); + break; + + case NUM_TYP: + match(NUM); + param = _new AST(NUM,text); + break; + + case CHAR_TYP: + match(CHAR); + param = _new AST(CHAR,text); + break; + + case STR_TYP: + match(STRING); + param = _new AST(STRING,text); + break; + + case SYM_TYP: + match(SYMBOL); + param = _new AST(SYMBOL,text); + break; + + case EXPR_TYP: + param = Literal(); + break; + + default: + throw Exception(lookaheadToken(1).line(), lookaheadToken(1).column()); + break; + } + params.push_back(param); + } + + return NULL; +} + bool DLParser::speculate_GroupExpr(void) { bool success = true; @@ -91,19 +199,19 @@ AST* DLParser::Expression(void) AST* DLParser::AssignExpr(void) { AST* ret = NULL; - //if( isMacro( lookaheadToken(1) ) ) - //{ - // ret = MacroExpansion(); - //} - //else - //{ + if( isMacro( lookaheadToken(1) ) ) + { + ret = MacroExpansion(); + } + else + { ret = LogicalExpr(); if(lookaheadType(1) == ASSIGN) { match(ASSIGN); ret = new AST(ASSIGN, 2, ret, LogicalExpr()); } - //} + } return ret; } diff --git a/source/dlparser/dlparser.h b/source/dlparser/dlparser.h index d011f7a..3eafbd6 100644 --- a/source/dlparser/dlparser.h +++ b/source/dlparser/dlparser.h @@ -18,6 +18,7 @@ class DLParser : public BTParser bool isMacro(Token& token); bool speculate_GroupExpr(void); bool speculate_MapLiteral(void); + bool speculate_MacroPatternMatch(Pattern patt); /********************************************************************** * EBNF Syntax Grammar @@ -103,6 +104,8 @@ class DLParser : public BTParser AST* MacroDefinition(void); std::list MacroPatternList(void); Pattern MacroPattern(void); + AST* MacroExpansion(); + AST* MacroPatternMatch(Pattern patt); // Helper rules for lists and blocks of expressions AST* ExpList(TokenType_T node_type, TokenType_T terminator); diff --git a/source/dlparser/macro/macro.cpp b/source/dlparser/macro/macro.cpp index db05bab..20171cf 100644 --- a/source/dlparser/macro/macro.cpp +++ b/source/dlparser/macro/macro.cpp @@ -1,5 +1,9 @@ #include "macro.h" +Macro::Macro() +{ +} + Macro::Macro(const std::list& patts) : patterns(patts) { } @@ -8,3 +12,13 @@ Macro::~Macro() { } +std::list::iterator Macro::begin() +{ + return patterns.begin(); +} + +std::list::iterator Macro::end() +{ + return patterns.end(); +} + diff --git a/source/dlparser/macro/macro.h b/source/dlparser/macro/macro.h index bb5c36c..f5b62ae 100644 --- a/source/dlparser/macro/macro.h +++ b/source/dlparser/macro/macro.h @@ -8,8 +8,11 @@ class Macro { private: std::list patterns; public: + Macro(); Macro(const std::list& patts); ~Macro(); + std::list::iterator begin(); + std::list::iterator end(); }; #endif diff --git a/source/dlparser/macro/pattern.cpp b/source/dlparser/macro/pattern.cpp index c29cec4..74526ff 100644 --- a/source/dlparser/macro/pattern.cpp +++ b/source/dlparser/macro/pattern.cpp @@ -8,3 +8,13 @@ Pattern::~Pattern() { } +std::list::iterator Pattern::begin() +{ + return pattern.begin(); +} + +std::list::iterator Pattern::end() +{ + return pattern.end(); +} + diff --git a/source/dlparser/macro/pattern.h b/source/dlparser/macro/pattern.h index 00d3688..a884ae4 100644 --- a/source/dlparser/macro/pattern.h +++ b/source/dlparser/macro/pattern.h @@ -7,7 +7,7 @@ typedef enum { MAP_TYP, VECT_TYP, - LiST_TYP, + LIST_TYP, BLK_TYP, ID_TYP, NUM_TYP, @@ -24,6 +24,8 @@ class Pattern { public: Pattern(const std::list& patt, const AST* ast); ~Pattern(); + std::list::iterator begin(); + std::list::iterator end(); }; #endif