some cleanup

pull/1/head
cosmonaut 2021-04-21 22:26:34 -07:00
parent 26ffaf45d4
commit c2b4cd4b4a
3 changed files with 11 additions and 44 deletions

View File

@ -31,7 +31,7 @@ ADD_FLEX_BISON_DEPENDENCY(Scanner Parser)
include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable( add_executable(
wraith_compile wraith
ast.c ast.c
stack.c stack.c
${BISON_Parser_OUTPUTS} ${BISON_Parser_OUTPUTS}
@ -39,4 +39,8 @@ add_executable(
compiler.c compiler.c
) )
target_link_libraries(wraith_compile PUBLIC LLVM) if(NOT MSVC)
set_property(TARGET wraith PROPERTY COMPILE_FLAGS "-std=gnu99 -Wall -Wno-strict-aliasing -pedantic")
endif()
target_link_libraries(wraith PUBLIC LLVM)

2
ast.c
View File

@ -210,7 +210,6 @@ Node* MakeFunctionSignatureNode(
Node* typeNode, Node* typeNode,
Node* arguments Node* arguments
) { ) {
uint32_t i;
Node* node = (Node*) malloc(sizeof(Node)); Node* node = (Node*) malloc(sizeof(Node));
node->syntaxKind = FunctionSignature; node->syntaxKind = FunctionSignature;
node->childCount = 3; node->childCount = 3;
@ -283,7 +282,6 @@ Node* MakeFunctionCallExpressionNode(
Node *identifierNode, Node *identifierNode,
Node *argumentSequenceNode Node *argumentSequenceNode
) { ) {
int32_t i;
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->children = (Node**) malloc(sizeof(Node*) * 2);

View File

@ -30,9 +30,6 @@ typedef struct ScopeFrame
{ {
LocalVariable *localVariables; LocalVariable *localVariables;
uint32_t localVariableCount; uint32_t localVariableCount;
FunctionArgument *arguments;
uint32_t argumentCount;
} ScopeFrame; } ScopeFrame;
typedef struct Scope typedef struct Scope
@ -65,8 +62,6 @@ static Scope* CreateScope()
Scope *scope = malloc(sizeof(Scope)); Scope *scope = malloc(sizeof(Scope));
scope->scopeStack = malloc(sizeof(ScopeFrame)); scope->scopeStack = malloc(sizeof(ScopeFrame));
scope->scopeStack[0].argumentCount = 0;
scope->scopeStack[0].arguments = NULL;
scope->scopeStack[0].localVariableCount = 0; scope->scopeStack[0].localVariableCount = 0;
scope->scopeStack[0].localVariables = NULL; scope->scopeStack[0].localVariables = NULL;
scope->scopeStackCount = 1; scope->scopeStackCount = 1;
@ -78,8 +73,6 @@ static void PushScopeFrame(Scope *scope)
{ {
uint32_t index = scope->scopeStackCount; uint32_t index = scope->scopeStackCount;
scope->scopeStack = realloc(scope->scopeStack, sizeof(ScopeFrame) * (scope->scopeStackCount + 1)); scope->scopeStack = realloc(scope->scopeStack, sizeof(ScopeFrame) * (scope->scopeStackCount + 1));
scope->scopeStack[index].argumentCount = 0;
scope->scopeStack[index].arguments = NULL;
scope->scopeStack[index].localVariableCount = 0; scope->scopeStack[index].localVariableCount = 0;
scope->scopeStack[index].localVariables = NULL; scope->scopeStack[index].localVariables = NULL;
@ -91,15 +84,6 @@ static void PopScopeFrame(Scope *scope)
uint32_t i; uint32_t i;
uint32_t index = scope->scopeStackCount - 1; uint32_t index = scope->scopeStackCount - 1;
if (scope->scopeStack[index].arguments != NULL)
{
for (i = 0; i < scope->scopeStack[index].argumentCount; i += 1)
{
free(scope->scopeStack[index].arguments[i].name);
}
free(scope->scopeStack[index].arguments);
}
if (scope->scopeStack[index].localVariables != NULL) if (scope->scopeStack[index].localVariables != NULL)
{ {
for (i = 0; i < scope->scopeStack[index].localVariableCount; i += 1) for (i = 0; i < scope->scopeStack[index].localVariableCount; i += 1)
@ -126,18 +110,6 @@ static void AddLocalVariable(Scope *scope, LLVMValueRef pointer, char *name)
scopeFrame->localVariableCount += 1; scopeFrame->localVariableCount += 1;
} }
static void AddFunctionArgument(Scope *scope, LLVMValueRef value, char *name)
{
ScopeFrame *scopeFrame = &scope->scopeStack[scope->scopeStackCount - 1];
uint32_t index = scopeFrame->argumentCount;
scopeFrame->arguments = realloc(scopeFrame->arguments, sizeof(FunctionArgument) * (scopeFrame->argumentCount + 1));
scopeFrame->arguments[index].name = strdup(name);
scopeFrame->arguments[index].value = value;
scopeFrame->argumentCount += 1;
}
static LLVMValueRef FindStructFieldPointer(LLVMBuilderRef builder, LLVMValueRef structPointer, char *name) static LLVMValueRef FindStructFieldPointer(LLVMBuilderRef builder, LLVMValueRef structPointer, char *name)
{ {
int32_t i, j; int32_t i, j;
@ -195,14 +167,6 @@ static LLVMValueRef FindVariableValue(LLVMBuilderRef builder, char *name)
for (i = scope->scopeStackCount - 1; i >= 0; i -= 1) for (i = scope->scopeStackCount - 1; i >= 0; i -= 1)
{ {
for (j = 0; j < scope->scopeStack[i].argumentCount; j += 1)
{
if (strcmp(scope->scopeStack[i].arguments[j].name, name) == 0)
{
return scope->scopeStack[i].arguments[j].value;
}
}
for (j = 0; j < scope->scopeStack[i].localVariableCount; j += 1) for (j = 0; j < scope->scopeStack[i].localVariableCount; j += 1)
{ {
if (strcmp(scope->scopeStack[i].localVariables[j].name, name) == 0) if (strcmp(scope->scopeStack[i].localVariables[j].name, name) == 0)
@ -413,8 +377,6 @@ static LLVMValueRef CompileExpression(
LLVMValueRef function, LLVMValueRef function,
Node *expression Node *expression
) { ) {
LLVMValueRef var;
switch (expression->syntaxKind) switch (expression->syntaxKind)
{ {
case AccessExpression: case AccessExpression:
@ -440,7 +402,6 @@ static LLVMValueRef CompileExpression(
/* FIXME: we need a scope structure */ /* FIXME: we need a scope structure */
static void CompileReturn(LLVMValueRef wStructValue, LLVMBuilderRef builder, LLVMValueRef function, Node *returnStatemement) static void CompileReturn(LLVMValueRef wStructValue, LLVMBuilderRef builder, LLVMValueRef function, Node *returnStatemement)
{ {
uint32_t i, j;
LLVMValueRef expression = CompileExpression(wStructValue, builder, function, returnStatemement->children[0]); LLVMValueRef expression = CompileExpression(wStructValue, builder, function, returnStatemement->children[0]);
LLVMBuildRet(builder, expression); LLVMBuildRet(builder, expression);
} }
@ -452,7 +413,6 @@ static void CompileReturnVoid(LLVMBuilderRef builder)
static void CompileAssignment(LLVMValueRef wStructValue, LLVMBuilderRef builder, LLVMValueRef function, Node *assignmentStatement) static void CompileAssignment(LLVMValueRef wStructValue, LLVMBuilderRef builder, LLVMValueRef function, Node *assignmentStatement)
{ {
LLVMValueRef fieldPointer;
LLVMValueRef result = CompileExpression(wStructValue, builder, function, assignmentStatement->children[1]); LLVMValueRef result = CompileExpression(wStructValue, builder, function, assignmentStatement->children[1]);
LLVMValueRef identifier; LLVMValueRef identifier;
if (assignmentStatement->children[0]->syntaxKind == AccessExpression) if (assignmentStatement->children[0]->syntaxKind == AccessExpression)
@ -463,6 +423,11 @@ static void CompileAssignment(LLVMValueRef wStructValue, LLVMBuilderRef builder,
{ {
identifier = FindVariablePointer(assignmentStatement->children[0]->value.string); identifier = FindVariablePointer(assignmentStatement->children[0]->value.string);
} }
else
{
printf("Identifier not found!");
return;
}
LLVMBuildStore(builder, result, identifier); LLVMBuildStore(builder, result, identifier);
} }