From dcfdfd252ac29ef26f5397c40f1014f6d517a0bc Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 06:36:35 +0100 Subject: [PATCH 01/43] feat: unified hashmaps in header --- src/compiler/compiler.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/compiler/compiler.h b/src/compiler/compiler.h index 7d26c01..fceaca2 100644 --- a/src/compiler/compiler.h +++ b/src/compiler/compiler.h @@ -24,8 +24,7 @@ struct Context { int functionCount; // Hashmaps - struct Hashmap* variableHashMap; - struct Hashmap* functionHashMap; + struct Hashmap* hashMap; }; /** From b7257c22b31fd89b91c8fbdb0bd09258b6c31218 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 06:43:07 +0100 Subject: [PATCH 02/43] feat: started working on ir --- src/compiler/compiler.c | 3 +-- src/compiler/ir.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/compiler/ir.h diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 09b3875..c7ce922 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -34,8 +34,7 @@ struct Context parseContext(struct ASTNode* node) { ctx.variables = malloc(sizeof(struct Variable*) * 50); ctx.functions = malloc(sizeof(struct Function*) * 50); - ctx.variableHashMap = createHashmap(512, 500); - ctx.functionHashMap = createHashmap(512, 500); + ctx.hashMap = createHashmap(512, 500); ctx.variableCount = 0; ctx.functionCount = 0; diff --git a/src/compiler/ir.h b/src/compiler/ir.h new file mode 100644 index 0000000..e3e42a1 --- /dev/null +++ b/src/compiler/ir.h @@ -0,0 +1,34 @@ +/** + * The compiler's internal IR. + */ + +#ifndef IR_H +#define IR_H + +enum IRNodeType { + IR_FUNCTION, + + IR_VARIABLE, + IR_FUNCTION_ARGUMENT, + IR_FUNCTION_BODY_VARIABLE +}; + +typedef IR_TYPE enum IRNodeType; + +struct IRNode { + + IR_TYPE type; + + // Shared Properties + + char* nodeName; + char* type; + + // Variable Properties + char* value; + +} + +typedef IR_NODE struct IRNode; + +#endif From 88039ae9f738c80c60a7c5f2af8dac87b0f88547 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 06:45:56 +0100 Subject: [PATCH 03/43] feat: added ir struct --- src/compiler/ir.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/compiler/ir.h b/src/compiler/ir.h index e3e42a1..cf234c4 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -2,6 +2,8 @@ * The compiler's internal IR. */ +#include "../../utils/hashmap.h" + #ifndef IR_H #define IR_H @@ -27,6 +29,12 @@ struct IRNode { // Variable Properties char* value; + // Function Properties + IR_NODE* variables; + int variableIndex; + + struct Hashmap variableMap; + } typedef IR_NODE struct IRNode; From 78d0536cb8f07e2adb17ccf98ae87ff9b6f6dc11 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:32:14 +0100 Subject: [PATCH 04/43] feat: added IR_CTX struct --- src/compiler/ir.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/compiler/ir.h b/src/compiler/ir.h index cf234c4..64a2024 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -39,4 +39,13 @@ struct IRNode { typedef IR_NODE struct IRNode; +struct IRContext { + IR_NODE* nodes; + int nodeIndex; + + struct Hashmap nodeMap; +}; + +typedef IR_CTX struct IRContext; + #endif From 652d73deb4d74d74017b84a6859d4564ee84633e Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:37:18 +0100 Subject: [PATCH 05/43] feat: removed old compiling & context parsing logic as the remaking of those is imminent --- src/compiler/compiler.c | 123 ---------------------------------------- 1 file changed, 123 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index c7ce922..8be8456 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -11,126 +11,3 @@ #include "../parser/ast.h" #include "../utils/logging.c" -#include "./att/att-win.h" -#include "./att/att-linux.h" - -#include "../utils/hashmap.h" -#include "../utils/hash.h" - -/** - * Assembly format defintions. Will be changed by the architure. - */ -char** ARGUMENT_REGISTRIES = NULL; -char** SECTION_TYPES = NULL; - -/** - * The maximum hash the hashmaps can store. - */ -#define MAX_HASH_CAPACITY 256000 - -struct Context parseContext(struct ASTNode* node) { - struct Context ctx = {0}; - - ctx.variables = malloc(sizeof(struct Variable*) * 50); - ctx.functions = malloc(sizeof(struct Function*) * 50); - - ctx.hashMap = createHashmap(512, 500); - - ctx.variableCount = 0; - ctx.functionCount = 0; - - - - return ctx; - -} - -/** - * Compiles the context down to assembly. - */ -char* compileV2(struct Context context) { - char* firstSection = malloc(1024); - char* sections = malloc(1024); - char* main = malloc(1024); - - firstSection[0] = '\0'; - sections[0] = '\0'; - main[0] = '\0'; - - int sectionIndex = 0; - int stackSize = 0; - - // Platform def - ARGUMENT_REGISTRIES = ATTWIN_ARGUMENT_REGISTRIES; - SECTION_TYPES = ATTWIN_SECTION_TYPES; - - strcat(firstSection, ".LC0:\n .globl main"); - - for(int i = 0; i < context.variableCount; ++i) { - if(context.variables[i]->type[0] == 's') { - if(sectionIndex == 0) { - strcat(firstSection, "\n "); - strcat(firstSection, SECTION_TYPES[0]); - strcat(firstSection, " "); - strcat(firstSection, "\""); - strcat(firstSection, context.variables[i]->value); - strcat(firstSection, "\""); - } - else { - strcat(sections, ".LC"); - - char secI[5]; - snprintf(secI, 5, "%d", sectionIndex); - - strcat(sections, secI); - strcat(sections, ":\n "); - strcat(sections, SECTION_TYPES[0]); - strcat(sections, " "); - strcat(sections, "\""); - strcat(sections, context.variables[i]->value); - strcat(sections, "\""); - } - sectionIndex++; - } - else if(context.variables[i]->type[0] == 'n') { - stackSize += 4; - - strcat(main, "\n movq $"); - strcat(main, context.variables[i]->value); - strcat(main, ", -"); - - char sI[5]; - snprintf(sI, 5, "%d", stackSize); - - strcat(main, sI); - strcat(main, "(%rsp)"); - } - } - - char* buff = malloc(1024); - - buff[0] = '\0'; - - strcat(buff, firstSection); - strcat(buff, sections); - strcat(buff, "\n\nmain:"); - - char size[5]; - snprintf(size, 5, "%d", stackSize); - - if(stackSize > 0) { - strcat(buff, "\n subq $"); - strcat(buff, size); - strcat(buff, ", %rsp"); - } - - strcat(buff, main); - - if(stackSize > 0) { - strcat(buff, "\n addq $"); - strcat(buff, size); - strcat(buff, ", %rsp"); - } - - return buff; -} From c0e7cef5ae8080c3f48a062c7829d0d04b901034 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:40:32 +0100 Subject: [PATCH 06/43] feat: added new ir functions --- src/compiler/compiler.h | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/compiler/compiler.h b/src/compiler/compiler.h index fceaca2..f0506f8 100644 --- a/src/compiler/compiler.h +++ b/src/compiler/compiler.h @@ -2,36 +2,25 @@ * The compiler of Quickfall. */ -#ifndef COMPILER_2_H -#define COMPILER_2_H +#ifndef COMPILER_H +#define COMPILER_H #include "../utils/hashmap.h" #include "../parser/ast.h" -#include "./objects.h" -enum Platform { - ATT_WINDOWS, - ATT_LINUX -}; +#include "./ir.h" /** - * A context is the less abstract way Quickfall represents the code before converting it to assembly. + * Parses the AST tree into a context. + * @param tree the AST tree. */ -struct Context { - struct Variable** variables; - struct Function** functions; - int variableCount; - int functionCount; - - // Hashmaps - struct Hashmap* hashMap; -}; +IR_CTX* makeContext(AST_NODE* tree); /** - * Parses the AST tree into a context. + * Compiles the Context tree to an executable named the provided name. + * @param ctx the IR context. + * @param char the output file name. */ -struct Context parseContext(struct ASTNode* node); - -char* compileV2(struct Context context); +void compile(IR_CTX* ctx, char* outputFileName); #endif From cbdbe43d28e490b39c7218e86b5a046b5376e829 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:41:31 +0100 Subject: [PATCH 07/43] feat: added AST_NODE tree pointer in IR_NODE struct --- src/compiler/ir.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compiler/ir.h b/src/compiler/ir.h index 64a2024..2071fc5 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -35,7 +35,8 @@ struct IRNode { struct Hashmap variableMap; -} + AST_NODE* tree; +}; typedef IR_NODE struct IRNode; From 51a82774c43e1607855e4833e668bc8fb791fce7 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:43:53 +0100 Subject: [PATCH 08/43] feat: removed old compiler depreceated code --- src/compiler/att/att-linux.h | 13 ------------- src/compiler/att/att-win.h | 13 ------------- src/compiler/objects.h | 24 ------------------------ 3 files changed, 50 deletions(-) delete mode 100644 src/compiler/att/att-linux.h delete mode 100644 src/compiler/att/att-win.h delete mode 100644 src/compiler/objects.h diff --git a/src/compiler/att/att-linux.h b/src/compiler/att/att-linux.h deleted file mode 100644 index fb6645e..0000000 --- a/src/compiler/att/att-linux.h +++ /dev/null @@ -1,13 +0,0 @@ -/** - * The Linux-AT & T assembly format. - */ - -/** - * The registries used for function arguments. - */ -char* ATTLINUX_ARGUMENT_REGISTRIES[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"}; - -/** - * The types of different data values that are registered in sections and not the stack. - */ -char* ATTLINUX_SECTION_TYPES[] = {".string"}; diff --git a/src/compiler/att/att-win.h b/src/compiler/att/att-win.h deleted file mode 100644 index d7f8a1f..0000000 --- a/src/compiler/att/att-win.h +++ /dev/null @@ -1,13 +0,0 @@ -/** - * The Windows-AT & T assembly format. - */ - -/** - * The registries used for function arguments. - */ -char* ATTWIN_ARGUMENT_REGISTRIES[] = {"rcx", "rdx", "r8", "r9"}; - -/** - * The types of different data values that are registered in sections and not the stack. - */ -char* ATTWIN_SECTION_TYPES[] = {".ascii"}; diff --git a/src/compiler/objects.h b/src/compiler/objects.h deleted file mode 100644 index 9cd808d..0000000 --- a/src/compiler/objects.h +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Compiling objects such as functions, variables... - */ - -#ifndef COMPILER_OBJECTS_H -#define COMPILER_OBJECTS_H - -#include "../parser/ast.h" - -struct Variable { - char* name; - char* value; - char* type; - unsigned char varType; // 0 = variable, 1 = Func argument, 2 = Function body argument -}; - -struct Function { - char* name; - struct Variable* variables; - int variableCount; - struct ASTNode* body; -}; - -#endif From 4c2831d4d07299bc6d2bf01a9c7579748691081e Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:45:55 +0100 Subject: [PATCH 09/43] fix: cli/main.c imports --- src/cli/main.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cli/main.c b/src/cli/main.c index fa8c7ee..f92db6b 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -9,10 +9,14 @@ #include #include #include -#include "../parser/ast.h" + #include "../lexer/lexer.h" + +#include "../parser/ast.h" #include "../parser/parser.h" + #include "../compiler/compiler.h" +#include "../compiler/ir.h" #include "../utils/logging.c" From 1022438aa679c2d9b805f3b7cf234899ee67a12e Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:47:39 +0100 Subject: [PATCH 10/43] feat: adapted the compiler's & ir usage to the updated version --- src/cli/main.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index f92db6b..754b4ea 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -115,18 +115,14 @@ int main(int argc, char* argv[]) { struct LexerResult result = runLexer(buff); struct ASTNode* root = parseNodes(result, 0, AST_ROOT); - struct Context ctx = parseContext(root); + IR_CTX* ctx = makeContext(root); - char* output = compileV2(ctx); - - if(output == NULL) { - printf("Error: the compiled output is null! Something went wrong!\n"); + if(ctx == NULL) { + printf("Error: the IR context is null! Something went wrong during compiling! Please check any logs for errors\n"); return -1; } - fptr = fopen(outputFile, "w"); - fprintf(fptr, output); - fclose(fptr); + compile(ctx, outputFile); break; case 'v': From 3a1b8b7bf10fef41d7b24ce96d5a9bbc488598a5 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:49:21 +0100 Subject: [PATCH 11/43] fix: fixed type aliases definition for ir structures in IR header --- src/compiler/ir.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compiler/ir.h b/src/compiler/ir.h index 2071fc5..c928a63 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -2,7 +2,7 @@ * The compiler's internal IR. */ -#include "../../utils/hashmap.h" +#include "../utils/hashmap.h" #ifndef IR_H #define IR_H @@ -15,7 +15,7 @@ enum IRNodeType { IR_FUNCTION_BODY_VARIABLE }; -typedef IR_TYPE enum IRNodeType; +typedef enum IRNodeType IR_TYPE; struct IRNode { @@ -30,7 +30,7 @@ struct IRNode { char* value; // Function Properties - IR_NODE* variables; + struct IRNode* variables; int variableIndex; struct Hashmap variableMap; @@ -38,7 +38,7 @@ struct IRNode { AST_NODE* tree; }; -typedef IR_NODE struct IRNode; +typedef struct IRNode IR_NODE; struct IRContext { IR_NODE* nodes; @@ -47,6 +47,6 @@ struct IRContext { struct Hashmap nodeMap; }; -typedef IR_CTX struct IRContext; +typedef struct IRContext IR_CTX; #endif From 0ba2005e06a6f6aaafecbd1894c32a3417778209 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:50:21 +0100 Subject: [PATCH 12/43] fix: includes including an depreceated useless header that was removed prior --- src/compiler/compiler.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 8be8456..5d941b1 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -7,7 +7,8 @@ #include #include "./compiler.h" -#include "./objects.h" +#include "./ir.h" + #include "../parser/ast.h" #include "../utils/logging.c" From d6190fd28d7a75811d9f9be7e56d679a3f49b422 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:51:13 +0100 Subject: [PATCH 13/43] fix: type -> nodeType in IR_NODE IR struct header --- src/compiler/ir.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/ir.h b/src/compiler/ir.h index c928a63..178792d 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -19,7 +19,7 @@ typedef enum IRNodeType IR_TYPE; struct IRNode { - IR_TYPE type; + IR_TYPE nodeType; // Shared Properties From d0a66cbc31b405c66869f409710d35d0d6b1d4a5 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:53:31 +0100 Subject: [PATCH 14/43] chore: removed useless imports in compiler header impl --- src/compiler/compiler.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 5d941b1..62a7cfd 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -2,13 +2,9 @@ * The compiler of Quickfall. */ -#include -#include -#include - #include "./compiler.h" #include "./ir.h" #include "../parser/ast.h" -#include "../utils/logging.c" + From 68e75ff46d0427c9d1c20be18b9799433dcb5e89 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:55:00 +0100 Subject: [PATCH 15/43] feat: added empty sample compiler header func impls --- src/compiler/compiler.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 62a7cfd..7fcf0e0 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -7,4 +7,19 @@ #include "../parser/ast.h" +/** + * Parses the AST tree into a context. + * @param tree the AST tree. + */ +IR_CTX* makeContext(AST_NODE* tree) { + +} + +/** + * Compiles the Context tree to an executable named the provided file name. + * @param ctx the IR context. + * @param char the output file name. + */ +void compile(IR_CTX* ctx, char* outputFileName) { +} From 2d60aa2807585f4756edd7e16a021713e50c21fc Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 10:57:16 +0100 Subject: [PATCH 16/43] feat: added ctx preinit props --- src/compiler/compiler.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 7fcf0e0..2ec5a53 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -2,17 +2,24 @@ * The compiler of Quickfall. */ +#include + #include "./compiler.h" #include "./ir.h" #include "../parser/ast.h" +#include "../utils/hashmap.h" + /** * Parses the AST tree into a context. * @param tree the AST tree. */ IR_CTX* makeContext(AST_NODE* tree) { - + IR_CTX* ctx = malloc(sizeof(IR_CTX)); + + ctx->nodeIndex = 0; + ctx->nodeHashmap = createHashmap(512,200); } /** From 83e8a26c3ff6f4f666fdda39cd1d443ede7bfcb1 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:01:25 +0100 Subject: [PATCH 17/43] feat: added createIRNode to IR header --- src/compiler/compiler.c | 18 ++++++++++++++++++ src/compiler/ir.h | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 2ec5a53..4d301ef 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -18,8 +18,26 @@ IR_CTX* makeContext(AST_NODE* tree) { IR_CTX* ctx = malloc(sizeof(IR_CTX)); + int buffSize = 32; + ctx->nodes = malloc(sizeof(IR_NODE) * 32); + ctx->nodeIndex = 0; ctx->nodeHashmap = createHashmap(512,200); + + while(tree->next != NULL) { + tree = tree->next; + + switch(tree->type) { + case AST_VARIABLE_DECLARATION: + + + + if(ctx->nodeIndex > buffSize) { + buffSize = buffSize * 1.5; + ctx->nodes = realloc(ctx->nodes, buffSize); + } + } + } } /** diff --git a/src/compiler/ir.h b/src/compiler/ir.h index 178792d..ea105f9 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -49,4 +49,11 @@ struct IRContext { typedef struct IRContext IR_CTX; +/** + * Creates an IR node based on the type and the name given. + * @param type the IR type of the node. + * @param nodeName the name of the IR node. + */ +IR_NODE* createIRNode(IR_TYPE type, char* nodeName); + #endif From 807a5a9850e6c66dbe740bc1ec144da61d333f74 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:04:38 +0100 Subject: [PATCH 18/43] feat: added createIRNode impl --- src/compiler/ir.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/compiler/ir.c diff --git a/src/compiler/ir.c b/src/compiler/ir.c new file mode 100644 index 0000000..5c4266a --- /dev/null +++ b/src/compiler/ir.c @@ -0,0 +1,27 @@ +/** + * The compiler's internal IR. + */ + +#include + +#include "../utils/hashmap.h" + +/** + * Creates an IR node based on the type and the name given. + * @param type the IR type of the node. + * @param nodeName the name of the IR node. + */ +IR_NODE* createIRNode(IR_TYPE type, char* nodeName) { + IR_NODE* node = malloc(sizeof(IR_NODE)); + + node->nodeType = type; + node->nodeName = nodeName; + + node->variableIndex = 0; + + if(type == IR_FUNCTION) { + node->variableHashmap = createHashmap(512, 50); + } + + return node; +} From 072fa1c34000cbb05a6b022e4de5a746f1c368a7 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:06:49 +0100 Subject: [PATCH 19/43] feat: changed Hashmap to Hashmap ptr (variableMap) in IR header IR_NODE struct --- src/compiler/ir.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/ir.h b/src/compiler/ir.h index ea105f9..ce0ad47 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -33,7 +33,7 @@ struct IRNode { struct IRNode* variables; int variableIndex; - struct Hashmap variableMap; + struct Hashmap* variableMap; AST_NODE* tree; }; From 6cee74dc2f57c2a74937db3b1c4799977482427e Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:07:48 +0100 Subject: [PATCH 20/43] fix: imports missing for new concepts such as IR --- src/compiler/ir.c | 4 +++- src/compiler/ir.h | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/compiler/ir.c b/src/compiler/ir.c index 5c4266a..8f0d187 100644 --- a/src/compiler/ir.c +++ b/src/compiler/ir.c @@ -4,6 +4,8 @@ #include +#include "./ir.h" + #include "../utils/hashmap.h" /** @@ -20,7 +22,7 @@ IR_NODE* createIRNode(IR_TYPE type, char* nodeName) { node->variableIndex = 0; if(type == IR_FUNCTION) { - node->variableHashmap = createHashmap(512, 50); + node->variableMap = createHashmap(512, 50); } return node; diff --git a/src/compiler/ir.h b/src/compiler/ir.h index ce0ad47..c0cd5e1 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -2,6 +2,8 @@ * The compiler's internal IR. */ +#include "../parser/ast.h" + #include "../utils/hashmap.h" #ifndef IR_H From 57b170c844c26775245b7ef9ac3bc1b737739195 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:08:37 +0100 Subject: [PATCH 21/43] fix: changed typo from ctx->nodeHashmap to ctx->nodeMap --- src/compiler/compiler.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 4d301ef..94a48cf 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -22,7 +22,7 @@ IR_CTX* makeContext(AST_NODE* tree) { ctx->nodes = malloc(sizeof(IR_NODE) * 32); ctx->nodeIndex = 0; - ctx->nodeHashmap = createHashmap(512,200); + ctx->nodeMap = createHashmap(512,200); while(tree->next != NULL) { tree = tree->next; From 87aa30835aa61a09bd3faa822636c0119974bfae Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:09:38 +0100 Subject: [PATCH 22/43] feat: changed type of nodeMap in IR_CTX struct: Hashmap -> Hashmap ptr --- src/compiler/ir.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/ir.h b/src/compiler/ir.h index c0cd5e1..6a1be28 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -46,7 +46,7 @@ struct IRContext { IR_NODE* nodes; int nodeIndex; - struct Hashmap nodeMap; + struct Hashmap* nodeMap; }; typedef struct IRContext IR_CTX; From 1ae7ae466db1fa875d2725309b269796f80edbc4 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:12:59 +0100 Subject: [PATCH 23/43] feat: added wip AST variable declaration -> IR Ctx variable --- src/compiler/compiler.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 94a48cf..5ca7b35 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -29,8 +29,14 @@ IR_CTX* makeContext(AST_NODE* tree) { switch(tree->type) { case AST_VARIABLE_DECLARATION: + IR_NODE* node = createIRNode(IR_VARIABLE, tree->left->value); - + node->type = tree->value; + + if(tree->right != NULL && tree->right->value) node->value = tree->right->value; + + ctx->nodes[ctx->nodeIndex] = node; + ctx->nodeIndex++; if(ctx->nodeIndex > buffSize) { buffSize = buffSize * 1.5; From 63367ebdd2971f7959e204eeeec270fc5e0ab54d Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:16:41 +0100 Subject: [PATCH 24/43] feat: changed some stuff to pointers to allow usage with current compiler structure, todo: potentially convert some thing to normal vars to allow for stack speed --- src/compiler/compiler.c | 3 +++ src/compiler/ir.h | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 5ca7b35..5b0b294 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -9,6 +9,7 @@ #include "../parser/ast.h" +#include "../utils/hash.h" #include "../utils/hashmap.h" /** @@ -38,6 +39,8 @@ IR_CTX* makeContext(AST_NODE* tree) { ctx->nodes[ctx->nodeIndex] = node; ctx->nodeIndex++; + hashPut(ctx->nodeMap, hashstr(node->nodeName), node); + if(ctx->nodeIndex > buffSize) { buffSize = buffSize * 1.5; ctx->nodes = realloc(ctx->nodes, buffSize); diff --git a/src/compiler/ir.h b/src/compiler/ir.h index 6a1be28..bb3fd5f 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -32,7 +32,7 @@ struct IRNode { char* value; // Function Properties - struct IRNode* variables; + struct IRNode** variables; int variableIndex; struct Hashmap* variableMap; @@ -43,7 +43,7 @@ struct IRNode { typedef struct IRNode IR_NODE; struct IRContext { - IR_NODE* nodes; + IR_NODE** nodes; int nodeIndex; struct Hashmap* nodeMap; From c4d6e4efadb7b9751014d5ae866dd3a6ac16470f Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:17:44 +0100 Subject: [PATCH 25/43] feat: made createIRNode inline to remove some waster nanoseconds --- src/compiler/ir.c | 2 +- src/compiler/ir.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/ir.c b/src/compiler/ir.c index 8f0d187..11a2af9 100644 --- a/src/compiler/ir.c +++ b/src/compiler/ir.c @@ -13,7 +13,7 @@ * @param type the IR type of the node. * @param nodeName the name of the IR node. */ -IR_NODE* createIRNode(IR_TYPE type, char* nodeName) { +inline IR_NODE* createIRNode(IR_TYPE type, char* nodeName) { IR_NODE* node = malloc(sizeof(IR_NODE)); node->nodeType = type; diff --git a/src/compiler/ir.h b/src/compiler/ir.h index bb3fd5f..07c53ff 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -56,6 +56,6 @@ typedef struct IRContext IR_CTX; * @param type the IR type of the node. * @param nodeName the name of the IR node. */ -IR_NODE* createIRNode(IR_TYPE type, char* nodeName); +inline IR_NODE* createIRNode(IR_TYPE type, char* nodeName); #endif From 6dbe50745622a9547af5ef85350f759b03d271dc Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:21:33 +0100 Subject: [PATCH 26/43] fix: issues that happened after making function inline --- src/compiler/ir.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/ir.h b/src/compiler/ir.h index 07c53ff..0a0007a 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -56,6 +56,6 @@ typedef struct IRContext IR_CTX; * @param type the IR type of the node. * @param nodeName the name of the IR node. */ -inline IR_NODE* createIRNode(IR_TYPE type, char* nodeName); +inline extern IR_NODE* createIRNode(IR_TYPE type, char* nodeName); #endif From 1c6caa4e27afa774f94a1ff1e2749662ddf04833 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:22:59 +0100 Subject: [PATCH 27/43] feat: added hashmap check before appending a var --- src/compiler/compiler.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 5b0b294..4ab307f 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -30,6 +30,13 @@ IR_CTX* makeContext(AST_NODE* tree) { switch(tree->type) { case AST_VARIABLE_DECLARATION: + + int hash = hashstr(tree->left->value); + + if(hashGet(ctx->nodeMap, hash) != NULL) { + return NULL; + } + IR_NODE* node = createIRNode(IR_VARIABLE, tree->left->value); node->type = tree->value; @@ -39,7 +46,7 @@ IR_CTX* makeContext(AST_NODE* tree) { ctx->nodes[ctx->nodeIndex] = node; ctx->nodeIndex++; - hashPut(ctx->nodeMap, hashstr(node->nodeName), node); + hashPut(ctx->nodeMap, hash, node); if(ctx->nodeIndex > buffSize) { buffSize = buffSize * 1.5; From f13a3a03b5479029b6c80f19ca9df4283b24ca59 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:31:42 +0100 Subject: [PATCH 28/43] feat: added AST function declaration -> IR ctx func --- src/compiler/compiler.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 4ab307f..5042943 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -52,6 +52,36 @@ IR_CTX* makeContext(AST_NODE* tree) { buffSize = buffSize * 1.5; ctx->nodes = realloc(ctx->nodes, buffSize); } + break; + + case AST_FUNCTION_DECLARATION: + + hash = hashstr(tree->left->value); + + if(hashGet(ctx->nodeMap, hash) != NULL) { + return NULL; + } + + node = createIRNode(IR_FUNCTION, tree->left->right->value); + + node->type = tree->left->value; + + while(tree->left->left->next != NULL) { + + IR_NODE* var = createIRNode(IR_FUNCTION_ARGUMENT, tree->right->value); + + node->variables[node->variableIndex] = var; + node->variableIndex++; + + hashPut(node->variableMap, hashstr(tree->right->value), var); + + tree->left->left = tree->left->left->next; + } + + ctx->nodes[ctx->nodeIndex] = node; + ctx->nodeIndex; + + hashPut(ctx->nodeMap, hash, node); } } } From cf380dc7e1878557f8f7f717c3447fcf82819d89 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:44:48 +0100 Subject: [PATCH 29/43] tests: added lexer test header --- tests/lexer.h | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/lexer.h diff --git a/tests/lexer.h b/tests/lexer.h new file mode 100644 index 0000000..c15093b --- /dev/null +++ b/tests/lexer.h @@ -0,0 +1,8 @@ +/** + * Lexer test for Quickfall. + */ + +/** + * Runs the parser test. + */ +extern inline int runParserTest(char* buff); From 7243e55feeb5afb895fc3d17f2a2b2b987b58d97 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:46:49 +0100 Subject: [PATCH 30/43] tests: added lexer test --- tests/lexer.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/lexer.c diff --git a/tests/lexer.c b/tests/lexer.c new file mode 100644 index 0000000..ab69727 --- /dev/null +++ b/tests/lexer.c @@ -0,0 +1,15 @@ +/** + * Lexer test for Quickfall. + */ + +#include "../src/lexer/lexer.h" +#include "../src/lexer/tokens.h" + +/** + * Runs the lexer test. + */ +inline int runParserTest(char* buff) { + struct LexerResult result = runLexer(buff); + + if(result.size == 0) return -1; +} From 54fdd1b7a79c9fb7afe6064f4d96a3b73156a52c Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:48:09 +0100 Subject: [PATCH 31/43] tests: added lexer test header --- tests/ir.c | 38 ++++++++++++++++++++++++++++++++++++++ tests/lexer.c | 2 +- tests/lexer.h | 4 ++-- tests/main.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 tests/ir.c create mode 100644 tests/main.c diff --git a/tests/ir.c b/tests/ir.c new file mode 100644 index 0000000..d39153c --- /dev/null +++ b/tests/ir.c @@ -0,0 +1,38 @@ +/** + * Test to check if the IR is working correctly. + */ + +#include +#include + +#include "../src/lexer/lexer.h" +#include "../src/lexer/tokens.h" + +#include "../src/parser/parser.h" +#include "../src/parser/ast.h" + +#include "../src/compiler/compiler.h" +#include "../src/compiler/ir.h" + +int main(int argc, char** argv) { + + if(argc < 2) { + printf("Usage: \n"); + return -1; + } + + FILE* fptr = fopen(argv[1], "r"); + + if(fptr == NULL) { + return -1; + } + + fseek(fptr, 0, SEEK_END); + int size = ftell(fptr); + fseek(fptr, 0, SEEK_SET); + + char* buff = malloc(size + 1); + + fread(buff, 1, size, fptr); + buff[size] = '\0'; + diff --git a/tests/lexer.c b/tests/lexer.c index ab69727..aaaadeb 100644 --- a/tests/lexer.c +++ b/tests/lexer.c @@ -8,7 +8,7 @@ /** * Runs the lexer test. */ -inline int runParserTest(char* buff) { +inline int runLexerTest(char* buff) { struct LexerResult result = runLexer(buff); if(result.size == 0) return -1; diff --git a/tests/lexer.h b/tests/lexer.h index c15093b..b591bf9 100644 --- a/tests/lexer.h +++ b/tests/lexer.h @@ -3,6 +3,6 @@ */ /** - * Runs the parser test. + * Runs the lexer test. */ -extern inline int runParserTest(char* buff); +extern inline int runLexerTest(char* buff); diff --git a/tests/main.c b/tests/main.c new file mode 100644 index 0000000..014a89c --- /dev/null +++ b/tests/main.c @@ -0,0 +1,42 @@ +/** + * Quickfall's Testing suite. + */ + +#include +#include +#include + +int main(int argc, char** argv) { + if(argc < 3) { + printf("Usage: \0"); + return -1; + } + + FILE* fptr = fopen(argv[1], "w"); + + fseek(fptr, 0, SEEK_END); + int size = ftell(fptr); + fseek(fptr, 0, SEEK_SET); + + char* buff = malloc(size + 1); + + fread(buff, 1, size, fptr); + fclose(fptr); + + buff[size] = '\0'; + + for(int i = 2; i < argv; ++i) { + char* testName = argv[i]; + + + + + if(strcmp(testName, "parser") == 0) { + + } + else if(strcmp(testName, "ir") == 0) { + + } + } + +} From f7259f7662d69d4c28c2e170142b4d79241eae4e Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 11:49:56 +0100 Subject: [PATCH 32/43] tests: implemented parser test --- tests/parser.c | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/tests/parser.c b/tests/parser.c index f453916..7eddba5 100644 --- a/tests/parser.c +++ b/tests/parser.c @@ -12,33 +12,12 @@ void dumpASTTree(AST_NODE* node, int depth); -int main(int argc, char* argv[]) { - if(argc < 2) { - printf("Usage: \n"); - return -1; - } - - FILE* fptr = fopen(argv[1], "r"); - - if(fptr == NULL) { - printf("Couldn't open file!\n"); - return -1; - } - - fseek(fptr, 0, SEEK_END); - int size = ftell(fptr); - fseek(fptr, 0, SEEK_SET); - - char* buff = malloc(size + 1); - - fread(buff, 1, size, fptr); - buff[size] = '\0'; - fclose(fptr); - +inline int runParserTest(char* buff) { struct LexerResult result = runLexer(buff); - struct ASTNode* root = parseNodes(result, 0, AST_ROOT); - dumpASTTree(root, 0); + AST_NODE* root = parseNodes(buff, 0, AST_ROOT); + + dumpASTTree(node, 0); } char* debug[12] = {"Root", "Type Node", "Variable Name", "Variable Value", "Variable Declaration", "Variable Reference", "Function Declaration", "Function Header", "Math Operator", "Math Operation", "Math Operation Header", "Parameter"}; From 7e9d3e5fc1f92b42aa855ddf48dc5e9d75da5759 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 12:03:10 +0100 Subject: [PATCH 33/43] feat: added test suite --- tests/ir.c | 22 ---------------------- tests/lexer.c | 4 +++- tests/lexer.h | 2 +- tests/main.c | 18 +++++++++++++++--- tests/parser.c | 6 +++--- tests/parser.h | 8 ++++++++ 6 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 tests/parser.h diff --git a/tests/ir.c b/tests/ir.c index d39153c..1c4f7d7 100644 --- a/tests/ir.c +++ b/tests/ir.c @@ -14,25 +14,3 @@ #include "../src/compiler/compiler.h" #include "../src/compiler/ir.h" -int main(int argc, char** argv) { - - if(argc < 2) { - printf("Usage: \n"); - return -1; - } - - FILE* fptr = fopen(argv[1], "r"); - - if(fptr == NULL) { - return -1; - } - - fseek(fptr, 0, SEEK_END); - int size = ftell(fptr); - fseek(fptr, 0, SEEK_SET); - - char* buff = malloc(size + 1); - - fread(buff, 1, size, fptr); - buff[size] = '\0'; - diff --git a/tests/lexer.c b/tests/lexer.c index aaaadeb..862cd39 100644 --- a/tests/lexer.c +++ b/tests/lexer.c @@ -8,8 +8,10 @@ /** * Runs the lexer test. */ -inline int runLexerTest(char* buff) { +int runLexerTest(char* buff) { struct LexerResult result = runLexer(buff); if(result.size == 0) return -1; + + return 0; } diff --git a/tests/lexer.h b/tests/lexer.h index b591bf9..bd1cf4d 100644 --- a/tests/lexer.h +++ b/tests/lexer.h @@ -5,4 +5,4 @@ /** * Runs the lexer test. */ -extern inline int runLexerTest(char* buff); +int runLexerTest(char* buff); diff --git a/tests/main.c b/tests/main.c index 014a89c..edbaf44 100644 --- a/tests/main.c +++ b/tests/main.c @@ -6,6 +6,9 @@ #include #include +#include "./lexer.h" +#include "./parser.h" + int main(int argc, char** argv) { if(argc < 3) { printf("Usage: \0"); @@ -28,15 +31,24 @@ int main(int argc, char** argv) { for(int i = 2; i < argv; ++i) { char* testName = argv[i]; + int code = 0; - - + if(strcmp(testName, "lexer") == 0) { + code = runLexerTest(buff); + } if(strcmp(testName, "parser") == 0) { - + code = runParserTest(buff); } else if(strcmp(testName, "ir") == 0) { } + + if(code != -1) { + printf("[OK] %s Test\n", testName); + } + else { + printf("[FAILED] %s Test\n", testName); + } } } diff --git a/tests/parser.c b/tests/parser.c index 7eddba5..ba5986f 100644 --- a/tests/parser.c +++ b/tests/parser.c @@ -12,12 +12,12 @@ void dumpASTTree(AST_NODE* node, int depth); -inline int runParserTest(char* buff) { +int runParserTest(char* buff) { struct LexerResult result = runLexer(buff); - AST_NODE* root = parseNodes(buff, 0, AST_ROOT); + AST_NODE* root = parseNodes(result, 0, AST_ROOT); - dumpASTTree(node, 0); + dumpASTTree(root, 0); } char* debug[12] = {"Root", "Type Node", "Variable Name", "Variable Value", "Variable Declaration", "Variable Reference", "Function Declaration", "Function Header", "Math Operator", "Math Operation", "Math Operation Header", "Parameter"}; diff --git a/tests/parser.h b/tests/parser.h new file mode 100644 index 0000000..905c0f6 --- /dev/null +++ b/tests/parser.h @@ -0,0 +1,8 @@ +/** + * Quickfall's parser test. + */ + +/** + * Runs the parser test. + */ +int runParserTest(char* buff); From a162879bb17931df343d8b125c83667347f1fa02 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 12:07:04 +0100 Subject: [PATCH 34/43] tests: fixed some tests errors --- tests/lexer.c | 4 ++++ tests/main.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/lexer.c b/tests/lexer.c index 862cd39..556b993 100644 --- a/tests/lexer.c +++ b/tests/lexer.c @@ -11,6 +11,10 @@ int runLexerTest(char* buff) { struct LexerResult result = runLexer(buff); + for(int i = 0; i < result.size; ++i) { + printf("Token type: %d\n", result.tokens[i].type); + } + if(result.size == 0) return -1; return 0; diff --git a/tests/main.c b/tests/main.c index edbaf44..e8d3ed1 100644 --- a/tests/main.c +++ b/tests/main.c @@ -15,7 +15,7 @@ int main(int argc, char** argv) { return -1; } - FILE* fptr = fopen(argv[1], "w"); + FILE* fptr = fopen(argv[1], "r"); fseek(fptr, 0, SEEK_END); int size = ftell(fptr); From 31babd86030b869fcfcf807ccda137b89b91d84c Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 12:21:16 +0100 Subject: [PATCH 35/43] bump: test suite --- src/compiler/compiler.c | 3 +++ tests/ir.c | 14 +++++++++++++ tests/ir.h | 5 +++++ tests/main.c | 44 +++++++++++++++++++++++++++-------------- 4 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 tests/ir.h diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 5042943..3f512be 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -2,6 +2,7 @@ * The compiler of Quickfall. */ +#include #include #include "./compiler.h" @@ -34,6 +35,7 @@ IR_CTX* makeContext(AST_NODE* tree) { int hash = hashstr(tree->left->value); if(hashGet(ctx->nodeMap, hash) != NULL) { + printf("Variable %s is already declared!\n", tree->left->value); return NULL; } @@ -59,6 +61,7 @@ IR_CTX* makeContext(AST_NODE* tree) { hash = hashstr(tree->left->value); if(hashGet(ctx->nodeMap, hash) != NULL) { + printf("Function %s was already declared!\n", tree->left->value); return NULL; } diff --git a/tests/ir.c b/tests/ir.c index 1c4f7d7..1972b9a 100644 --- a/tests/ir.c +++ b/tests/ir.c @@ -14,3 +14,17 @@ #include "../src/compiler/compiler.h" #include "../src/compiler/ir.h" +int runIRTest(char* buff) { + struct LexerResult result = runLexer(buff); + AST_NODE* node = parseNodes(result, 0, AST_ROOT); + + IR_CTX* ctx = makeContext(node); + + if(ctx == NULL) return -1; + + printf("CTX dump:\n\n"); + + for(int i = 0; i < ctx->nodeIndex; ++i) { + printf(" %s (type: %d)\n", ctx->nodes[i]->nodeName, ctx->nodes[i]->type); + } +} diff --git a/tests/ir.h b/tests/ir.h new file mode 100644 index 0000000..d38f655 --- /dev/null +++ b/tests/ir.h @@ -0,0 +1,5 @@ +/** + * Quickfall's IR test. + */ + +int runIRTest(char* buff); diff --git a/tests/main.c b/tests/main.c index e8d3ed1..8c8ecd1 100644 --- a/tests/main.c +++ b/tests/main.c @@ -6,8 +6,31 @@ #include #include +#include "../src/utils/logging.c" + #include "./lexer.h" #include "./parser.h" +#include "./ir.h" + +void runTest(char* buff, char* testName) { + int code = 0; + + if(strcmp(testName, "lexer") == 0) { + code = runLexerTest(buff); + } + else if(strcmp(testName, "parser") == 0) { + code = runParserTest(buff); + } + else if(strcmp(testName, "ir") == 0) { + code = runIRTest(buff); + } + else { + printf("Unknown Test!\n"); + } + + if(code == -1) printf("[%sFAILED%s] The test %s failed!\n", TEXT_HRED, RESET, testName); + else printf("[%sOK%s] The test %s was OK!\n", TEXT_HGREEN, RESET, testName); +} int main(int argc, char** argv) { if(argc < 3) { @@ -31,24 +54,15 @@ int main(int argc, char** argv) { for(int i = 2; i < argv; ++i) { char* testName = argv[i]; - int code = 0; - - if(strcmp(testName, "lexer") == 0) { - code = runLexerTest(buff); - } - if(strcmp(testName, "parser") == 0) { - code = runParserTest(buff); - } - else if(strcmp(testName, "ir") == 0) { + if(strcmp(testName, "all") == 0) { + char* tests[] = {"lexer", "parser", "ir"}; - } - - if(code != -1) { - printf("[OK] %s Test\n", testName); + for(int i = 0; i < 3; ++i) { + runTest(buff, tests[i]); + } } else { - printf("[FAILED] %s Test\n", testName); + runTest(buff, testName); } } - } From 226eac8e946cadc00b471b99fa4449da655d9ecf Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 12:27:14 +0100 Subject: [PATCH 36/43] bump: bumped new ir test --- src/compiler/compiler.c | 3 +++ tests/ir.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 3f512be..82be951 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -54,6 +54,7 @@ IR_CTX* makeContext(AST_NODE* tree) { buffSize = buffSize * 1.5; ctx->nodes = realloc(ctx->nodes, buffSize); } + break; case AST_FUNCTION_DECLARATION: @@ -87,6 +88,8 @@ IR_CTX* makeContext(AST_NODE* tree) { hashPut(ctx->nodeMap, hash, node); } } + + return ctx; } /** diff --git a/tests/ir.c b/tests/ir.c index 1972b9a..dfa6b10 100644 --- a/tests/ir.c +++ b/tests/ir.c @@ -25,6 +25,6 @@ int runIRTest(char* buff) { printf("CTX dump:\n\n"); for(int i = 0; i < ctx->nodeIndex; ++i) { - printf(" %s (type: %d)\n", ctx->nodes[i]->nodeName, ctx->nodes[i]->type); + if(ctx->nodes[i]->nodeName != NULL) printf(" %s (type: %d)\n", ctx->nodes[i]->nodeName, ctx->nodes[i]->nodeType); } } From f05f4ddd143403fbcb848e61d2f09e65661fbb1b Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 12:28:02 +0100 Subject: [PATCH 37/43] feat: added IR_ASM_FUNCTION --- src/compiler/ir.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/compiler/ir.h b/src/compiler/ir.h index 0a0007a..54cb88a 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -12,6 +12,8 @@ enum IRNodeType { IR_FUNCTION, + IR_ASM_FUNCTION, + IR_VARIABLE, IR_FUNCTION_ARGUMENT, IR_FUNCTION_BODY_VARIABLE From f7c72d65c7ac294f5b073348c7ef4553633654a0 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 15 Dec 2024 12:34:20 +0100 Subject: [PATCH 38/43] feat: added assembly functions in IR. --- src/compiler/compiler.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 82be951..528f56b 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -86,6 +86,33 @@ IR_CTX* makeContext(AST_NODE* tree) { ctx->nodeIndex; hashPut(ctx->nodeMap, hash, node); + + case ASM_ASM_FUNCTION_DECLARATION: + + hash = strhash(tree->left->right); + + if(hashGet(ctx->nodeMap, hash) != NULL) { + printf("Assembly function %s is already defined!\n"); + return NULL; + } + + IR_NODE* node = createIRNode(IR_ASM_FUNCTION, tree->left->right); + + node->value = tree->value; + + while(tree->left->left->next != NULL) { + + IR_NODE* var = createIRNode(IR_FUNCTION_ARGUMENT, tree->left->left->right->value); + + node->variables[node->variableIndex] = var; + nodes->variableIndex++; + + hashPut(node->variableMap, hashstr(tree->left->left->right->value), var) + + + tree->left->left = tree->left->left->next; + } + } } From 36367267f2a29db9f71179282cdf9c78ba385190 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 18 Dec 2024 00:32:50 +0100 Subject: [PATCH 39/43] bump: some code to fix runtime --- src/compiler/compiler.c | 16 +++++++++------- src/parser/asts/functions.c | 21 ++++++++++++--------- src/parser/parser.c | 1 + tests/ir.c | 2 ++ tests/main.c | 2 +- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 528f56b..bd7552d 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -87,17 +87,17 @@ IR_CTX* makeContext(AST_NODE* tree) { hashPut(ctx->nodeMap, hash, node); - case ASM_ASM_FUNCTION_DECLARATION: - - hash = strhash(tree->left->right); + case AST_ASM_FUNCTION_DECLARATION: + + hash = hashstr(tree->left->right->value); if(hashGet(ctx->nodeMap, hash) != NULL) { printf("Assembly function %s is already defined!\n"); return NULL; } - IR_NODE* node = createIRNode(IR_ASM_FUNCTION, tree->left->right); - + node = createIRNode(IR_ASM_FUNCTION, tree->left->right->value); + node->value = tree->value; while(tree->left->left->next != NULL) { @@ -105,14 +105,16 @@ IR_CTX* makeContext(AST_NODE* tree) { IR_NODE* var = createIRNode(IR_FUNCTION_ARGUMENT, tree->left->left->right->value); node->variables[node->variableIndex] = var; - nodes->variableIndex++; + node->variableIndex++; - hashPut(node->variableMap, hashstr(tree->left->left->right->value), var) + hashPut(node->variableMap, hashstr(tree->left->left->right->value), var); tree->left->left = tree->left->left->next; } + break; + } } diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index e9e0fb9..5745988 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -59,11 +59,8 @@ AST_NODE* parseParameters(struct LexerResult result, int index) { stack++; break; case PAREN_CLOSE: - if(stack != 0) { - root->endingIndex = index; - return root; - } - return NULL; + root->endingIndex = index; + return root; default: return NULL; @@ -147,14 +144,19 @@ AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) { AST_NODE* node = createASTNode(AST_ASM_FUNCTION_DECLARATION); node->left = createASTNode(AST_FUNCTION_HEADER); - - if(result.tokens[index].type != KEYWORD) { + + if(result.tokens[index + 1].type != KEYWORD) { return NULL; } - AST_NODE* params = parseParameters(result, index + 1); + node->left->right = createASTNode(AST_VARIABLE_NAME); + node->left->right->value = result.tokens[index + 1].value; - if(params == NULL) return NULL; + AST_NODE* params = parseParameters(result, index + 2); + + if(params == NULL) { + return NULL; + } node->left->left = params; @@ -189,6 +191,7 @@ AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) { buff[buffIndex] = '\0'; + node->endingIndex = index; node->value = buff; return node; } diff --git a/src/parser/parser.c b/src/parser/parser.c index 15bada7..bd250e7 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -46,6 +46,7 @@ AST_NODE* parseNodes(struct LexerResult result, int index, enum ASTNodeType type break; case ASM_FUNCTION: node = parseASMFunctionDeclaration(result, index); + if(node != NULL) { current->next = node; current = node; diff --git a/tests/ir.c b/tests/ir.c index dfa6b10..e50b919 100644 --- a/tests/ir.c +++ b/tests/ir.c @@ -27,4 +27,6 @@ int runIRTest(char* buff) { for(int i = 0; i < ctx->nodeIndex; ++i) { if(ctx->nodes[i]->nodeName != NULL) printf(" %s (type: %d)\n", ctx->nodes[i]->nodeName, ctx->nodes[i]->nodeType); } + + return 1; } diff --git a/tests/main.c b/tests/main.c index 8c8ecd1..3d46b60 100644 --- a/tests/main.c +++ b/tests/main.c @@ -51,7 +51,7 @@ int main(int argc, char** argv) { buff[size] = '\0'; - for(int i = 2; i < argv; ++i) { + for(int i = 2; i < argc; ++i) { char* testName = argv[i]; if(strcmp(testName, "all") == 0) { From a0fd9b94946a88399bd7a43666abea5fa34f8cf5 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 18 Dec 2024 16:34:22 +0100 Subject: [PATCH 40/43] chore: updated makefile --- Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 994709e..21ef43c 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,9 @@ SRC_DIR = src # The directory containing the benchmarks of Quickfall. BENCH_SRC_DIR = benchmarks +# The directory containg the tests +TESTS_SRC_DIR = tests + # # Output definition # @@ -64,6 +67,9 @@ SOURCES = $(subst src/cli/main.c,,${SRCS}) BENCH_SOURCES = ${SOURCES} $(wildcard ${BENCH_SRC_DIR}/**/*.c) +TSRCS = ${SOURCES} $(wildcard ${TESTS_SRC_DIR}/*.c) +TEST_SOURCES = $(subst tests/main.c,,${TSRCS}) + # # Building logic # @@ -90,5 +96,5 @@ $(BENCH_TARGET): $(COMPILER) $(FLAGS) $(SOURCES) benchmarks/main.c -o $(BENCH_TARGET) $(TEST_TARGET): - $(COMPILER) $(FLAGS) $(SOURCES) tests/parser.c -o $(TEST_TARGET) + $(COMPILER) $(FLAGS) $(TEST_SOURCES) tests/main.c -o $(TEST_TARGET) From f9dfc865aa6c40a99a1648d4ddda50c3cdf9bad2 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 18 Dec 2024 17:14:48 +0100 Subject: [PATCH 41/43] fix: parser fixups --- src/compiler/compiler.c | 7 ++++++- src/parser/asts/functions.c | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index bd7552d..280d288 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -83,7 +83,7 @@ IR_CTX* makeContext(AST_NODE* tree) { } ctx->nodes[ctx->nodeIndex] = node; - ctx->nodeIndex; + ctx->nodeIndex++; hashPut(ctx->nodeMap, hash, node); @@ -113,6 +113,11 @@ IR_CTX* makeContext(AST_NODE* tree) { tree->left->left = tree->left->left->next; } + ctx->nodes[ctx->nodeIndex] = node; + ctx->nodeIndex++; + + hashPut(ctx->nodeMap, hash, node); + break; } diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 5745988..802ce07 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -160,7 +160,7 @@ AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) { node->left->left = params; - index = params->endingIndex; + index = params->endingIndex + 2; int buffSize = 32; int buffIndex = 0; @@ -193,5 +193,6 @@ AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) { node->endingIndex = index; node->value = buff; + return node; } From 47f55b4bd6fc77f0f7d1204f83965f81c7f28f4e Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 18 Dec 2024 17:34:06 +0100 Subject: [PATCH 42/43] feat: added byte support for numbers --- src/lexer/lexer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 650d144..60dc117 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -38,7 +38,7 @@ struct LexerResult runLexer(char* string) { } else if (isdigit(c)) { int numLen = 0; - while(isdigit(c)) { + while(isdigit(c) || c == 'x') { buff[numLen] = c; numLen++; From d050573641e85cbcdadce6ebcfee992b653d993b Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 18 Dec 2024 17:38:39 +0100 Subject: [PATCH 43/43] feat: added byte parsing for asmf bodies --- src/parser/asts/functions.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 802ce07..fea19dc 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -2,6 +2,7 @@ * Function-related AST parsing. */ +#include #include #include "./variables.h" @@ -164,7 +165,7 @@ AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) { int buffSize = 32; int buffIndex = 0; - char* buff = malloc(buffSize); + uint8_t* buff = malloc(sizeof(uint8_t) * buffSize); for(; index <= result.size; ++index) { struct Token t = result.tokens[index]; @@ -177,22 +178,11 @@ AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) { return NULL; } - char c; - while(c = *t.value++) { - buff[buffIndex] = c; - buffIndex++; - - if(buffIndex >= buffSize) { - buffSize *= 1.5; - buff = realloc(buff, buffSize); - } - } + buff[buffIndex] = strtol(t.value, NULL, 16); } - buff[buffIndex] = '\0'; - node->endingIndex = index; - node->value = buff; + node->value = (char*) buff; return node; }