Compare commits

...

3 Commits

5 changed files with 149 additions and 14 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>
@ -43,7 +44,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
case IfStatement: { case IfStatement: {
Node *stmtSeq = astNode->children[1]; Node *stmtSeq = astNode->children[1];
IdNode *ifNode = MakeIdNode(LexicalScope, "if", parent); IdNode *ifNode = MakeIdNode(OrderedScope, "if", parent);
for (i = 0; i < stmtSeq->childCount; i++) { for (i = 0; i < stmtSeq->childCount; i++) {
AddChildToNode(ifNode, MakeIdTree(stmtSeq->children[i], ifNode)); AddChildToNode(ifNode, MakeIdTree(stmtSeq->children[i], ifNode));
} }
@ -53,15 +54,15 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
case IfElseStatement: { case IfElseStatement: {
Node *ifNode = astNode->children[0]; Node *ifNode = astNode->children[0];
Node *elseStmts = astNode->children[1]; Node *elseStmts = astNode->children[1];
IdNode *ifElseNode = MakeIdNode(LexicalScope, "if-else", parent); IdNode *ifElseNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, ifElseNode); IdNode *ifBranch = MakeIdTree(ifNode, ifElseNode);
IdNode *elseBranch = MakeIdNode(LexicalScope, "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;
} }
@ -69,7 +70,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
case ForLoop: { case ForLoop: {
Node *loopDecl = astNode->children[0]; Node *loopDecl = astNode->children[0];
Node *loopBody = astNode->children[3]; Node *loopBody = astNode->children[3];
IdNode *loopNode = MakeIdNode(LexicalScope, "for-loop", parent); IdNode *loopNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(loopNode, MakeIdTree(loopDecl, loopNode)); AddChildToNode(loopNode, MakeIdTree(loopDecl, loopNode));
for (i = 0; i < loopBody->childCount; i++) { for (i = 0; i < loopBody->childCount; i++) {
AddChildToNode(loopNode, MakeIdTree(loopBody->children[i], loopNode)); AddChildToNode(loopNode, MakeIdTree(loopBody->children[i], loopNode));
@ -111,7 +112,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
} }
case DeclarationSequence: { case DeclarationSequence: {
IdNode *declSeqNode = MakeIdNode(LexicalScope, "", parent); IdNode *declSeqNode = MakeIdNode(UnorderedScope, "", parent);
for (i = 0; i < astNode->childCount; i++) { for (i = 0; i < astNode->childCount; i++) {
AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i], declSeqNode)); AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i], declSeqNode));
} }
@ -130,8 +131,11 @@ void PrintIdNode(IdNode *node) {
} }
switch(node->type) { switch(node->type) {
case LexicalScope: case OrderedScope:
printf("Scope (%s)\n", node->name); printf("OrderedScope (%s)\n", node->name);
break;
case UnorderedScope:
printf("UnorderedScope (%s)\n", node->name);
break; break;
case Struct: case Struct:
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag)); printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
@ -177,9 +181,9 @@ int PrintAncestors(IdNode *node) {
return indent; return indent;
} }
IdNode* FindId(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;
} }
@ -213,3 +217,74 @@ IdNode* FindId(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 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 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;
}
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

@ -7,7 +7,8 @@
#include "ast.h" #include "ast.h"
typedef enum NodeType { typedef enum NodeType {
LexicalScope, UnorderedScope,
OrderedScope,
Struct, Struct,
Function, Function,
Variable Variable
@ -30,10 +31,12 @@ typedef struct IdStatus {
} IdStatus; } IdStatus;
IdNode* FindId(IdNode *root, NodeType targetType, char *targetName);
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* LookupId(IdNode *node, IdNode *prev, char* target);
//IdStatus CheckIds(Node *root); //IdStatus CheckIds(Node *root);

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 struct 'Program'\n");
IdNode *node = LookdownId(idTree, Struct, "Program");
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);
} }