forked from cosmonaut/wraith-lang
some cleanup
parent
26ffaf45d4
commit
c2b4cd4b4a
|
@ -31,7 +31,7 @@ ADD_FLEX_BISON_DEPENDENCY(Scanner Parser)
|
|||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
add_executable(
|
||||
wraith_compile
|
||||
wraith
|
||||
ast.c
|
||||
stack.c
|
||||
${BISON_Parser_OUTPUTS}
|
||||
|
@ -39,4 +39,8 @@ add_executable(
|
|||
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
2
ast.c
|
@ -210,7 +210,6 @@ Node* MakeFunctionSignatureNode(
|
|||
Node* typeNode,
|
||||
Node* arguments
|
||||
) {
|
||||
uint32_t i;
|
||||
Node* node = (Node*) malloc(sizeof(Node));
|
||||
node->syntaxKind = FunctionSignature;
|
||||
node->childCount = 3;
|
||||
|
@ -283,7 +282,6 @@ Node* MakeFunctionCallExpressionNode(
|
|||
Node *identifierNode,
|
||||
Node *argumentSequenceNode
|
||||
) {
|
||||
int32_t i;
|
||||
Node* node = (Node*) malloc(sizeof(Node));
|
||||
node->syntaxKind = FunctionCallExpression;
|
||||
node->children = (Node**) malloc(sizeof(Node*) * 2);
|
||||
|
|
45
compiler.c
45
compiler.c
|
@ -30,9 +30,6 @@ typedef struct ScopeFrame
|
|||
{
|
||||
LocalVariable *localVariables;
|
||||
uint32_t localVariableCount;
|
||||
|
||||
FunctionArgument *arguments;
|
||||
uint32_t argumentCount;
|
||||
} ScopeFrame;
|
||||
|
||||
typedef struct Scope
|
||||
|
@ -65,8 +62,6 @@ static Scope* CreateScope()
|
|||
Scope *scope = malloc(sizeof(Scope));
|
||||
|
||||
scope->scopeStack = malloc(sizeof(ScopeFrame));
|
||||
scope->scopeStack[0].argumentCount = 0;
|
||||
scope->scopeStack[0].arguments = NULL;
|
||||
scope->scopeStack[0].localVariableCount = 0;
|
||||
scope->scopeStack[0].localVariables = NULL;
|
||||
scope->scopeStackCount = 1;
|
||||
|
@ -78,8 +73,6 @@ static void PushScopeFrame(Scope *scope)
|
|||
{
|
||||
uint32_t index = scope->scopeStackCount;
|
||||
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].localVariables = NULL;
|
||||
|
||||
|
@ -91,15 +84,6 @@ static void PopScopeFrame(Scope *scope)
|
|||
uint32_t i;
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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 (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)
|
||||
{
|
||||
if (strcmp(scope->scopeStack[i].localVariables[j].name, name) == 0)
|
||||
|
@ -413,8 +377,6 @@ static LLVMValueRef CompileExpression(
|
|||
LLVMValueRef function,
|
||||
Node *expression
|
||||
) {
|
||||
LLVMValueRef var;
|
||||
|
||||
switch (expression->syntaxKind)
|
||||
{
|
||||
case AccessExpression:
|
||||
|
@ -440,7 +402,6 @@ static LLVMValueRef CompileExpression(
|
|||
/* FIXME: we need a scope structure */
|
||||
static void CompileReturn(LLVMValueRef wStructValue, LLVMBuilderRef builder, LLVMValueRef function, Node *returnStatemement)
|
||||
{
|
||||
uint32_t i, j;
|
||||
LLVMValueRef expression = CompileExpression(wStructValue, builder, function, returnStatemement->children[0]);
|
||||
LLVMBuildRet(builder, expression);
|
||||
}
|
||||
|
@ -452,7 +413,6 @@ static void CompileReturnVoid(LLVMBuilderRef builder)
|
|||
|
||||
static void CompileAssignment(LLVMValueRef wStructValue, LLVMBuilderRef builder, LLVMValueRef function, Node *assignmentStatement)
|
||||
{
|
||||
LLVMValueRef fieldPointer;
|
||||
LLVMValueRef result = CompileExpression(wStructValue, builder, function, assignmentStatement->children[1]);
|
||||
LLVMValueRef identifier;
|
||||
if (assignmentStatement->children[0]->syntaxKind == AccessExpression)
|
||||
|
@ -463,6 +423,11 @@ static void CompileAssignment(LLVMValueRef wStructValue, LLVMBuilderRef builder,
|
|||
{
|
||||
identifier = FindVariablePointer(assignmentStatement->children[0]->value.string);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Identifier not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
LLVMBuildStore(builder, result, identifier);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue