diff --git a/src/cli/main.c b/src/cli/main.c index 754b4ea..358187b 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "../lexer/lexer.h" @@ -16,6 +17,7 @@ #include "../parser/parser.h" #include "../compiler/compiler.h" +#include "../compiler/pe/pe.h" #include "../compiler/ir.h" #include "../utils/logging.c" @@ -23,6 +25,9 @@ // Version #define VERSION "0.1.0" +uint8_t code_section[6] = { + 0xB8, 0x4C, 0x00, 0x00, 0x00, 0xC3 +}; void showCommandEntry(char* commandName, char* description, int argumentCount, char* argumentNames[], char* argumentDescriptions[]) { printf("\n > %s\n\n %s%sDescription%s: %s\n", commandName, STYLE_BOLD, STYLE_UNDERLINE, RESET, description); @@ -108,11 +113,11 @@ int main(int argc, char* argv[]) { char* buff = malloc(size + 1); // Allocates one more byte for the \0 char. - fread(buff, 1, size, fptr); + size = fread(buff, 1, size, fptr); buff[size] = '\0'; fclose(fptr); - struct LexerResult result = runLexer(buff); + struct LexerResult result = runLexer(buff, size); struct ASTNode* root = parseNodes(result, 0, AST_ROOT); IR_CTX* ctx = makeContext(root); @@ -122,7 +127,10 @@ int main(int argc, char* argv[]) { return -1; } - compile(ctx, outputFile); + fptr = fopen(outputFile, "w"); + compile(ctx, fptr); + + //compilePE(fptr, code_section, sizeof(code_section)); break; case 'v': diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 280d288..5992f9f 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -2,6 +2,7 @@ * The compiler of Quickfall. */ +#include #include #include @@ -26,9 +27,7 @@ IR_CTX* makeContext(AST_NODE* tree) { ctx->nodeIndex = 0; ctx->nodeMap = createHashmap(512,200); - while(tree->next != NULL) { - tree = tree->next; - + while(tree != NULL) { switch(tree->type) { case AST_VARIABLE_DECLARATION: @@ -59,10 +58,10 @@ IR_CTX* makeContext(AST_NODE* tree) { case AST_FUNCTION_DECLARATION: - hash = hashstr(tree->left->value); + hash = hashstr(tree->left->right->value); if(hashGet(ctx->nodeMap, hash) != NULL) { - printf("Function %s was already declared!\n", tree->left->value); + printf("Function %s was already declared!\n", tree->left->right->value); return NULL; } @@ -86,9 +85,9 @@ IR_CTX* makeContext(AST_NODE* tree) { ctx->nodeIndex++; hashPut(ctx->nodeMap, hash, node); + break; case AST_ASM_FUNCTION_DECLARATION: - hash = hashstr(tree->left->right->value); if(hashGet(ctx->nodeMap, hash) != NULL) { @@ -121,6 +120,8 @@ IR_CTX* makeContext(AST_NODE* tree) { break; } + + tree = tree->next; } return ctx; @@ -131,6 +132,45 @@ IR_CTX* makeContext(AST_NODE* tree) { * @param ctx the IR context. * @param char the output file name. */ -void compile(IR_CTX* ctx, char* outputFileName) { +void compile(IR_CTX* ctx, FILE* out) { + + uint8_t* buff = malloc(sizeof(uint8_t) * 512); + int i = 0; + + int h = hashstr("main"); + + if(hashGet(ctx->nodeMap, h) == NULL) { + printf("Error: the main function wasn't defined!\n"); + return; + } + + IR_NODE* node = hashGet(ctx->nodeMap, h); + + if(node->nodeType != IR_FUNCTION) { + printf("Error: main must be a function!\n"); + return; + } + + while(node->tree->next != NULL) { + + if(node->tree->type == AST_FUNCTION_INVOKE) { + int hash = hashstr(node->tree->value); + + AST_NODE* wa = hashGet(ctx->nodeMap, hash); + + if(wa == NULL) { + printf("Error: The %s function doesn't exist!\n", node->tree->value); + return; + } + + if(wa->type == IR_ASM_FUNCTION) { + printf("%d", wa->value); + } + } + + node->tree = node->tree->next; + } + + } diff --git a/src/compiler/compiler.h b/src/compiler/compiler.h index f0506f8..93ec267 100644 --- a/src/compiler/compiler.h +++ b/src/compiler/compiler.h @@ -19,8 +19,8 @@ IR_CTX* makeContext(AST_NODE* tree); /** * Compiles the Context tree to an executable named the provided name. * @param ctx the IR context. - * @param char the output file name. + * @param char the output file. */ -void compile(IR_CTX* ctx, char* outputFileName); +void compile(IR_CTX* ctx, FILE* outputFileName); #endif diff --git a/src/compiler/ir.h b/src/compiler/ir.h index 54cb88a..bec18d7 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -49,6 +49,9 @@ struct IRContext { int nodeIndex; struct Hashmap* nodeMap; + + IR_NODE* mainFunc; + }; typedef struct IRContext IR_CTX; diff --git a/src/parser/ast.h b/src/parser/ast.h index d801728..9c9c916 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -25,7 +25,9 @@ enum ASTNodeType { AST_FUNCTION_HEADER, AST_FUNCTION_INVOKE, - + + AST_FUNCTION_ROOT, + AST_MATH_OPERATOR, AST_MATH_OPERATION, AST_MATH_OP_HEADER, diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 0f1e164..12aec00 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -62,7 +62,10 @@ AST_NODE* parseParameters(struct LexerResult result, int index) { case PAREN_CLOSE: root->endingIndex = index; return root; + case PAREN_OPEN: + continue; default: + printf("Type: %d", t.type); return NULL; } @@ -109,6 +112,7 @@ AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index) { node->left = createASTNode(AST_FUNCTION_HEADER); if(result.tokens[index].type != KEYWORD) { + printf("jd"); return NULL; } @@ -136,7 +140,9 @@ AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index) { node->left->left = params; - node->right = parseNodes(result, params->endingIndex, AST_ROOT); + node->right = parseNodes(result, params->endingIndex, AST_FUNCTION_ROOT); + + node->endingIndex = node->right->endingIndex; return node; } @@ -199,4 +205,4 @@ AST_NODE* parseFunctionInvoke(struct LexerResult result, int index) { node->endingIndex = args->endingIndex; return node; -} \ No newline at end of file +} diff --git a/src/parser/parser.c b/src/parser/parser.c index ed6960a..169e1d8 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -27,8 +27,14 @@ AST_NODE* parseNodes(struct LexerResult result, int index, enum ASTNodeType type AST_NODE* node = NULL; switch(t.type) { + case BRACKETS_CLOSE: + if(type == AST_FUNCTION_ROOT) { + root->endingIndex = index; + return root; + } + break; case FUNCTION: - node = parseFunctionDeclaration(result, index); + node = parseFunctionDeclaration(result, index + 1); if(node != NULL) { current->next = node; current = node; diff --git a/src/utils/hashmap.c b/src/utils/hashmap.c index a6cfb1c..fd2f351 100644 --- a/src/utils/hashmap.c +++ b/src/utils/hashmap.c @@ -2,6 +2,7 @@ * A simple hashmap implementation. */ +#include #include #include "./hashmap.h"