wraith-lang/src/identcheck.c

327 lines
10 KiB
C
Raw Normal View History

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"
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);
node->parent = parent;
2021-05-07 00:16:10 +00:00
node->childCount = 0;
node->childCapacity = 0;
node->children = NULL;
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;
node->children = (IdNode**)malloc(sizeof(IdNode*) * node->childCapacity);
} else if (node->childCount == node->childCapacity) {
node->childCapacity *= 2;
node->children = realloc(node->children, sizeof(IdNode*) * node->childCapacity);
}
node->children[node->childCount] = child;
node->childCount += 1;
}
IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
uint32_t i;
IdNode *mainNode;
2021-05-07 00:16:10 +00:00
switch (astNode->syntaxKind) {
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;
}
}
case IfStatement: {
mainNode = MakeIdNode(OrderedScope, "if", parent);
Node *clause = astNode->children[0];
Node *stmtSeq = astNode->children[1];
for (i = 0; i < clause->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(clause->children[i], mainNode));
}
for (i = 0; i < stmtSeq->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(stmtSeq->children[i], mainNode));
}
break;
}
2021-05-07 00:16:10 +00:00
case IfElseStatement: {
Node *ifNode = astNode->children[0];
Node *elseStmts = astNode->children[1];
mainNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, mainNode);
IdNode *elseBranch = MakeIdNode(OrderedScope, "else", mainNode);
AddChildToNode(mainNode, ifBranch);
for (i = 0; i < elseStmts->childCount; i++) {
AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i], elseBranch));
2021-05-07 00:16:10 +00:00
}
AddChildToNode(mainNode, elseBranch);
break;
}
case ForLoop: {
Node *loopDecl = astNode->children[0];
Node *loopBody = astNode->children[3];
mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(mainNode, MakeIdTree(loopDecl, mainNode));
for (i = 0; i < loopBody->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(loopBody->children[i], mainNode));
}
break;
2021-05-07 00:16:10 +00:00
}
2021-05-08 00:49:35 +00:00
case Declaration: {
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 00:16:10 +00:00
case StructDeclaration: {
Node *idNode = astNode->children[0];
Node *declsNode = astNode->children[1];
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++) {
AddChildToNode(mainNode, MakeIdTree(declsNode->children[i], mainNode));
2021-05-07 00:16:10 +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];
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++) {
AddChildToNode(mainNode, MakeIdTree(funcArgsNode->children[i], mainNode));
2021-05-07 00:16:10 +00:00
}
for (i = 0; i < bodyStatementsNode->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(bodyStatementsNode->children[i], mainNode));
2021-05-07 00:16:10 +00:00
}
break;
2021-05-07 00:16:10 +00:00
}
case DeclarationSequence: {
mainNode = MakeIdNode(UnorderedScope, "", parent);
for (i = 0; i < astNode->childCount; i++) {
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;
}
break;
}
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-07 00:16:10 +00:00
}
astNode->idLink = mainNode;
return mainNode;
2021-05-07 00:16:10 +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;
}
switch(node->type) {
case Placeholder:
printf("Placeholder (%s)\n", node->name);
break;
case OrderedScope:
printf("OrderedScope (%s)\n", node->name);
break;
case UnorderedScope:
printf("UnorderedScope (%s)\n", node->name);
2021-05-08 00:49:35 +00:00
break;
case Struct:
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
break;
case Function:
printf("%s : Function<%s>\n", node->name, TypeTagToString(node->typeTag));
break;
case Variable:
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
break;
}
}
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("| ");
}
PrintIdNode(tree);
2021-05-07 00:16:10 +00:00
for (i = 0; i < tree->childCount; i++) {
PrintIdTree(tree->children[i], tabCount + 1);
}
}
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;
}
IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
if (root == NULL) {
2021-05-08 21:51:15 +00:00
fprintf(stderr, "wraith: Attempted to call LookdownId on a null value.\n");
return NULL;
}
IdNode *result = NULL;
IdNode **frontier = (IdNode**)malloc(sizeof(IdNode*));
frontier[0] = root;
uint32_t frontierCount = 1;
while (frontierCount > 0) {
IdNode *current = frontier[0];
if (current->type == targetType && strcmp(current->name, targetName) == 0) {
result = current;
break;
}
uint32_t i;
for(i = 1; i < frontierCount; i++) {
frontier[i-1] = frontier[i];
}
size_t newSize = frontierCount + current->childCount - 1;
if (frontierCount != newSize) {
frontier = realloc(frontier, sizeof(IdNode*) * newSize);
}
for (i = 0; i < current->childCount; i++) {
frontier[frontierCount + i - 1] = current->children[i];
}
frontierCount = newSize;
}
free(frontier);
return result;
}
2021-05-08 21:51:15 +00:00
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 && node->type != Placeholder) {
2021-05-08 21:51:15 +00:00
return node;
}
2021-05-08 22:12:53 +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.
if (prev == NULL) {
return LookupId(node->parent, node, target);
}
2021-05-08 21:51:15 +00:00
uint32_t idxLimit;
2021-05-08 22:12:53 +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.
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];
if (child == prev || child->type == Placeholder) {
// 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;
}
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);
}