From 41aa820bca252aed3b0458c57d04bb6670cfeb26 Mon Sep 17 00:00:00 2001 From: Sam Vervaeck Date: Tue, 30 May 2023 13:34:39 +0200 Subject: [PATCH] Fix scope resolution --- include/bolt/CST.hpp | 1 + src/CST.cc | 43 +++++++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/include/bolt/CST.hpp b/include/bolt/CST.hpp index 1fb9b2793..36da0658b 100644 --- a/include/bolt/CST.hpp +++ b/include/bolt/CST.hpp @@ -262,6 +262,7 @@ namespace bolt { void addSymbol(ByteString Name, Node* Decl, SymbolKind Kind); void scan(Node* X); + void scanChild(Node* X); void visitPattern(Pattern* P, Node* ToInsert); diff --git a/src/CST.cc b/src/CST.cc index 54939c6b9..fdd0cc129 100644 --- a/src/CST.cc +++ b/src/CST.cc @@ -61,15 +61,41 @@ namespace bolt { void Scope::scan(Node* X) { switch (X->getKind()) { - case NodeKind::ExpressionStatement: - case NodeKind::ReturnStatement: - case NodeKind::IfStatement: - break; case NodeKind::SourceFile: { auto File = static_cast(X); for (auto Element: File->Elements) { - scan(Element); + scanChild(Element); + } + break; + } + case NodeKind::LetDeclaration: + { + auto Decl = static_cast(X); + for (auto Param: Decl->Params) { + visitPattern(Param->Pattern, Param); + } + if (Decl->Body) { + scanChild(Decl->Body); + } + break; + } + default: + ZEN_UNREACHABLE + } + } + + void Scope::scanChild(Node* X) { + switch (X->getKind()) { + case NodeKind::LetExprBody: + case NodeKind::ExpressionStatement: + case NodeKind::ReturnStatement: + break; + case NodeKind::LetBlockBody: + { + auto Block = static_cast(X); + for (auto Element: Block->Elements) { + scanChild(Element); } break; } @@ -78,13 +104,10 @@ namespace bolt { auto Decl = static_cast(X); addSymbol(Decl->Name->getCanonicalText(), Decl, SymbolKind::Class); for (auto Element: Decl->Elements) { - scan(Element); + scanChild(Element); } break; } - case NodeKind::InstanceDeclaration: - // FIXME is this right? - break; case NodeKind::LetDeclaration: { auto Decl = static_cast(X); @@ -150,7 +173,7 @@ namespace bolt { ZEN_ASSERT(Path.Modules.empty()); auto Curr = this; do { - auto Found= Curr->lookupDirect(Path, Kind); + auto Found = Curr->lookupDirect(Path, Kind); if (Found) { return Found; }