Fixes bugs in LookupId
parent
aa2449f7df
commit
32541d4794
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in New Issue