]> git.mdlowis.com Git - archive/dlang.git/commitdiff
Implemented macro parameter parsing.
authorMike D. Lowis <mike@mdlowis.com>
Wed, 7 Mar 2012 18:16:15 +0000 (13:16 -0500)
committerMike D. Lowis <mike@mdlowis.com>
Wed, 7 Mar 2012 18:16:15 +0000 (13:16 -0500)
example.dl
source/dlparser/dlparser.cpp
source/dlparser/dlparser.h
source/dlparser/macro/pattern.cpp
source/dlparser/macro/pattern.h

index fbe8218f921458aa4dd2f56e7ecdd9712538b35f..4254f91b98f21e70ac91fed54c007f0a2f6310eb 100644 (file)
@@ -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)
+{
+
+}
index 868e04818ed19ab1471b403467058d63bdd25e84..247002eb7a25747fe552b2df5d174eca2a376de7 100644 (file)
@@ -4,6 +4,16 @@
 
 DLParser::DLParser() : BTParser(_new DLLexer())
 {
+    pattern_types.insert( std::pair<std::string,PatternType_T>( "Map", MAP_TYP ));
+    pattern_types.insert( std::pair<std::string,PatternType_T>( "Vector", VECT_TYP ));
+    pattern_types.insert( std::pair<std::string,PatternType_T>( "List", LIST_TYP ));
+    pattern_types.insert( std::pair<std::string,PatternType_T>( "Block", BLK_TYP ));
+    pattern_types.insert( std::pair<std::string,PatternType_T>( "Id", ID_TYP ));
+    pattern_types.insert( std::pair<std::string,PatternType_T>( "Num", NUM_TYP ));
+    pattern_types.insert( std::pair<std::string,PatternType_T>( "Char", CHAR_TYP ));
+    pattern_types.insert( std::pair<std::string,PatternType_T>( "String", STR_TYP ));
+    pattern_types.insert( std::pair<std::string,PatternType_T>( "Symbol", SYM_TYP ));
+    pattern_types.insert( std::pair<std::string,PatternType_T>( "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<AST*> params;
+    std::vector<AST*> params;
     std::list<PatternType_T>::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);
index 3eafbd601d8c95c03290e34795294ce72e4296b9..5c55491e5a215184e5f1fdbf9f172f628879a644 100644 (file)
@@ -62,6 +62,7 @@ class DLParser : public BTParser
         //         | SYMBOL
         //
         // MapLiteral = '{' (Literal ':' AssignExpr)* '}'
+        //
         // VectorLiteral = '[' ExpList ']'
         //
         // ListLiteral = '(' ExpList ')'
index 74526ff7aa17a0ba5fb5e112edb81a283fb3c4e2..fbf53872f61055d306e683badf368265fba9bf6e 100644 (file)
@@ -18,3 +18,16 @@ std::list<PatternType_T>::iterator Pattern::end()
     return pattern.end();
 }
 
+AST* Pattern::accept(std::vector<AST*>& 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;
+}
+
index a884ae48fa575f28a045d014a2bff241590ebb62..b8591baf4c2a97e1f1f0d134ac6c0afa75e3884c 100644 (file)
@@ -2,19 +2,20 @@
 #define PATTERN_H
 
 #include <list>
+#include <vector>
 #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<PatternType_T>::iterator begin();
         std::list<PatternType_T>::iterator end();
+        AST* accept(std::vector<AST*>& params);
 };
 
 #endif