]> git.mdlowis.com Git - archive/dlang.git/commitdiff
Created macro processing class to register new macros and expand macro usages on...
authorMike D. Lowis <mike@mdlowis.com>
Thu, 10 May 2012 20:30:45 +0000 (16:30 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Thu, 10 May 2012 20:30:45 +0000 (16:30 -0400)
example.dl
source/dllexer/dllexer.cpp
source/dllexer/dllexer.h
source/dlparser/dlparser.cpp
source/dlparser/dlparser.h
source/visitors/macroprocessor.cpp [new file with mode: 0644]
source/visitors/macroprocessor.h [new file with mode: 0644]

index 6bc0e7d600e26711ff14eca8082ad13073e4bf7f..70fe059cdc06d74017e2aae810259e34a9750006 100644 (file)
@@ -11,53 +11,52 @@ foo(1 2)
 foo(1 2 3)
 
 # Definition and assignment
-#define foo 5 end
-#set! foo 6 end
+define foo 5 end
+set! foo 6 end
 
 # Lambda expressions
-#lambda () end
-#lambda (a) end
-#lambda (a b) end
-#lambda (a b c) end
-#
-#lambda ()
-#    foo()
-#end
-#
-#lambda (a)
-#    foo(a)
-#end
-#
-#lambda (a b)
-#    foo(a b)
-#end
-#
-#lambda (a b c)
-#    foo(a b c)
-#end
+lambda () end
+lambda (a) end
+lambda (a b) end
+lambda (a b c) end
+
+lambda ()
+    foo(a)
+end
+
+lambda (a)
+    foo(a)
+end
+
+lambda (a b)
+    foo(a b)
+end
+
+lambda (a b c)
+    foo(a b c)
+end
 
 # Begin block
-#begin end
-#
-#begin
-#    foo()
-#end
-#
-#begin
-#    foo()
-#    bar()
-#end
+begin end
+
+begin
+    foo()
+end
+
+begin
+    foo()
+    bar()
+end
 
 # If statement
-#if conditional
-#    if_branch
-#else
-#    else_branch
-#end
-#
-#if conditional
-#    if_branch
-#end
+if conditional
+    if_branch
+    else_branch
+end
+
+if conditional
+    if_branch
+end
 
 # Infix operator expression
 (1 add 1)
@@ -66,12 +65,12 @@ foo(1 2 3)
 (1 - (1 + 1))
 
 # Macros
-#macro let (:= =) ;
-#    (a := b)
-#        define a b end
-#    (a = b)
-#        set! a b end
-#end
-#
-#let foo := "bar" ;
-#let foo = 5 ;
+macro let (:= =) ;
+    (a := b)
+        define a b end
+    (a = b)
+        set! a b end
+end
+
+let foo := "bar" end
+let foo = 5 end
index 8385b6b78ee6d945b620d8e5e61b4b4d88542e1e..8b1cbca060592b3cfacaaff63fec596d7d51d552 100644 (file)
@@ -120,7 +120,7 @@ Token DLLexer::next(void)
 
             Id(ret);
 
-            if( escaped && (ret.text().compare( terminator_string ) == 0) )
+            if( !escaped && (ret.text().compare( terminator_string ) == 0) )
             {
                 ret.type( TERM );
             }
index d68789c994e6b7fba7a8feebfa9aa3f93d660148..27e7f8ee493a7ba98c02fddb2cbdc6887ce37108 100644 (file)
@@ -18,16 +18,17 @@ typedef enum TokenTypes
     EXP_LIST   = 8,
     MACRO      = 9,
     TRANSFORM  = 10,
-    LPAR       = 11,
-    RPAR       = 12,
-    TERM       = 13,
+    MACRO_APP  = 11,
+    LPAR       = 12,
+    RPAR       = 13,
+    TERM       = 14,
 
     // Datatypes
-    ID         = 14,
-    NUM        = 15,
-    CHAR       = 16,
-    STRING     = 17,
-    SYMBOL     = 18,
+    ID         = 15,
+    NUM        = 16,
+    CHAR       = 17,
+    STRING     = 18,
+    SYMBOL     = 19,
 } eTokenTypes;
 
 typedef struct {
index e6d3403476d6e3fc9797767afba0be787bfc6df0..8af9bdddb97fdcb1bd44935c6e3dd213950d044e 100644 (file)
@@ -1,17 +1,18 @@
 #include "dlparser.h"
 #include "exception.h"
 #include "common.h"
+#include "macroprocessor.h"
 
 using namespace std;
 
 DLParser::DLParser() : BTParser()
 {
-    core_forms.insert( pair<string,eTokenTypes>("define", DEFINE) );
-    core_forms.insert( pair<string,eTokenTypes>("set!",   ASSIGN) );
-    core_forms.insert( pair<string,eTokenTypes>("lambda", LAMBDA) );
-    core_forms.insert( pair<string,eTokenTypes>("begin",  BEGIN) );
-    core_forms.insert( pair<string,eTokenTypes>("if",     IF) );
-    core_forms.insert( pair<string,eTokenTypes>("macro",  MACRO) );
+    core_forms["define"] = DEFINE;
+    core_forms["set!"]   = ASSIGN;
+    core_forms["lambda"] = LAMBDA;
+    core_forms["begin"]  = BEGIN;
+    core_forms["if"]     = IF;
+    core_forms["macro"]  = MACRO;
 }
 
 DLParser::~DLParser()
@@ -20,17 +21,17 @@ DLParser::~DLParser()
 
 bool DLParser::isMacroName(void)
 {
-    return false;
+    return (macros.count( lookaheadToken(1).text() ) > 0);
 }
 
 bool DLParser::isCoreFormName(void)
 {
-    return false;
+    return (core_forms.count( lookaheadToken(1).text() ) > 0);
 }
 
 eTokenTypes DLParser::getCoreFormId(void)
 {
-    return (eTokenTypes)0;
+    return core_forms[ lookaheadToken(1).text() ];
 }
 
 void DLParser::parse(void)
@@ -87,6 +88,10 @@ AST* DLParser::Expression(void)
         ret = Application();
     }
 
