X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/3715a3f575b615f66e8ea7e57f83849e8bae4deb..17860defa84c6d8bc0e8bc088a7e09361f17db07:/src/visit.h diff --git a/src/visit.h b/src/visit.h index 0cfb9e6..3fe2cb2 100644 --- a/src/visit.h +++ b/src/visit.h @@ -5,17 +5,17 @@ #include struct Visitor { - std::function & namespaces)> onType = [](auto, auto){}; - std::function & namespaces)> onExpr = [](auto, auto){}; - std::function & namespaces)> onStmt = [](auto, auto){}; - std::function & namespaces)> onBody = [](auto, auto){}; - std::function & namespaces)> onFunction = [](auto, auto){}; - std::function & namespaces)> onVariable = [](auto, auto){}; - std::function &, const std::vector & namespaces)> onStructMethod = [](auto, auto){}; - std::function &, const std::vector & namespaces)> onStructMember = [](auto, auto){}; - std::function & namespaces)> onStruct = [](auto, auto){}; - std::function & namespaces)> onNamespace = [](auto, auto){}; - std::function & namespaces)> onProgram = [](auto, auto){}; + std::function ctx)> onType = [](auto, auto){}; + std::function ctx)> onExpr = [](auto, auto){}; + std::function ctx)> onStmt = [](auto, auto){}; + std::function ctx)> onBody = [](auto, auto){}; + std::function ctx)> onFunction = [](auto, auto){}; + std::function ctx)> onVariable = [](auto, auto){}; + std::function &, const std::shared_ptr ctx)> onStructMethod = [](auto, auto){}; + std::function &, const std::shared_ptr ctx)> onStructMember = [](auto, auto){}; + std::function ctx)> onStruct = [](auto, auto){}; + std::function ctx)> onNamespace = [](auto, auto){}; + std::function ctx)> onProgram = [](auto, auto){}; }; #define VISIT(XS) for (auto x : XS) visit(x); @@ -23,7 +23,7 @@ struct Visitor { struct Visit { private: Visitor v; - std::vector namespaces; + std::shared_ptr ctx; public: Visit(Visitor v) { @@ -31,11 +31,11 @@ public: } void visit(const Type & x) { - v.onType(x, namespaces); + v.onType(x, ctx); } void visit(const Expr & x) { - v.onExpr(x, namespaces); + v.onExpr(x, ctx); switch (x.type) { @@ -79,7 +79,7 @@ public: } void visit(const Stmt & x) { - v.onStmt(x, namespaces); + v.onStmt(x, ctx); switch (x.type) { @@ -126,32 +126,36 @@ public: } void visit(const Body & x) { - v.onBody(x, namespaces); + v.onBody(x, ctx); + + ctx = x.ctx; VISIT(x.ctx->variables) VISIT(x.statements) + + ctx = ctx->parent; } void visit(const Namespace & x) { - v.onNamespace(x, namespaces); - - namespaces.push_back(x.name); + v.onNamespace(x, ctx); + + ctx = x.ctx; VISIT(x.namespaces) VISIT(x.ctx->variables) - VISIT(x.structs) - VISIT(x.functions) + VISIT(x.ctx->structs) + VISIT(x.ctx->functions) - namespaces.pop_back(); + ctx = ctx->parent; } void visit(const Variable & x) { - v.onVariable(x, namespaces); + v.onVariable(x, ctx); visit(x.type); } void visit(const Function & x) { - v.onFunction(x, namespaces); + v.onFunction(x, ctx); if (x.defined) { visit(x.body); @@ -161,31 +165,35 @@ public: } void visit(const StructMember & x) { - v.onStructMethod(x, namespaces); + v.onStructMethod(x, ctx); visit(x.t); } void visit(const StructMember & x) { - v.onStructMember(x, namespaces); + v.onStructMember(x, ctx); visit(x.t); } void visit(const Struct & x) { - v.onStruct(x, namespaces); + v.onStruct(x, ctx); VISIT(x.members) VISIT(x.methods) } void visit(const Program & x) { - v.onProgram(x, namespaces); + v.onProgram(x, ctx); + + ctx = x.ctx; VISIT(x.namespaces) VISIT(x.ctx->variables) - VISIT(x.structs) - VISIT(x.functions) + VISIT(x.ctx->structs) + VISIT(x.ctx->functions) + + ctx = nullptr; } };