wraith-lang/compiler.c

82 lines
1.6 KiB
C

#include <stdio.h>
#include <llvm-c/Core.h>
#include "y.tab.h"
#include "ast.h"
#include "stack.h"
extern FILE *yyin;
Stack *stack;
Node *rootNode;
LLVMTypeRef WraithTypeToLLVMType(PrimitiveType type)
{
switch (type)
{
case Int:
return LLVMInt64Type();
case UInt:
return LLVMInt64Type();
}
return NULL;
}
void CompileFunction(LLVMModuleRef module, Node *functionDeclaration)
{
uint32_t i;
Node *functionSignature = functionDeclaration->children[0];
LLVMTypeRef paramTypes[functionSignature->children[2]->childCount];
for (i = 0; i < functionSignature->children[2]->childCount; i += 1)
{
LLVMTypeRef paramType = WraithTypeToLLVMType(functionSignature->children[0]->type);
}
LLVMTypeRef returnType = WraithTypeToLLVMType(functionSignature->children[1]->type);
LLVMAddFunction(module, functionSignature->children[0]->value.string, returnType);
}
void Compile(LLVMModuleRef module, Node *node)
{
uint32_t i;
switch (node->syntaxKind)
{
case FunctionDeclaration:
CompileFunction(module, node);
break;
}
for (i = 0; i < node->childCount; i += 1)
{
Compile(module, node->children[i]);
}
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Please provide a file.\n");
return 1;
}
stack = CreateStack();
FILE *fp = fopen(argv[1], "r");
yyin = fp;
yyparse(fp, stack);
fclose(fp);
PrintTree(rootNode, 0);
LLVMModuleRef module = LLVMModuleCreateWithName("my_module");
Compile(module, rootNode);
return 0;
}