AST refactor #3

Merged
cosmonaut merged 3 commits from ast_refactor into main 2021-05-16 07:25:46 +00:00
7 changed files with 665 additions and 351 deletions

View File

@ -54,6 +54,8 @@ struct Program
myStruct.myInt = 4; myStruct.myInt = 4;
} }
return myStruct.myInt; Console.PrintLine("%i", myStruct.myInt);
return 0;
} }
} }

420
src/ast.c
View File

@ -16,7 +16,6 @@ const char* SyntaxKindString(SyntaxKind syntaxKind)
case Comment: return "Comment"; case Comment: return "Comment";
case CustomTypeNode: return "CustomTypeNode"; case CustomTypeNode: return "CustomTypeNode";
case Declaration: return "Declaration"; case Declaration: return "Declaration";
case Expression: return "Expression";
case ForLoop: return "ForLoop"; case ForLoop: return "ForLoop";
case DeclarationSequence: return "DeclarationSequence"; case DeclarationSequence: return "DeclarationSequence";
case FunctionArgumentSequence: return "FunctionArgumentSequence"; case FunctionArgumentSequence: return "FunctionArgumentSequence";
@ -45,7 +44,7 @@ const char* SyntaxKindString(SyntaxKind syntaxKind)
uint8_t IsPrimitiveType( uint8_t IsPrimitiveType(
Node *typeNode Node *typeNode
) { ) {
return typeNode->children[0]->syntaxKind == PrimitiveTypeNode; return typeNode->type.typeNode->syntaxKind == PrimitiveTypeNode;
} }
Node* MakePrimitiveTypeNode( Node* MakePrimitiveTypeNode(
@ -53,8 +52,7 @@ Node* MakePrimitiveTypeNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = PrimitiveTypeNode; node->syntaxKind = PrimitiveTypeNode;
node->primitiveType = type; node->primitiveType.type = type;
node->childCount = 0;
return node; return node;
} }
@ -63,8 +61,7 @@ Node* MakeCustomTypeNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = CustomTypeNode; node->syntaxKind = CustomTypeNode;
node->value.string = strdup(name); node->customType.name = strdup(name);
node->childCount = 0;
return node; return node;
} }
@ -73,9 +70,7 @@ Node* MakeReferenceTypeNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = ReferenceTypeNode; node->syntaxKind = ReferenceTypeNode;
node->childCount = 1; node->referenceType.type = typeNode;
node->children = (Node**) malloc(sizeof(Node*));
node->children[0] = typeNode;
return node; return node;
} }
@ -84,9 +79,7 @@ Node* MakeTypeNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = Type; node->syntaxKind = Type;
node->childCount = 1; node->type.typeNode = typeNode;
node->children = (Node**) malloc(sizeof(Node*));
node->children[0] = typeNode;
return node; return node;
} }
@ -95,8 +88,7 @@ Node* MakeIdentifierNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = Identifier; node->syntaxKind = Identifier;
node->value.string = strdup(id); node->identifier.name = strdup(id);
node->childCount = 0;
node->typeTag = NULL; node->typeTag = NULL;
return node; return node;
} }
@ -107,8 +99,7 @@ Node* MakeNumberNode(
char *ptr; char *ptr;
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = Number; node->syntaxKind = Number;
node->value.number = strtoul(numberString, &ptr, 10); node->number.value = strtoul(numberString, &ptr, 10);
node->childCount = 0;
return node; return node;
} }
@ -118,8 +109,7 @@ Node* MakeStringNode(
size_t slen = strlen(string); size_t slen = strlen(string);
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = StringLiteral; node->syntaxKind = StringLiteral;
node->value.string = strndup(string + 1, slen - 2); node->stringLiteral.string = strndup(string + 1, slen - 2);
node->childCount = 0;
return node; return node;
} }
@ -127,10 +117,10 @@ Node* MakeStaticNode()
{ {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = StaticModifier; node->syntaxKind = StaticModifier;
node->childCount = 0;
return node; return node;
} }
/* FIXME: this sucks */
Node* MakeFunctionModifiersNode( Node* MakeFunctionModifiersNode(
Node **pModifierNodes, Node **pModifierNodes,
uint32_t modifierCount uint32_t modifierCount
@ -138,13 +128,14 @@ Node* MakeFunctionModifiersNode(
uint32_t i; uint32_t i;
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionModifiers; node->syntaxKind = FunctionModifiers;
node->childCount = modifierCount; node->functionModifiers.count = modifierCount;
node->functionModifiers.sequence = NULL;
if (modifierCount > 0) if (modifierCount > 0)
{ {
node->children = malloc(sizeof(Node*) * node->childCount); node->functionModifiers.sequence = malloc(sizeof(Node*) * node->functionModifiers.count);
for (i = 0; i < modifierCount; i += 1) for (i = 0; i < modifierCount; i += 1)
{ {
node->children[i] = pModifierNodes[i]; node->functionModifiers.sequence[i] = pModifierNodes[i];
} }
} }
@ -157,10 +148,8 @@ Node* MakeUnaryNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = UnaryExpression; node->syntaxKind = UnaryExpression;
node->operator.unaryOperator = operator; node->unaryExpression.operator = operator;
node->children = malloc(sizeof(Node*)); node->unaryExpression.child = child;
node->children[0] = child;
node->childCount = 1;
return node; return node;
} }
@ -171,11 +160,9 @@ Node* MakeBinaryNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = BinaryExpression; node->syntaxKind = BinaryExpression;
node->operator.binaryOperator = operator; node->binaryExpression.left = left;
node->children = malloc(sizeof(Node*) * 2); node->binaryExpression.right = right;
node->children[0] = left; node->binaryExpression.operator = operator;
node->children[1] = right;
node->childCount = 2;
return node; return node;
} }
@ -185,10 +172,8 @@ Node* MakeDeclarationNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = Declaration; node->syntaxKind = Declaration;
node->children = (Node**) malloc(sizeof(Node*) * 2); node->declaration.type = typeNode;
node->childCount = 2; node->declaration.identifier = identifierNode;
node->children[0] = typeNode;
node->children[1] = identifierNode;
return node; return node;
} }
@ -198,10 +183,8 @@ Node* MakeAssignmentNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = Assignment; node->syntaxKind = Assignment;
node->childCount = 2; node->assignmentStatement.left = left;
node->children = malloc(sizeof(Node*) * 2); node->assignmentStatement.right = right;
node->children[0] = left;
node->children[1] = right;
return node; return node;
} }
@ -210,9 +193,9 @@ Node* StartStatementSequenceNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = StatementSequence; node->syntaxKind = StatementSequence;
node->children = (Node**) malloc(sizeof(Node*)); node->statementSequence.sequence = (Node**) malloc(sizeof(Node*));
node->childCount = 1; node->statementSequence.sequence[0] = statementNode;
node->children[0] = statementNode; node->statementSequence.count = 1;
return node; return node;
} }
@ -220,9 +203,9 @@ Node* AddStatement(
Node* statementSequenceNode, Node* statementSequenceNode,
Node *statementNode Node *statementNode
) { ) {
statementSequenceNode->children = realloc(statementSequenceNode->children, sizeof(Node*) * (statementSequenceNode->childCount + 1)); statementSequenceNode->statementSequence.sequence = realloc(statementSequenceNode->statementSequence.sequence, sizeof(Node*) * (statementSequenceNode->statementSequence.count + 1));
statementSequenceNode->children[statementSequenceNode->childCount] = statementNode; statementSequenceNode->statementSequence.sequence[statementSequenceNode->statementSequence.count] = statementNode;
statementSequenceNode->childCount += 1; statementSequenceNode->statementSequence.count += 1;
return statementSequenceNode; return statementSequenceNode;
} }
@ -231,9 +214,7 @@ Node* MakeReturnStatementNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = Return; node->syntaxKind = Return;
node->children = (Node**) malloc(sizeof(Node*)); node->returnStatement.expression = expressionNode;
node->childCount = 1;
node->children[0] = expressionNode;
return node; return node;
} }
@ -241,8 +222,6 @@ Node* MakeReturnVoidStatementNode()
{ {
Node *node = (Node*) malloc(sizeof(Node)); Node *node = (Node*) malloc(sizeof(Node));
node->syntaxKind = ReturnVoid; node->syntaxKind = ReturnVoid;
node->childCount = 0;
node->children = NULL;
return node; return node;
} }
@ -251,9 +230,9 @@ Node *StartFunctionSignatureArgumentsNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionSignatureArguments; node->syntaxKind = FunctionSignatureArguments;
node->childCount = 1; node->functionSignatureArguments.sequence = (Node**) malloc(sizeof(Node*));
node->children = (Node**) malloc(sizeof(Node*)); node->functionSignatureArguments.sequence[0] = argumentNode;
node->children[0] = argumentNode; node->functionSignatureArguments.count = 1;
return node; return node;
} }
@ -261,9 +240,9 @@ Node* AddFunctionSignatureArgumentNode(
Node *argumentsNode, Node *argumentsNode,
Node *argumentNode Node *argumentNode
) { ) {
argumentsNode->children = realloc(argumentsNode->children, sizeof(Node*) * (argumentsNode->childCount + 1)); argumentsNode->functionSignatureArguments.sequence = realloc(argumentsNode->functionSignatureArguments.sequence, sizeof(Node*) * (argumentsNode->functionSignatureArguments.count + 1));
argumentsNode->children[argumentsNode->childCount] = argumentNode; argumentsNode->functionSignatureArguments.sequence[argumentsNode->functionSignatureArguments.count] = argumentNode;
argumentsNode->childCount += 1; argumentsNode->functionSignatureArguments.count += 1;
return argumentsNode; return argumentsNode;
} }
@ -271,8 +250,8 @@ Node *MakeEmptyFunctionSignatureArgumentsNode()
{ {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionSignatureArguments; node->syntaxKind = FunctionSignatureArguments;
node->childCount = 0; node->functionSignatureArguments.sequence = NULL;
node->children = NULL; node->functionSignatureArguments.count = 0;
return node; return node;
} }
@ -284,12 +263,10 @@ Node* MakeFunctionSignatureNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionSignature; node->syntaxKind = FunctionSignature;
node->childCount = 4; node->functionSignature.identifier = identifierNode;
node->children = (Node**) malloc(sizeof(Node*) * (node->childCount)); node->functionSignature.type = typeNode;
node->children[0] = identifierNode; node->functionSignature.arguments = arguments;
node->children[1] = typeNode; node->functionSignature.modifiers = modifiersNode;
node->children[2] = arguments;
node->children[3] = modifiersNode;
return node; return node;
} }
@ -299,10 +276,8 @@ Node* MakeFunctionDeclarationNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionDeclaration; node->syntaxKind = FunctionDeclaration;
node->childCount = 2; node->functionDeclaration.functionSignature = functionSignatureNode;
node->children = (Node**) malloc(sizeof(Node*) * 2); node->functionDeclaration.functionBody = functionBodyNode;
node->children[0] = functionSignatureNode;
node->children[1] = functionBodyNode;
return node; return node;
} }
@ -312,10 +287,8 @@ Node* MakeStructDeclarationNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = StructDeclaration; node->syntaxKind = StructDeclaration;
node->childCount = 2; node->structDeclaration.identifier = identifierNode;
node->children = (Node**) malloc(sizeof(Node*) * 2); node->structDeclaration.declarationSequence = declarationSequenceNode;
node->children[0] = identifierNode;
node->children[1] = declarationSequenceNode;
return node; return node;
} }
@ -324,9 +297,9 @@ Node* StartDeclarationSequenceNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = DeclarationSequence; node->syntaxKind = DeclarationSequence;
node->children = (Node**) malloc(sizeof(Node*)); node->declarationSequence.sequence = (Node**) malloc(sizeof(Node*));
node->childCount = 1; node->declarationSequence.sequence[0] = declarationNode;
node->children[0] = declarationNode; node->declarationSequence.count = 1;
return node; return node;
} }
@ -334,9 +307,9 @@ Node* AddDeclarationNode(
Node *declarationSequenceNode, Node *declarationSequenceNode,
Node *declarationNode Node *declarationNode
) { ) {
declarationSequenceNode->children = realloc(declarationSequenceNode->children, sizeof(Node*) * (declarationSequenceNode->childCount + 1)); declarationSequenceNode->declarationSequence.sequence = (Node**) realloc(declarationSequenceNode->declarationSequence.sequence, sizeof(Node*) * (declarationSequenceNode->declarationSequence.count + 1));
declarationSequenceNode->children[declarationSequenceNode->childCount] = declarationNode; declarationSequenceNode->declarationSequence.sequence[declarationSequenceNode->declarationSequence.count] = declarationNode;
declarationSequenceNode->childCount += 1; declarationSequenceNode->declarationSequence.count += 1;
return declarationSequenceNode; return declarationSequenceNode;
} }
@ -345,9 +318,9 @@ Node* StartFunctionArgumentSequenceNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionArgumentSequence; node->syntaxKind = FunctionArgumentSequence;
node->childCount = 1; node->functionArgumentSequence.sequence = (Node**) malloc(sizeof(Node*));
node->children = (Node**) malloc(sizeof(Node*)); node->functionArgumentSequence.sequence[0] = argumentNode;
node->children[0] = argumentNode; node->functionArgumentSequence.count = 1;
return node; return node;
} }
@ -355,9 +328,9 @@ Node* AddFunctionArgumentNode(
Node *argumentSequenceNode, Node *argumentSequenceNode,
Node *argumentNode Node *argumentNode
) { ) {
argumentSequenceNode->children = realloc(argumentSequenceNode->children, sizeof(Node*) * (argumentSequenceNode->childCount + 1)); argumentSequenceNode->functionArgumentSequence.sequence = (Node**) realloc(argumentSequenceNode->functionArgumentSequence.sequence, sizeof(Node*) * (argumentSequenceNode->functionArgumentSequence.count + 1));
argumentSequenceNode->children[argumentSequenceNode->childCount] = argumentNode; argumentSequenceNode->functionArgumentSequence.sequence[argumentSequenceNode->functionArgumentSequence.count] = argumentNode;
argumentSequenceNode->childCount += 1; argumentSequenceNode->functionArgumentSequence.count += 1;
return argumentSequenceNode; return argumentSequenceNode;
} }
@ -365,8 +338,8 @@ Node *MakeEmptyFunctionArgumentSequenceNode()
{ {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionArgumentSequence; node->syntaxKind = FunctionArgumentSequence;
node->childCount = 0; node->functionArgumentSequence.count = 0;
node->children = NULL; node->functionArgumentSequence.sequence = NULL;
return node; return node;
} }
@ -376,10 +349,8 @@ Node* MakeFunctionCallExpressionNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionCallExpression; node->syntaxKind = FunctionCallExpression;
node->children = (Node**) malloc(sizeof(Node*) * 2); node->functionCallExpression.identifier = identifierNode;
node->childCount = 2; node->functionCallExpression.argumentSequence = argumentSequenceNode;
node->children[0] = identifierNode;
node->children[1] = argumentSequenceNode;
return node; return node;
} }
@ -389,10 +360,8 @@ Node* MakeAccessExpressionNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = AccessExpression; node->syntaxKind = AccessExpression;
node->children = (Node**) malloc(sizeof(Node*) * 2); node->accessExpression.accessee = accessee;
node->childCount = 2; node->accessExpression.accessor = accessor;
node->children[0] = accessee;
node->children[1] = accessor;
return node; return node;
} }
@ -400,9 +369,7 @@ Node* MakeAllocNode(Node *typeNode)
{ {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = AllocExpression; node->syntaxKind = AllocExpression;
node->childCount = 1; node->allocExpression.type = typeNode;
node->children = (Node**) malloc(sizeof(Node*));
node->children[0] = typeNode;
return node; return node;
} }
@ -412,40 +379,34 @@ Node* MakeIfNode(
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = IfStatement; node->syntaxKind = IfStatement;
node->childCount = 2; node->ifStatement.expression = expressionNode;
node->children = (Node**) malloc(sizeof(Node*)); node->ifStatement.statementSequence = statementSequenceNode;
node->children[0] = expressionNode;
node->children[1] = statementSequenceNode;
return node; return node;
} }
Node* MakeIfElseNode( Node* MakeIfElseNode(
Node *ifNode, Node *ifNode,
Node *statementSequenceNode Node *elseNode
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = IfElseStatement; node->syntaxKind = IfElseStatement;
node->childCount = 2; node->ifElseStatement.ifStatement = ifNode;
node->children = (Node**) malloc(sizeof(Node*)); node->ifElseStatement.elseStatement = elseNode;
node->children[0] = ifNode;
node->children[1] = statementSequenceNode;
return node; return node;
} }
Node* MakeForLoopNode( Node* MakeForLoopNode(
Node *identifierNode, Node *declarationNode,
Node *startNumberNode, Node *startNumberNode,
Node *endNumberNode, Node *endNumberNode,
Node *statementSequenceNode Node *statementSequenceNode
) { ) {
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = ForLoop; node->syntaxKind = ForLoop;
node->childCount = 4; node->forLoop.declaration = declarationNode;
node->children = (Node**) malloc(sizeof(Node*) * 4); node->forLoop.startNumber = startNumberNode;
node->children[0] = identifierNode; node->forLoop.endNumber = endNumberNode;
node->children[1] = startNumberNode; node->forLoop.statementSequence = statementSequenceNode;
node->children[2] = endNumberNode;
node->children[3] = statementSequenceNode;
return node; return node;
} }
@ -462,25 +423,35 @@ static const char* PrimitiveTypeToString(PrimitiveType type)
return "Unknown"; return "Unknown";
} }
static void PrintBinaryOperator(BinaryOperator expression) static void PrintUnaryOperator(UnaryOperator operator)
{ {
switch (expression) switch (operator)
{ {
case Add: case Negate:
printf("+"); printf("!");
break;
case Subtract:
printf("-");
break;
case Multiply:
printf("*");
break; break;
} }
} }
static void PrintNode(Node *node, int tabCount) static void PrintBinaryOperator(BinaryOperator operator)
{
switch (operator)
{
case Add:
printf("(+)");
break;
case Subtract:
printf("(-)");
break;
case Multiply:
printf("(*)");
break;
}
}
void PrintNode(Node *node, uint32_t tabCount)
{ {
uint32_t i; uint32_t i;
for (i = 0; i < tabCount; i += 1) for (i = 0; i < tabCount; i += 1)
@ -491,45 +462,173 @@ static void PrintNode(Node *node, int tabCount)
printf("%s: ", SyntaxKindString(node->syntaxKind)); printf("%s: ", SyntaxKindString(node->syntaxKind));
switch (node->syntaxKind) switch (node->syntaxKind)
{ {
case BinaryExpression: case AccessExpression:
PrintBinaryOperator(node->operator.binaryOperator); printf("\n");
break; PrintNode(node->accessExpression.accessee, tabCount + 1);
PrintNode(node->accessExpression.accessor, tabCount + 1);
return;
case Declaration: case AllocExpression:
break; 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: case CustomTypeNode:
printf("%s", node->value.string); printf("%s\n", node->customType.name);
break; return;
case PrimitiveTypeNode: case Declaration:
printf("%s", PrimitiveTypeToString(node->primitiveType)); printf("\n");
break; 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.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 Identifier: case Identifier:
if (node->typeTag == NULL) { if (node->typeTag == NULL) {
printf("%s", node->value.string); printf("%s\n", node->identifier.name);
} else { } else {
char *type = TypeTagToString(node->typeTag); char *type = TypeTagToString(node->typeTag);
printf("%s<%s>", node->value.string, type); printf("%s<%s>\n", node->identifier.name, type);
} }
break; 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: case Number:
printf("%lu", node->value.number); printf("%lu\n", node->number.value);
break; return;
}
printf("\n"); case PrimitiveTypeNode:
} printf("%s\n", PrimitiveTypeToString(node->primitiveType.type));
return;
void PrintTree(Node *node, uint32_t tabCount) case ReferenceTypeNode:
{ printf("\n");
uint32_t i; PrintNode(node->referenceType.type, tabCount + 1);
PrintNode(node, tabCount); return;
for (i = 0; i < node->childCount; i += 1)
{ case Return:
PrintTree(node->children[i], tabCount + 1); 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:
printf("%s", node->stringLiteral.string);
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;
} }
} }
@ -542,40 +641,39 @@ TypeTag* MakeTypeTag(Node *node) {
TypeTag *tag = (TypeTag*)malloc(sizeof(TypeTag)); TypeTag *tag = (TypeTag*)malloc(sizeof(TypeTag));
switch (node->syntaxKind) { switch (node->syntaxKind) {
case Type: case Type:
tag = MakeTypeTag(node->children[0]); tag = MakeTypeTag(node->type.typeNode);
break; break;
case PrimitiveTypeNode: case PrimitiveTypeNode:
tag->type = Primitive; tag->type = Primitive;
tag->value.primitiveType = node->primitiveType; tag->value.primitiveType = node->primitiveType.type;
break; break;
case ReferenceTypeNode: case ReferenceTypeNode:
tag->type = Reference; tag->type = Reference;
tag->value.referenceType = MakeTypeTag(node->children[0]); tag->value.referenceType = MakeTypeTag(node->referenceType.type);
break; break;
case CustomTypeNode: case CustomTypeNode:
tag->type = Custom; tag->type = Custom;
tag->value.customType = strdup(node->value.string); tag->value.customType = strdup(node->customType.name);
break; break;
case Declaration: case Declaration:
tag = MakeTypeTag(node->children[0]); tag = MakeTypeTag(node->declaration.type);
break; break;
case StructDeclaration: case StructDeclaration:
tag->type = Custom; tag->type = Custom;
tag->value.customType = strdup(node->children[0]->value.string); tag->value.customType = strdup(node->structDeclaration.identifier->identifier.name);
printf("Struct tag: %s\n", TypeTagToString(tag));
break; break;
case FunctionDeclaration: case FunctionDeclaration:
tag = MakeTypeTag(node->children[0]->children[1]); tag = MakeTypeTag(node->functionDeclaration.functionSignature->functionSignature.type);
break; break;
default: default:
fprintf(stderr, fprintf(stderr,
"wraith: Attempted to call MakeTypeTag on" "wraith: Attempted to call MakeTypeTag on"
" node with unsupported SyntaxKind: %s\n", " node with unsupported SyntaxKind: %s\n",
SyntaxKindString(node->syntaxKind)); SyntaxKindString(node->syntaxKind));
@ -605,4 +703,4 @@ char* TypeTagToString(TypeTag *tag) {
case Custom: case Custom:
return tag->value.customType; return tag->value.customType;
} }
} }

