2021-04-18 22:29:54 +00:00
|
|
|
#include "ast.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-05-07 00:15:17 +00:00
|
|
|
#include "util.h"
|
2021-04-18 22:29:54 +00:00
|
|
|
|
|
|
|
const char* SyntaxKindString(SyntaxKind syntaxKind)
|
|
|
|
{
|
|
|
|
switch(syntaxKind)
|
|
|
|
{
|
2021-04-22 04:29:38 +00:00
|
|
|
case AccessExpression: return "AccessExpression";
|
2021-04-28 19:49:45 +00:00
|
|
|
case AllocExpression: return "Alloc";
|
2021-04-18 22:29:54 +00:00
|
|
|
case Assignment: return "Assignment";
|
|
|
|
case BinaryExpression: return "BinaryExpression";
|
|
|
|
case Comment: return "Comment";
|
2021-04-24 19:59:30 +00:00
|
|
|
case CustomTypeNode: return "CustomTypeNode";
|
2021-04-18 22:29:54 +00:00
|
|
|
case Declaration: return "Declaration";
|
2021-05-07 02:53:28 +00:00
|
|
|
case ForLoop: return "ForLoop";
|
2021-04-18 22:29:54 +00:00
|
|
|
case DeclarationSequence: return "DeclarationSequence";
|
2021-04-20 17:47:40 +00:00
|
|
|
case FunctionArgumentSequence: return "FunctionArgumentSequence";
|
|
|
|
case FunctionCallExpression: return "FunctionCallExpression";
|
2021-04-18 22:29:54 +00:00
|
|
|
case FunctionDeclaration: return "FunctionDeclaration";
|
2021-04-22 07:35:42 +00:00
|
|
|
case FunctionModifiers: return "FunctionModifiers";
|
2021-04-18 22:29:54 +00:00
|
|
|
case FunctionSignature: return "FunctionSignature";
|
2021-04-20 01:18:45 +00:00
|
|
|
case FunctionSignatureArguments: return "FunctionSignatureArguments";
|
2021-04-18 22:29:54 +00:00
|
|
|
case Identifier: return "Identifier";
|
2021-04-29 19:42:51 +00:00
|
|
|
case IfStatement: return "If";
|
|
|
|
case IfElseStatement: return "IfElse";
|
2021-04-18 22:29:54 +00:00
|
|
|
case Number: return "Number";
|
2021-04-24 19:59:30 +00:00
|
|
|
case PrimitiveTypeNode: return "PrimitiveTypeNode";
|
|
|
|
case ReferenceTypeNode: return "ReferenceTypeNode";
|
2021-04-18 22:29:54 +00:00
|
|
|
case Return: return "Return";
|
|
|
|
case StatementSequence: return "StatementSequence";
|
2021-04-22 07:35:42 +00:00
|
|
|
case StaticModifier: return "StaticModifier";
|
2021-04-18 22:29:54 +00:00
|
|
|
case StringLiteral: return "StringLiteral";
|
|
|
|
case StructDeclaration: return "StructDeclaration";
|
|
|
|
case Type: return "Type";
|
|
|
|
case UnaryExpression: return "UnaryExpression";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 19:59:30 +00:00
|
|
|
uint8_t IsPrimitiveType(
|
|
|
|
Node *typeNode
|
|
|
|
) {
|
2021-05-15 22:34:15 +00:00
|
|
|
return typeNode->type.typeNode->syntaxKind == PrimitiveTypeNode;
|
2021-04-24 19:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakePrimitiveTypeNode(
|
2021-04-18 22:29:54 +00:00
|
|
|
PrimitiveType type
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
2021-04-24 19:59:30 +00:00
|
|
|
node->syntaxKind = PrimitiveTypeNode;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->primitiveType.type = type;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-21 06:51:26 +00:00
|
|
|
Node* MakeCustomTypeNode(
|
2021-04-24 19:59:30 +00:00
|
|
|
char *name
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = CustomTypeNode;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->customType.name = strdup(name);
|
2021-04-24 19:59:30 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeReferenceTypeNode(
|
|
|
|
Node *typeNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = ReferenceTypeNode;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->referenceType.type = typeNode;
|
2021-04-24 19:59:30 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeTypeNode(
|
|
|
|
Node* typeNode
|
2021-04-21 06:51:26 +00:00
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Type;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->type.typeNode = typeNode;
|
2021-04-21 06:51:26 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-18 22:29:54 +00:00
|
|
|
Node* MakeIdentifierNode(
|
|
|
|
const char *id
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Identifier;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->identifier.name = strdup(id);
|
2021-05-13 04:54:09 +00:00
|
|
|
node->typeTag = NULL;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeNumberNode(
|
|
|
|
const char *numberString
|
|
|
|
) {
|
|
|
|
char *ptr;
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Number;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->number.value = strtoul(numberString, &ptr, 10);
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeStringNode(
|
|
|
|
const char *string
|
|
|
|
) {
|
2021-04-30 19:17:44 +00:00
|
|
|
size_t slen = strlen(string);
|
2021-04-18 22:29:54 +00:00
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = StringLiteral;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->stringLiteral.string = strndup(string + 1, slen - 2);
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-22 07:35:42 +00:00
|
|
|
Node* MakeStaticNode()
|
|
|
|
{
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = StaticModifier;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:34:15 +00:00
|
|
|
/* FIXME: this sucks */
|
2021-04-22 07:35:42 +00:00
|
|
|
Node* MakeFunctionModifiersNode(
|
|
|
|
Node **pModifierNodes,
|
|
|
|
uint32_t modifierCount
|
|
|
|
) {
|
|
|
|
uint32_t i;
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionModifiers;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->functionModifiers.count = modifierCount;
|
|
|
|
node->functionModifiers.sequence = NULL;
|
2021-04-22 07:35:42 +00:00
|
|
|
if (modifierCount > 0)
|
|
|
|
{
|
2021-05-15 22:34:15 +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)
|
|
|
|
{
|
2021-05-15 22:34:15 +00:00
|
|
|
node->functionModifiers.sequence[i] = pModifierNodes[i];
|
2021-04-22 07:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-18 22:29:54 +00:00
|
|
|
Node* MakeUnaryNode(
|
|
|
|
UnaryOperator operator,
|
|
|
|
Node *child
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = UnaryExpression;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->unaryExpression.operator = operator;
|
|
|
|
node->unaryExpression.child = child;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeBinaryNode(
|
|
|
|
BinaryOperator operator,
|
|
|
|
Node *left,
|
|
|
|
Node *right
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = BinaryExpression;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->binaryExpression.left = left;
|
|
|
|
node->binaryExpression.right = right;
|
|
|
|
node->binaryExpression.operator = operator;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeDeclarationNode(
|
|
|
|
Node* typeNode,
|
|
|
|
Node* identifierNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Declaration;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->declaration.type = typeNode;
|
|
|
|
node->declaration.identifier = identifierNode;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeAssignmentNode(
|
|
|
|
Node *left,
|
|
|
|
Node *right
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Assignment;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->assignmentStatement.left = left;
|
|
|
|
node->assignmentStatement.right = right;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:42:51 +00:00
|
|
|
Node* StartStatementSequenceNode(
|
|
|
|
Node *statementNode
|
2021-04-18 22:29:54 +00:00
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = StatementSequence;
|
2021-05-15 22:34:15 +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-04-29 19:42:51 +00:00
|
|
|
Node* AddStatement(
|
|
|
|
Node* statementSequenceNode,
|
|
|
|
Node *statementNode
|
|
|
|
) {
|
2021-05-15 22:34:15 +00:00
|
|
|
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-04-18 22:29:54 +00:00
|
|
|
Node* MakeReturnStatementNode(
|
|
|
|
Node *expressionNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Return;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->returnStatement.expression = expressionNode;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-21 02:00:18 +00:00
|
|
|
Node* MakeReturnVoidStatementNode()
|
|
|
|
{
|
|
|
|
Node *node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = ReturnVoid;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:42:51 +00:00
|
|
|
Node *StartFunctionSignatureArgumentsNode(
|
|
|
|
Node *argumentNode
|
2021-04-20 01:18:45 +00:00
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionSignatureArguments;
|
2021-05-15 22:34:15 +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-04-29 19:42:51 +00:00
|
|
|
Node* AddFunctionSignatureArgumentNode(
|
|
|
|
Node *argumentsNode,
|
|
|
|
Node *argumentNode
|
|
|
|
) {
|
2021-05-15 22:34:15 +00:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionSignatureArguments;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->functionSignatureArguments.sequence = NULL;
|
|
|
|
node->functionSignatureArguments.count = 0;
|
2021-04-20 01:18:45 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-18 22:29:54 +00:00
|
|
|
Node* MakeFunctionSignatureNode(
|
|
|
|
Node *identifierNode,
|
|
|
|
Node* typeNode,
|
2021-04-22 07:35:42 +00:00
|
|
|
Node* arguments,
|
|
|
|
Node* modifiersNode
|
2021-04-18 22:29:54 +00:00
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionSignature;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->functionSignature.identifier = identifierNode;
|
|
|
|
node->functionSignature.type = typeNode;
|
|
|
|
node->functionSignature.arguments = arguments;
|
|
|
|
node->functionSignature.modifiers = modifiersNode;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeFunctionDeclarationNode(
|
|
|
|
Node* functionSignatureNode,
|
|
|
|
Node* functionBodyNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionDeclaration;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->functionDeclaration.functionSignature = functionSignatureNode;
|
|
|
|
node->functionDeclaration.functionBody = functionBodyNode;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeStructDeclarationNode(
|
|
|
|
Node *identifierNode,
|
|
|
|
Node *declarationSequenceNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = StructDeclaration;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->structDeclaration.identifier = identifierNode;
|
|
|
|
node->structDeclaration.declarationSequence = declarationSequenceNode;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:42:51 +00:00
|
|
|
Node* StartDeclarationSequenceNode(
|
|
|
|
Node *declarationNode
|
2021-04-18 22:29:54 +00:00
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = DeclarationSequence;
|
2021-05-15 22:34:15 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* AddDeclarationNode(
|
|
|
|
Node *declarationSequenceNode,
|
|
|
|
Node *declarationNode
|
|
|
|
) {
|
2021-05-15 22:34:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* StartFunctionArgumentSequenceNode(
|
|
|
|
Node *argumentNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionArgumentSequence;
|
2021-05-15 22:34:15 +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-04-29 19:42:51 +00:00
|
|
|
Node* AddFunctionArgumentNode(
|
|
|
|
Node *argumentSequenceNode,
|
|
|
|
Node *argumentNode
|
2021-04-20 17:47:40 +00:00
|
|
|
) {
|
2021-05-15 22:34:15 +00:00
|
|
|
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-04-20 17:47:40 +00:00
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionArgumentSequence;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->functionArgumentSequence.count = 0;
|
|
|
|
node->functionArgumentSequence.sequence = NULL;
|
2021-04-20 17:47:40 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeFunctionCallExpressionNode(
|
|
|
|
Node *identifierNode,
|
|
|
|
Node *argumentSequenceNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionCallExpression;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->functionCallExpression.identifier = identifierNode;
|
|
|
|
node->functionCallExpression.argumentSequence = argumentSequenceNode;
|
2021-04-20 17:47:40 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-22 04:29:38 +00:00
|
|
|
Node* MakeAccessExpressionNode(
|
|
|
|
Node *accessee,
|
|
|
|
Node *accessor
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = AccessExpression;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->accessExpression.accessee = accessee;
|
|
|
|
node->accessExpression.accessor = accessor;
|
2021-04-22 04:29:38 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-28 19:49:45 +00:00
|
|
|
Node* MakeAllocNode(Node *typeNode)
|
|
|
|
{
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = AllocExpression;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->allocExpression.type = typeNode;
|
2021-04-28 19:49:45 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-29 04:25:25 +00:00
|
|
|
Node* MakeIfNode(
|
|
|
|
Node *expressionNode,
|
|
|
|
Node *statementSequenceNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = IfStatement;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->ifStatement.expression = expressionNode;
|
|
|
|
node->ifStatement.statementSequence = statementSequenceNode;
|
2021-04-29 04:25:25 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:42:51 +00:00
|
|
|
Node* MakeIfElseNode(
|
|
|
|
Node *ifNode,
|
2021-05-15 22:34:15 +00:00
|
|
|
Node *elseNode
|
2021-04-29 19:42:51 +00:00
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = IfElseStatement;
|
2021-05-15 22:34:15 +00:00
|
|
|
node->ifElseStatement.ifStatement = ifNode;
|
|
|
|
node->ifElseStatement.elseStatement = elseNode;
|
2021-04-29 19:42:51 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-30 06:49:35 +00:00
|
|
|
Node* MakeForLoopNode(
|
2021-05-15 22:34:15 +00:00
|
|
|
Node *declarationNode,
|
2021-04-30 06:49:35 +00:00
|
|
|
Node *startNumberNode,
|
|
|
|
Node *endNumberNode,
|
|
|
|
Node *statementSequenceNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = ForLoop;
|
2021-05-15 22:34:15 +00:00
|
|
|
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-04-18 22:29:54 +00:00
|
|
|
static const char* PrimitiveTypeToString(PrimitiveType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Int: return "Int";
|
|
|
|
case UInt: return "UInt";
|
|
|
|
case Bool: return "Bool";
|
2021-04-21 02:00:18 +00:00
|
|
|
case Void: return "Void";
|
2021-04-18 22:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:34:15 +00:00
|
|
|
static void PrintUnaryOperator(UnaryOperator operator)
|
2021-04-18 22:29:54 +00:00
|
|
|
{
|
2021-05-15 22:34:15 +00:00
|
|
|
switch (operator)
|
|
|
|
{
|
|
|
|
case Negate:
|
|
|
|
printf("!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintBinaryOperator(BinaryOperator operator)
|
|
|
|
{
|
|
|
|
switch (operator)
|
2021-04-18 22:29:54 +00:00
|
|
|
{
|
|
|
|
case Add:
|
|
|
|
printf("+");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Subtract:
|
|
|
|
printf("-");
|
|
|
|
break;
|
2021-04-20 17:47:40 +00:00
|
|
|
|
|
|
|
case Multiply:
|
|
|
|
printf("*");
|
|
|
|
break;
|
2021-04-18 22:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:34:15 +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-15 22:34:15 +00:00
|
|
|
case AccessExpression:
|
|
|
|
PrintNode(node->accessExpression.accessee, tabCount + 1);
|
|
|
|
PrintNode(node->accessExpression.accessor, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AllocExpression:
|
|
|
|
PrintNode(node->allocExpression.type, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Assignment:
|
|
|
|
PrintNode(node->assignmentStatement.left, tabCount + 1);
|
|
|
|
PrintNode(node->assignmentStatement.right, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
2021-04-18 22:29:54 +00:00
|
|
|
case BinaryExpression:
|
2021-05-15 22:34:15 +00:00
|
|
|
PrintNode(node->binaryExpression.left, tabCount + 1);
|
|
|
|
PrintBinaryOperator(node->binaryExpression.operator);
|
|
|
|
PrintNode(node->binaryExpression.right, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CustomTypeNode:
|
|
|
|
printf("%s", node->customType.name);
|
2021-04-18 22:29:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Declaration:
|
2021-05-15 22:34:15 +00:00
|
|
|
PrintNode(node->declaration.identifier, tabCount + 1);
|
|
|
|
PrintNode(node->declaration.type, tabCount + 1);
|
2021-04-18 22:29:54 +00:00
|
|
|
break;
|
|
|
|
|
2021-05-15 22:34:15 +00:00
|
|
|
case DeclarationSequence:
|
|
|
|
for (i = 0; i < node->declarationSequence.count; i += 1)
|
|
|
|
{
|
|
|
|
PrintNode(node->declarationSequence.sequence[i], tabCount + 1);
|
|
|
|
}
|
2021-04-24 19:59:30 +00:00
|
|
|
break;
|
|
|
|
|
2021-05-15 22:34:15 +00:00
|
|
|
case ForLoop:
|
|
|
|
PrintNode(node->forLoop.declaration, tabCount + 1);
|
|
|
|
PrintNode(node->forLoop.startNumber, tabCount + 1);
|
|
|
|
PrintNode(node->forLoop.endNumber, tabCount + 1);
|
|
|
|
PrintNode(node->forLoop.statementSequence, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FunctionArgumentSequence:
|
|
|
|
for (i = 0; i < node->functionArgumentSequence.count; i += 1)
|
|
|
|
{
|
|
|
|
PrintNode(node->functionArgumentSequence.sequence[i], tabCount + 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FunctionCallExpression:
|
|
|
|
PrintNode(node->functionCallExpression.identifier, tabCount + 1);
|
|
|
|
PrintNode(node->functionCallExpression.argumentSequence, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FunctionDeclaration:
|
|
|
|
PrintNode(node->functionDeclaration.functionSignature, tabCount + 1);
|
|
|
|
PrintNode(node->functionDeclaration.functionBody, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FunctionModifiers:
|
|
|
|
for (i = 0; i < node->functionModifiers.count; i += 1)
|
|
|
|
{
|
|
|
|
PrintNode(node->functionModifiers.sequence[i], tabCount + 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FunctionSignature:
|
|
|
|
PrintNode(node->functionSignature.identifier, tabCount + 1);
|
|
|
|
PrintNode(node->functionSignature.arguments, tabCount + 1);
|
|
|
|
PrintNode(node->functionSignature.type, tabCount + 1);
|
|
|
|
PrintNode(node->functionSignature.modifiers, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FunctionSignatureArguments:
|
|
|
|
for (i = 0; i < node->functionSignatureArguments.count; i += 1)
|
|
|
|
{
|
|
|
|
PrintNode(node->functionSignatureArguments.sequence[i], tabCount + 1);
|
|
|
|
}
|
2021-04-18 22:29:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Identifier:
|
2021-05-13 04:54:09 +00:00
|
|
|
if (node->typeTag == NULL) {
|
2021-05-15 22:34:15 +00:00
|
|
|
printf("%s", node->identifier.name);
|
2021-05-13 04:54:09 +00:00
|
|
|
} else {
|
|
|
|
char *type = TypeTagToString(node->typeTag);
|
2021-05-15 22:34:15 +00:00
|
|
|
printf("%s<%s>", node->identifier.name, type);
|
2021-05-13 04:54:09 +00:00
|
|
|
}
|
2021-04-18 22:29:54 +00:00
|
|
|
break;
|
|
|
|
|
2021-05-15 22:34:15 +00:00
|
|
|
case IfStatement:
|
|
|
|
PrintNode(node->ifStatement.expression, tabCount + 1);
|
|
|
|
PrintNode(node->ifStatement.statementSequence, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IfElseStatement:
|
|
|
|
PrintNode(node->ifElseStatement.ifStatement, tabCount + 1);
|
|
|
|
PrintNode(node->ifElseStatement.elseStatement, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
2021-04-18 22:29:54 +00:00
|
|
|
case Number:
|
2021-05-15 22:34:15 +00:00
|
|
|
printf("%lu", node->number.value);
|
2021-04-18 22:29:54 +00:00
|
|
|
break;
|
|
|
|
|
2021-05-15 22:34:15 +00:00
|
|
|
case PrimitiveTypeNode:
|
|
|
|
printf("%s", PrimitiveTypeToString(node->primitiveType.type));
|
|
|
|
break;
|
2021-04-18 22:29:54 +00:00
|
|
|
|
2021-05-15 22:34:15 +00:00
|
|
|
case ReferenceTypeNode:
|
|
|
|
PrintNode(node->referenceType.type, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Return:
|
|
|
|
PrintNode(node->returnStatement.expression, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ReturnVoid:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StatementSequence:
|
|
|
|
for (i = 0; i < node->statementSequence.count; i += 1)
|
|
|
|
{
|
|
|
|
PrintNode(node->statementSequence.sequence[i], tabCount + 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StaticModifier:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StringLiteral:
|
|
|
|
printf("%s", node->stringLiteral.string);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StructDeclaration:
|
|
|
|
PrintNode(node->structDeclaration.identifier, tabCount + 1);
|
|
|
|
PrintNode(node->structDeclaration.declarationSequence, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type:
|
|
|
|
PrintNode(node->type.typeNode, tabCount + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UnaryExpression:
|
|
|
|
PrintUnaryOperator(node->unaryExpression.operator);
|
|
|
|
PrintNode(node->unaryExpression.child, tabCount + 1);
|
|
|
|
break;
|
2021-04-18 22:29:54 +00:00
|
|
|
}
|
2021-05-15 22:34:15 +00:00
|
|
|
|
|
|
|
printf("\n");
|
2021-04-18 22:29:54 +00:00
|
|
|
}
|
2021-05-08 00:49:35 +00:00
|
|
|
|
|
|
|
TypeTag* MakeTypeTag(Node *node) {
|
|
|
|
if (node == NULL) {
|
|
|
|
fprintf(stderr, "wraith: Attempted to call MakeTypeTag on null value.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeTag *tag = (TypeTag*)malloc(sizeof(TypeTag));
|
|
|
|
switch (node->syntaxKind) {
|
|
|
|
case Type:
|
2021-05-15 22:34:15 +00:00
|
|
|
tag = MakeTypeTag(node->type.typeNode);
|
2021-05-08 00:49:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PrimitiveTypeNode:
|
|
|
|
tag->type = Primitive;
|
2021-05-15 22:34:15 +00:00
|
|
|
tag->value.primitiveType = node->primitiveType.type;
|
2021-05-08 00:49:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ReferenceTypeNode:
|
|
|
|
tag->type = Reference;
|
2021-05-15 22:34:15 +00:00
|
|
|
tag->value.referenceType = MakeTypeTag(node->referenceType.type);
|
2021-05-08 00:49:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CustomTypeNode:
|
|
|
|
tag->type = Custom;
|
2021-05-15 22:34:15 +00:00
|
|
|
tag->value.customType = strdup(node->customType.name);
|
2021-05-08 00:49:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Declaration:
|
2021-05-15 22:34:15 +00:00
|
|
|
tag = MakeTypeTag(node->declaration.type);
|
2021-05-08 00:49:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case StructDeclaration:
|
|
|
|
tag->type = Custom;
|
2021-05-15 22:34:15 +00:00
|
|
|
tag->value.customType = strdup(node->structDeclaration.identifier->identifier.name);
|
2021-05-13 04:54:09 +00:00
|
|
|
printf("Struct tag: %s\n", TypeTagToString(tag));
|
2021-05-08 00:49:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FunctionDeclaration:
|
2021-05-15 22:34:15 +00:00
|
|
|
tag = MakeTypeTag(node->functionDeclaration.functionSignature->functionSignature.type);
|
2021-05-08 00:49:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-05-15 22:34:15 +00:00
|
|
|
fprintf(stderr,
|
2021-05-08 00:49:35 +00:00
|
|
|
"wraith: Attempted to call MakeTypeTag on"
|
|
|
|
" node with unsupported SyntaxKind: %s\n",
|
|
|
|
SyntaxKindString(node->syntaxKind));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* TypeTagToString(TypeTag *tag) {
|
|
|
|
if (tag == NULL) {
|
|
|
|
fprintf(stderr, "wraith: Attempted to call TypeTagToString with null value\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
return tag->value.customType;
|
|
|
|
}
|
2021-05-15 22:34:15 +00:00
|
|
|
}
|