Compare commits

..

No commits in common. "32541d47946478b6fd784a10cba495dd83b696a3" and "743828450c1955e8a0ee6a01be9cb1888ee7f7b5" have entirely different histories.

5 changed files with 14 additions and 149 deletions

View File

@ -1,15 +1,4 @@
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) {
@ -20,6 +9,7 @@ struct Program {
signTag: int = 1;
}
lol: int = 420;
myBool: bool;
if (myBool) {
if (myBool) {
@ -33,12 +23,6 @@ struct Program {
}
}
someInt: int = 9585858;
return 0;
}
static Bar(): int {
return 0;
}
}

View File

@ -593,7 +593,6 @@ 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:

View File

@ -1,4 +1,3 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
@ -44,7 +43,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
case IfStatement: {
Node *stmtSeq = astNode->children[1];
IdNode *ifNode = MakeIdNode(OrderedScope, "if", parent);
IdNode *ifNode = MakeIdNode(LexicalScope, "if", parent);
for (i = 0; i < stmtSeq->childCount; i++) {
AddChildToNode(ifNode, MakeIdTree(stmtSeq->children[i], ifNode));
}
@ -54,15 +53,15 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
case IfElseStatement: {
Node *ifNode = astNode->children[0];
Node *elseStmts = astNode->children[1];
IdNode *ifElseNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifElseNode = MakeIdNode(LexicalScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, ifElseNode);
IdNode *elseBranch = MakeIdNode(OrderedScope, "else", ifElseNode);
IdNode *elseBranch = MakeIdNode(LexicalScope, "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;
}
@ -70,7 +69,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
case ForLoop: {
Node *loopDecl = astNode->children[0];
Node *loopBody = astNode->children[3];
IdNode *loopNode = MakeIdNode(OrderedScope, "for-loop", parent);
IdNode *loopNode = MakeIdNode(LexicalScope, "for-loop", parent);
AddChildToNode(loopNode, MakeIdTree(loopDecl, loopNode));
for (i = 0; i < loopBody->childCount; i++) {
AddChildToNode(loopNode, MakeIdTree(loopBody->children[i], loopNode));
@ -112,7 +111,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
}
case DeclarationSequence: {
IdNode *declSeqNode = MakeIdNode(UnorderedScope, "", parent);
IdNode *declSeqNode = MakeIdNode(LexicalScope, "", parent);
for (i = 0; i < astNode->childCount; i++) {
AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i], declSeqNode));
}
@ -131,11 +130,8 @@ void PrintIdNode(IdNode *node) {
}
switch(node->type) {
case OrderedScope:
printf("OrderedScope (%s)\n", node->name);
break;
case UnorderedScope:
printf("UnorderedScope (%s)\n", node->name);
case LexicalScope:
printf("Scope (%s)\n", node->name);
break;
case Struct:
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
@ -181,9 +177,9 @@ int PrintAncestors(IdNode *node) {
return indent;
}
IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
IdNode* FindId(IdNode *root, NodeType targetType, char *targetName) {
if (root == NULL) {
fprintf(stderr, "wraith: Attempted to call LookdownId on a null value.\n");
fprintf(stderr, "wraith: Attempted to call FindId on a null value.\n");
return NULL;
}
@ -217,74 +213,3 @@ 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 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,8 +7,7 @@
#include "ast.h"
typedef enum NodeType {
UnorderedScope,
OrderedScope,
LexicalScope,
Struct,
Function,
Variable
@ -31,12 +30,10 @@ typedef struct IdStatus {
} IdStatus;
IdNode* FindId(IdNode *root, NodeType targetType, char *targetName);
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);
IdNode* LookupId(IdNode *node, IdNode *prev, char* target);
//IdStatus CheckIds(Node *root);

View File

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