wraith-lang/compiler.c

82 lines
1.6 KiB
C
Raw Normal View History

2021-04-18 22:29:54 +00:00
#include <stdio.h>
#include <llvm-c/Core.h>
2021-04-18 22:45:06 +00:00
2021-04-18 22:29:54 +00:00
#include "y.tab.h"
2021-04-18 22:45:06 +00:00
#include "ast.h"
2021-04-18 22:29:54 +00:00
#include "stack.h"
extern FILE *yyin;
Stack *stack;
2021-04-18 22:45:06 +00:00
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]);
}
}
2021-04-18 22:29:54 +00:00
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);
2021-04-18 22:45:06 +00:00
PrintTree(rootNode, 0);
LLVMModuleRef module = LLVMModuleCreateWithName("my_module");
Compile(module, rootNode);
2021-04-18 22:29:54 +00:00
return 0;
}