201
src/ast.h
View File

@ -4,6 +4,15 @@
#include <stdint.h> #include <stdint.h>
#include "identcheck.h" #include "identcheck.h"
/* -Wpedantic nameless union/struct silencing */
#ifndef WRAITHNAMELESS
#ifdef __GNUC__
#define WRAITHNAMELESS __extension__
#else
#define WRAITHNAMELESS
#endif /* __GNUC__ */
#endif /* WRAITHNAMELESS */
typedef enum typedef enum
{ {
AccessExpression, AccessExpression,
@ -14,7 +23,6 @@ typedef enum
CustomTypeNode, CustomTypeNode,
Declaration, Declaration,
DeclarationSequence, DeclarationSequence,
Expression,
ForLoop, ForLoop,
FunctionArgumentSequence, FunctionArgumentSequence,
FunctionCallExpression, FunctionCallExpression,
@ -92,25 +100,184 @@ typedef struct TypeTag
} value; } value;
} TypeTag; } TypeTag;
typedef struct Node typedef struct Node Node;
struct Node
{ {
Node *parent;
SyntaxKind syntaxKind; SyntaxKind syntaxKind;
struct Node **children; WRAITHNAMELESS union
uint32_t childCount;
union
{ {
UnaryOperator unaryOperator; struct
BinaryOperator binaryOperator; {
} operator; Node *accessee;
union Node *accessor;
{ } accessExpression;
char *string;
uint64_t number; struct
} value; {
PrimitiveType primitiveType; Node *type;
} allocExpression;
struct
{
Node *left;
Node *right;
} assignmentStatement;
struct
{
Node *left;
Node *right;
BinaryOperator operator;
} binaryExpression;
struct
{
} comment;
struct
{
char *name;
} customType;
struct
{
Node *type;
Node *identifier;
} declaration;
struct
{
Node **sequence;
uint32_t count;
} declarationSequence;
struct
{
Node *declaration;
Node *startNumber;
Node *endNumber;
Node *statementSequence;
} forLoop;
struct
{
Node **sequence;
uint32_t count;
} functionArgumentSequence;
struct
{
Node *identifier; /* FIXME: need better name */
Node *argumentSequence;
} functionCallExpression;
struct
{
Node *functionSignature;
Node *functionBody;
} functionDeclaration;
struct
{
Node **sequence;
uint32_t count;
} functionModifiers;
struct
{
Node *identifier;
Node *type;
Node *arguments;
Node *modifiers;
} functionSignature;
struct
{
Node **sequence;
uint32_t count;
} functionSignatureArguments;
struct
{
char *name;
} identifier;
struct
{
Node *expression;
Node *statementSequence;
} ifStatement;
struct
{
Node *ifStatement;
Node *elseStatement;
} ifElseStatement;
struct
{
uint64_t value;
} number;
struct
{
PrimitiveType type;
} primitiveType;
struct
{
Node *type;
} referenceType;
struct
{
Node *expression;
} returnStatement;
struct
{
} returnVoidStatement;
struct
{
Node **sequence;
uint32_t count;
} statementSequence;
struct
{
} staticModifier; /* FIXME: modifiers should just be an enum */
struct
{
char *string;
} stringLiteral;
struct
{
Node *identifier;
Node *declarationSequence;
} structDeclaration;
struct
{
Node *typeNode;
} type; /* FIXME: this needs a refactor */
struct
{
Node *child;
UnaryOperator operator;
} unaryExpression;
};
TypeTag *typeTag; TypeTag *typeTag;
IdNode *idLink; IdNode *idLink;
} Node; };
const char* SyntaxKindString(SyntaxKind syntaxKind); const char* SyntaxKindString(SyntaxKind syntaxKind);
@ -223,7 +390,7 @@ Node* MakeIfNode(
); );
Node* MakeIfElseNode( Node* MakeIfElseNode(
Node *ifNode, Node *ifNode,
Node *statementSequenceNode Node *elseNode /* can be a conditional or a statement sequence */
); );
Node* MakeForLoopNode( Node* MakeForLoopNode(
Node *identifierNode, Node *identifierNode,
@ -232,7 +399,7 @@ Node* MakeForLoopNode(
Node *statementSequenceNode Node *statementSequenceNode
); );
void PrintTree(Node *node, uint32_t tabCount); void PrintNode(Node *node, uint32_t tabCount);
const char* SyntaxKindString(SyntaxKind syntaxKind); const char* SyntaxKindString(SyntaxKind syntaxKind);
TypeTag* MakeTypeTag(Node *node); TypeTag* MakeTypeTag(Node *node);

View File

@ -263,7 +263,7 @@ static void AddStructDeclaration(
for (i = 0; i < fieldDeclarationCount; i += 1) for (i = 0; i < fieldDeclarationCount; i += 1)
{ {
structTypeDeclarations[index].fields = realloc(structTypeDeclarations[index].fields, sizeof(StructTypeField) * (structTypeDeclarations[index].fieldCount + 1)); structTypeDeclarations[index].fields = realloc(structTypeDeclarations[index].fields, sizeof(StructTypeField) * (structTypeDeclarations[index].fieldCount + 1));
structTypeDeclarations[index].fields[i].name = strdup(fieldDeclarations[i]->children[1]->value.string); structTypeDeclarations[index].fields[i].name = strdup(fieldDeclarations[i]->declaration.identifier->identifier.name);
structTypeDeclarations[index].fields[i].index = i; structTypeDeclarations[index].fields[i].index = i;
structTypeDeclarations[index].fieldCount += 1; structTypeDeclarations[index].fieldCount += 1;
} }
@ -319,16 +319,15 @@ static LLVMTypeRef ResolveType(Node* typeNode)
{ {
if (IsPrimitiveType(typeNode)) if (IsPrimitiveType(typeNode))
{ {
return WraithTypeToLLVMType(typeNode->children[0]->primitiveType); return WraithTypeToLLVMType(typeNode->type.typeNode->primitiveType.type);
} }
else if (typeNode->children[0]->syntaxKind == CustomTypeNode) else if (typeNode->type.typeNode->syntaxKind == CustomTypeNode)
{ {
char *typeName = typeNode->children[0]->value.string; return LookupCustomType(typeNode->type.typeNode->customType.name);
return LookupCustomType(typeName);
} }
else if (typeNode->children[0]->syntaxKind == ReferenceTypeNode) else if (typeNode->type.typeNode->syntaxKind == ReferenceTypeNode)
{ {
return LLVMPointerType(ResolveType(typeNode->children[0]->children[0]), 0); return LLVMPointerType(ResolveType(typeNode->type.typeNode->referenceType.type), 0);
} }
else else
{ {
@ -443,24 +442,24 @@ static LLVMValueRef CompileExpression(
static LLVMValueRef CompileNumber( static LLVMValueRef CompileNumber(
Node *numberExpression Node *numberExpression
) { ) {
return LLVMConstInt(LLVMInt64Type(), numberExpression->value.number, 0); return LLVMConstInt(LLVMInt64Type(), numberExpression->number.value, 0);
} }
static LLVMValueRef CompileString( static LLVMValueRef CompileString(
LLVMBuilderRef builder, LLVMBuilderRef builder,
Node *stringExpression Node *stringExpression
) { ) {
return LLVMBuildGlobalStringPtr(builder, stringExpression->value.string, "stringConstant"); return LLVMBuildGlobalStringPtr(builder, stringExpression->stringLiteral.string, "stringConstant");
} }
static LLVMValueRef CompileBinaryExpression( static LLVMValueRef CompileBinaryExpression(
LLVMBuilderRef builder, LLVMBuilderRef builder,
Node *binaryExpression Node *binaryExpression
) { ) {
LLVMValueRef left = CompileExpression(builder, binaryExpression->children[0]); LLVMValueRef left = CompileExpression(builder, binaryExpression->binaryExpression.left);
LLVMValueRef right = CompileExpression(builder, binaryExpression->children[1]); LLVMValueRef right = CompileExpression(builder, binaryExpression->binaryExpression.right);
switch (binaryExpression->operator.binaryOperator) switch (binaryExpression->binaryExpression.operator)
{ {
case Add: case Add:
return LLVMBuildAdd(builder, left, right, "addResult"); return LLVMBuildAdd(builder, left, right, "addResult");
@ -494,11 +493,11 @@ static LLVMValueRef CompileBinaryExpression(
/* FIXME THIS IS ALL BROKEN */ /* FIXME THIS IS ALL BROKEN */
static LLVMValueRef CompileFunctionCallExpression( static LLVMValueRef CompileFunctionCallExpression(
LLVMBuilderRef builder, LLVMBuilderRef builder,
Node *expression Node *functionCallExpression
) { ) {
uint32_t i; uint32_t i;
uint32_t argumentCount = 0; uint32_t argumentCount = 0;
LLVMValueRef args[expression->children[1]->childCount + 1]; LLVMValueRef args[functionCallExpression->functionCallExpression.argumentSequence->functionArgumentSequence.count + 1];
LLVMValueRef function; LLVMValueRef function;
uint8_t isStatic; uint8_t isStatic;
LLVMValueRef structInstance; LLVMValueRef structInstance;
@ -506,25 +505,26 @@ static LLVMValueRef CompileFunctionCallExpression(
char *returnName = ""; char *returnName = "";
/* FIXME: this needs to be recursive on access chains */ /* FIXME: this needs to be recursive on access chains */
if (expression->children[0]->syntaxKind == AccessExpression) /* FIXME: this needs to be able to call same-struct functions implicitly */
if (functionCallExpression->functionCallExpression.identifier->syntaxKind == AccessExpression)
{ {
LLVMTypeRef typeReference = FindStructType( LLVMTypeRef typeReference = FindStructType(
expression->children[0]->children[0]->value.string functionCallExpression->functionCallExpression.identifier->accessExpression.accessee->identifier.name
); );
if (typeReference != NULL) if (typeReference != NULL)
{ {
function = LookupFunctionByType( function = LookupFunctionByType(
typeReference, typeReference,
expression->children[0]->children[1]->value.string, functionCallExpression->functionCallExpression.identifier->accessExpression.accessor->identifier.name,
&functionReturnType, &functionReturnType,
&isStatic &isStatic
); );
} }
else else
{ {
structInstance = FindVariablePointer(expression->children[0]->children[0]->value.string); structInstance = FindVariablePointer(functionCallExpression->functionCallExpression.identifier->accessExpression.accessee->identifier.name);
function = LookupFunctionByInstance(structInstance, expression->children[0]->children[1]->value.string, &functionReturnType, &isStatic); function = LookupFunctionByInstance(structInstance, functionCallExpression->functionCallExpression.identifier->accessExpression.accessor->identifier.name, &functionReturnType, &isStatic);
} }
} }
else else
@ -539,9 +539,9 @@ static LLVMValueRef CompileFunctionCallExpression(
argumentCount += 1; argumentCount += 1;
} }
for (i = 0; i < expression->children[1]->childCount; i += 1) for (i = 0; i < functionCallExpression->functionCallExpression.argumentSequence->functionArgumentSequence.count; i += 1)
{ {
args[argumentCount] = CompileExpression(builder, expression->children[1]->children[i]); args[argumentCount] = CompileExpression(builder, functionCallExpression->functionCallExpression.argumentSequence->functionArgumentSequence.sequence[i]);
argumentCount += 1; argumentCount += 1;
} }
@ -555,30 +555,26 @@ static LLVMValueRef CompileFunctionCallExpression(
static LLVMValueRef CompileAccessExpressionForStore( static LLVMValueRef CompileAccessExpressionForStore(
LLVMBuilderRef builder, LLVMBuilderRef builder,
Node *expression Node *accessExpression
) { ) {
Node *accessee = expression->children[0]; LLVMValueRef accesseeValue = FindVariablePointer(accessExpression->accessExpression.accessee->identifier.name);
Node *accessor = expression->children[1]; return FindStructFieldPointer(builder, accesseeValue, accessExpression->accessExpression.accessor->identifier.name);
LLVMValueRef accesseeValue = FindVariablePointer(accessee->value.string);
return FindStructFieldPointer(builder, accesseeValue, accessor->value.string);
} }
static LLVMValueRef CompileAccessExpression( static LLVMValueRef CompileAccessExpression(
LLVMBuilderRef builder, LLVMBuilderRef builder,
Node *expression Node *accessExpression
) { ) {
Node *accessee = expression->children[0]; LLVMValueRef accesseeValue = FindVariablePointer(accessExpression->accessExpression.accessee->identifier.name);
Node *accessor = expression->children[1]; LLVMValueRef access = FindStructFieldPointer(builder, accesseeValue, accessExpression->accessExpression.accessor->identifier.name);
LLVMValueRef accesseeValue = FindVariablePointer(accessee->value.string); return LLVMBuildLoad(builder, access, accessExpression->accessExpression.accessor->identifier.name);
LLVMValueRef access = FindStructFieldPointer(builder, accesseeValue, accessor->value.string);
return LLVMBuildLoad(builder, access, accessor->value.string);
} }
static LLVMValueRef CompileAllocExpression( static LLVMValueRef CompileAllocExpression(
LLVMBuilderRef builder, LLVMBuilderRef builder,
Node *expression Node *allocExpression
) { ) {
LLVMTypeRef type = ResolveType(expression->children[0]); LLVMTypeRef type = ResolveType(allocExpression->allocExpression.type);
return LLVMBuildMalloc(builder, type, "allocation"); return LLVMBuildMalloc(builder, type, "allocation");
} }
@ -601,7 +597,7 @@ static LLVMValueRef CompileExpression(
return CompileFunctionCallExpression(builder, expression); return CompileFunctionCallExpression(builder, expression);
case Identifier: case Identifier:
return FindVariableValue(builder, expression->value.string); return FindVariableValue(builder, expression->identifier.name);
case Number: case Number:
return CompileNumber(expression); return CompileNumber(expression);
@ -619,7 +615,7 @@ static LLVMBasicBlockRef CompileStatement(LLVMBuilderRef builder, LLVMValueRef f
static LLVMBasicBlockRef CompileReturn(LLVMBuilderRef builder, LLVMValueRef function, Node *returnStatemement) static LLVMBasicBlockRef CompileReturn(LLVMBuilderRef builder, LLVMValueRef function, Node *returnStatemement)
{ {
LLVMValueRef expression = CompileExpression(builder, returnStatemement->children[0]); LLVMValueRef expression = CompileExpression(builder, returnStatemement->returnStatement.expression);
LLVMBuildRet(builder, expression); LLVMBuildRet(builder, expression);
return LLVMGetLastBasicBlock(function); return LLVMGetLastBasicBlock(function);
} }
@ -634,11 +630,11 @@ static LLVMBasicBlockRef CompileReturnVoid(LLVMBuilderRef builder, LLVMValueRef
static LLVMValueRef CompileFunctionVariableDeclaration(LLVMBuilderRef builder, LLVMValueRef function, Node *variableDeclaration) static LLVMValueRef CompileFunctionVariableDeclaration(LLVMBuilderRef builder, LLVMValueRef function, Node *variableDeclaration)
{ {
LLVMValueRef variable; LLVMValueRef variable;
char *variableName = variableDeclaration->children[1]->value.string; char *variableName = variableDeclaration->declaration.identifier->identifier.name;
char *ptrName = strdup(variableName); char *ptrName = strdup(variableName);
strcat(ptrName, "_ptr"); strcat(ptrName, "_ptr");
variable = LLVMBuildAlloca(builder, ResolveType(variableDeclaration->children[0]), ptrName); variable = LLVMBuildAlloca(builder, ResolveType(variableDeclaration->declaration.type), ptrName);
free(ptrName); free(ptrName);
@ -649,19 +645,19 @@ static LLVMValueRef CompileFunctionVariableDeclaration(LLVMBuilderRef builder, L
static LLVMBasicBlockRef CompileAssignment(LLVMBuilderRef builder, LLVMValueRef function, Node *assignmentStatement) static LLVMBasicBlockRef CompileAssignment(LLVMBuilderRef builder, LLVMValueRef function, Node *assignmentStatement)
{ {
LLVMValueRef result = CompileExpression(builder, assignmentStatement->children[1]); LLVMValueRef result = CompileExpression(builder, assignmentStatement->assignmentStatement.right);
LLVMValueRef identifier; LLVMValueRef identifier;
if (assignmentStatement->children[0]->syntaxKind == AccessExpression) if (assignmentStatement->assignmentStatement.left->syntaxKind == AccessExpression)
{ {
identifier = CompileAccessExpressionForStore(builder, assignmentStatement->children[0]); identifier = CompileAccessExpressionForStore(builder, assignmentStatement->assignmentStatement.left);
} }
else if (assignmentStatement->children[0]->syntaxKind == Identifier) else if (assignmentStatement->assignmentStatement.left->syntaxKind == Identifier)
{ {
identifier = FindVariablePointer(assignmentStatement->children[0]->value.string); identifier = FindVariablePointer(assignmentStatement->assignmentStatement.left->identifier.name);
} }
else if (assignmentStatement->children[0]->syntaxKind == Declaration) else if (assignmentStatement->assignmentStatement.left->syntaxKind == Declaration)
{ {
identifier = CompileFunctionVariableDeclaration(builder, function, assignmentStatement->children[0]); identifier = CompileFunctionVariableDeclaration(builder, function, assignmentStatement->assignmentStatement.left);
} }
else else
{ {
@ -677,7 +673,7 @@ static LLVMBasicBlockRef CompileAssignment(LLVMBuilderRef builder, LLVMValueRef
static LLVMBasicBlockRef CompileIfStatement(LLVMBuilderRef builder, LLVMValueRef function, Node *ifStatement) static LLVMBasicBlockRef CompileIfStatement(LLVMBuilderRef builder, LLVMValueRef function, Node *ifStatement)
{ {
uint32_t i; uint32_t i;
LLVMValueRef conditional = CompileExpression(builder, ifStatement->children[0]); LLVMValueRef conditional = CompileExpression(builder, ifStatement->ifStatement.expression);
LLVMBasicBlockRef block = LLVMAppendBasicBlock(function, "ifBlock"); LLVMBasicBlockRef block = LLVMAppendBasicBlock(function, "ifBlock");
LLVMBasicBlockRef afterCond = LLVMAppendBasicBlock(function, "afterCond"); LLVMBasicBlockRef afterCond = LLVMAppendBasicBlock(function, "afterCond");
@ -686,9 +682,9 @@ static LLVMBasicBlockRef CompileIfStatement(LLVMBuilderRef builder, LLVMValueRef
LLVMPositionBuilderAtEnd(builder, block); LLVMPositionBuilderAtEnd(builder, block);
for (i = 0; i < ifStatement->children[1]->childCount; i += 1) for (i = 0; i < ifStatement->ifStatement.statementSequence->statementSequence.count; i += 1)
{ {
CompileStatement(builder, function, ifStatement->children[1]->children[i]); CompileStatement(builder, function, ifStatement->ifStatement.statementSequence->statementSequence.sequence[i]);
} }
LLVMBuildBr(builder, afterCond); LLVMBuildBr(builder, afterCond);
@ -700,7 +696,7 @@ static LLVMBasicBlockRef CompileIfStatement(LLVMBuilderRef builder, LLVMValueRef
static LLVMBasicBlockRef CompileIfElseStatement(LLVMBuilderRef builder, LLVMValueRef function, Node *ifElseStatement) static LLVMBasicBlockRef CompileIfElseStatement(LLVMBuilderRef builder, LLVMValueRef function, Node *ifElseStatement)
{ {
uint32_t i; uint32_t i;
LLVMValueRef conditional = CompileExpression(builder, ifElseStatement->children[0]->children[0]); LLVMValueRef conditional = CompileExpression(builder, ifElseStatement->ifElseStatement.ifStatement->ifStatement.expression);
LLVMBasicBlockRef ifBlock = LLVMAppendBasicBlock(function, "ifBlock"); LLVMBasicBlockRef ifBlock = LLVMAppendBasicBlock(function, "ifBlock");
LLVMBasicBlockRef elseBlock = LLVMAppendBasicBlock(function, "elseBlock"); LLVMBasicBlockRef elseBlock = LLVMAppendBasicBlock(function, "elseBlock");
@ -710,25 +706,25 @@ static LLVMBasicBlockRef CompileIfElseStatement(LLVMBuilderRef builder, LLVMValu
LLVMPositionBuilderAtEnd(builder, ifBlock); LLVMPositionBuilderAtEnd(builder, ifBlock);
for (i = 0; i < ifElseStatement->children[0]->children[1]->childCount; i += 1) for (i = 0; i < ifElseStatement->ifElseStatement.ifStatement->ifStatement.statementSequence->statementSequence.count; i += 1)
{ {
CompileStatement(builder, function, ifElseStatement->children[0]->children[1]->children[i]); CompileStatement(builder, function, ifElseStatement->ifElseStatement.ifStatement->ifStatement.statementSequence->statementSequence.sequence[i]);
} }
LLVMBuildBr(builder, afterCond); LLVMBuildBr(builder, afterCond);
LLVMPositionBuilderAtEnd(builder, elseBlock); LLVMPositionBuilderAtEnd(builder, elseBlock);
if (ifElseStatement->children[1]->syntaxKind == StatementSequence) if (ifElseStatement->ifElseStatement.elseStatement->syntaxKind == StatementSequence)
{ {
for (i = 0; i < ifElseStatement->children[1]->childCount; i += 1) for (i = 0; i < ifElseStatement->ifElseStatement.elseStatement->statementSequence.count; i += 1)
{ {
CompileStatement(builder, function, ifElseStatement->children[1]->children[i]); CompileStatement(builder, function, ifElseStatement->ifElseStatement.elseStatement->statementSequence.sequence[i]);
} }
} }
else else
{ {
CompileStatement(builder, function, ifElseStatement->children[1]); CompileStatement(builder, function, ifElseStatement->ifElseStatement.elseStatement);
} }
LLVMBuildBr(builder, afterCond); LLVMBuildBr(builder, afterCond);
@ -744,8 +740,8 @@ static LLVMBasicBlockRef CompileForLoopStatement(LLVMBuilderRef builder, LLVMVal
LLVMBasicBlockRef checkBlock = LLVMAppendBasicBlock(function, "loopCheck"); LLVMBasicBlockRef checkBlock = LLVMAppendBasicBlock(function, "loopCheck");
LLVMBasicBlockRef bodyBlock = LLVMAppendBasicBlock(function, "loopBody"); LLVMBasicBlockRef bodyBlock = LLVMAppendBasicBlock(function, "loopBody");
LLVMBasicBlockRef afterLoopBlock = LLVMAppendBasicBlock(function, "afterLoop"); LLVMBasicBlockRef afterLoopBlock = LLVMAppendBasicBlock(function, "afterLoop");
char *iteratorVariableName = forLoopStatement->children[0]->children[1]->value.string; char *iteratorVariableName = forLoopStatement->forLoop.declaration->declaration.identifier->identifier.name;
LLVMTypeRef iteratorVariableType = ResolveType(forLoopStatement->children[0]->children[0]); LLVMTypeRef iteratorVariableType = ResolveType(forLoopStatement->forLoop.declaration->declaration.type);
PushScopeFrame(scope); PushScopeFrame(scope);
@ -762,13 +758,13 @@ static LLVMBasicBlockRef CompileForLoopStatement(LLVMBuilderRef builder, LLVMVal
LLVMValueRef nextValue = LLVMBuildAdd( LLVMValueRef nextValue = LLVMBuildAdd(
builder, builder,
iteratorValue, iteratorValue,
LLVMConstInt(iteratorVariableType, forLoopStatement->children[1]->value.number, 0), LLVMConstInt(iteratorVariableType, 1, 0), /* FIXME: add custom increment value */
"next" "next"
); );
LLVMPositionBuilderAtEnd(builder, checkBlock); LLVMPositionBuilderAtEnd(builder, checkBlock);
LLVMValueRef iteratorEndValue = CompileNumber(forLoopStatement->children[2]); LLVMValueRef iteratorEndValue = CompileNumber(forLoopStatement->forLoop.endNumber);
LLVMValueRef comparison = LLVMBuildICmp(builder, LLVMIntULE, iteratorValue, iteratorEndValue, "iteratorCompare"); LLVMValueRef comparison = LLVMBuildICmp(builder, LLVMIntULE, iteratorValue, iteratorEndValue, "iteratorCompare");
LLVMBuildCondBr(builder, comparison, bodyBlock, afterLoopBlock); LLVMBuildCondBr(builder, comparison, bodyBlock, afterLoopBlock);
@ -776,9 +772,9 @@ static LLVMBasicBlockRef CompileForLoopStatement(LLVMBuilderRef builder, LLVMVal
LLVMPositionBuilderAtEnd(builder, bodyBlock); LLVMPositionBuilderAtEnd(builder, bodyBlock);
LLVMBasicBlockRef lastBlock; LLVMBasicBlockRef lastBlock;
for (i = 0; i < forLoopStatement->children[3]->childCount; i += 1) for (i = 0; i < forLoopStatement->forLoop.statementSequence->statementSequence.count; i += 1)
{ {
lastBlock = CompileStatement(builder, function, forLoopStatement->children[3]->children[i]); lastBlock = CompileStatement(builder, function, forLoopStatement->forLoop.statementSequence->statementSequence.sequence[i]);
} }
LLVMBuildBr(builder, checkBlock); LLVMBuildBr(builder, checkBlock);
@ -786,7 +782,7 @@ static LLVMBasicBlockRef CompileForLoopStatement(LLVMBuilderRef builder, LLVMVal
LLVMPositionBuilderBefore(builder, LLVMGetFirstInstruction(checkBlock)); LLVMPositionBuilderBefore(builder, LLVMGetFirstInstruction(checkBlock));
LLVMValueRef incomingValues[2]; LLVMValueRef incomingValues[2];
incomingValues[0] = CompileNumber(forLoopStatement->children[1]); incomingValues[0] = CompileNumber(forLoopStatement->forLoop.startNumber);
incomingValues[1] = nextValue; incomingValues[1] = nextValue;
LLVMBasicBlockRef incomingBlocks[2]; LLVMBasicBlockRef incomingBlocks[2];
@ -848,17 +844,17 @@ static void CompileFunction(
uint32_t i; uint32_t i;
uint8_t hasReturn = 0; uint8_t hasReturn = 0;
uint8_t isStatic = 0; uint8_t isStatic = 0;
Node *functionSignature = functionDeclaration->children[0]; Node *functionSignature = functionDeclaration->functionDeclaration.functionSignature;
Node *functionBody = functionDeclaration->children[1]; Node *functionBody = functionDeclaration->functionDeclaration.functionBody;
uint32_t argumentCount = functionSignature->children[2]->childCount; uint32_t argumentCount = functionSignature->functionSignature.arguments->functionSignatureArguments.count;
LLVMTypeRef paramTypes[argumentCount + 1]; LLVMTypeRef paramTypes[argumentCount + 1];
uint32_t paramIndex = 0; uint32_t paramIndex = 0;
if (functionSignature->children[3]->childCount > 0) if (functionSignature->functionSignature.modifiers->functionModifiers.count > 0)
{ {
for (i = 0; i < functionSignature->children[3]->childCount; i += 1) for (i = 0; i < functionSignature->functionSignature.modifiers->functionModifiers.count; i += 1)
{ {
if (functionSignature->children[3]->children[i]->syntaxKind == StaticModifier) if (functionSignature->functionSignature.modifiers->functionModifiers.sequence[i]->syntaxKind == StaticModifier)
{ {
isStatic = 1; isStatic = 1;
break; break;
@ -875,22 +871,22 @@ static void CompileFunction(
PushScopeFrame(scope); PushScopeFrame(scope);
/* FIXME: should work for non-primitive types */ /* FIXME: should work for non-primitive types */
for (i = 0; i < functionSignature->children[2]->childCount; i += 1) for (i = 0; i < functionSignature->functionSignature.arguments->functionSignatureArguments.count; i += 1)
{ {
paramTypes[paramIndex] = WraithTypeToLLVMType(functionSignature->children[2]->children[i]->children[0]->children[0]->primitiveType); paramTypes[paramIndex] = ResolveType(functionSignature->functionSignature.arguments->functionSignatureArguments.sequence[i]->declaration.type);
paramIndex += 1; paramIndex += 1;
} }
LLVMTypeRef returnType = WraithTypeToLLVMType(functionSignature->children[1]->children[0]->primitiveType); LLVMTypeRef returnType = ResolveType(functionSignature->functionSignature.type);
LLVMTypeRef functionType = LLVMFunctionType(returnType, paramTypes, paramIndex, 0); LLVMTypeRef functionType = LLVMFunctionType(returnType, paramTypes, paramIndex, 0);
char *functionName = strdup(parentStructName); char *functionName = strdup(parentStructName);
strcat(functionName, "_"); strcat(functionName, "_");
strcat(functionName, functionSignature->children[0]->value.string); strcat(functionName, functionSignature->functionSignature.identifier->identifier.name);
LLVMValueRef function = LLVMAddFunction(module, functionName, functionType); LLVMValueRef function = LLVMAddFunction(module, functionName, functionType);
free(functionName); free(functionName);
DeclareStructFunction(wStructPointerType, function, returnType, isStatic, functionSignature->children[0]->value.string); DeclareStructFunction(wStructPointerType, function, returnType, isStatic, functionSignature->functionSignature.identifier->identifier.name);
LLVMBasicBlockRef entry = LLVMAppendBasicBlock(function, "entry"); LLVMBasicBlockRef entry = LLVMAppendBasicBlock(function, "entry");
LLVMBuilderRef builder = LLVMCreateBuilder(); LLVMBuilderRef builder = LLVMCreateBuilder();
@ -902,20 +898,20 @@ static void CompileFunction(
AddStructVariablesToScope(builder, wStructPointer); AddStructVariablesToScope(builder, wStructPointer);
} }
for (i = 0; i < functionSignature->children[2]->childCount; i += 1) for (i = 0; i < functionSignature->functionSignature.arguments->functionSignatureArguments.count; i += 1)
{ {
char *ptrName = strdup(functionSignature->children[2]->children[i]->children[1]->value.string); char *ptrName = strdup(functionSignature->functionSignature.arguments->functionSignatureArguments.sequence[i]->declaration.identifier->identifier.name);
strcat(ptrName, "_ptr"); strcat(ptrName, "_ptr");
LLVMValueRef argument = LLVMGetParam(function, i + !isStatic); LLVMValueRef argument = LLVMGetParam(function, i + !isStatic);
LLVMValueRef argumentCopy = LLVMBuildAlloca(builder, LLVMTypeOf(argument), ptrName); LLVMValueRef argumentCopy = LLVMBuildAlloca(builder, LLVMTypeOf(argument), ptrName);
LLVMBuildStore(builder, argument, argumentCopy); LLVMBuildStore(builder, argument, argumentCopy);
free(ptrName); free(ptrName);
AddLocalVariable(scope, argumentCopy, NULL, functionSignature->children[2]->children[i]->children[1]->value.string); AddLocalVariable(scope, argumentCopy, NULL, functionSignature->functionSignature.arguments->functionSignatureArguments.sequence[i]->declaration.identifier->identifier.name);
} }
for (i = 0; i < functionBody->childCount; i += 1) for (i = 0; i < functionBody->statementSequence.count; i += 1)
{ {
CompileStatement(builder, function, functionBody->children[i]); CompileStatement(builder, function, functionBody->statementSequence.sequence[i]);
} }
hasReturn = LLVMGetBasicBlockTerminator(LLVMGetLastBasicBlock(function)) != NULL; hasReturn = LLVMGetBasicBlockTerminator(LLVMGetLastBasicBlock(function)) != NULL;
@ -938,12 +934,12 @@ static void CompileStruct(LLVMModuleRef module, LLVMContextRef context, Node *no
{ {
uint32_t i; uint32_t i;
uint32_t fieldCount = 0; uint32_t fieldCount = 0;
uint32_t declarationCount = node->children[1]->childCount; uint32_t declarationCount = node->structDeclaration.declarationSequence->declarationSequence.count;
uint8_t packed = 1; uint8_t packed = 1;
LLVMTypeRef types[declarationCount]; LLVMTypeRef types[declarationCount];
Node *currentDeclarationNode; Node *currentDeclarationNode;
Node *fieldDeclarations[declarationCount]; Node *fieldDeclarations[declarationCount];
char *structName = node->children[0]->value.string; char *structName = node->structDeclaration.identifier->identifier.name;
PushScopeFrame(scope); PushScopeFrame(scope);
@ -953,12 +949,12 @@ static void CompileStruct(LLVMModuleRef module, LLVMContextRef context, Node *no
/* first, build the structure definition */ /* first, build the structure definition */
for (i = 0; i < declarationCount; i += 1) for (i = 0; i < declarationCount; i += 1)
{ {
currentDeclarationNode = node->children[1]->children[i]; currentDeclarationNode = node->structDeclaration.declarationSequence->declarationSequence.sequence[i];
switch (currentDeclarationNode->syntaxKind) switch (currentDeclarationNode->syntaxKind)
{ {
case Declaration: /* this is badly named */ case Declaration: /* this is badly named */
types[fieldCount] = ResolveType(currentDeclarationNode->children[0]); types[fieldCount] = ResolveType(currentDeclarationNode->declaration.type);
fieldDeclarations[fieldCount] = currentDeclarationNode; fieldDeclarations[fieldCount] = currentDeclarationNode;
fieldCount += 1; fieldCount += 1;
break; break;
@ -966,12 +962,12 @@ static void CompileStruct(LLVMModuleRef module, LLVMContextRef context, Node *no
} }
LLVMStructSetBody(wStructType, types, fieldCount, packed); LLVMStructSetBody(wStructType, types, fieldCount, packed);
AddStructDeclaration(wStructType, wStructPointerType, node->children[0]->value.string, fieldDeclarations, fieldCount); AddStructDeclaration(wStructType, wStructPointerType, structName, fieldDeclarations, fieldCount);
/* now we can wire up the functions */ /* now we can wire up the functions */
for (i = 0; i < declarationCount; i += 1) for (i = 0; i < declarationCount; i += 1)
{ {
currentDeclarationNode = node->children[1]->children[i]; currentDeclarationNode = node->structDeclaration.declarationSequence->declarationSequence.sequence[i];
switch (currentDeclarationNode->syntaxKind) switch (currentDeclarationNode->syntaxKind)
{ {
@ -984,15 +980,15 @@ static void CompileStruct(LLVMModuleRef module, LLVMContextRef context, Node *no
PopScopeFrame(scope); PopScopeFrame(scope);
} }
static void Compile(LLVMModuleRef module, LLVMContextRef context, Node *node) static void Compile(LLVMModuleRef module, LLVMContextRef context, Node *declarationSequenceNode)
{ {
uint32_t i; uint32_t i;
for (i = 0; i < node->childCount; i += 1) for (i = 0; i < declarationSequenceNode->declarationSequence.count; i += 1)
{ {
if (node->children[i]->syntaxKind == StructDeclaration) if (declarationSequenceNode->declarationSequence.sequence[i]->syntaxKind == StructDeclaration)
{ {
CompileStruct(module, context, node->children[i]); CompileStruct(module, context, declarationSequenceNode->declarationSequence.sequence[i]);
} }
else else
{ {

View File

@ -38,104 +38,95 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
uint32_t i; uint32_t i;
IdNode *mainNode; IdNode *mainNode;
switch (astNode->syntaxKind) { switch (astNode->syntaxKind) {
case AccessExpression:
AddChildToNode(parent, MakeIdTree(astNode->accessExpression.accessee, parent));
AddChildToNode(parent, MakeIdTree(astNode->accessExpression.accessor, parent));
return NULL;
case AllocExpression:
AddChildToNode(parent, MakeIdTree(astNode->allocExpression.type, parent));
return NULL;
case Assignment: { case Assignment: {
if (astNode->children[0]->syntaxKind == Declaration) { if (astNode->assignmentStatement.left->syntaxKind == Declaration) {
return MakeIdTree(astNode->children[0], parent); return MakeIdTree(astNode->assignmentStatement.left, parent);
} else { } else {
for (i = 0; i < astNode->childCount; i++) { AddChildToNode(parent, MakeIdTree(astNode->assignmentStatement.left, parent));
AddChildToNode(parent, MakeIdTree(astNode->children[i], parent)); AddChildToNode(parent, MakeIdTree(astNode->assignmentStatement.right, parent));
}
return NULL; return NULL;
} }
} }
case IfStatement: { case BinaryExpression:
mainNode = MakeIdNode(OrderedScope, "if", parent); AddChildToNode(parent, MakeIdTree(astNode->binaryExpression.left, parent));
Node *clause = astNode->children[0]; AddChildToNode(parent, MakeIdTree(astNode->binaryExpression.right, parent));
Node *stmtSeq = astNode->children[1]; return NULL;
for (i = 0; i < clause->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(clause->children[i], mainNode));
}
for (i = 0; i < stmtSeq->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(stmtSeq->children[i], mainNode));
}
break;
}
case IfElseStatement: {
Node *ifNode = astNode->children[0];
Node *elseStmts = astNode->children[1];
mainNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, mainNode);
IdNode *elseBranch = MakeIdNode(OrderedScope, "else", mainNode);
AddChildToNode(mainNode, ifBranch);
for (i = 0; i < elseStmts->childCount; i++) {
AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i], elseBranch));
}
AddChildToNode(mainNode, elseBranch);
break;
}
case ForLoop: {
Node *loopDecl = astNode->children[0];
Node *loopBody = astNode->children[3];
mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(mainNode, MakeIdTree(loopDecl, mainNode));
for (i = 0; i < loopBody->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(loopBody->children[i], mainNode));
}
break;
}
case Declaration: { case Declaration: {
mainNode = MakeIdNode(Variable, astNode->children[1]->value.string, parent); Node *idNode = astNode->declaration.identifier;
mainNode = MakeIdNode(Variable, idNode->identifier.name, parent);
mainNode->typeTag = MakeTypeTag(astNode); mainNode->typeTag = MakeTypeTag(astNode);
astNode->children[1]->typeTag = mainNode->typeTag; idNode->typeTag = mainNode->typeTag;
break;
}
case StructDeclaration: {
Node *idNode = astNode->children[0];
Node *declsNode = astNode->children[1];
mainNode = MakeIdNode(Struct, idNode->value.string, parent);
mainNode->typeTag = MakeTypeTag(astNode);
for (i = 0; i < declsNode->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(declsNode->children[i], mainNode));
}
break;
}
case FunctionDeclaration: {
Node *sigNode = astNode->children[0];
Node *funcNameNode = sigNode->children[0];
Node *funcArgsNode = sigNode->children[2];
Node *bodyStatementsNode = astNode->children[1];
mainNode = MakeIdNode(Function, funcNameNode->value.string, parent);
mainNode->typeTag = MakeTypeTag(astNode);
astNode->children[0]->children[0]->typeTag = mainNode->typeTag;
for (i = 0; i < funcArgsNode->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(funcArgsNode->children[i], mainNode));
}
for (i = 0; i < bodyStatementsNode->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(bodyStatementsNode->children[i], mainNode));
}
break; break;
} }
case DeclarationSequence: { case DeclarationSequence: {
mainNode = MakeIdNode(UnorderedScope, "", parent); mainNode = MakeIdNode(UnorderedScope, "", parent);
for (i = 0; i < astNode->childCount; i++) { for (i = 0; i < astNode->declarationSequence.count; i++) {
AddChildToNode(mainNode, MakeIdTree(astNode->children[i], mainNode)); AddChildToNode(
mainNode, MakeIdTree(astNode->declarationSequence.sequence[i], mainNode));
} }
break; break;
} }
case ForLoop: {
Node *loopDecl = astNode->forLoop.declaration;
Node *loopBody = astNode->forLoop.statementSequence;
mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(mainNode, MakeIdTree(loopDecl, mainNode));
AddChildToNode(mainNode, MakeIdTree(loopBody, mainNode));
break;
}
case FunctionArgumentSequence:
for (i = 0; i < astNode->functionArgumentSequence.count; i++) {
AddChildToNode(
parent, MakeIdTree(astNode->functionArgumentSequence.sequence[i], parent));
}
return NULL;
case FunctionCallExpression:
AddChildToNode(parent, MakeIdTree(astNode->functionCallExpression.identifier, parent));
AddChildToNode(
parent, MakeIdTree(astNode->functionCallExpression.argumentSequence, parent));
return NULL;
case FunctionDeclaration: {
Node *sigNode = astNode->functionDeclaration.functionSignature;
Node *idNode = sigNode->functionSignature.identifier;
char *funcName = idNode->identifier.name;
mainNode = MakeIdNode(Function, funcName, parent);
mainNode->typeTag = MakeTypeTag(astNode);
idNode->typeTag = mainNode->typeTag;
MakeIdTree(sigNode->functionSignature.arguments, mainNode);
MakeIdTree(astNode->functionDeclaration.functionBody, mainNode);
break;
}
case FunctionSignatureArguments: {
for (i = 0; i < astNode->functionSignatureArguments.count; i++) {
Node *argNode = astNode->functionSignatureArguments.sequence[i];
AddChildToNode(parent, MakeIdTree(argNode, parent));
}
return NULL;
}
case Identifier: { case Identifier: {
mainNode = MakeIdNode(Placeholder, astNode->value.string, parent); char *name = astNode->identifier.name;
IdNode *lookupNode = LookupId(mainNode, NULL, astNode->value.string); mainNode = MakeIdNode(Placeholder, name, parent);
IdNode *lookupNode = LookupId(mainNode, NULL, name);
if (lookupNode == NULL) { if (lookupNode == NULL) {
fprintf(stderr, "wraith: Could not find IdNode for id %s\n", astNode->value.string); fprintf(stderr, "wraith: Could not find IdNode for id %s\n", name);
TypeTag *tag = (TypeTag*)malloc(sizeof(TypeTag)); TypeTag *tag = (TypeTag*)malloc(sizeof(TypeTag));
tag->type = Unknown; tag->type = Unknown;
astNode->typeTag = tag; astNode->typeTag = tag;
@ -145,12 +136,73 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
break; break;
} }
default: { case IfStatement: {
for (i = 0; i < astNode->childCount; i++) { Node *clause = astNode->ifStatement.expression;
AddChildToNode(parent, MakeIdTree(astNode->children[i], parent)); Node *stmtSeq = astNode->ifStatement.statementSequence;
mainNode = MakeIdNode(OrderedScope, "if", parent);
MakeIdTree(clause, mainNode);
MakeIdTree(stmtSeq, mainNode);
break;
}
case IfElseStatement: {
Node *ifNode = astNode->ifElseStatement.ifStatement;
Node *elseStmts = astNode->ifElseStatement.elseStatement;
mainNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, mainNode);
AddChildToNode(mainNode, ifBranch);
IdNode *elseScope = MakeIdNode(OrderedScope, "else", mainNode);
MakeIdTree(elseStmts, elseScope);
AddChildToNode(mainNode, elseScope);
break;
}
case ReferenceTypeNode:
AddChildToNode(parent, MakeIdTree(astNode->referenceType.type, parent));
return NULL;
case Return:
AddChildToNode(parent, MakeIdTree(astNode->returnStatement.expression, parent));
return NULL;
case StatementSequence: {
for (i = 0; i < astNode->statementSequence.count; i++) {
Node *argNode = astNode->statementSequence.sequence[i];
AddChildToNode(parent, MakeIdTree(argNode, parent));
} }
return NULL; return NULL;
} }
case StructDeclaration: {
Node *idNode = astNode->structDeclaration.identifier;
Node *declsNode = astNode->structDeclaration.declarationSequence;
mainNode = MakeIdNode(Struct, idNode->identifier.name, parent);
mainNode->typeTag = MakeTypeTag(astNode);
for (i = 0; i < declsNode->declarationSequence.count; i++) {
Node *decl = declsNode->declarationSequence.sequence[i];
AddChildToNode(mainNode, MakeIdTree(decl, mainNode));
}
break;
}
case Type:
AddChildToNode(parent, MakeIdTree(astNode->type.typeNode, parent));
return NULL;
case UnaryExpression:
AddChildToNode(parent, MakeIdTree(astNode->unaryExpression.child, parent));
return NULL;
case Comment:
case CustomTypeNode:
case FunctionModifiers:
case FunctionSignature:
case Number:
case PrimitiveTypeNode:
case ReturnVoid:
case StaticModifier:
case StringLiteral:
return NULL;
} }
astNode->idLink = mainNode; astNode->idLink = mainNode;
@ -203,7 +255,6 @@ void PrintIdTree(IdNode *tree, uint32_t tabCount) {
} }
} }
int PrintAncestors(IdNode *node) { int PrintAncestors(IdNode *node) {
if (node == NULL) return -1; if (node == NULL) return -1;

View File

@ -69,7 +69,7 @@ int main(int argc, char *argv[])
IdNode *idTree = MakeIdTree(rootNode, NULL); IdNode *idTree = MakeIdTree(rootNode, NULL);
PrintIdTree(idTree, /*tabCount=*/0); PrintIdTree(idTree, /*tabCount=*/0);
printf("\n"); printf("\n");
PrintTree(rootNode, /*tabCount=*/0); PrintNode(rootNode, /*tabCount=*/0);
} }
exitCode = Codegen(rootNode, optimizationLevel); exitCode = Codegen(rootNode, optimizationLevel);
} }

View File

@ -31,7 +31,7 @@ int Parse(char *inputFilename, Node **pRootNode, uint8_t parseVerbose)
{ {
if (parseVerbose) if (parseVerbose)
{ {
PrintTree(*pRootNode, 0); PrintNode(*pRootNode, 0);
} }
} }
else if (result == 1) else if (result == 1)