+    // Register any new macros and expand any existing macros
+    MacroProcessor processor( macros );
+    processor.visit( ret );
+
     return ret;
 }
 
@@ -105,16 +110,16 @@ AST* DLParser::CoreForm(void)
             break;
 
         case LAMBDA:
-            ret = new AST(LAMBDA, 2, IdList(), ExpList());
+            ret = new AST(LAMBDA, 2, IdList(), ExpList(TERM));
             break;
 
         case BEGIN:
-            ret = new AST(BEGIN, 1, ExpList());
+            ret = new AST(BEGIN, 1, ExpList(TERM));
             break;
 
         case IF:
             ret = new AST(IF, 2, Expression(), Expression());
-            if(lookaheadType(1) != RPAR)
+            if(lookaheadType(1) != TERM)
             {
                 ret->addChild( Expression() );
             }
@@ -159,6 +164,23 @@ AST* DLParser::Application(void)
     // Macro Expression
     if ( isMacroName() )
     {
+        // Save current terminator
+
+        // Register the new terminator
+
+        // Consume the name
+        cout << "Macro Usage" << endl;
+        ret = new AST( MACRO_APP, 1, new AST( lookaheadToken(1) ));
+        consume();
+
+        // Consume the expressions
+        while( lookaheadType(1) != TERM )
+        {
+            ret->addChild( Expression() );
+        }
+        match(TERM);
+
+        // Reset the terminator to its old value
     }
 
     // Traditional Function Application
@@ -167,7 +189,7 @@ AST* DLParser::Application(void)
         ret = new AST( lookaheadToken(1) );
         consume();
         match(LPAR);
-        ret = new AST(APPLY, 2, ret, ExpList());
+        ret = new AST(APPLY, 2, ret, ExpList(RPAR));
         match(RPAR);
     }
 
@@ -204,10 +226,10 @@ AST* DLParser::Literal(void)
     return ret;
 }
 
-AST* DLParser::ExpList(void)
+AST* DLParser::ExpList(eTokenTypes term)
 {
     AST* ret = new AST(EXP_LIST);
-    while(RPAR != lookaheadType(1))
+    while(term != lookaheadType(1))
     {
         ret->addChild( Expression() );
     }
index 2683d629296b57367094e3d4cb064db7c4bd37c3..5e4931097699c69e39f6b0d3b4548413d99ca731 100644 (file)
@@ -30,7 +30,7 @@ class DLParser : public BTParser
         AST* CoreForm(void);
         AST* Application(void);
         AST* Literal(void);
-        AST* ExpList(void);
+        AST* ExpList(eTokenTypes term);
         AST* IdList(void);
 };
 
diff --git a/source/visitors/macroprocessor.cpp b/source/visitors/macroprocessor.cpp
new file mode 100644 (file)
index 0000000..dc058e6
--- /dev/null
@@ -0,0 +1,38 @@
+#include "macroprocessor.h"
+
+MacroProcessor::MacroProcessor(std::set<std::string> &macros) : macro_registry(macros)
+{
+}
+
+void MacroProcessor::beforeVisit(AST* cur, int depth)
+{
+}
+
+void MacroProcessor::afterVisit(AST* cur, int depth)
+{
+}
+
+void MacroProcessor::beforeChildren(AST* cur, int depth)
+{
+    if (cur->type() == MACRO_APP)
+    {
+        //expandMacro(cur);
+    }
+}
+
+void MacroProcessor::afterChildren(AST* cur, int depth)
+{
+    if (cur->type() == MACRO)
+    {
+        macro_registry.insert( (*(cur->children()->begin()))->text() );
+    }
+}
+
+void MacroProcessor::beforeChild(AST* cur, int depth)
+{
+}
+
+void MacroProcessor::afterChild(AST* cur, int depth)
+{
+}
+
diff --git a/source/visitors/macroprocessor.h b/source/visitors/macroprocessor.h
new file mode 100644 (file)
index 0000000..a9ba0bd
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef MACRO_PROCESSOR_H
+#define MACRO_PROCESSOR_H
+
+#include <set>
+#include "ivisitor.h"
+#include "dllexer.h"
+
+class MacroProcessor : public IVisitor {
+    protected:
+        std::set<std::string>& macro_registry;
+    public:
+        MacroProcessor(std::set<std::string>& macros);
+    private:
+        void beforeVisit(AST* cur, int depth);
+        void afterVisit(AST* cur, int depth);
+        void beforeChildren(AST* cur, int depth);
+        void afterChildren(AST* cur, int depth);
+        void beforeChild(AST* cur, int depth);
+        void afterChild(AST* cur, int depth);
+};
+
+#endif