forked from cosmonaut/wraith-lang
246 lines
4.7 KiB
C
246 lines
4.7 KiB
C
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
typedef enum
|
|
{
|
|
Assignment,
|
|
BinaryExpression,
|
|
Comment,
|
|
Declaration,
|
|
Expression,
|
|
ForLoop,
|
|
Identifier,
|
|
Number,
|
|
Return,
|
|
StringLiteral,
|
|
UnaryExpression
|
|
} SyntaxKind;
|
|
|
|
typedef enum
|
|
{
|
|
Negate
|
|
} UnaryOperator;
|
|
|
|
typedef enum
|
|
{
|
|
Add,
|
|
Subtract
|
|
} BinaryOperator;
|
|
|
|
typedef enum
|
|
{
|
|
Bool,
|
|
Int,
|
|
UInt,
|
|
Float,
|
|
Double,
|
|
String
|
|
} PrimitiveType;
|
|
|
|
typedef union
|
|
{
|
|
UnaryOperator unaryOperator;
|
|
BinaryOperator binaryOperator;
|
|
} Operator;
|
|
|
|
typedef struct Node
|
|
{
|
|
SyntaxKind syntaxKind;
|
|
struct Node **children;
|
|
uint32_t childCount;
|
|
union
|
|
{
|
|
UnaryOperator unaryOperator;
|
|
BinaryOperator binaryOperator;
|
|
} operator;
|
|
union
|
|
{
|
|
char *string;
|
|
uint64_t number;
|
|
} value;
|
|
PrimitiveType type;
|
|
} Node;
|
|
|
|
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;
|
|
}
|
|
|
|
const char* SyntaxKindString(SyntaxKind syntaxKind)
|
|
{
|
|
switch(syntaxKind)
|
|
{
|
|
case Assignment: return "Assignment";
|
|
case BinaryExpression: return "BinaryExpression";
|
|
case Declaration: return "Declaration";
|
|
case Identifier: return "Identifier";
|
|
case Number: return "Number";
|
|
case StringLiteral: return "StringLiteral";
|
|
case UnaryExpression: return "UnaryExpression";
|
|
default: return "Unknown";
|
|
}
|
|
}
|
|
|
|
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
|
|
) {
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
node->syntaxKind = StringLiteral;
|
|
node->value.string = strdup(string);
|
|
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;
|
|
}
|
|
|
|
Node* MakeDeclarationNode(
|
|
PrimitiveType type,
|
|
const char *id
|
|
) {
|
|
Node* node = (Node*) malloc(sizeof(Node));
|
|
node->syntaxKind = Declaration;
|
|
node->type = type;
|
|
node->value.string = strdup(id);
|
|
node->childCount = 0;
|
|
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;
|
|
}
|
|
|
|
static const char* PrimitiveTypeToString(PrimitiveType type)
|
|
{
|
|
switch (type)
|
|
{
|
|
case Int: return "Int";
|
|
case UInt: return "UInt";
|
|
case Bool: return "Bool";
|
|
}
|
|
|
|
return "Unknown";
|
|
}
|
|
|
|
static void PrintBinaryOperator(BinaryOperator expression)
|
|
{
|
|
switch (expression)
|
|
{
|
|
case Add:
|
|
printf("+");
|
|
break;
|
|
|
|
case Subtract:
|
|
printf("-");
|
|
break;
|
|
}
|
|
|
|
printf("\n");
|
|
}
|
|
|
|
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:
|
|
printf("%s %s", PrimitiveTypeToString(node->type), node->value.string);
|
|
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);
|
|
}
|
|
}
|