From 32541d47946478b6fd784a10cba495dd83b696a3 Mon Sep 17 00:00:00 2001 From: venko Date: Sat, 8 May 2021 15:12:53 -0700 Subject: [PATCH] Fixes bugs in LookupId --- src/identcheck.c | 26 +++++++++++++++++++++----- src/main.c | 4 ++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/identcheck.c b/src/identcheck.c index e8a51c1..c1c56e9 100644 --- a/src/identcheck.c +++ b/src/identcheck.c @@ -238,23 +238,39 @@ IdNode* LookupId(IdNode *node, IdNode *prev, char *target) { return node; } - // If the current node forms an ordered scope then we want to prevent ourselves from looking up - // identifiers declared after the scope we have just come from. + // If this is the start of our search, we should not attempt to look at child nodes. Only + // looking up the scope tree is valid at this point. + // + // This has the notable side-effect that this function will return NULL if you attempt to look + // up a struct's internals starting from the node representing the struct itself. This is + // because an IdNode corresponds to the location *where an identifier is first declared.* Thus, + // an identifier has no knowledge of identifiers declared "inside" of it. + if (prev == NULL) { + return LookupId(node->parent, node, target); + } + uint32_t idxLimit; - if (prev == NULL || !ScopeHasOrdering(node)) { - idxLimit = node->childCount; - } else { + // If the current node forms an ordered scope then we want to prevent ourselves from looking + // up identifiers declared after the scope we have just come from. + if (ScopeHasOrdering(node)) { uint32_t i; for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++) { if (node->children[i] == prev) { break; } } + } else { + idxLimit = node->childCount; } uint32_t i; for (i = 0; i < idxLimit; i++) { IdNode *child = node->children[i]; + if (child == prev) { + // Do not inspect the node we just came from. + continue; + } + if (strcmp(child->name, target) == 0) { return child; } diff --git a/src/main.c b/src/main.c index d50aeb5..cd1fbbb 100644 --- a/src/main.c +++ b/src/main.c @@ -69,8 +69,8 @@ int main(int argc, char *argv[]) IdNode *idTree = MakeIdTree(rootNode, NULL); PrintIdTree(idTree, /*tabCount=*/0); - printf("\nSeeking to variable 'myStructInt'\n"); - IdNode *node = LookdownId(idTree, Variable, "myStructInt"); + printf("\nSeeking to struct 'Program'\n"); + IdNode *node = LookdownId(idTree, Struct, "Program"); if (node == NULL) printf("Failed.\n"); printf("\n[attempting to look up MyStruct]: \n");