Fixes bugs in LookupId

pull/1/head
venko 2021-05-08 15:12:53 -07:00
parent aa2449f7df
commit 32541d4794
2 changed files with 23 additions and 7 deletions

View File

@ -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;
}

View File

@ -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");