From 392ce41e9734a6e343f2be495338c577ec46bbce Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Sun, 18 Apr 2021 15:45:06 -0700 Subject: [PATCH] initializing compiler structure --- compiler.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- wraith.y | 5 ++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/compiler.c b/compiler.c index e3bac9a..e7f1c9d 100644 --- a/compiler.c +++ b/compiler.c @@ -1,11 +1,60 @@ #include - #include + #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[]) { @@ -22,7 +71,11 @@ int main(int argc, char *argv[]) yyparse(fp, stack); fclose(fp); - LLVMModuleRef mod = LLVMModuleCreateWithName("my_module"); + PrintTree(rootNode, 0); + + LLVMModuleRef module = LLVMModuleCreateWithName("my_module"); + + Compile(module, rootNode); return 0; } diff --git a/wraith.y b/wraith.y index bfad84f..95da9f5 100644 --- a/wraith.y +++ b/wraith.y @@ -15,6 +15,8 @@ void yyerror(FILE *fp, Stack *stack, char *s) extern char *yytext; extern int yylex (void); extern FILE *yyin; + +extern Node *rootNode; %} %define api.value.type {struct Node*} @@ -73,7 +75,8 @@ Program : Declarations declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount); PopStackFrame(stack); - PrintTree(declarationSequence, 0); + + rootNode = declarationSequence; } Type : INT