]> gitweb.ps.run Git - toc/blobdiff - src/visit.h
add antlr source code and ReadMe
[toc] / src / visit.h
index 3fe2cb2b96f833aa16ae47e63b026bcb287b127d..279a4fcab65084e7835aa0bc030958179d1fcb51 100644 (file)
@@ -4,22 +4,26 @@
 \r
 #include <functional>\r
 \r
+// struct with callback functions for all relevant types\r
+// tree can be walked selectively by providing only\r
+// needed callbacks\r
 struct Visitor {\r
-  std::function<void(const Type &, const std::shared_ptr<Context> ctx)> onType = [](auto, auto){};\r
-  std::function<void(const Expr &, const std::shared_ptr<Context> ctx)> onExpr = [](auto, auto){};\r
-  std::function<void(const Stmt &, const std::shared_ptr<Context> ctx)> onStmt = [](auto, auto){};\r
-  std::function<void(const Body &, const std::shared_ptr<Context> ctx)> onBody = [](auto, auto){};\r
-  std::function<void(const Function &, const std::shared_ptr<Context> ctx)> onFunction = [](auto, auto){};\r
-  std::function<void(const Variable &, const std::shared_ptr<Context> ctx)> onVariable = [](auto, auto){};\r
+  std::function<void(const Type &, const std::shared_ptr<Context> ctx)>                   onType = [](auto, auto){};\r
+  std::function<void(const Expr &, const std::shared_ptr<Context> ctx)>                   onExpr = [](auto, auto){};\r
+  std::function<void(const Stmt &, const std::shared_ptr<Context> ctx)>                   onStmt = [](auto, auto){};\r
+  std::function<void(const Body &, const std::shared_ptr<Context> ctx)>                   onBody = [](auto, auto){};\r
+  std::function<void(const Function &, const std::shared_ptr<Context> ctx)>               onFunction = [](auto, auto){};\r
+  std::function<void(const Variable &, const std::shared_ptr<Context> ctx)>               onVariable = [](auto, auto){};\r
   std::function<void(const StructMember<Function> &, const std::shared_ptr<Context> ctx)> onStructMethod = [](auto, auto){};\r
   std::function<void(const StructMember<Variable> &, const std::shared_ptr<Context> ctx)> onStructMember = [](auto, auto){};\r
-  std::function<void(const Struct &, const std::shared_ptr<Context> ctx)> onStruct = [](auto, auto){};\r
-  std::function<void(const Namespace &, const std::shared_ptr<Context> ctx)> onNamespace = [](auto, auto){};\r
-  std::function<void(const Program &, const std::shared_ptr<Context> ctx)> onProgram = [](auto, auto){};\r
+  std::function<void(const Struct &, const std::shared_ptr<Context> ctx)>                 onStruct = [](auto, auto){};\r
+  std::function<void(const Namespace &, const std::shared_ptr<Context> ctx)>              onNamespace = [](auto, auto){};\r
+  std::function<void(const Program &, const std::shared_ptr<Context> ctx)>                onProgram = [](auto, auto){};\r
 };\r
 \r
 #define VISIT(XS) for (auto x : XS) visit(x);\r
 \r
+// simply walk IR by recursively calling functions for all children\r
 struct Visit {\r
 private:\r
   Visitor v;\r
@@ -141,7 +145,7 @@ public:
     \r
     ctx = x.ctx;\r
 \r
-    VISIT(x.namespaces)\r
+    VISIT(x.ctx->namespaces)\r
     VISIT(x.ctx->variables)\r
     VISIT(x.ctx->structs)\r
     VISIT(x.ctx->functions)\r
@@ -158,9 +162,9 @@ public:
     v.onFunction(x, ctx);\r
 \r
     if (x.defined) {\r
-      visit(x.body);\r
       for (auto v : x.parameters)\r
         visit(v.type);\r
+      visit(x.body);\r
     }\r
   }\r
   void visit(const StructMember<Function> & x)\r
@@ -188,7 +192,7 @@ public:
 \r
     ctx = x.ctx;\r
 \r
-    VISIT(x.namespaces)\r
+    VISIT(x.ctx->namespaces)\r
     VISIT(x.ctx->variables)\r
     VISIT(x.ctx->structs)\r
     VISIT(x.ctx->functions)\r