2021-04-18 20:14:50 +00:00
|
|
|
#ifndef WRAITH_AST_H
|
|
|
|
#define WRAITH_AST_H
|
|
|
|
|
2021-04-16 07:35:35 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2021-04-16 21:40:28 +00:00
|
|
|
#include <string.h>
|
2021-04-16 07:35:35 +00:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
Assignment,
|
|
|
|
BinaryExpression,
|
|
|
|
Comment,
|
2021-04-16 21:40:28 +00:00
|
|
|
Declaration,
|
2021-04-18 20:14:50 +00:00
|
|
|
DeclarationSequence,
|
2021-04-16 07:35:35 +00:00
|
|
|
Expression,
|
|
|
|
ForLoop,
|
2021-04-18 20:14:50 +00:00
|
|
|
FunctionDeclaration,
|
|
|
|
FunctionSignature,
|
2021-04-16 07:35:35 +00:00
|
|
|
Identifier,
|
|
|
|
Number,
|
|
|
|
Return,
|
2021-04-18 20:14:50 +00:00
|
|
|
StatementSequence,
|
2021-04-16 21:40:28 +00:00
|
|
|
StringLiteral,
|
2021-04-18 20:14:50 +00:00
|
|
|
StructDeclaration,
|
|
|
|
Type,
|
2021-04-16 07:35:35 +00:00
|
|
|
UnaryExpression
|
|
|
|
} SyntaxKind;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
Negate
|
|
|
|
} UnaryOperator;
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
Add,
|
|
|
|
Subtract
|
|
|
|
} BinaryOperator;
|
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
Bool,
|
|
|
|
Int,
|
|
|
|
UInt,
|
|
|
|
Float,
|
|
|
|
Double,
|
|
|
|
String
|
|
|
|
} PrimitiveType;
|
|
|
|
|
2021-04-16 07:35:35 +00:00
|
|
|
typedef union
|
|
|
|
{
|
|
|
|
UnaryOperator unaryOperator;
|
|
|
|
BinaryOperator binaryOperator;
|
|
|
|
} Operator;
|
|
|
|
|
|
|
|
typedef struct Node
|
|
|
|
{
|
|
|
|
SyntaxKind syntaxKind;
|
|
|
|
struct Node **children;
|
|
|
|
uint32_t childCount;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
UnaryOperator unaryOperator;
|
|
|
|
BinaryOperator binaryOperator;
|
|
|
|
} operator;
|
2021-04-16 21:40:28 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
char *string;
|
|
|
|
uint64_t number;
|
|
|
|
} value;
|
|
|
|
PrimitiveType type;
|
2021-04-16 07:35:35 +00:00
|
|
|
} Node;
|
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
char* strdup (const char* s)
|
|
|
|
{
|
|
|
|
size_t slen = strlen(s);
|
|
|
|
char* result = malloc(slen + 1);
|
|
|
|
if(result == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(result, s, slen+1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-16 07:35:35 +00:00
|
|
|
const char* SyntaxKindString(SyntaxKind syntaxKind)
|
|
|
|
{
|
|
|
|
switch(syntaxKind)
|
|
|
|
{
|
|
|
|
case Assignment: return "Assignment";
|
|
|
|
case BinaryExpression: return "BinaryExpression";
|
2021-04-18 20:14:50 +00:00
|
|
|
case Comment: return "Comment";
|
2021-04-16 21:40:28 +00:00
|
|
|
case Declaration: return "Declaration";
|
2021-04-18 20:14:50 +00:00
|
|
|
case DeclarationSequence: return "DeclarationSequence";
|
|
|
|
case FunctionDeclaration: return "FunctionDeclaration";
|
|
|
|
case FunctionSignature: return "FunctionSignature";
|
2021-04-16 07:35:35 +00:00
|
|
|
case Identifier: return "Identifier";
|
|
|
|
case Number: return "Number";
|
2021-04-18 20:14:50 +00:00
|
|
|
case Return: return "Return";
|
|
|
|
case StatementSequence: return "StatementSequence";
|
2021-04-16 21:40:28 +00:00
|
|
|
case StringLiteral: return "StringLiteral";
|
2021-04-18 20:14:50 +00:00
|
|
|
case StructDeclaration: return "StructDeclaration";
|
|
|
|
case Type: return "Type";
|
2021-04-16 07:35:35 +00:00
|
|
|
case UnaryExpression: return "UnaryExpression";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 20:14:50 +00:00
|
|
|
Node* MakeTypeNode(
|
|
|
|
PrimitiveType type
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Type;
|
|
|
|
node->type = type;
|
|
|
|
node->childCount = 0;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-16 07:35:35 +00:00
|
|
|
Node* MakeIdentifierNode(
|
|
|
|
const char *id
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Identifier;
|
2021-04-16 21:40:28 +00:00
|
|
|
node->value.string = strdup(id);
|
2021-04-16 07:35:35 +00:00
|
|
|
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
|
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
2021-04-16 21:40:28 +00:00
|
|
|
node->syntaxKind = StringLiteral;
|
|
|
|
node->value.string = strdup(string);
|
2021-04-16 07:35:35 +00:00
|
|
|
node->childCount = 0;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
Node* MakeDeclarationNode(
|
2021-04-18 20:14:50 +00:00
|
|
|
Node* typeNode,
|
|
|
|
Node* identifierNode
|
2021-04-16 21:40:28 +00:00
|
|
|
) {
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = Declaration;
|
2021-04-18 20:14:50 +00:00
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * 2);
|
|
|
|
node->childCount = 2;
|
|
|
|
node->children[0] = typeNode;
|
|
|
|
node->children[1] = identifierNode;
|
2021-04-16 21:40:28 +00:00
|
|
|
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-18 20:14:50 +00:00
|
|
|
Node* MakeStatementSequenceNode(
|
|
|
|
Node** pNodes,
|
|
|
|
uint32_t nodeCount
|
|
|
|
) {
|
|
|
|
int32_t i;
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = StatementSequence;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * nodeCount);
|
|
|
|
node->childCount = nodeCount;
|
|
|
|
for (i = nodeCount - 1; i >= 0; i -= 1)
|
|
|
|
{
|
|
|
|
node->children[nodeCount - 1 - i] = pNodes[i];
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeFunctionSignatureNode(
|
|
|
|
Node *identifierNode,
|
|
|
|
Node* typeNode,
|
|
|
|
Node* arguments
|
|
|
|
) {
|
|
|
|
uint32_t i;
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = FunctionSignature;
|
|
|
|
node->childCount = 3;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * (node->childCount));
|
|
|
|
node->children[0] = identifierNode;
|
|
|
|
node->children[1] = typeNode;
|
|
|
|
node->children[2] = arguments;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MakeDeclarationSequenceNode(
|
|
|
|
Node **pNodes,
|
|
|
|
uint32_t nodeCount
|
|
|
|
) {
|
|
|
|
int32_t i;
|
|
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
|
|
node->syntaxKind = DeclarationSequence;
|
|
|
|
node->children = (Node**) malloc(sizeof(Node*) * nodeCount);
|
|
|
|
node->childCount = nodeCount;
|
|
|
|
for (i = nodeCount - 1; i >= 0; i -= 1)
|
|
|
|
{
|
|
|
|
node->children[nodeCount - 1 - i] = pNodes[i];
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
static const char* PrimitiveTypeToString(PrimitiveType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Int: return "Int";
|
|
|
|
case UInt: return "UInt";
|
|
|
|
case Bool: return "Bool";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2021-04-16 07:35:35 +00:00
|
|
|
static void PrintBinaryOperator(BinaryOperator expression)
|
|
|
|
{
|
|
|
|
switch (expression)
|
|
|
|
{
|
|
|
|
case Add:
|
|
|
|
printf("+");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Subtract:
|
|
|
|
printf("-");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
static void PrintNode(Node *node, int tabCount)
|
2021-04-16 07:35:35 +00:00
|
|
|
{
|
2021-04-16 21:40:28 +00:00
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < tabCount; i += 1)
|
|
|
|
{
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s: ", SyntaxKindString(node->syntaxKind));
|
2021-04-16 07:35:35 +00:00
|
|
|
switch (node->syntaxKind)
|
|
|
|
{
|
|
|
|
case BinaryExpression:
|
|
|
|
PrintBinaryOperator(node->operator.binaryOperator);
|
|
|
|
break;
|
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
case Declaration:
|
2021-04-18 20:14:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Type:
|
|
|
|
printf("%s", PrimitiveTypeToString(node->type));
|
2021-04-16 21:40:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Identifier:
|
|
|
|
printf("%s", node->value.string);
|
|
|
|
break;
|
|
|
|
|
2021-04-16 07:35:35 +00:00
|
|
|
case Number:
|
2021-04-16 21:40:28 +00:00
|
|
|
printf("%lu", node->value.number);
|
2021-04-16 07:35:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-16 21:40:28 +00:00
|
|
|
|
|
|
|
printf("\n");
|
2021-04-16 07:35:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
void PrintTree(Node *node, uint32_t tabCount)
|
2021-04-16 07:35:35 +00:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2021-04-16 21:40:28 +00:00
|
|
|
PrintNode(node, tabCount);
|
2021-04-16 07:35:35 +00:00
|
|
|
for (i = 0; i < node->childCount; i += 1)
|
|
|
|
{
|
2021-04-16 21:40:28 +00:00
|
|
|
PrintTree(node->children[i], tabCount + 1);
|
2021-04-16 07:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-18 20:14:50 +00:00
|
|
|
|
|
|
|
#endif /* WRAITH_AST_H */
|