]> gitweb.ps.run Git - toc/blobdiff - src/visit.h
add comments, fix struct/function lookup
[toc] / src / visit.h
index 0cfb9e6036b7442cb7a0896ff9ea8a11c465117d..1175a86adc7424e6693c4bac874b0dfc8cf4c287 100644 (file)
@@ -5,17 +5,17 @@
 #include <functional>\r
 \r
 struct Visitor {\r
-  std::function<void(const Type &, const std::vector<std::string> & namespaces)> onType = [](auto, auto){};\r
-  std::function<void(const Expr &, const std::vector<std::string> & namespaces)> onExpr = [](auto, auto){};\r
-  std::function<void(const Stmt &, const std::vector<std::string> & namespaces)> onStmt = [](auto, auto){};\r
-  std::function<void(const Body &, const std::vector<std::string> & namespaces)> onBody = [](auto, auto){};\r
-  std::function<void(const Function &, const std::vector<std::string> & namespaces)> onFunction = [](auto, auto){};\r
-  std::function<void(const Variable &, const std::vector<std::string> & namespaces)> onVariable = [](auto, auto){};\r
-  std::function<void(const StructMember<Function> &, const std::vector<std::string> & namespaces)> onStructMethod = [](auto, auto){};\r
-  std::function<void(const StructMember<Variable> &, const std::vector<std::string> & namespaces)> onStructMember = [](auto, auto){};\r
-  std::function<void(const Struct &, const std::vector<std::string> & namespaces)> onStruct = [](auto, auto){};\r
-  std::function<void(const Namespace &, const std::vector<std::string> & namespaces)> onNamespace = [](auto, auto){};\r
-  std::function<void(const Program &, const std::vector<std::string> & namespaces)> onProgram = [](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
 };\r
 \r
 #define VISIT(XS) for (auto x : XS) visit(x);\r
@@ -23,7 +23,7 @@ struct Visitor {
 struct Visit {\r
 private:\r
   Visitor v;\r
-  std::vector<std::string> namespaces;\r
+  std::shared_ptr<Context> ctx;\r
 public:\r
   Visit(Visitor v)\r
   {\r
@@ -31,11 +31,11 @@ public:
   }\r
   void visit(const Type & x)\r
   {\r
-    v.onType(x, namespaces);\r
+    v.onType(x, ctx);\r
   }\r
   void visit(const Expr & x)\r
   {\r
-    v.onExpr(x, namespaces);\r
+    v.onExpr(x, ctx);\r
 \r
     switch (x.type)\r
     {\r
@@ -79,7 +79,7 @@ public:
   }\r
   void visit(const Stmt & x)\r
   {\r
-    v.onStmt(x, namespaces);\r
+    v.onStmt(x, ctx);\r
 \r
     switch (x.type)\r
     {\r
@@ -126,32 +126,36 @@ public:
   }\r
   void visit(const Body & x)\r
   {\r
-    v.onBody(x, namespaces);\r
+    v.onBody(x, ctx);\r
+\r
+    ctx = x.ctx;\r
 \r
     VISIT(x.ctx->variables)\r
     VISIT(x.statements)\r
+\r
+    ctx = ctx->parent;\r
   }\r
   void visit(const Namespace & x)\r
   {\r
-    v.onNamespace(x, namespaces);\r
-\r
-    namespaces.push_back(x.name);\r
+    v.onNamespace(x, ctx);\r
+    \r
+    ctx = x.ctx;\r
 \r
-    VISIT(x.namespaces)\r
+    VISIT(x.ctx->namespaces)\r
     VISIT(x.ctx->variables)\r
-    VISIT(x.structs)\r
-    VISIT(x.functions)\r
+    VISIT(x.ctx->structs)\r
+    VISIT(x.ctx->functions)\r
 \r
-    namespaces.pop_back();\r
+    ctx = ctx->parent;\r
   }\r
   void visit(const Variable & x)\r
   {\r
-    v.onVariable(x, namespaces);\r
+    v.onVariable(x, ctx);\r
     visit(x.type);\r
   }\r
   void visit(const Function & x)\r
   {\r
-    v.onFunction(x, namespaces);\r
+    v.onFunction(x, ctx);\r
 \r
     if (x.defined) {\r
       visit(x.body);\r
@@ -161,31 +165,35 @@ public:
   }\r
   void visit(const StructMember<Function> & x)\r
   {\r
-    v.onStructMethod(x, namespaces);\r
+    v.onStructMethod(x, ctx);\r
 \r
     visit(x.t);\r
   }\r
   void visit(const StructMember<Variable> & x)\r
   {\r
-    v.onStructMember(x, namespaces);\r
+    v.onStructMember(x, ctx);\r
 \r
     visit(x.t);\r
   }\r
   void visit(const Struct & x)\r
   {\r
-    v.onStruct(x, namespaces);\r
+    v.onStruct(x, ctx);\r
 \r
     VISIT(x.members)\r
     VISIT(x.methods)\r
   }\r
   void visit(const Program & x)\r
   {\r
-    v.onProgram(x, namespaces);\r
+    v.onProgram(x, ctx);\r
 \r
-    VISIT(x.namespaces)\r
+    ctx = x.ctx;\r
+\r
+    VISIT(x.ctx->namespaces)\r
     VISIT(x.ctx->variables)\r
-    VISIT(x.structs)\r
-    VISIT(x.functions)\r
+    VISIT(x.ctx->structs)\r
+    VISIT(x.ctx->functions)\r
+\r
+    ctx = nullptr;\r
   }\r
 };\r
 \r