]> git.mdlowis.com Git - archive/dlang.git/commitdiff
Macro parameter parsing is in place. AST substitution still needs to be implemented
authorMike D. Lowis <mike@mdlowis.com>
Tue, 6 Mar 2012 22:00:33 +0000 (17:00 -0500)
committerMike D. Lowis <mike@mdlowis.com>
Tue, 6 Mar 2012 22:00:33 +0000 (17:00 -0500)
example.dl
source/dlparser/dlparser.cpp
source/dlparser/dlparser.h
source/dlparser/macro/macro.cpp
source/dlparser/macro/macro.h
source/dlparser/macro/pattern.cpp
source/dlparser/macro/pattern.h

index 7b4b8985d24c16229fadcf8f1c5619a87d8c6369..fbe8218f921458aa4dd2f56e7ecdd9712538b35f 100644 (file)
@@ -52,3 +52,8 @@ foo = {
     (Ex Bk) : exec_if($1, $2),
     (Ex Bk Bk) : exec_if($1, $2, $3)
 ]
+
+#if (1==1)
+#{
+#
+#}
index 6a1f668298737324525f08ae0e70266d87817e73..868e04818ed19ab1471b403467058d63bdd25e84 100644 (file)
@@ -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<Pattern>::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 <macro-name>";
+    }
+
+    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<AST*> params;
+    std::list<PatternType_T>::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;
 }
 
index d011f7adcacdb8c83be642eaa88ded9d05a02533..3eafbd601d8c95c03290e34795294ce72e4296b9 100644 (file)
@@ -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<Pattern> 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);
index db05bab524b94eb67715e2ae7887ede74b5f8844..20171cf18cc47fe881ad351794f1263c8f947709 100644 (file)
@@ -1,5 +1,9 @@
 #include "macro.h"
 
+Macro::Macro()
+{
+}
+
 Macro::Macro(const std::list<Pattern>& patts) : patterns(patts)
 {
 }
@@ -8,3 +12,13 @@ Macro::~Macro()
 {
 }
 
+std::list<Pattern>::iterator Macro::begin()
+{
+    return patterns.begin();
+}
+
+std::list<Pattern>::iterator Macro::end()
+{
+    return patterns.end();
+}
+
index bb5c36c31c116752418924534042516faaa2a427..f5b62ae41af723da16739ab3e32e99089e13b8ca 100644 (file)
@@ -8,8 +8,11 @@ class Macro {
     private:
         std::list<Pattern> patterns;
     public:
+        Macro();
         Macro(const std::list<Pattern>& patts);
         ~Macro();
+        std::list<Pattern>::iterator begin();
+        std::list<Pattern>::iterator end();
 };
 
 #endif
index c29cec49dce8f9af04d7e68daaaaca7c7e892b45..74526ff7aa17a0ba5fb5e112edb81a283fb3c4e2 100644 (file)
@@ -8,3 +8,13 @@ Pattern::~Pattern()
 {
 }
 
+std::list<PatternType_T>::iterator Pattern::begin()
+{
+    return pattern.begin();
+}
+
+std::list<PatternType_T>::iterator Pattern::end()
+{
+    return pattern.end();
+}
+
index 00d368898bf35248219bbbbf8b10717a2aac714c..a884ae48fa575f28a045d014a2bff241590ebb62 100644 (file)
@@ -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<PatternType_T>& patt, const AST* ast);
         ~Pattern();
+        std::list<PatternType_T>::iterator begin();
+        std::list<PatternType_T>::iterator end();
 };
 
 #endif