diff --git a/iftest.w b/iftest.w index 73df20c..efa5bfa 100644 --- a/iftest.w +++ b/iftest.w @@ -1,4 +1,15 @@ +struct MyStruct { + static MyFunc(): int { + myStructInt: int = 595959959; + return myStructInt; + } +} + struct Program { + static Foo(): int { + return 0; + } + static Main(): int { myInt: int = 54; if (myInt < 0) { @@ -9,7 +20,6 @@ struct Program { signTag: int = 1; } - lol: int = 420; myBool: bool; if (myBool) { if (myBool) { @@ -23,6 +33,12 @@ struct Program { } } + someInt: int = 9585858; + + return 0; + } + + static Bar(): int { return 0; } } diff --git a/src/ast.c b/src/ast.c index 3fb4869..130f094 100644 --- a/src/ast.c +++ b/src/ast.c @@ -593,6 +593,7 @@ char* TypeTagToString(TypeTag *tag) { size_t innerStrLen = strlen(inner); char *result = malloc(sizeof(char) * (innerStrLen + 5)); sprintf(result, "Ref<%s>", inner); + free(inner); return result; } case Custom: diff --git a/src/identcheck.c b/src/identcheck.c index 88d8112..e8a51c1 100644 --- a/src/identcheck.c +++ b/src/identcheck.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -58,10 +59,10 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) { IdNode *elseBranch = MakeIdNode(OrderedScope, "else", ifElseNode); AddChildToNode(ifElseNode, ifBranch); - AddChildToNode(ifElseNode, elseBranch); for (i = 0; i < elseStmts->childCount; i++) { AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i], elseBranch)); } + AddChildToNode(ifElseNode, elseBranch); return ifElseNode; } @@ -182,7 +183,7 @@ int PrintAncestors(IdNode *node) { IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) { if (root == NULL) { - fprintf(stderr, "wraith: Attempted to call FindId on a null value.\n"); + fprintf(stderr, "wraith: Attempted to call LookdownId on a null value.\n"); return NULL; } @@ -216,3 +217,58 @@ IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) { free(frontier); return result; } + +bool ScopeHasOrdering(IdNode *node) { + switch (node->type) { + case OrderedScope: + case Function: + case Variable: // this is only technically true + return true; + default: + return false; + } +} + +IdNode* LookupId(IdNode *node, IdNode *prev, char *target) { + if (node == NULL) { + return NULL; + } + + if (strcmp(node->name, target) == 0) { + 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. + uint32_t idxLimit; + if (prev == NULL || !ScopeHasOrdering(node)) { + idxLimit = node->childCount; + } else { + uint32_t i; + for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++) { + if (node->children[i] == prev) { + break; + } + } + } + + uint32_t i; + for (i = 0; i < idxLimit; i++) { + IdNode *child = node->children[i]; + if (strcmp(child->name, target) == 0) { + return child; + } + + if (child->type == Struct) { + uint32_t j; + for (j = 0; j < child->childCount; j++) { + IdNode *grandchild = child->children[j]; + if (strcmp(grandchild->name, target) == 0) { + return grandchild; + } + } + } + } + + return LookupId(node->parent, node, target); +} \ No newline at end of file diff --git a/src/identcheck.h b/src/identcheck.h index 7a71bf0..b41576b 100644 --- a/src/identcheck.h +++ b/src/identcheck.h @@ -32,6 +32,7 @@ typedef struct IdStatus { IdNode* MakeIdTree(Node *astNode, IdNode *parent); +void PrintIdNode(IdNode *node); void PrintIdTree(IdNode *tree, uint32_t tabCount); int PrintAncestors(IdNode *node); IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName); diff --git a/src/main.c b/src/main.c index 691c1db..d50aeb5 100644 --- a/src/main.c +++ b/src/main.c @@ -65,9 +65,49 @@ int main(int argc, char *argv[]) } else { - if (parseVerbose) { + { // This shit only works if you're using iftest.w IdNode *idTree = MakeIdTree(rootNode, NULL); PrintIdTree(idTree, /*tabCount=*/0); + + printf("\nSeeking to variable 'myStructInt'\n"); + IdNode *node = LookdownId(idTree, Variable, "myStructInt"); + if (node == NULL) printf("Failed.\n"); + + printf("\n[attempting to look up MyStruct]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "MyStruct")); + + printf("\n[attempting to look up MyFunc]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "MyFunc")); + + printf("\n[attempting to look up myStructInt]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "myStructInt")); + + printf("\n[attempting to look up Program]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "Program")); + + printf("\n[attempting to look up Foo]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "Foo")); + + printf("\n[attempting to look up Main]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "Main")); + + printf("\n[attempting to look up myInt]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "myInt")); + + printf("\n[attempting to look up signTag]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "signTag")); + + printf("\n[attempting to look up myBool]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "myBool")); + + printf("\n[attempting to look up lol]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "lol")); + + printf("\n[attempting to look up someInt]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "someInt")); + + printf("\n[attempting to look up Bar]: \n"); + PrintIdNode(LookupId(node, /*prev=*/NULL, "Bar")); } exitCode = Codegen(rootNode, optimizationLevel); }