Adds error message to FindId, extra test code in main

identifiers
venko 2021-05-07 13:35:28 -07:00
parent 8f86392cf3
commit 565d815deb
3 changed files with 22 additions and 3 deletions

6
commenttest.w Normal file
View File

@ -0,0 +1,6 @@
struct Program {
// This triggers a parse error
static Main(): int {
return 0;
}
}

View File

@ -154,6 +154,11 @@ int PrintAncestors(IdNode *node) {
}
IdNode* FindId(IdNode *root, NodeType targetType, char *targetName) {
if (root == NULL) {
fprintf(stderr, "wraith: Attempted to call FindId on a null value.\n");
return NULL;
}
IdNode *result = NULL;
IdNode **frontier = (IdNode**)malloc(sizeof(IdNode*));
frontier[0] = root;

View File

@ -65,13 +65,21 @@ int main(int argc, char *argv[])
}
else
{
IdNode *idTree = MakeIdTree(rootNode, NULL);
PrintIdTree(idTree, /*tabCount=*/0);
{
IdNode *idTree = MakeIdTree(rootNode, NULL);
PrintIdTree(idTree, /*tabCount=*/0);
printf("\n");
printf("Searching for a variable named 'lol' and printing its ancestors\n");
IdNode *deepest = FindId(idTree, Variable, "lol");
PrintAncestors(deepest);
printf("\n");
printf("Now searching for the same variable but with the caveat that it"
" must be inside an if-block, and printing its ancestors\n");
deepest = FindId(idTree, LexicalScope, "if");
deepest = FindId(deepest, Variable, "lol");
PrintAncestors(deepest);
}
exitCode = Codegen(rootNode, optimizationLevel);
}