2021-05-08 21:51:15 +00:00
|
|
|
#include <stdbool.h>
|
2021-05-07 00:16:10 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ast.h"
|
|
|
|
#include "identcheck.h"
|
|
|
|
|
2021-05-07 20:22:51 +00:00
|
|
|
IdNode* MakeIdNode(NodeType type, char *name, IdNode *parent) {
|
2021-05-07 00:16:10 +00:00
|
|
|
IdNode *node = (IdNode*)malloc(sizeof(IdNode));
|
|
|
|
node->type = type;
|
|
|
|
node->name = strdup(name);
|
2021-05-07 20:22:51 +00:00
|
|
|
node->parent = parent;
|
2021-05-07 00:16:10 +00:00
|
|
|
node->childCount = 0;
|
|
|
|
node->childCapacity = 0;
|
|
|
|
node->children = NULL;
|
2021-05-08 00:51:23 +00:00
|
|
|
node->typeTag = NULL;
|
2021-05-07 00:16:10 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddChildToNode(IdNode *node, IdNode *child) {
|
|
|
|
if (child == NULL) return;
|
|
|
|
|
|
|
|
if (node->children == NULL) {
|
|
|
|
node->childCapacity = 2;
|
2021-05-14 18:53:09 +00:00
|
|
|
node->children = (IdNode**) malloc(sizeof(IdNode*) * node->childCapacity);
|
2021-05-07 00:16:10 +00:00
|
|
|
} else if (node->childCount == node->childCapacity) {
|
|
|
|
node->childCapacity *= 2;
|
2021-05-14 18:53:09 +00:00
|
|
|
node->children = (IdNode**) realloc(node->children, sizeof(IdNode*) * node->childCapacity);
|
2021-05-07 00:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node->children[node->childCount] = child;
|
|
|
|
node->childCount += 1;
|
|
|
|
}
|
|
|
|
|
2021-05-07 20:22:51 +00:00
|
|
|
IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
|
2021-05-07 02:53:28 +00:00
|
|
|
uint32_t i;
|
2021-05-13 04:54:09 +00:00
|
|
|
IdNode *mainNode;
|
2021-05-07 00:16:10 +00:00
|
|
|
switch (astNode->syntaxKind) {
|
2021-05-13 04:54:09 +00:00
|
|
|
case Assignment: {
|
|
|
|
if (astNode->children[0]->syntaxKind == Declaration) {
|
|
|
|
return MakeIdTree(astNode->children[0], parent);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < astNode->childCount; i++) {
|
|
|
|
AddChildToNode(parent, MakeIdTree(astNode->children[i], parent));
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 02:53:28 +00:00
|
|
|
|
|
|
|
case IfStatement: {
|
2021-05-13 04:54:09 +00:00
|
|
|
mainNode = MakeIdNode(OrderedScope, "if", parent);
|
|
|
|
Node *clause = astNode->children[0];
|
2021-05-07 02:53:28 +00:00
|
|
|
Node *stmtSeq = astNode->children[1];
|
2021-05-13 04:54:09 +00:00
|
|
|
for (i = 0; i < clause->childCount; i++) {
|
|
|
|
AddChildToNode(mainNode, MakeIdTree(clause->children[i], mainNode));
|
|
|
|
}
|
2021-05-07 02:53:28 +00:00
|
|
|
for (i = 0; i < stmtSeq->childCount; i++) {
|
2021-05-13 04:54:09 +00:00
|
|
|
AddChildToNode(mainNode, MakeIdTree(stmtSeq->children[i], mainNode));
|
2021-05-07 02:53:28 +00:00
|
|
|
}
|
2021-05-13 04:54:09 +00:00
|
|
|
break;
|
2021-05-07 02:53:28 +00:00
|
|
|
}
|
2021-05-07 00:16:10 +00:00
|
|
|
|
2021-05-07 02:53:28 +00:00
|
|
|
case IfElseStatement: {
|
|
|
|
Node *ifNode = astNode->children[0];
|
|
|
|
Node *elseStmts = astNode->children[1];
|
2021-05-13 04:54:09 +00:00
|
|
|
mainNode = MakeIdNode(OrderedScope, "if-else", parent);
|
|
|
|
IdNode *ifBranch = MakeIdTree(ifNode, mainNode);
|
|
|
|
IdNode *elseBranch = MakeIdNode(OrderedScope, "else", mainNode);
|
2021-05-07 02:53:28 +00:00
|
|
|
|
2021-05-13 04:54:09 +00:00
|
|
|
AddChildToNode(mainNode, ifBranch);
|
2021-05-07 02:53:28 +00:00
|
|
|
for (i = 0; i < elseStmts->childCount; i++) {
|
2021-05-07 20:22:51 +00:00
|
|
|
AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i], elseBranch));
|
2021-05-07 00:16:10 +00:00
|
|
|
}
|
2021-05-13 04:54:09 +00:00
|
|
|
AddChildToNode(mainNode, elseBranch);
|
|
|
|
break;
|
2021-05-07 02:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case ForLoop: {
|
|
|
|
Node *loopDecl = astNode->children[0];
|
|
|
|
Node *loopBody = astNode->children[3];
|
2021-05-13 04:54:09 +00:00
|
|
|
mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
|
|
|
|
AddChildToNode(mainNode, MakeIdTree(loopDecl, mainNode));
|
2021-05-07 02:53:28 +00:00
|
|
|
for (i = 0; i < loopBody->childCount; i++) {
|
2021-05-13 04:54:09 +00:00
|
|
|
AddChildToNode(mainNode, MakeIdTree(loopBody->children[i], mainNode));
|
2021-05-07 02:53:28 +00:00
|
|
|
}
|
2021-05-13 04:54:09 +00:00
|
|
|
break;
|
2021-05-07 00:16:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 00:49:35 +00:00
|
|
|
case Declaration: {
|
2021-05-13 04:54:09 +00:00
|
|
|
mainNode = MakeIdNode(Variable, astNode->children[1]->value.string, parent);
|
|
|
|
mainNode->typeTag = MakeTypeTag(astNode);
|
|
|
|
astNode->children[1]->typeTag = mainNode->typeTag;
|
|
|
|
break;
|
2021-05-08 00:49:35 +00:00
|
|
|
}
|
2021-05-07 02:53:28 +00:00
|
|
|
|
2021-05-07 00:16:10 +00:00
|
|
|
case StructDeclaration: {
|
|
|
|
Node *idNode = astNode->children[0];
|
|
|
|
Node *declsNode = astNode->children[1];
|
2021-05-13 04:54:09 +00:00
|
|
|
mainNode = MakeIdNode(Struct, idNode->value.string, parent);
|
|
|
|
mainNode->typeTag = MakeTypeTag(astNode);
|
2021-05-07 00:16:10 +00:00
|
|
|
for (i = 0; i < declsNode->childCount; i++) {
|
2021-05-13 04:54:09 +00:00
|
|
|
AddChildToNode(mainNode, MakeIdTree(declsNode->children[i], mainNode));
|
2021-05-07 00:16:10 +00:00
|
|
|
}
|
2021-05-13 04:54:09 +00:00
|
|
|
break;
|
2021-05-07 00:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case FunctionDeclaration: {
|
|
|
|
Node *sigNode = astNode->children[0];
|
|
|
|
Node *funcNameNode = sigNode->children[0];
|
|
|
|
Node *funcArgsNode = sigNode->children[2];
|
|
|
|
Node *bodyStatementsNode = astNode->children[1];
|
2021-05-13 04:54:09 +00:00
|
|
|
mainNode = MakeIdNode(Function, funcNameNode->value.string, parent);
|
|
|
|
mainNode->typeTag = MakeTypeTag(astNode);
|
|
|
|
astNode->children[0]->children[0]->typeTag = mainNode->typeTag;
|
2021-05-07 00:16:10 +00:00
|
|
|
for (i = 0; i < funcArgsNode->childCount; i++) {
|
2021-05-13 04:54:09 +00:00
|
|
|
AddChildToNode(mainNode, MakeIdTree(funcArgsNode->children[i], mainNode));
|
2021-05-07 00:16:10 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < bodyStatementsNode->childCount; i++) {
|
2021-05-13 04:54:09 +00:00
|
|
|
AddChildToNode(mainNode, MakeIdTree(bodyStatementsNode->children[i], mainNode));
|
2021-05-07 00:16:10 +00:00
|
|
|
}
|
2021-05-13 04:54:09 +00:00
|
|
|
break;
|
2021-05-07 00:16:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 02:53:28 +00:00
|
|
|
case DeclarationSequence: {
|
2021-05-13 04:54:09 +00:00
|
|
|
mainNode = MakeIdNode(UnorderedScope, "", parent);
|
2021-05-07 02:53:28 +00:00
|
|
|
for (i = 0; i < astNode->childCount; i++) {
|
2021-05-13 04:54:09 +00:00
|
|
|
AddChildToNode(mainNode, MakeIdTree(astNode->children[i], mainNode));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Identifier: {
|
|
|
|
mainNode = MakeIdNode(Placeholder, astNode->value.string, parent);
|
|
|
|
IdNode *lookupNode = LookupId(mainNode, NULL, astNode->value.string);
|
|
|
|
if (lookupNode == NULL) {
|
|
|
|
fprintf(stderr, "wraith: Could not find IdNode for id %s\n", astNode->value.string);
|
|
|
|
TypeTag *tag = (TypeTag*)malloc(sizeof(TypeTag));
|
|
|
|
tag->type = Unknown;
|
|
|
|
astNode->typeTag = tag;
|
|
|
|
} else {
|
|
|
|
astNode->typeTag = lookupNode->typeTag;
|
2021-05-07 02:53:28 +00:00
|
|
|
}
|
2021-05-13 04:54:09 +00:00
|
|
|
break;
|
2021-05-07 02:53:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 04:54:09 +00:00
|
|
|
default: {
|
|
|
|
for (i = 0; i < astNode->childCount; i++) {
|
|
|
|
AddChildToNode(parent, MakeIdTree(astNode->children[i], parent));
|
|
|
|
}
|
2021-05-07 00:16:10 +00:00
|
|
|
return NULL;
|
2021-05-13 04:54:09 +00:00
|
|
|
}
|
2021-05-07 00:16:10 +00:00
|
|
|
}
|
2021-05-13 04:54:09 +00:00
|
|
|
|
|
|
|
astNode->idLink = mainNode;
|
|
|
|
return mainNode;
|
2021-05-07 00:16:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 20:22:51 +00:00
|
|
|
void PrintIdNode(IdNode *node) {
|
2021-05-08 00:49:35 +00:00
|
|
|
if (node == NULL) {
|
|
|
|
fprintf(stderr, "wraith: Attempted to call PrintIdNode with null value.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-07 20:22:51 +00:00
|
|
|
switch(node->type) {
|
2021-05-13 04:54:09 +00:00
|
|
|
case Placeholder:
|
|
|
|
printf("Placeholder (%s)\n", node->name);
|
|
|
|
break;
|
2021-05-14 18:53:09 +00:00
|
|
|
case OrderedScope:
|
|
|
|
printf("OrderedScope (%s)\n", node->name);
|
2021-05-08 19:09:25 +00:00
|
|
|
break;
|
2021-05-14 18:53:09 +00:00
|
|
|
case UnorderedScope:
|
|
|
|
printf("UnorderedScope (%s)\n", node->name);
|
2021-05-08 00:49:35 +00:00
|
|
|
break;
|
|
|
|
case Struct:
|
2021-05-14 18:53:09 +00:00
|
|
|
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
|
2021-05-08 00:49:35 +00:00
|
|
|
break;
|
|
|
|
case Function:
|
|
|
|
printf("%s : Function<%s>\n", node->name, TypeTagToString(node->typeTag));
|
|
|
|
break;
|
2021-05-14 18:53:09 +00:00
|
|
|
case Variable:
|
|
|
|
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
|
2021-05-08 00:49:35 +00:00
|
|
|
break;
|
2021-05-07 20:22:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:16:10 +00:00
|
|
|
void PrintIdTree(IdNode *tree, uint32_t tabCount) {
|
2021-05-07 21:36:37 +00:00
|
|
|
if (tree == NULL) {
|
|
|
|
fprintf(stderr, "wraith: Attempted to call PrintIdTree on a null value.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:16:10 +00:00
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < tabCount; i++) {
|
|
|
|
printf("| ");
|
|
|
|
}
|
|
|
|
|
2021-05-07 20:22:51 +00:00
|
|
|
PrintIdNode(tree);
|
2021-05-07 00:16:10 +00:00
|
|
|
|
|
|
|
for (i = 0; i < tree->childCount; i++) {
|
|
|
|
PrintIdTree(tree->children[i], tabCount + 1);
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 20:22:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
int PrintAncestors(IdNode *node) {
|
|
|
|
if (node == NULL) return -1;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
int indent = 1;
|
|
|
|
indent += PrintAncestors(node->parent);
|
|
|
|
for (i = 0; i < indent; i++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
PrintIdNode(node);
|
|
|
|
return indent;
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:09:25 +00:00
|
|
|
IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
|
2021-05-07 20:35:28 +00:00
|
|
|
if (root == NULL) {
|
2021-05-08 21:51:15 +00:00
|
|
|
fprintf(stderr, "wraith: Attempted to call LookdownId on a null value.\n");
|
2021-05-07 20:35:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-05-07 20:22:51 +00:00
|
|
|
IdNode *result = NULL;
|
|
|
|
IdNode **frontier = (IdNode**)malloc(sizeof(IdNode*));
|
|
|
|
frontier[0] = root;
|
|
|
|
uint32_t frontierCount = 1;
|
|
|
|
|
2021-05-07 21:07:57 +00:00
|
|
|
while (frontierCount > 0) {
|
|
|
|
IdNode *current = frontier[0];
|
2021-05-07 20:22:51 +00:00
|
|
|
|
|
|
|
if (current->type == targetType && strcmp(current->name, targetName) == 0) {
|
|
|
|
result = current;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t i;
|
2021-05-07 21:07:57 +00:00
|
|
|
for(i = 1; i < frontierCount; i++) {
|
|
|
|
frontier[i-1] = frontier[i];
|
|
|
|
}
|
|
|
|
size_t newSize = frontierCount + current->childCount - 1;
|
|
|
|
if (frontierCount != newSize) {
|
2021-05-14 18:53:09 +00:00
|
|
|
frontier = (IdNode**) realloc(frontier, sizeof(IdNode*) * newSize);
|
2021-05-07 21:07:57 +00:00
|
|
|
}
|
2021-05-07 20:22:51 +00:00
|
|
|
for (i = 0; i < current->childCount; i++) {
|
2021-05-07 21:07:57 +00:00
|
|
|
frontier[frontierCount + i - 1] = current->children[i];
|
2021-05-07 20:22:51 +00:00
|
|
|
}
|
2021-05-07 21:07:57 +00:00
|
|
|
frontierCount = newSize;
|
2021-05-07 20:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(frontier);
|
|
|
|
return result;
|
2021-05-07 21:07:57 +00:00
|
|
|
}
|
2021-05-08 21:51:15 +00:00
|
|
|
|
|
|
|
bool ScopeHasOrdering(IdNode *node) {
|
|
|
|
switch (node->type) {
|
|
|
|
case OrderedScope:
|
|
|
|
case Function:
|
2021-05-14 18:53:09 +00:00
|
|
|
case Variable: /* this is only technically true */
|
2021-05-08 21:51:15 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
|
|
|
|
if (node == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-05-13 04:54:09 +00:00
|
|
|
if (strcmp(node->name, target) == 0 && node->type != Placeholder) {
|
2021-05-08 21:51:15 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:53:09 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2021-05-08 22:12:53 +00:00
|
|
|
if (prev == NULL) {
|
|
|
|
return LookupId(node->parent, node, target);
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:53:09 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2021-05-08 21:51:15 +00:00
|
|
|
uint32_t idxLimit;
|
2021-05-08 22:12:53 +00:00
|
|
|
if (ScopeHasOrdering(node)) {
|
2021-05-08 21:51:15 +00:00
|
|
|
uint32_t i;
|
|
|
|
for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++) {
|
|
|
|
if (node->children[i] == prev) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 22:12:53 +00:00
|
|
|
} else {
|
|
|
|
idxLimit = node->childCount;
|
2021-05-08 21:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < idxLimit; i++) {
|
|
|
|
IdNode *child = node->children[i];
|
2021-05-13 04:54:09 +00:00
|
|
|
if (child == prev || child->type == Placeholder) {
|
2021-05-14 18:53:09 +00:00
|
|
|
/* Do not inspect the node we just came from or placeholders. */
|
2021-05-08 22:12:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-08 21:51:15 +00:00
|
|
|
if (strcmp(child->name, target) == 0) {
|
|
|
|
return child;
|
2021-05-14 18:53:09 +00:00
|
|
|
}
|
2021-05-08 21:51:15 +00:00
|
|
|
|
|
|
|
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;
|
2021-05-14 18:53:09 +00:00
|
|
|
}
|
2021-05-08 21:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return LookupId(node->parent, node, target);
|
2021-05-13 04:54:09 +00:00
|
|
|
}
|