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 Expression: return "Expression";
|
|
|
|
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
|
|
|
|
) {
|
|
|
|
return typeNode->children[0]->syntaxKind == PrimitiveTypeNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
node->primitiveType = type;
|
2021-04-18 22:29:54 +00:00
|
|
|
node->childCount = 0;
|
|
|
|
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;
|
|
|
|
node->value.string = strdup(name);
|
|
|
|
node->childCount = 0;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeReferenceTypeNode(
|
|
|
|
Node *typeNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = ReferenceTypeNode;
|
|
|
|
node->childCount = 1;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*));
|
|
|
|
node->children[0] = typeNode;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeTypeNode(
|
|
|
|
Node* typeNode
|
2021-04-21 06:51:26 +00:00
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Type;
|
|
|
|
node->childCount = 1;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*));
|
2021-04-24 19:59:30 +00:00
|
|
|
node->children[0] = 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;
|
|
|
|
node->value.string = strdup(id);
|
|
|
|
node->childCount = 0;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeNumberNode(
|
|
|
|
const char *numberString
|
|
|
|
) {
|
|
|
|
char *ptr;
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Number;
|
|
|
|
node->value.number = strtoul(numberString, &ptr, 10);
|
|
|
|
node->childCount = 0;
|
|
|
|
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-04-30 19:17:44 +00:00
|
|
|
node->value.string = strndup(string + 1, slen - 2);
|
2021-04-18 22:29:54 +00:00
|
|
|
node->childCount = 0;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-22 07:35:42 +00:00
|
|
|
Node* MakeStaticNode()
|
|
|
|
{
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = StaticModifier;
|
|
|
|
node->childCount = 0;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeFunctionModifiersNode(
|
|
|
|
Node **pModifierNodes,
|
|
|
|
uint32_t modifierCount
|
|
|
|
) {
|
|
|
|
uint32_t i;
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionModifiers;
|
|
|
|
node->childCount = modifierCount;
|
|
|
|
if (modifierCount > 0)
|
|
|
|
{
|
|
|
|
node->children = malloc(sizeof(Node*) * node->childCount);
|
|
|
|
for (i = 0; i < modifierCount; i += 1)
|
|
|
|
{
|
|
|
|
node->children[i] = pModifierNodes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
node->operator.unaryOperator = operator;
|
|
|
|
node->children = malloc(sizeof(Node*));
|
|
|
|
node->children[0] = child;
|
|
|
|
node->childCount = 1;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeBinaryNode(
|
|
|
|
BinaryOperator operator,
|
|
|
|
Node *left,
|
|
|
|
Node *right
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = BinaryExpression;
|
|
|
|
node->operator.binaryOperator = operator;
|
|
|
|
node->children = malloc(sizeof(Node*) * 2);
|
|
|
|
node->children[0] = left;
|
|
|
|
node->children[1] = right;
|
|
|
|
node->childCount = 2;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeDeclarationNode(
|
|
|
|
Node* typeNode,
|
|
|
|
Node* identifierNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Declaration;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * 2);
|
|
|
|
node->childCount = 2;
|
|
|
|
node->children[0] = typeNode;
|
|
|
|
node->children[1] = identifierNode;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeAssignmentNode(
|
|
|
|
Node *left,
|
|
|
|
Node *right
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Assignment;
|
|
|
|
node->childCount = 2;
|
|
|
|
node->children = malloc(sizeof(Node*) * 2);
|
|
|
|
node->children[0] = left;
|
|
|
|
node->children[1] = right;
|
|
|
|
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-04-29 19:42:51 +00:00
|
|
|
node->children = (Node**) malloc(sizeof(Node*));
|
|
|
|
node->childCount = 1;
|
|
|
|
node->children[0] = statementNode;
|
2021-04-18 22:29:54 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:42:51 +00:00
|
|
|
Node* AddStatement(
|
|
|
|
Node* statementSequenceNode,
|
|
|
|
Node *statementNode
|
|
|
|
) {
|
|
|
|
statementSequenceNode->children = realloc(statementSequenceNode->children, sizeof(Node*) * (statementSequenceNode->childCount + 1));
|
|
|
|
statementSequenceNode->children[statementSequenceNode->childCount] = statementNode;
|
|
|
|
statementSequenceNode->childCount += 1;
|
|
|
|
return statementSequenceNode;
|
|
|
|
}
|
|
|
|
|
2021-04-18 22:29:54 +00:00
|
|
|
Node* MakeReturnStatementNode(
|
|
|
|
Node *expressionNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Return;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*));
|
|
|
|
node->childCount = 1;
|
|
|
|
node->children[0] = expressionNode;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-21 02:00:18 +00:00
|
|
|
Node* MakeReturnVoidStatementNode()
|
|
|
|
{
|
|
|
|
Node *node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = ReturnVoid;
|
|
|
|
node->childCount = 0;
|
|
|
|
node->children = NULL;
|
|
|
|
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-04-29 19:42:51 +00:00
|
|
|
node->childCount = 1;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*));
|
|
|
|
node->children[0] = argumentNode;
|
|
|
|
return node;
|
|
|
|
}
|
2021-04-20 01:18:45 +00:00
|
|
|
|
2021-04-29 19:42:51 +00:00
|
|
|
Node* AddFunctionSignatureArgumentNode(
|
|
|
|
Node *argumentsNode,
|
|
|
|
Node *argumentNode
|
|
|
|
) {
|
|
|
|
argumentsNode->children = realloc(argumentsNode->children, sizeof(Node*) * (argumentsNode->childCount + 1));
|
|
|
|
argumentsNode->children[argumentsNode->childCount] = argumentNode;
|
|
|
|
argumentsNode->childCount += 1;
|
|
|
|
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;
|
|
|
|
node->childCount = 0;
|
|
|
|
node->children = NULL;
|
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-04-22 07:35:42 +00:00
|
|
|
node->childCount = 4;
|
2021-04-18 22:29:54 +00:00
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * (node->childCount));
|
|
|
|
node->children[0] = identifierNode;
|
|
|
|
node->children[1] = typeNode;
|
|
|
|
node->children[2] = arguments;
|
2021-04-22 07:35:42 +00:00
|
|
|
node->children[3] = 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;
|
|
|
|
node->childCount = 2;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * 2);
|
|
|
|
node->children[0] = functionSignatureNode;
|
|
|
|
node->children[1] = functionBodyNode;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeStructDeclarationNode(
|
|
|
|
Node *identifierNode,
|
|
|
|
Node *declarationSequenceNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = StructDeclaration;
|
|
|
|
node->childCount = 2;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * 2);
|
|
|
|
node->children[0] = identifierNode;
|
|
|
|
node->children[1] = declarationSequenceNode;
|
|
|
|
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-04-29 19:42:51 +00:00
|
|
|
node->children = (Node**) malloc(sizeof(Node*));
|
|
|
|
node->childCount = 1;
|
|
|
|
node->children[0] = declarationNode;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* AddDeclarationNode(
|
|
|
|
Node *declarationSequenceNode,
|
|
|
|
Node *declarationNode
|
|
|
|
) {
|
|
|
|
declarationSequenceNode->children = realloc(declarationSequenceNode->children, sizeof(Node*) * (declarationSequenceNode->childCount + 1));
|
|
|
|
declarationSequenceNode->children[declarationSequenceNode->childCount] = declarationNode;
|
|
|
|
declarationSequenceNode->childCount += 1;
|
|
|
|
return declarationSequenceNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* StartFunctionArgumentSequenceNode(
|
|
|
|
Node *argumentNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionArgumentSequence;
|
|
|
|
node->childCount = 1;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*));
|
|
|
|
node->children[0] = argumentNode;
|
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-04-29 19:42:51 +00:00
|
|
|
argumentSequenceNode->children = realloc(argumentSequenceNode->children, sizeof(Node*) * (argumentSequenceNode->childCount + 1));
|
|
|
|
argumentSequenceNode->children[argumentSequenceNode->childCount] = argumentNode;
|
|
|
|
argumentSequenceNode->childCount += 1;
|
|
|
|
return argumentSequenceNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *MakeEmptyFunctionArgumentSequenceNode()
|
|
|
|
{
|
2021-04-20 17:47:40 +00:00
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionArgumentSequence;
|
2021-04-29 19:42:51 +00:00
|
|
|
node->childCount = 0;
|
|
|
|
node->children = 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;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * 2);
|
|
|
|
node->childCount = 2;
|
|
|
|
node->children[0] = identifierNode;
|
|
|
|
node->children[1] = argumentSequenceNode;
|
|
|
|
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;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * 2);
|
|
|
|
node->childCount = 2;
|
|
|
|
node->children[0] = accessee;
|
|
|
|
node->children[1] = accessor;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-28 19:49:45 +00:00
|
|
|
Node* MakeAllocNode(Node *typeNode)
|
|
|
|
{
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = AllocExpression;
|
|
|
|
node->childCount = 1;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*));
|
|
|
|
node->children[0] = typeNode;
|
|
|
|
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;
|
|
|
|
node->childCount = 2;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*));
|
|
|
|
node->children[0] = expressionNode;
|
|
|
|
node->children[1] = statementSequenceNode;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:42:51 +00:00
|
|
|
Node* MakeIfElseNode(
|
|
|
|
Node *ifNode,
|
|
|
|
Node *statementSequenceNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = IfElseStatement;
|
|
|
|
node->childCount = 2;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*));
|
|
|
|
node->children[0] = ifNode;
|
|
|
|
node->children[1] = statementSequenceNode;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-30 06:49:35 +00:00
|
|
|
Node* MakeForLoopNode(
|
|
|
|
Node *identifierNode,
|
|
|
|
Node *startNumberNode,
|
|
|
|
Node *endNumberNode,
|
|
|
|
Node *statementSequenceNode
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = ForLoop;
|
|
|
|
node->childCount = 4;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * 4);
|
|
|
|
node->children[0] = identifierNode;
|
|
|
|
node->children[1] = startNumberNode;
|
|
|
|
node->children[2] = endNumberNode;
|
|
|
|
node->children[3] = statementSequenceNode;
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintBinaryOperator(BinaryOperator expression)
|
|
|
|
{
|
|
|
|
switch (expression)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintNode(Node *node, int tabCount)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < tabCount; i += 1)
|
|
|
|
{
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s: ", SyntaxKindString(node->syntaxKind));
|
|
|
|
switch (node->syntaxKind)
|
|
|
|
{
|
|
|
|
case BinaryExpression:
|
|
|
|
PrintBinaryOperator(node->operator.binaryOperator);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Declaration:
|
|
|
|
break;
|
|
|
|
|
2021-04-24 19:59:30 +00:00
|
|
|
case CustomTypeNode:
|
|
|
|
printf("%s", node->value.string);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PrimitiveTypeNode:
|
|
|
|
printf("%s", PrimitiveTypeToString(node->primitiveType));
|
2021-04-18 22:29:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Identifier:
|
|
|
|
printf("%s", node->value.string);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Number:
|
|
|
|
printf("%lu", node->value.number);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintTree(Node *node, uint32_t tabCount)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
PrintNode(node, tabCount);
|
|
|
|
for (i = 0; i < node->childCount; i += 1)
|
|
|
|
{
|
|
|
|
PrintTree(node->children[i], tabCount + 1);
|
|
|
|
}
|
|
|
|
}
|