Implements identifier lookup.

pull/1/head
venko 2021-05-08 14:51:15 -07:00
parent 252dc9b87f
commit aa2449f7df
5 changed files with 118 additions and 4 deletions

View File

@ -1,4 +1,15 @@
struct MyStruct {
static MyFunc(): int {
myStructInt: int = 595959959;
return myStructInt;
}
}
struct Program { struct Program {
static Foo(): int {
return 0;
}
static Main(): int { static Main(): int {
myInt: int = 54; myInt: int = 54;
if (myInt < 0) { if (myInt < 0) {
@ -9,7 +20,6 @@ struct Program {
signTag: int = 1; signTag: int = 1;
} }
lol: int = 420;
myBool: bool; myBool: bool;
if (myBool) { if (myBool) {
if (myBool) { if (myBool) {
@ -23,6 +33,12 @@ struct Program {
} }
} }
someInt: int = 9585858;
return 0;
}
static Bar(): int {
return 0; return 0;
} }
} }

View File

@ -593,6 +593,7 @@ char* TypeTagToString(TypeTag *tag) {
size_t innerStrLen = strlen(inner); size_t innerStrLen = strlen(inner);
char *result = malloc(sizeof(char) * (innerStrLen + 5)); char *result = malloc(sizeof(char) * (innerStrLen + 5));
sprintf(result, "Ref<%s>", inner); sprintf(result, "Ref<%s>", inner);
free(inner);
return result; return result;
} }
case Custom: case Custom:

View File

@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
@ -58,10 +59,10 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
IdNode *elseBranch = MakeIdNode(OrderedScope, "else", ifElseNode); IdNode *elseBranch = MakeIdNode(OrderedScope, "else", ifElseNode);
AddChildToNode(ifElseNode, ifBranch); AddChildToNode(ifElseNode, ifBranch);
AddChildToNode(ifElseNode, elseBranch);
for (i = 0; i < elseStmts->childCount; i++) { for (i = 0; i < elseStmts->childCount; i++) {
AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i], elseBranch)); AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i], elseBranch));
} }
AddChildToNode(ifElseNode, elseBranch);
return ifElseNode; return ifElseNode;
} }
@ -182,7 +183,7 @@ int PrintAncestors(IdNode *node) {
IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) { IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
if (root == NULL) { 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; return NULL;
} }
@ -216,3 +217,58 @@ IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
free(frontier); free(frontier);
return result; 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);
}

View File

@ -32,6 +32,7 @@ typedef struct IdStatus {
IdNode* MakeIdTree(Node *astNode, IdNode *parent); IdNode* MakeIdTree(Node *astNode, IdNode *parent);
void PrintIdNode(IdNode *node);
void PrintIdTree(IdNode *tree, uint32_t tabCount); void PrintIdTree(IdNode *tree, uint32_t tabCount);
int PrintAncestors(IdNode *node); int PrintAncestors(IdNode *node);
IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName); IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName);

View File

@ -65,9 +65,49 @@ int main(int argc, char *argv[])
} }
else else
{ {
if (parseVerbose) { { // This shit only works if you're using iftest.w
IdNode *idTree = MakeIdTree(rootNode, NULL); IdNode *idTree = MakeIdTree(rootNode, NULL);
PrintIdTree(idTree, /*tabCount=*/0); 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); exitCode = Codegen(rootNode, optimizationLevel);
} }