wraith-lang/src/identcheck.c

508 lines
13 KiB
C
Raw Normal View History

2021-05-08 21:51:15 +00:00
#include <stdbool.h>
2021-05-16 07:42:37 +00:00
#include <stdint.h>
2021-05-07 00:16:10 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "identcheck.h"
2021-05-16 07:42:37 +00:00
IdNode *MakeIdNode(NodeType type, char *name, IdNode *parent)
{
IdNode *node = (IdNode *)malloc(sizeof(IdNode));
2021-05-07 00:16:10 +00:00
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;
}
2021-05-16 07:42:37 +00:00
void AddChildToNode(IdNode *node, IdNode *child)
{
if (child == NULL)
return;
2021-05-07 00:16:10 +00:00
2021-05-16 07:42:37 +00:00
if (node->children == NULL)
{
2021-05-07 00:16:10 +00:00
node->childCapacity = 2;
2021-05-16 07:42:37 +00:00
node->children =
(IdNode **)malloc(sizeof(IdNode *) * node->childCapacity);
}
else if (node->childCount == node->childCapacity)
{
2021-05-07 00:16:10 +00:00
node->childCapacity *= 2;
2021-05-16 07:42:37 +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-16 07:42:37 +00:00
IdNode *MakeIdTree(Node *astNode, IdNode *parent)
{
uint32_t i;
IdNode *mainNode;
2021-05-16 07:42:37 +00:00
switch (astNode->syntaxKind)
{
case AccessExpression:
AddChildToNode(
parent,
MakeIdTree(astNode->accessExpression.accessee, parent));
AddChildToNode(
parent,
MakeIdTree(astNode->accessExpression.accessor, parent));
return NULL;
2021-05-16 07:42:37 +00:00
case AllocExpression:
2021-05-22 02:52:13 +00:00
astNode->typeTag = MakeTypeTag(astNode);
2021-05-16 07:42:37 +00:00
return NULL;
2021-05-16 07:42:37 +00:00
case Assignment:
{
if (astNode->assignmentStatement.left->syntaxKind == Declaration)
{
return MakeIdTree(astNode->assignmentStatement.left, parent);
}
2021-05-16 07:42:37 +00:00
else
{
AddChildToNode(
parent,
MakeIdTree(astNode->assignmentStatement.left, parent));
AddChildToNode(
parent,
MakeIdTree(astNode->assignmentStatement.right, parent));
return NULL;
}
2021-05-16 07:42:37 +00:00
}
2021-05-07 00:16:10 +00:00
2021-05-16 07:42:37 +00:00
case BinaryExpression:
AddChildToNode(
parent,
MakeIdTree(astNode->binaryExpression.left, parent));
AddChildToNode(
parent,
MakeIdTree(astNode->binaryExpression.right, parent));
return NULL;
2021-05-16 07:42:37 +00:00
case Declaration:
{
Node *idNode = astNode->declaration.identifier;
mainNode = MakeIdNode(Variable, idNode->identifier.name, parent);
mainNode->typeTag = MakeTypeTag(astNode);
idNode->typeTag = mainNode->typeTag;
break;
}
case DeclarationSequence:
{
mainNode = MakeIdNode(UnorderedScope, "", parent);
for (i = 0; i < astNode->declarationSequence.count; i++)
{
AddChildToNode(
mainNode,
MakeIdTree(astNode->declarationSequence.sequence[i], mainNode));
2021-05-07 00:16:10 +00:00
}
2021-05-16 07:42:37 +00:00
break;
}
2021-05-07 00:16:10 +00:00
2021-05-16 07:42:37 +00:00
case ForLoop:
{
Node *loopDecl = astNode->forLoop.declaration;
Node *loopBody = astNode->forLoop.statementSequence;
mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(mainNode, MakeIdTree(loopDecl, mainNode));
AddChildToNode(mainNode, MakeIdTree(loopBody, mainNode));
break;
}
2021-05-16 07:42:37 +00:00
case FunctionArgumentSequence:
for (i = 0; i < astNode->functionArgumentSequence.count; i++)
{
AddChildToNode(
2021-05-16 07:42:37 +00:00
parent,
MakeIdTree(
astNode->functionArgumentSequence.sequence[i],
parent));
2021-05-07 00:16:10 +00:00
}
2021-05-16 07:42:37 +00:00
return NULL;
2021-05-07 00:16:10 +00:00
2021-05-16 07:42:37 +00:00
case FunctionCallExpression:
AddChildToNode(
parent,
MakeIdTree(astNode->functionCallExpression.identifier, parent));
AddChildToNode(
parent,
MakeIdTree(
astNode->functionCallExpression.argumentSequence,
parent));
return NULL;
2021-05-16 07:42:37 +00:00
case FunctionDeclaration:
{
Node *sigNode = astNode->functionDeclaration.functionSignature;
Node *idNode = sigNode->functionSignature.identifier;
char *funcName = idNode->identifier.name;
mainNode = MakeIdNode(Function, funcName, parent);
mainNode->typeTag = MakeTypeTag(astNode);
idNode->typeTag = mainNode->typeTag;
MakeIdTree(sigNode->functionSignature.arguments, mainNode);
MakeIdTree(astNode->functionDeclaration.functionBody, mainNode);
2021-05-22 02:52:13 +00:00
MakeIdTree(sigNode->functionSignature.genericArguments, mainNode);
2021-05-16 07:42:37 +00:00
break;
}
2021-05-16 07:42:37 +00:00
case FunctionSignatureArguments:
{
for (i = 0; i < astNode->functionSignatureArguments.count; i++)
{
Node *argNode = astNode->functionSignatureArguments.sequence[i];
AddChildToNode(parent, MakeIdTree(argNode, parent));
}
2021-05-16 07:42:37 +00:00
return NULL;
}
2021-05-22 02:52:13 +00:00
case GenericArgument:
{
char *name = astNode->genericArgument.identifier->identifier.name;
mainNode = MakeIdNode(GenericType, name, parent);
break;
}
case GenericArguments:
{
for (i = 0; i < astNode->genericArguments.count; i += 1)
{
Node *argNode = astNode->genericArguments.arguments[i];
AddChildToNode(parent, MakeIdTree(argNode, parent));
}
return NULL;
}
2021-05-16 07:42:37 +00:00
case Identifier:
{
char *name = astNode->identifier.name;
mainNode = MakeIdNode(Placeholder, name, parent);
IdNode *lookupNode = LookupId(mainNode, NULL, name);
if (lookupNode == NULL)
{
fprintf(stderr, "wraith: Could not find IdNode for id %s\n", name);
TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag));
tag->type = Unknown;
astNode->typeTag = tag;
}
else
{
astNode->typeTag = lookupNode->typeTag;
}
2021-05-16 07:42:37 +00:00
break;
}
2021-05-16 07:42:37 +00:00
case IfStatement:
{
Node *clause = astNode->ifStatement.expression;
Node *stmtSeq = astNode->ifStatement.statementSequence;
mainNode = MakeIdNode(OrderedScope, "if", parent);
MakeIdTree(clause, mainNode);
MakeIdTree(stmtSeq, mainNode);
break;
}
2021-05-16 07:42:37 +00:00
case IfElseStatement:
{
Node *ifNode = astNode->ifElseStatement.ifStatement;
Node *elseStmts = astNode->ifElseStatement.elseStatement;
mainNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, mainNode);
AddChildToNode(mainNode, ifBranch);
IdNode *elseScope = MakeIdNode(OrderedScope, "else", mainNode);
MakeIdTree(elseStmts, elseScope);
AddChildToNode(mainNode, elseScope);
break;
}
2021-05-16 07:42:37 +00:00
case ReferenceTypeNode:
AddChildToNode(parent, MakeIdTree(astNode->referenceType.type, parent));
return NULL;
case Return:
AddChildToNode(
parent,
MakeIdTree(astNode->returnStatement.expression, parent));
return NULL;
case StatementSequence:
{
for (i = 0; i < astNode->statementSequence.count; i++)
{
Node *argNode = astNode->statementSequence.sequence[i];
AddChildToNode(parent, MakeIdTree(argNode, parent));
}
2021-05-16 07:42:37 +00:00
return NULL;
}
2021-05-16 07:42:37 +00:00
case StructDeclaration:
{
Node *idNode = astNode->structDeclaration.identifier;
Node *declsNode = astNode->structDeclaration.declarationSequence;
mainNode = MakeIdNode(Struct, idNode->identifier.name, parent);
mainNode->typeTag = MakeTypeTag(astNode);
for (i = 0; i < declsNode->declarationSequence.count; i++)
{
Node *decl = declsNode->declarationSequence.sequence[i];
AddChildToNode(mainNode, MakeIdTree(decl, mainNode));
}
2021-05-16 07:42:37 +00:00
break;
}
2021-05-16 07:42:37 +00:00
case Type:
AddChildToNode(parent, MakeIdTree(astNode->type.typeNode, parent));
return NULL;
2021-05-16 07:42:37 +00:00
case UnaryExpression:
AddChildToNode(
parent,
MakeIdTree(astNode->unaryExpression.child, parent));
return NULL;
2021-05-16 07:42:37 +00:00
case Comment:
case CustomTypeNode:
case FunctionModifiers:
case FunctionSignature:
case Number:
case PrimitiveTypeNode:
case ReturnVoid:
case StaticModifier:
case StringLiteral:
return NULL;
2021-05-07 00:16:10 +00:00
}
astNode->idLink = mainNode;
return mainNode;
2021-05-07 00:16:10 +00:00
}
2021-05-16 07:42:37 +00:00
void PrintIdNode(IdNode *node)
{
if (node == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call PrintIdNode with null value.\n");
2021-05-08 00:49:35 +00:00
return;
}
2021-05-16 07:42:37 +00:00
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);
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-22 02:52:13 +00:00
case GenericType:
printf("Generic type: %s\n", node->name);
break;
case Alloc:
printf("Alloc: %s\n", TypeTagToString(node->typeTag));
break;
}
}
2021-05-16 07:42:37 +00:00
void PrintIdTree(IdNode *tree, uint32_t tabCount)
{
if (tree == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call PrintIdTree on a null value.\n");
2021-05-07 21:36:37 +00:00
return;
}
2021-05-07 00:16:10 +00:00
uint32_t i;
2021-05-16 07:42:37 +00:00
for (i = 0; i < tabCount; i++)
{
2021-05-07 00:16:10 +00:00
printf("| ");
}
PrintIdNode(tree);
2021-05-07 00:16:10 +00:00
2021-05-16 07:42:37 +00:00
for (i = 0; i < tree->childCount; i++)
{
2021-05-07 00:16:10 +00:00
PrintIdTree(tree->children[i], tabCount + 1);
}
}
2021-05-16 07:42:37 +00:00
int PrintAncestors(IdNode *node)
{
if (node == NULL)
return -1;
int i;
int indent = 1;
indent += PrintAncestors(node->parent);
2021-05-16 07:42:37 +00:00
for (i = 0; i < indent; i++)
{
printf(" ");
}
PrintIdNode(node);
return indent;
}
2021-05-16 07:42:37 +00:00
IdNode *LookdownId(IdNode *root, NodeType targetType, char *targetName)
{
if (root == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call LookdownId on a null value.\n");
return NULL;
}
IdNode *result = NULL;
2021-05-16 07:42:37 +00:00
IdNode **frontier = (IdNode **)malloc(sizeof(IdNode *));
frontier[0] = root;
uint32_t frontierCount = 1;
2021-05-16 07:42:37 +00:00
while (frontierCount > 0)
{
IdNode *current = frontier[0];
2021-05-16 07:42:37 +00:00
if (current->type == targetType &&
strcmp(current->name, targetName) == 0)
{
result = current;
break;
}
uint32_t i;
2021-05-16 07:42:37 +00:00
for (i = 1; i < frontierCount; i++)
{
frontier[i - 1] = frontier[i];
}
size_t newSize = frontierCount + current->childCount - 1;
2021-05-16 07:42:37 +00:00
if (frontierCount != newSize)
{
frontier = (IdNode **)realloc(frontier, sizeof(IdNode *) * newSize);
}
2021-05-16 07:42:37 +00:00
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
2021-05-16 07:42:37 +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;
2021-05-08 21:51:15 +00:00
}
}
2021-05-16 07:42:37 +00:00
IdNode *LookupId(IdNode *node, IdNode *prev, char *target)
{
if (node == NULL)
{
2021-05-08 21:51:15 +00:00
return NULL;
}
2021-05-16 07:42:37 +00:00
if (strcmp(node->name, target) == 0 && node->type != Placeholder)
{
2021-05-08 21:51:15 +00:00
return node;
}
2021-05-16 07:42:37 +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.
2021-05-14 18:53:09 +00:00
*
2021-05-16 07:42:37 +00:00
* 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-14 18:53:09 +00:00
*/
2021-05-16 07:42:37 +00:00
if (prev == NULL)
{
2021-05-08 22:12:53 +00:00
return LookupId(node->parent, node, target);
}
2021-05-16 07:42:37 +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-14 18:53:09 +00:00
*/
2021-05-08 21:51:15 +00:00
uint32_t idxLimit;
2021-05-16 07:42:37 +00:00
if (ScopeHasOrdering(node))
{
2021-05-08 21:51:15 +00:00
uint32_t i;
2021-05-16 07:42:37 +00:00
for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++)
{
if (node->children[i] == prev)
{
2021-05-08 21:51:15 +00:00
break;
}
}
2021-05-16 07:42:37 +00:00
}
else
{
2021-05-08 22:12:53 +00:00
idxLimit = node->childCount;
2021-05-08 21:51:15 +00:00
}
uint32_t i;
2021-05-16 07:42:37 +00:00
for (i = 0; i < idxLimit; i++)
{
2021-05-08 21:51:15 +00:00
IdNode *child = node->children[i];
2021-05-16 07:42:37 +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-16 07:42:37 +00:00
if (strcmp(child->name, target) == 0)
{
2021-05-08 21:51:15 +00:00
return child;
2021-05-14 18:53:09 +00:00
}
2021-05-08 21:51:15 +00:00
2021-05-16 07:42:37 +00:00
if (child->type == Struct)
{
2021-05-08 21:51:15 +00:00
uint32_t j;
2021-05-16 07:42:37 +00:00
for (j = 0; j < child->childCount; j++)
{
2021-05-08 21:51:15 +00:00
IdNode *grandchild = child->children[j];
2021-05-16 07:42:37 +00:00
if (strcmp(grandchild->name, target) == 0)
{
2021-05-08 21:51:15 +00:00
return grandchild;
2021-05-14 18:53:09 +00:00
}
2021-05-08 21:51:15 +00:00
}
}
}
return LookupId(node->parent, node, target);
}