From: Mike D. Lowis Date: Wed, 7 Mar 2012 18:16:15 +0000 (-0500) Subject: Implemented macro parameter parsing. X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=66905fc457051d6f44fdc68f8164d389cb63ea80;p=archive%2Fdlang.git Implemented macro parameter parsing. --- diff --git a/example.dl b/example.dl index fbe8218..4254f91 100644 --- a/example.dl +++ b/example.dl @@ -49,11 +49,18 @@ foo = { # Macro % if [ - (Ex Bk) : exec_if($1, $2), - (Ex Bk Bk) : exec_if($1, $2, $3) + (Expression Block Block) : exec_if($1, $2, $3), + (Expression Block) : exec_if($1, $2) ] -#if (1==1) -#{ -# -#} +if (1==1) +{ + 1 + 1 +}{ + +} + +if (1 == 1) +{ + +} diff --git a/source/dlparser/dlparser.cpp b/source/dlparser/dlparser.cpp index 868e048..247002e 100644 --- a/source/dlparser/dlparser.cpp +++ b/source/dlparser/dlparser.cpp @@ -4,6 +4,16 @@ DLParser::DLParser() : BTParser(_new DLLexer()) { + pattern_types.insert( std::pair( "Map", MAP_TYP )); + pattern_types.insert( std::pair( "Vector", VECT_TYP )); + pattern_types.insert( std::pair( "List", LIST_TYP )); + pattern_types.insert( std::pair( "Block", BLK_TYP )); + pattern_types.insert( std::pair( "Id", ID_TYP )); + pattern_types.insert( std::pair( "Num", NUM_TYP )); + pattern_types.insert( std::pair( "Char", CHAR_TYP )); + pattern_types.insert( std::pair( "String", STR_TYP )); + pattern_types.insert( std::pair( "Symbol", SYM_TYP )); + pattern_types.insert( std::pair( "Expression", EXPR_TYP )); } DLParser::~DLParser() @@ -51,7 +61,7 @@ AST* DLParser::MacroExpansion() bool DLParser::speculate_MacroPatternMatch(Pattern patt) { - bool success = false; + bool success = true; mark(); try @@ -70,9 +80,8 @@ bool DLParser::speculate_MacroPatternMatch(Pattern patt) AST* DLParser::MacroPatternMatch(Pattern patt) { - std::list params; + std::vector params; std::list::iterator patt_it; - for(patt_it = patt.begin(); patt_it != patt.end(); patt_it++) { AST* param = NULL; @@ -88,6 +97,7 @@ AST* DLParser::MacroPatternMatch(Pattern patt) break; case LIST_TYP: + param = ListLiteral(); break; case BLK_TYP: @@ -120,7 +130,7 @@ AST* DLParser::MacroPatternMatch(Pattern patt) break; case EXPR_TYP: - param = Literal(); + param = LogicalExpr(); break; default: @@ -130,7 +140,7 @@ AST* DLParser::MacroPatternMatch(Pattern patt) params.push_back(param); } - return NULL; + return patt.accept( params ); } bool DLParser::speculate_GroupExpr(void) @@ -502,7 +512,14 @@ Pattern DLParser::MacroPattern(void) std::string text = lookaheadToken(1).text(); match(ID); //pattern.push_back( str_to_patt_type(text) ); - pattern.push_back( EXPR_TYP ); + if( pattern_types.find(text) != pattern_types.end() ) + { + pattern.push_back( pattern_types[ text ] ); + } + else + { + throw Exception(lookaheadToken(1).line(), lookaheadToken(1).column()); + } } while( lookaheadType(1) == ID ); match(RPAR); diff --git a/source/dlparser/dlparser.h b/source/dlparser/dlparser.h index 3eafbd6..5c55491 100644 --- a/source/dlparser/dlparser.h +++ b/source/dlparser/dlparser.h @@ -62,6 +62,7 @@ class DLParser : public BTParser // | SYMBOL // // MapLiteral = '{' (Literal ':' AssignExpr)* '}' + // // VectorLiteral = '[' ExpList ']' // // ListLiteral = '(' ExpList ')' diff --git a/source/dlparser/macro/pattern.cpp b/source/dlparser/macro/pattern.cpp index 74526ff..fbf5387 100644 --- a/source/dlparser/macro/pattern.cpp +++ b/source/dlparser/macro/pattern.cpp @@ -18,3 +18,16 @@ std::list::iterator Pattern::end() return pattern.end(); } +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 + return ret; +} + diff --git a/source/dlparser/macro/pattern.h b/source/dlparser/macro/pattern.h index a884ae4..b8591ba 100644 --- a/source/dlparser/macro/pattern.h +++ b/source/dlparser/macro/pattern.h @@ -2,19 +2,20 @@ #define PATTERN_H #include +#include #include "ast.h" typedef enum { - MAP_TYP, - VECT_TYP, - LIST_TYP, - BLK_TYP, - ID_TYP, - NUM_TYP, - CHAR_TYP, - STR_TYP, - SYM_TYP, - EXPR_TYP + MAP_TYP = 0, + VECT_TYP = 1, + LIST_TYP = 2, + BLK_TYP = 3, + ID_TYP = 4, + NUM_TYP = 5, + CHAR_TYP = 6, + STR_TYP = 7, + SYM_TYP = 8, + EXPR_TYP = 9 } PatternType_T; class Pattern { @@ -26,6 +27,7 @@ class Pattern { ~Pattern(); std::list::iterator begin(); std::list::iterator end(); + AST* accept(std::vector& params); }; #endif