wraith-lang/src/ast.c

1449 lines
40 KiB
C
Raw Normal View History

2021-04-18 22:29:54 +00:00
#include "ast.h"
#include <stdbool.h>
2021-04-18 22:29:54 +00:00
#include <stdio.h>
2021-05-16 07:42:37 +00:00
#include <stdlib.h>
2021-04-18 22:29:54 +00:00
2021-05-07 00:15:17 +00:00
#include "util.h"
2021-04-18 22:29:54 +00:00
2021-05-16 07:42:37 +00:00
const char *SyntaxKindString(SyntaxKind syntaxKind)
2021-04-18 22:29:54 +00:00
{
2021-05-16 07:42:37 +00:00
switch (syntaxKind)
2021-04-18 22:29:54 +00:00
{
2021-05-16 07:42:37 +00:00
case AccessExpression:
return "AccessExpression";
case AllocExpression:
return "Alloc";
case Assignment:
return "Assignment";
case BinaryExpression:
return "BinaryExpression";
case Comment:
return "Comment";
case CustomTypeNode:
return "CustomTypeNode";
case Declaration:
return "Declaration";
case ForLoop:
return "ForLoop";
case DeclarationSequence:
return "DeclarationSequence";
case FunctionArgumentSequence:
return "FunctionArgumentSequence";
case FunctionCallExpression:
return "FunctionCallExpression";
case FunctionDeclaration:
return "FunctionDeclaration";
case FunctionModifiers:
return "FunctionModifiers";
case FunctionSignature:
return "FunctionSignature";
case FunctionSignatureArguments:
return "FunctionSignatureArguments";
case GenericArgument:
return "GenericArgument";
case GenericArguments:
return "GenericArguments";
case GenericTypeNode:
return "GenericTypeNode";
2021-05-16 07:42:37 +00:00
case Identifier:
return "Identifier";
case IfStatement:
return "If";
case IfElseStatement:
return "IfElse";
case Number:
return "Number";
case PrimitiveTypeNode:
return "PrimitiveTypeNode";
case ReferenceTypeNode:
return "ReferenceTypeNode";
case Return:
return "Return";
case StatementSequence:
return "StatementSequence";
case StaticModifier:
return "StaticModifier";
case StringLiteral:
return "StringLiteral";
case StructDeclaration:
return "StructDeclaration";
case Type:
return "Type";
case UnaryExpression:
return "UnaryExpression";
default:
return "Unknown";
2021-04-18 22:29:54 +00:00
}
}
2021-05-16 07:42:37 +00:00
uint8_t IsPrimitiveType(Node *typeNode)
{
return typeNode->type.typeNode->syntaxKind == PrimitiveTypeNode;
2021-04-24 19:59:30 +00:00
}
2021-05-16 07:42:37 +00:00
Node *MakePrimitiveTypeNode(PrimitiveType type)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-24 19:59:30 +00:00
node->syntaxKind = PrimitiveTypeNode;
node->primitiveType.type = type;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeCustomTypeNode(char *name)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-24 19:59:30 +00:00
node->syntaxKind = CustomTypeNode;
node->customType.name = strdup(name);
2021-04-24 19:59:30 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeReferenceTypeNode(Node *typeNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-24 19:59:30 +00:00
node->syntaxKind = ReferenceTypeNode;
node->referenceType.type = typeNode;
2021-04-24 19:59:30 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeTypeNode(Node *typeNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-21 06:51:26 +00:00
node->syntaxKind = Type;
node->type.typeNode = typeNode;
2021-04-21 06:51:26 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeIdentifierNode(const char *id)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = Identifier;
node->identifier.name = strdup(id);
node->typeTag = NULL;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeNumberNode(const char *numberString)
{
2021-04-18 22:29:54 +00:00
char *ptr;
2021-05-16 07:42:37 +00:00
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = Number;
node->number.value = strtoul(numberString, &ptr, 10);
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeStringNode(const char *string)
{
2021-04-30 19:17:44 +00:00
size_t slen = strlen(string);
2021-05-16 07:42:37 +00:00
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = StringLiteral;
node->stringLiteral.string = strndup(string + 1, slen - 2);
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeStaticNode()
2021-04-22 07:35:42 +00:00
{
2021-05-16 07:42:37 +00:00
Node *node = (Node *)malloc(sizeof(Node));
2021-04-22 07:35:42 +00:00
node->syntaxKind = StaticModifier;
return node;
}
/* FIXME: this sucks */
2021-05-16 07:42:37 +00:00
Node *MakeFunctionModifiersNode(Node **pModifierNodes, uint32_t modifierCount)
{
2021-04-22 07:35:42 +00:00
uint32_t i;
2021-05-16 07:42:37 +00:00
Node *node = (Node *)malloc(sizeof(Node));
2021-04-22 07:35:42 +00:00
node->syntaxKind = FunctionModifiers;
node->functionModifiers.count = modifierCount;
node->functionModifiers.sequence = NULL;
2021-04-22 07:35:42 +00:00
if (modifierCount > 0)
{
2021-05-16 07:42:37 +00:00
node->functionModifiers.sequence =
malloc(sizeof(Node *) * node->functionModifiers.count);
2021-04-22 07:35:42 +00:00
for (i = 0; i < modifierCount; i += 1)
{
node->functionModifiers.sequence[i] = pModifierNodes[i];
2021-04-22 07:35:42 +00:00
}
}
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeUnaryNode(UnaryOperator operator, Node * child)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = UnaryExpression;
2021-05-16 07:42:37 +00:00
node->unaryExpression.operator= operator;
node->unaryExpression.child = child;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeBinaryNode(BinaryOperator operator, Node * left, Node *right)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = BinaryExpression;
node->binaryExpression.left = left;
node->binaryExpression.right = right;
2021-05-16 07:42:37 +00:00
node->binaryExpression.operator= operator;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeDeclarationNode(Node *typeNode, Node *identifierNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = Declaration;
node->declaration.type = typeNode;
node->declaration.identifier = identifierNode;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeAssignmentNode(Node *left, Node *right)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = Assignment;
node->assignmentStatement.left = left;
node->assignmentStatement.right = right;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *StartStatementSequenceNode(Node *statementNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = StatementSequence;
2021-05-16 07:42:37 +00:00
node->statementSequence.sequence = (Node **)malloc(sizeof(Node *));
node->statementSequence.sequence[0] = statementNode;
node->statementSequence.count = 1;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *AddStatement(Node *statementSequenceNode, Node *statementNode)
{
statementSequenceNode->statementSequence.sequence = realloc(
statementSequenceNode->statementSequence.sequence,
sizeof(Node *) * (statementSequenceNode->statementSequence.count + 1));
statementSequenceNode->statementSequence
.sequence[statementSequenceNode->statementSequence.count] =
statementNode;
statementSequenceNode->statementSequence.count += 1;
2021-04-29 19:42:51 +00:00
return statementSequenceNode;
}
2021-05-16 07:42:37 +00:00
Node *MakeReturnStatementNode(Node *expressionNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = Return;
node->returnStatement.expression = expressionNode;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeReturnVoidStatementNode()
2021-04-21 02:00:18 +00:00
{
2021-05-16 07:42:37 +00:00
Node *node = (Node *)malloc(sizeof(Node));
2021-04-21 02:00:18 +00:00
node->syntaxKind = ReturnVoid;
return node;
}
2021-05-16 07:42:37 +00:00
Node *StartFunctionSignatureArgumentsNode(Node *argumentNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-20 01:18:45 +00:00
node->syntaxKind = FunctionSignatureArguments;
2021-05-16 07:42:37 +00:00
node->functionSignatureArguments.sequence = (Node **)malloc(sizeof(Node *));
node->functionSignatureArguments.sequence[0] = argumentNode;
node->functionSignatureArguments.count = 1;
2021-04-29 19:42:51 +00:00
return node;
}
2021-04-20 01:18:45 +00:00
2021-05-16 07:42:37 +00:00
Node *AddFunctionSignatureArgumentNode(Node *argumentsNode, Node *argumentNode)
{
argumentsNode->functionSignatureArguments.sequence = realloc(
argumentsNode->functionSignatureArguments.sequence,
sizeof(Node *) * (argumentsNode->functionSignatureArguments.count + 1));
argumentsNode->functionSignatureArguments
.sequence[argumentsNode->functionSignatureArguments.count] =
argumentNode;
argumentsNode->functionSignatureArguments.count += 1;
2021-04-29 19:42:51 +00:00
return argumentsNode;
}
2021-04-20 01:18:45 +00:00
2021-04-29 19:42:51 +00:00
Node *MakeEmptyFunctionSignatureArgumentsNode()
{
2021-05-16 07:42:37 +00:00
Node *node = (Node *)malloc(sizeof(Node));
2021-04-29 19:42:51 +00:00
node->syntaxKind = FunctionSignatureArguments;
node->functionSignatureArguments.sequence = NULL;
node->functionSignatureArguments.count = 0;
2021-04-20 01:18:45 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeFunctionSignatureNode(
2021-04-18 22:29:54 +00:00
Node *identifierNode,
2021-05-16 07:42:37 +00:00
Node *typeNode,
Node *arguments,
2021-05-19 22:45:07 +00:00
Node *modifiersNode,
Node *genericArgumentsNode)
2021-05-16 07:42:37 +00:00
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = FunctionSignature;
node->functionSignature.identifier = identifierNode;
node->functionSignature.type = typeNode;
node->functionSignature.arguments = arguments;
node->functionSignature.modifiers = modifiersNode;
2021-05-19 22:45:07 +00:00
node->functionSignature.genericArguments = genericArgumentsNode;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeFunctionDeclarationNode(
Node *functionSignatureNode,
Node *functionBodyNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = FunctionDeclaration;
node->functionDeclaration.functionSignature = functionSignatureNode;
node->functionDeclaration.functionBody = functionBodyNode;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeStructDeclarationNode(
2021-04-18 22:29:54 +00:00
Node *identifierNode,
2021-05-16 07:42:37 +00:00
Node *declarationSequenceNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = StructDeclaration;
node->structDeclaration.identifier = identifierNode;
node->structDeclaration.declarationSequence = declarationSequenceNode;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *StartDeclarationSequenceNode(Node *declarationNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-18 22:29:54 +00:00
node->syntaxKind = DeclarationSequence;
2021-05-16 07:42:37 +00:00
node->declarationSequence.sequence = (Node **)malloc(sizeof(Node *));
node->declarationSequence.sequence[0] = declarationNode;
node->declarationSequence.count = 1;
2021-04-29 19:42:51 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *AddDeclarationNode(Node *declarationSequenceNode, Node *declarationNode)
{
declarationSequenceNode->declarationSequence.sequence = (Node **)realloc(
declarationSequenceNode->declarationSequence.sequence,
sizeof(Node *) *
(declarationSequenceNode->declarationSequence.count + 1));
declarationSequenceNode->declarationSequence
.sequence[declarationSequenceNode->declarationSequence.count] =
declarationNode;
declarationSequenceNode->declarationSequence.count += 1;
2021-04-29 19:42:51 +00:00
return declarationSequenceNode;
}
2021-05-16 07:42:37 +00:00
Node *StartFunctionArgumentSequenceNode(Node *argumentNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-29 19:42:51 +00:00
node->syntaxKind = FunctionArgumentSequence;
2021-05-16 07:42:37 +00:00
node->functionArgumentSequence.sequence = (Node **)malloc(sizeof(Node *));
node->functionArgumentSequence.sequence[0] = argumentNode;
node->functionArgumentSequence.count = 1;
2021-04-18 22:29:54 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *AddFunctionArgumentNode(Node *argumentSequenceNode, Node *argumentNode)
{
argumentSequenceNode->functionArgumentSequence.sequence = (Node **)realloc(
argumentSequenceNode->functionArgumentSequence.sequence,
sizeof(Node *) *
(argumentSequenceNode->functionArgumentSequence.count + 1));
argumentSequenceNode->functionArgumentSequence
.sequence[argumentSequenceNode->functionArgumentSequence.count] =
argumentNode;
argumentSequenceNode->functionArgumentSequence.count += 1;
2021-04-29 19:42:51 +00:00
return argumentSequenceNode;
}
Node *MakeEmptyFunctionArgumentSequenceNode()
{
2021-05-16 07:42:37 +00:00
Node *node = (Node *)malloc(sizeof(Node));
2021-04-20 17:47:40 +00:00
node->syntaxKind = FunctionArgumentSequence;
node->functionArgumentSequence.count = 0;
node->functionArgumentSequence.sequence = NULL;
2021-04-20 17:47:40 +00:00
return node;
}
2021-05-19 22:45:07 +00:00
Node *MakeGenericArgumentNode(Node *identifierNode, Node *constraintNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = GenericArgument;
node->genericArgument.identifier = identifierNode;
node->genericArgument.constraint = constraintNode;
return node;
}
Node *StartGenericArgumentsNode(Node *genericArgumentNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = GenericArguments;
node->genericArguments.arguments = (Node **)malloc(sizeof(Node *));
node->genericArguments.arguments[0] = genericArgumentNode;
node->genericArguments.count = 1;
return node;
}
Node *AddGenericArgument(Node *genericArgumentsNode, Node *genericArgumentNode)
{
genericArgumentsNode->genericArguments.arguments = (Node **)realloc(
genericArgumentsNode->genericArguments.arguments,
sizeof(Node *) * (genericArgumentsNode->genericArguments.count + 1));
genericArgumentsNode->genericArguments
.arguments[genericArgumentsNode->genericArguments.count] =
genericArgumentNode;
genericArgumentsNode->genericArguments.count += 1;
return genericArgumentsNode;
}
Node *MakeEmptyGenericArgumentsNode()
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = GenericArguments;
node->genericArguments.arguments = NULL;
node->genericArguments.count = 0;
return node;
}
Node *MakeGenericTypeNode(char *name)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = GenericTypeNode;
node->genericType.name = strdup(name);
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeFunctionCallExpressionNode(
2021-04-20 17:47:40 +00:00
Node *identifierNode,
2021-05-16 07:42:37 +00:00
Node *argumentSequenceNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-20 17:47:40 +00:00
node->syntaxKind = FunctionCallExpression;
node->functionCallExpression.identifier = identifierNode;
node->functionCallExpression.argumentSequence = argumentSequenceNode;
2021-04-20 17:47:40 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeAccessExpressionNode(Node *accessee, Node *accessor)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-22 04:29:38 +00:00
node->syntaxKind = AccessExpression;
node->accessExpression.accessee = accessee;
node->accessExpression.accessor = accessor;
2021-04-22 04:29:38 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeAllocNode(Node *typeNode)
2021-04-28 19:49:45 +00:00
{
2021-05-16 07:42:37 +00:00
Node *node = (Node *)malloc(sizeof(Node));
2021-04-28 19:49:45 +00:00
node->syntaxKind = AllocExpression;
node->allocExpression.type = typeNode;
2021-04-28 19:49:45 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeIfNode(Node *expressionNode, Node *statementSequenceNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-29 04:25:25 +00:00
node->syntaxKind = IfStatement;
node->ifStatement.expression = expressionNode;
node->ifStatement.statementSequence = statementSequenceNode;
2021-04-29 04:25:25 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeIfElseNode(Node *ifNode, Node *elseNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-29 19:42:51 +00:00
node->syntaxKind = IfElseStatement;
node->ifElseStatement.ifStatement = ifNode;
node->ifElseStatement.elseStatement = elseNode;
2021-04-29 19:42:51 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
Node *MakeForLoopNode(
Node *declarationNode,
2021-04-30 06:49:35 +00:00
Node *startNumberNode,
Node *endNumberNode,
2021-05-16 07:42:37 +00:00
Node *statementSequenceNode)
{
Node *node = (Node *)malloc(sizeof(Node));
2021-04-30 06:49:35 +00:00
node->syntaxKind = ForLoop;
node->forLoop.declaration = declarationNode;
node->forLoop.startNumber = startNumberNode;
node->forLoop.endNumber = endNumberNode;
node->forLoop.statementSequence = statementSequenceNode;
2021-04-30 06:49:35 +00:00
return node;
}
2021-05-16 07:42:37 +00:00
static const char *PrimitiveTypeToString(PrimitiveType type)
2021-04-18 22:29:54 +00:00
{
switch (type)
{
2021-05-16 07:42:37 +00:00
case Int:
return "Int";
case UInt:
return "UInt";
case Bool:
return "Bool";
case Void:
return "Void";
2021-04-18 22:29:54 +00:00
}
return "Unknown";
}
static void PrintUnaryOperator(UnaryOperator operator)
2021-04-18 22:29:54 +00:00
{
switch (operator)
{
2021-05-16 07:42:37 +00:00
case Negate:
printf("!");
break;
}
}
static void PrintBinaryOperator(BinaryOperator operator)
{
switch (operator)
2021-04-18 22:29:54 +00:00
{
2021-05-16 07:42:37 +00:00
case Add:
printf("(+)");
break;
2021-04-18 22:29:54 +00:00
2021-05-16 07:42:37 +00:00
case Subtract:
printf("(-)");
break;
2021-04-20 17:47:40 +00:00
2021-05-16 07:42:37 +00:00
case Multiply:
printf("(*)");
break;
2021-04-18 22:29:54 +00:00
}
}
void PrintNode(Node *node, uint32_t tabCount)
2021-04-18 22:29:54 +00:00
{
uint32_t i;
for (i = 0; i < tabCount; i += 1)
{
printf(" ");
}
printf("%s: ", SyntaxKindString(node->syntaxKind));
switch (node->syntaxKind)
{
2021-05-16 07:42:37 +00:00
case AccessExpression:
printf("\n");
PrintNode(node->accessExpression.accessee, tabCount + 1);
PrintNode(node->accessExpression.accessor, tabCount + 1);
return;
case AllocExpression:
printf("\n");
PrintNode(node->allocExpression.type, tabCount + 1);
return;
case Assignment:
printf("\n");
PrintNode(node->assignmentStatement.left, tabCount + 1);
PrintNode(node->assignmentStatement.right, tabCount + 1);
return;
case BinaryExpression:
PrintBinaryOperator(node->binaryExpression.operator);
printf("\n");
PrintNode(node->binaryExpression.left, tabCount + 1);
PrintNode(node->binaryExpression.right, tabCount + 1);
return;
case CustomTypeNode:
printf("%s\n", node->customType.name);
return;
case Declaration:
printf("\n");
PrintNode(node->declaration.identifier, tabCount + 1);
PrintNode(node->declaration.type, tabCount + 1);
return;
case DeclarationSequence:
printf("\n");
for (i = 0; i < node->declarationSequence.count; i += 1)
{
PrintNode(node->declarationSequence.sequence[i], tabCount + 1);
}
return;
case ForLoop:
printf("\n");
PrintNode(node->forLoop.declaration, tabCount + 1);
PrintNode(node->forLoop.startNumber, tabCount + 1);
PrintNode(node->forLoop.endNumber, tabCount + 1);
PrintNode(node->forLoop.statementSequence, tabCount + 1);
return;
case FunctionArgumentSequence:
printf("\n");
for (i = 0; i < node->functionArgumentSequence.count; i += 1)
{
PrintNode(node->functionArgumentSequence.sequence[i], tabCount + 1);
}
return;
case FunctionCallExpression:
printf("\n");
PrintNode(node->functionCallExpression.identifier, tabCount + 1);
PrintNode(node->functionCallExpression.argumentSequence, tabCount + 1);
return;
case FunctionDeclaration:
printf("\n");
PrintNode(node->functionDeclaration.functionSignature, tabCount + 1);
PrintNode(node->functionDeclaration.functionBody, tabCount + 1);
return;
case FunctionModifiers:
printf("\n");
for (i = 0; i < node->functionModifiers.count; i += 1)
{
PrintNode(node->functionModifiers.sequence[i], tabCount + 1);
}
return;
case FunctionSignature:
printf("\n");
PrintNode(node->functionSignature.identifier, tabCount + 1);
PrintNode(node->functionSignature.genericArguments, tabCount + 1);
2021-05-16 07:42:37 +00:00
PrintNode(node->functionSignature.arguments, tabCount + 1);
PrintNode(node->functionSignature.type, tabCount + 1);
PrintNode(node->functionSignature.modifiers, tabCount + 1);
return;
case FunctionSignatureArguments:
printf("\n");
for (i = 0; i < node->functionSignatureArguments.count; i += 1)
{
PrintNode(
node->functionSignatureArguments.sequence[i],
tabCount + 1);
}
return;
case GenericArgument:
printf("\n");
PrintNode(node->genericArgument.identifier, tabCount + 1);
/* Constraint nodes are not implemented. */
/* PrintNode(node->genericArgument.constraint, tabCount + 1); */
return;
case GenericArguments:
printf("\n");
for (i = 0; i < node->genericArguments.count; i += 1) {
PrintNode(node->genericArguments.arguments[i], tabCount + 1);
}
return;
case GenericTypeNode:
printf("%s\n", node->genericType.name);
return;
2021-05-16 07:42:37 +00:00
case Identifier:
if (node->typeTag == NULL)
{
printf("%s\n", node->identifier.name);
}
else
{
char *type = TypeTagToString(node->typeTag);
printf("%s<%s>\n", node->identifier.name, type);
}
return;
case IfStatement:
printf("\n");
PrintNode(node->ifStatement.expression, tabCount + 1);
PrintNode(node->ifStatement.statementSequence, tabCount + 1);
return;
case IfElseStatement:
printf("\n");
PrintNode(node->ifElseStatement.ifStatement, tabCount + 1);
PrintNode(node->ifElseStatement.elseStatement, tabCount + 1);
return;
case Number:
printf("%lu\n", node->number.value);
return;
case PrimitiveTypeNode:
printf("%s\n", PrimitiveTypeToString(node->primitiveType.type));
return;
case ReferenceTypeNode:
printf("\n");
PrintNode(node->referenceType.type, tabCount + 1);
return;
case Return:
printf("\n");
PrintNode(node->returnStatement.expression, tabCount + 1);
return;
case ReturnVoid:
return;
case StatementSequence:
printf("\n");
for (i = 0; i < node->statementSequence.count; i += 1)
{
PrintNode(node->statementSequence.sequence[i], tabCount + 1);
}
return;
case StaticModifier:
printf("\n");
return;
case StringLiteral:
2021-05-27 21:10:44 +00:00
printf("%s\n", node->stringLiteral.string);
2021-05-16 07:42:37 +00:00
return;
case StructDeclaration:
printf("\n");
PrintNode(node->structDeclaration.identifier, tabCount + 1);
PrintNode(node->structDeclaration.declarationSequence, tabCount + 1);
return;
case Type:
printf("\n");
PrintNode(node->type.typeNode, tabCount + 1);
return;
case UnaryExpression:
PrintUnaryOperator(node->unaryExpression.operator);
PrintNode(node->unaryExpression.child, tabCount + 1);
return;
2021-04-18 22:29:54 +00:00
}
}
2021-05-08 00:49:35 +00:00
void Recurse(Node *node, void (*func)(Node*))
{
uint32_t i;
switch (node->syntaxKind)
{
case AccessExpression:
func(node->accessExpression.accessee);
func(node->accessExpression.accessor);
return;
case AllocExpression:
func(node->allocExpression.type);
return;
case Assignment:
func(node->assignmentStatement.left);
func(node->assignmentStatement.right);
return;
case BinaryExpression:
func(node->binaryExpression.left);
func(node->binaryExpression.right);
return;
case Comment:
return;
case CustomTypeNode:
return;
case Declaration:
func(node->declaration.type);
func(node->declaration.identifier);
return;
case DeclarationSequence:
for (i = 0; i < node->declarationSequence.count; i += 1) {
func(node->declarationSequence.sequence[i]);
}
return;
case ForLoop:
func(node->forLoop.declaration);
func(node->forLoop.startNumber);
func(node->forLoop.endNumber);
func(node->forLoop.statementSequence);
return;
case FunctionArgumentSequence:
for (i = 0; i < node->functionArgumentSequence.count; i += 1) {
func(node->functionArgumentSequence.sequence[i]);
}
return;
case FunctionCallExpression:
func(node->functionCallExpression.identifier);
func(node->functionCallExpression.argumentSequence);
return;
case FunctionDeclaration:
func(node->functionDeclaration.functionSignature);
func(node->functionDeclaration.functionBody);
return;
case FunctionModifiers:
for (i = 0; i < node->functionModifiers.count; i += 1) {
func(node->functionModifiers.sequence[i]);
}
return;
case FunctionSignature:
func(node->functionSignature.identifier);
func(node->functionSignature.type);
func(node->functionSignature.arguments);
func(node->functionSignature.modifiers);
func(node->functionSignature.genericArguments);
return;
case FunctionSignatureArguments:
for (i = 0; i < node->functionSignatureArguments.count; i += 1) {
func(node->functionSignatureArguments.sequence[i]);
}
return;
case GenericArgument:
func(node->genericArgument.identifier);
func(node->genericArgument.constraint);
return;
case GenericArguments:
for (i = 0; i < node->genericArguments.count; i += 1) {
func(node->genericArguments.arguments[i]);
}
return;
case GenericTypeNode:
return;
case Identifier:
return;
case IfStatement:
func(node->ifStatement.expression);
func(node->ifStatement.statementSequence);
return;
case IfElseStatement:
func(node->ifElseStatement.ifStatement);
func(node->ifElseStatement.elseStatement);
return;
case Number:
return;
case PrimitiveTypeNode:
return;
case ReferenceTypeNode:
func(node->referenceType.type);
return;
case Return:
func(node->returnStatement.expression);
return;
case ReturnVoid:
return;
case StatementSequence:
for (i = 0; i < node->statementSequence.count; i += 1) {
func(node->statementSequence.sequence[i]);
}
return;
case StaticModifier:
return;
case StringLiteral:
return;
case StructDeclaration:
func(node->structDeclaration.identifier);
func(node->structDeclaration.declarationSequence);
return;
case Type:
return;
case UnaryExpression:
func(node->unaryExpression.child);
return;
default:
fprintf(stderr, "wraith: Unhandled SyntaxKind %s in recurse function.\n",
SyntaxKindString(node->syntaxKind));
return;
}
}
2021-05-16 07:42:37 +00:00
TypeTag *MakeTypeTag(Node *node)
{
if (node == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call MakeTypeTag on null value.\n");
2021-05-08 00:49:35 +00:00
return NULL;
}
2021-05-16 07:42:37 +00:00
TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag));
switch (node->syntaxKind)
{
case Type:
tag = MakeTypeTag(node->type.typeNode);
break;
case PrimitiveTypeNode:
tag->type = Primitive;
tag->value.primitiveType = node->primitiveType.type;
break;
case ReferenceTypeNode:
tag->type = Reference;
tag->value.referenceType = MakeTypeTag(node->referenceType.type);
break;
case CustomTypeNode:
tag->type = Custom;
tag->value.customType = strdup(node->customType.name);
break;
case Declaration:
tag = MakeTypeTag(node->declaration.type);
break;
case StructDeclaration:
tag->type = Custom;
tag->value.customType =
strdup(node->structDeclaration.identifier->identifier.name);
break;
case FunctionDeclaration:
tag = MakeTypeTag(node->functionDeclaration.functionSignature
->functionSignature.type);
break;
2021-05-22 02:52:13 +00:00
case AllocExpression:
tag = MakeTypeTag(node->allocExpression.type);
break;
2021-05-30 01:27:13 +00:00
case GenericArgument:
tag->type = Generic;
tag->value.genericType = strdup(node->genericArgument.identifier
->identifier.name);
break;
case GenericTypeNode:
tag->type = Generic;
tag->value.genericType = strdup(node->genericType.name);
2021-05-30 01:27:13 +00:00
break;
2021-05-16 07:42:37 +00:00
default:
fprintf(
stderr,
"wraith: Attempted to call MakeTypeTag on"
" node with unsupported SyntaxKind: %s\n",
SyntaxKindString(node->syntaxKind));
return NULL;
2021-05-08 00:49:35 +00:00
}
2021-05-30 01:27:13 +00:00
2021-05-08 00:49:35 +00:00
return tag;
}
2021-05-16 07:42:37 +00:00
char *TypeTagToString(TypeTag *tag)
{
if (tag == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call TypeTagToString with null value\n");
2021-05-08 00:49:35 +00:00
return NULL;
}
2021-05-16 07:42:37 +00:00
switch (tag->type)
{
case Unknown:
return "Unknown";
case Primitive:
return PrimitiveTypeToString(tag->value.primitiveType);
case Reference:
{
char *inner = TypeTagToString(tag->value.referenceType);
size_t innerStrLen = strlen(inner);
char *result = malloc(sizeof(char) * (innerStrLen + 5));
sprintf(result, "Ref<%s>", inner);
return result;
}
case Custom:
{
char *result = malloc(sizeof(char) * (strlen(tag->value.customType) + 8));
sprintf(result, "Custom<%s>", tag->value.customType);
return result;
}
case Generic:
{
char *result = malloc(sizeof(char) * (strlen(tag->value.customType) + 9));
sprintf(result, "Generic<%s>", tag->value.customType);
return result;
}
2021-05-08 00:49:35 +00:00
}
}
2021-05-30 01:27:13 +00:00
void LinkParentPointers(Node *node, Node *prev)
{
2021-05-30 01:27:13 +00:00
if (node == NULL) return;
2021-05-30 01:27:13 +00:00
node->parent = prev;
uint32_t i;
switch (node->syntaxKind)
{
2021-05-30 01:27:13 +00:00
case AccessExpression:
LinkParentPointers(node->accessExpression.accessee, node);
LinkParentPointers(node->accessExpression.accessor, node);
return;
case AllocExpression:
LinkParentPointers(node->allocExpression.type, node);
return;
case Assignment:
LinkParentPointers(node->assignmentStatement.left, node);
LinkParentPointers(node->assignmentStatement.right, node);
return;
case BinaryExpression:
LinkParentPointers(node->binaryExpression.left, node);
LinkParentPointers(node->binaryExpression.right, node);
return;
case Comment:
return;
case CustomTypeNode:
return;
case Declaration:
LinkParentPointers(node->declaration.type, node);
LinkParentPointers(node->declaration.identifier, node);
return;
case DeclarationSequence:
for (i = 0; i < node->declarationSequence.count; i += 1) {
LinkParentPointers(node->declarationSequence.sequence[i], node);
}
return;
case ForLoop:
LinkParentPointers(node->forLoop.declaration, node);
LinkParentPointers(node->forLoop.startNumber, node);
LinkParentPointers(node->forLoop.endNumber, node);
LinkParentPointers(node->forLoop.statementSequence, node);
return;
case FunctionArgumentSequence:
for (i = 0; i < node->functionArgumentSequence.count; i += 1) {
LinkParentPointers(node->functionArgumentSequence.sequence[i], node);
}
return;
case FunctionCallExpression:
LinkParentPointers(node->functionCallExpression.identifier, node);
LinkParentPointers(node->functionCallExpression.argumentSequence, node);
return;
case FunctionDeclaration:
LinkParentPointers(node->functionDeclaration.functionSignature, node);
LinkParentPointers(node->functionDeclaration.functionBody, node);
return;
case FunctionModifiers:
for (i = 0; i < node->functionModifiers.count; i += 1) {
LinkParentPointers(node->functionModifiers.sequence[i], node);
}
return;
case FunctionSignature:
LinkParentPointers(node->functionSignature.identifier, node);
LinkParentPointers(node->functionSignature.type, node);
LinkParentPointers(node->functionSignature.arguments, node);
LinkParentPointers(node->functionSignature.modifiers, node);
LinkParentPointers(node->functionSignature.genericArguments, node);
return;
case FunctionSignatureArguments:
for (i = 0; i < node->functionSignatureArguments.count; i += 1) {
LinkParentPointers(node->functionSignatureArguments.sequence[i], node);
}
return;
case GenericArgument:
LinkParentPointers(node->genericArgument.identifier, node);
LinkParentPointers(node->genericArgument.constraint, node);
return;
case GenericArguments:
for (i = 0; i < node->genericArguments.count; i += 1) {
LinkParentPointers(node->genericArguments.arguments[i], node);
}
return;
case GenericTypeNode:
return;
case Identifier:
return;
case IfStatement:
LinkParentPointers(node->ifStatement.expression, node);
LinkParentPointers(node->ifStatement.statementSequence, node);
return;
case IfElseStatement:
LinkParentPointers(node->ifElseStatement.ifStatement, node);
LinkParentPointers(node->ifElseStatement.elseStatement, node);
return;
case Number:
return;
case PrimitiveTypeNode:
return;
case ReferenceTypeNode:
LinkParentPointers(node->referenceType.type, node);
return;
case Return:
LinkParentPointers(node->returnStatement.expression, node);
return;
case ReturnVoid:
return;
2021-05-30 01:27:13 +00:00
case StatementSequence:
for (i = 0; i < node->statementSequence.count; i += 1) {
LinkParentPointers(node->statementSequence.sequence[i], node);
}
return;
case StaticModifier:
return;
case StringLiteral:
return;
case StructDeclaration:
LinkParentPointers(node->structDeclaration.identifier, node);
LinkParentPointers(node->structDeclaration.declarationSequence, node);
return;
case Type:
return;
case UnaryExpression:
LinkParentPointers(node->unaryExpression.child, node);
return;
default:
fprintf(stderr, "wraith: Unhandled SyntaxKind %s in recurse function.\n",
SyntaxKindString(node->syntaxKind));
return;
}
}
Node *GetIdFromStruct(Node *structDecl)
{
if (structDecl->syntaxKind != StructDeclaration)
{
fprintf(stderr, "wraith: Attempted to call GetIdFromStruct on node with kind: %s.\n",
SyntaxKindString(structDecl->syntaxKind));
return NULL;
}
return structDecl->structDeclaration.identifier;
}
Node *GetIdFromFunction(Node *funcDecl)
{
if (funcDecl->syntaxKind != FunctionDeclaration)
{
fprintf(stderr, "wraith: Attempted to call GetIdFromFunction on node with kind: %s.\n",
SyntaxKindString(funcDecl->syntaxKind));
return NULL;
}
Node *sig = funcDecl->functionDeclaration.functionSignature;
return sig->functionSignature.identifier;
}
Node *GetIdFromDeclaration(Node *decl)
{
if (decl->syntaxKind != Declaration)
{
fprintf(stderr, "wraith: Attempted to call GetIdFromDeclaration on node with kind: %s.\n",
SyntaxKindString(decl->syntaxKind));
}
return decl->declaration.identifier;
}
bool AssignmentHasDeclaration(Node *assign)
{
return (assign->syntaxKind == Assignment
&& assign->assignmentStatement.left->syntaxKind == Declaration);
}
Node *GetIdFromAssignment(Node *assign)
{
if (assign->syntaxKind != Assignment)
{
fprintf(stderr, "wraith: Attempted to call GetIdFromAssignment on node with kind: %s.\n",
SyntaxKindString(assign->syntaxKind));
}
if (AssignmentHasDeclaration(assign))
{
return GetIdFromDeclaration(assign->assignmentStatement.left);
}
return NULL;
}
bool NodeMayHaveId(Node *node)
{
switch (node->syntaxKind)
{
case StructDeclaration:
case FunctionDeclaration:
case Declaration:
case Assignment:
return true;
default:
return false;
}
}
Node *TryGetId(Node *node)
{
switch (node->syntaxKind)
{
2021-05-30 01:27:13 +00:00
case Assignment:
return GetIdFromAssignment(node);
case Declaration:
return GetIdFromDeclaration(node);
2021-05-30 01:27:13 +00:00
case FunctionDeclaration:
return GetIdFromFunction(node);
case StructDeclaration:
return GetIdFromStruct(node);
default:
return NULL;
}
}
Node *LookupFunctionArgId(Node *funcDecl, char *target)
{
Node *args =
funcDecl->functionDeclaration.functionSignature->functionSignature.arguments;
uint32_t i;
for (i = 0; i < args->functionArgumentSequence.count; i += 1)
{
Node *arg = args->functionArgumentSequence.sequence[i];
if (arg->syntaxKind != Declaration)
{
fprintf(stderr,
"wraith: Encountered %s node in function signature args list.\n",
SyntaxKindString(arg->syntaxKind));
continue;
}
Node *argId = GetIdFromDeclaration(arg);
if (argId != NULL && strcmp(target, argId->identifier.name) == 0)
{
return argId;
}
}
return NULL;
}
2021-05-30 01:27:13 +00:00
Node *LookupStructInternalId(Node *structDecl, char *target)
{
2021-05-30 01:27:13 +00:00
Node *decls = structDecl->structDeclaration.declarationSequence;
uint32_t i;
for (i = 0; i < decls->declarationSequence.count; i += 1)
{
Node *match = TryGetId(decls->declarationSequence.sequence[i]);
if (match != NULL && strcmp(target, match->identifier.name) == 0)
return match;
}
return NULL;
}
2021-05-30 01:27:13 +00:00
Node *InspectNode(Node *node, char *target)
{
/* If this node may have an identifier declaration inside it, attempt to look up the identifier
* node itself, returning it if it matches the given target name. */
2021-05-30 01:27:13 +00:00
if (NodeMayHaveId(node))
{
2021-05-30 01:27:13 +00:00
Node *candidateId = TryGetId(node);
if (candidateId != NULL && strcmp(target, candidateId->identifier.name) == 0)
{
return candidateId;
}
2021-05-30 01:27:13 +00:00
}
2021-05-30 01:27:13 +00:00
/* If the candidate node was not the one we wanted, but the node node is a function
* declaration, it's possible that the identifier we want is one of the function's
* parameters rather than the function's name itself. */
if (node->syntaxKind == FunctionDeclaration)
{
Node *match = LookupFunctionArgId(node, target);
if (match != NULL) return match;
}
2021-05-30 01:27:13 +00:00
/* Likewise if the node node is a struct declaration, inspect the struct's internals
* to see if a top-level definition is the one we're looking for. */
if (node->syntaxKind == StructDeclaration)
{
Node *match = LookupStructInternalId(node, target);
if (match != NULL) return match;
}
return NULL;
}
Node *LookupIdNode(Node *current, Node *prev, char *target)
{
if (current == NULL) return NULL;
Node *match;
/* First inspect the current node to see if it contains the target identifier. */
match = InspectNode(current, target);
if (match != NULL) return match;
/* If this is the start of our search, we should not attempt to look at
* child nodes. Only looking up the AST 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
2021-05-30 01:27:13 +00:00
* representing the struct itself. The same is true for functions. */
if (prev == NULL)
{
return LookupIdNode(current->parent, current, target);
}
uint32_t i;
uint32_t idxLimit;
switch (current->syntaxKind)
{
case DeclarationSequence:
for (i = 0; i < current->declarationSequence.count; i += 1)
{
Node *decl = current->declarationSequence.sequence[i];
2021-05-30 01:27:13 +00:00
match = InspectNode(decl, target);
if (match != NULL) return match;
/*Node *declId = TryGetId(decl);
if (declId != NULL && strcmp(target, declId->identifier.name) == 0)
return declId;*/
}
break;
case StatementSequence:
idxLimit = current->statementSequence.count;
for (i = 0; i < current->statementSequence.count; i += 1)
{
if (current->statementSequence.sequence[i] == prev)
{
idxLimit = i;
break;
}
}
for (i = 0; i < idxLimit; i += 1)
{
Node *stmt = current->statementSequence.sequence[i];
if (stmt == prev) continue;
2021-05-30 01:27:13 +00:00
if (strcmp(target, "g") == 0) {
printf("info: %s\n", SyntaxKindString(stmt->syntaxKind));
}
match = InspectNode(stmt, target);
if (match != NULL) return match;
/*if (NodeMayHaveId(stmt))
{
Node *candidateId = TryGetId(current);
if (candidateId != NULL && strcmp(target, candidateId->identifier.name) == 0)
return candidateId;
2021-05-30 01:27:13 +00:00
}*/
}
break;
}
2021-05-30 01:27:13 +00:00
return LookupIdNode(current->parent, current, target);
}
2021-05-30 01:27:13 +00:00
void IdentifierPass(Node *node)
{
if (node == NULL) return;
switch (node->syntaxKind)
{
case AllocExpression:
node->typeTag = MakeTypeTag(node);
break;
case Declaration:
node->declaration.identifier->typeTag = MakeTypeTag(node);
break;
case FunctionDeclaration:
node->functionDeclaration.functionSignature
->functionSignature.identifier->typeTag = MakeTypeTag(node);
break;;
case StructDeclaration:
node->structDeclaration.identifier->typeTag = MakeTypeTag(node);
break;
case GenericArgument:
node->genericArgument.identifier->typeTag = MakeTypeTag(node);
break;
case Identifier:
{
if (node->typeTag != NULL) return;
char *name = node->identifier.name;
Node *declaration = LookupIdNode(node, NULL, name);
if (declaration == NULL)
{
/* FIXME: Express this case as an error with AST information. */
fprintf(stderr, "wraith: Could not find definition of identifier %s.\n", name);
TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag));
tag->type = Unknown;
node->typeTag = tag;
}
else
{
node->typeTag = declaration->typeTag;
}
break;
}
}
Recurse(node, *IdentifierPass);
}