From e031566a0182180581764ac8c5b8a313df9ae599 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:04:30 +0100 Subject: [PATCH 001/121] feat: started working on actual IR --- src/ir/ir.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/ir/ir.h diff --git a/src/ir/ir.h b/src/ir/ir.h new file mode 100644 index 0000000..95069b8 --- /dev/null +++ b/src/ir/ir.h @@ -0,0 +1,20 @@ +/** + * The Intermediate Representation of Quickfall Code. + */ + +#ifndef IR2_H +#define IR2_H + +/** + * An IR instruction. + */ +typedef struct { + + unsigned char* opCode; + + void* params; + int paramCount; + +} IR_INSTRUCTION; + +#endif \ No newline at end of file From 32f1dd08d960578a384dc866e34eb36d1f0d3d7f Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:06:30 +0100 Subject: [PATCH 002/121] feat: added block struct --- src/ir/ir.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/ir/ir.h b/src/ir/ir.h index 95069b8..d0f29fd 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -17,4 +17,14 @@ typedef struct { } IR_INSTRUCTION; +/** + * An IR basic block. + */ +typedef struct { + + IR_INSTRUCTION* instructions; + int instructionCount; + +} IR_BASIC_BLOCK; + #endif \ No newline at end of file From e8c280f9d9328b1b50af89d81f3e389a83795861 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:08:40 +0100 Subject: [PATCH 003/121] feat: removed old IR --- src/compiler/compiler.c | 165 +--------------------------------------- src/compiler/compiler.h | 10 +-- src/compiler/ir.c | 29 ------- src/compiler/ir.h | 67 ---------------- 4 files changed, 2 insertions(+), 269 deletions(-) delete mode 100644 src/compiler/ir.c delete mode 100644 src/compiler/ir.h diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 7081887..a37f77a 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -16,173 +16,10 @@ #include "./pe/pe.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)); - - int buffSize = 32; - ctx->nodes = malloc(sizeof(IR_NODE) * 32); - - ctx->nodeIndex = 0; - ctx->nodeMap = createHashmap(512,200); - - while(tree != NULL) { - switch(tree->type) { - case AST_VARIABLE_DECLARATION: - - int hash = hashstr(tree->left->value); - - if(hashGet(ctx->nodeMap, hash) != NULL) { - printf("Variable %s is already declared!\n", tree->left->value); - return NULL; - } - - 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++; - - hashPut(ctx->nodeMap, hash, node); - - if(ctx->nodeIndex > buffSize) { - buffSize = buffSize * 1.5; - ctx->nodes = realloc(ctx->nodes, buffSize); - } - - break; - - case AST_FUNCTION_DECLARATION: - - hash = hashstr(tree->left->right->value); - - if(hashGet(ctx->nodeMap, hash) != NULL) { - printf("Function %s was already declared!\n", tree->left->right->value); - 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->left->left->right->value), var); - - tree->left->left = tree->left->left->next; - } - - node->tree = tree->right; - - ctx->nodes[ctx->nodeIndex] = node; - 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) { - printf("Assembly function %s is already defined!\n"); - return NULL; - } - - node = createIRNode(IR_ASM_FUNCTION, tree->left->right->value); - - node->value = tree->value; - node->valueSize = tree->valueSize; - - while(tree->left->left->next != NULL) { - - IR_NODE* var = createIRNode(IR_FUNCTION_ARGUMENT, tree->left->left->right->value); - - node->variables[node->variableIndex] = var; - node->variableIndex++; - - hashPut(node->variableMap, hashstr(tree->left->left->right->value), var); - - - tree->left->left = tree->left->left->next; - } - - ctx->nodes[ctx->nodeIndex] = node; - ctx->nodeIndex++; - - hashPut(ctx->nodeMap, hash, node); - - break; - - } - - tree = tree->next; - } - - return ctx; -} - /** * Compiles the Context tree to an executable named the provided file name. * @param ctx the IR context. * @param out the output file. */ -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 != NULL) { - - if(node->tree->type == AST_FUNCTION_INVOKE) { - - int hash = hashstr(node->tree->value); - - IR_NODE* wa = hashGet(ctx->nodeMap, hash); - - if(wa == NULL) { - printf("Error: The %s function doesn't exist!\n", node->tree->value); - return; - } - - if(wa->nodeType == IR_ASM_FUNCTION) { - unsigned char b; - unsigned char* ptr = (unsigned char*) wa->value; - - for(int ii = 0; ii < wa->valueSize; ++ii) { - buff[i] = ptr[ii]; - ++i; - } - } - } - - node->tree = node->tree->next; - } - - //todo: change format based on user - compilePE(out, buff, i); +void compile(FILE* out) { } diff --git a/src/compiler/compiler.h b/src/compiler/compiler.h index 93ec267..d998191 100644 --- a/src/compiler/compiler.h +++ b/src/compiler/compiler.h @@ -8,19 +8,11 @@ #include "../utils/hashmap.h" #include "../parser/ast.h" -#include "./ir.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 name. * @param ctx the IR context. * @param char the output file. */ -void compile(IR_CTX* ctx, FILE* outputFileName); +void compile(FILE* outputFileName); #endif diff --git a/src/compiler/ir.c b/src/compiler/ir.c deleted file mode 100644 index 11a2af9..0000000 --- a/src/compiler/ir.c +++ /dev/null @@ -1,29 +0,0 @@ -/** - * The compiler's internal IR. - */ - -#include - -#include "./ir.h" - -#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. - */ -inline 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->variableMap = createHashmap(512, 50); - } - - return node; -} diff --git a/src/compiler/ir.h b/src/compiler/ir.h deleted file mode 100644 index 43c6e59..0000000 --- a/src/compiler/ir.h +++ /dev/null @@ -1,67 +0,0 @@ -/** - * The compiler's internal IR. - */ - -#include "../parser/ast.h" - -#include "../utils/hashmap.h" - -#ifndef IR_H -#define IR_H - -typedef enum { - IR_FUNCTION, - - IR_ASM_FUNCTION, - - IR_VARIABLE, - IR_FUNCTION_ARGUMENT, - IR_FUNCTION_BODY_VARIABLE -} IR_TYPE; - -/** - * An IR Node. - */ -typedef struct IR_NODE { - - IR_TYPE nodeType; - - // Shared Properties - - char* nodeName; - char* type; - - // Variable Properties - void* value; - int valueSize; - - // Function Properties - struct IR_NODE** variables; - int variableIndex; - - struct Hashmap* variableMap; - - AST_NODE* tree; -} IR_NODE; - -/** - * The overall IR context. - */ -typedef struct { - IR_NODE** nodes; - int nodeIndex; - - struct Hashmap* nodeMap; - - IR_NODE* mainFunc; - -} 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. - */ -inline extern IR_NODE* createIRNode(IR_TYPE type, char* nodeName); - -#endif From e57648a8bcbf986ebfc902243c4413227b91e960 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:13:31 +0100 Subject: [PATCH 004/121] feat: moved structures into struct header --- src/ir/ir.h | 22 ---------------------- src/ir/structs.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 src/ir/structs.h diff --git a/src/ir/ir.h b/src/ir/ir.h index d0f29fd..68b629b 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -5,26 +5,4 @@ #ifndef IR2_H #define IR2_H -/** - * An IR instruction. - */ -typedef struct { - - unsigned char* opCode; - - void* params; - int paramCount; - -} IR_INSTRUCTION; - -/** - * An IR basic block. - */ -typedef struct { - - IR_INSTRUCTION* instructions; - int instructionCount; - -} IR_BASIC_BLOCK; - #endif \ No newline at end of file diff --git a/src/ir/structs.h b/src/ir/structs.h new file mode 100644 index 0000000..059b563 --- /dev/null +++ b/src/ir/structs.h @@ -0,0 +1,30 @@ +/** + * Quickfall IR Structures. + */ + +#ifndef IR_STRUCTS_H +#define IR_STRUCTS_H + +/** + * An IR instruction. + */ +typedef struct { + + unsigned char* opCode; + + void* params; + int paramCount; + +} IR_INSTRUCTION; + +/** + * An IR basic block. + */ +typedef struct { + + IR_INSTRUCTION* instructions; + int instructionCount; + +} IR_BASIC_BLOCK; + +#endif \ No newline at end of file From 331e6043f16c7fa377378e4467d1e6023ea7eb37 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:14:20 +0100 Subject: [PATCH 005/121] feat: added IR functions --- src/ir/structs.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ir/structs.h b/src/ir/structs.h index 059b563..5b3c5af 100644 --- a/src/ir/structs.h +++ b/src/ir/structs.h @@ -27,4 +27,16 @@ typedef struct { } IR_BASIC_BLOCK; +/** + * An IR function. + */ +typedef struct { + + char* funcName; + + IR_BASIC_BLOCK* blocks; + int blockCount; + +} IR_FUNCTION; + #endif \ No newline at end of file From fa95e0293e85f9ebce1e4e3ccbe729b8fe8c613b Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:16:22 +0100 Subject: [PATCH 006/121] feat: added appendInstruction in header --- src/ir/ir.h | 11 +++++++++++ src/ir/structs.h | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ir/ir.h b/src/ir/ir.h index 68b629b..f585547 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -5,4 +5,15 @@ #ifndef IR2_H #define IR2_H +#include "./structs.h" + +/** + * Appends an IR instruction into the basic block. + * @parma block the IR basic block. + * @param opCode the operation code of the instruction. + * @param params the parameters of the operation. + * @param paramsCount the count of the parameters of the operation. + */ +void appendInstruction(IR_BASIC_BLOCK block, unsigned char opCode, void* params, int paramsCount); + #endif \ No newline at end of file diff --git a/src/ir/structs.h b/src/ir/structs.h index 5b3c5af..6882255 100644 --- a/src/ir/structs.h +++ b/src/ir/structs.h @@ -10,7 +10,7 @@ */ typedef struct { - unsigned char* opCode; + unsigned char opCode; void* params; int paramCount; From 1c2b8e5516c0ab5b0b9de38eb70af1896d9c5e6f Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:23:50 +0100 Subject: [PATCH 007/121] feat: added BLOCK_SWAP ir instruction --- src/ir/instructions.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/ir/instructions.h diff --git a/src/ir/instructions.h b/src/ir/instructions.h new file mode 100644 index 0000000..213f4d2 --- /dev/null +++ b/src/ir/instructions.h @@ -0,0 +1,17 @@ +/** + * The instruction codes for the Quickfall IR. + */ + +#ifndef IR_INSTRUCTIONS +#define IR_INSTRUCTIONS + +/** + * The instruction codes of IR. + */ +typedef enum { + + BLOCK_SWAP + +} IR_INSTRUCTION_CODES; + +#endif \ No newline at end of file From 6d2773187196fd9db86a088e771ab7a6e8f09c02 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:24:10 +0100 Subject: [PATCH 008/121] feat: added COND_BLOCK_SWAP ir instruction --- src/ir/instructions.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 213f4d2..98a56cd 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -10,7 +10,8 @@ */ typedef enum { - BLOCK_SWAP + BLOCK_SWAP, + COND_BLOCK_SWAP } IR_INSTRUCTION_CODES; From 4a3152562d109281a8afb57c76d44eb8c3f3b8bb Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:24:49 +0100 Subject: [PATCH 009/121] feat: added LOGICAL_BLOCK_SWAP ir instruction --- src/ir/instructions.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 98a56cd..bc9371a 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -11,7 +11,8 @@ typedef enum { BLOCK_SWAP, - COND_BLOCK_SWAP + COND_BLOCK_SWAP, + LOGICAL_BLOCK_SWAP, } IR_INSTRUCTION_CODES; From 8f71b1cfd54b04ccae894ae87148d42ff596e2a1 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:37:33 +0100 Subject: [PATCH 010/121] feat: added S_ALLOC ir instruction --- src/ir/instructions.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index bc9371a..8f9ecab 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -14,6 +14,8 @@ typedef enum { COND_BLOCK_SWAP, LOGICAL_BLOCK_SWAP, + S_ALLOC, + } IR_INSTRUCTION_CODES; #endif \ No newline at end of file From d6e348d4a1edd3b1cef3fef070f7a1d2b7928236 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:38:17 +0100 Subject: [PATCH 011/121] feat: added PTR_SET ir instruction --- src/ir/instructions.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 8f9ecab..79c094c 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -15,6 +15,7 @@ typedef enum { LOGICAL_BLOCK_SWAP, S_ALLOC, + PTR_SET, } IR_INSTRUCTION_CODES; From 3c874a789f6b7cf4da582520a9ea1d37a1cd7eb1 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:38:37 +0100 Subject: [PATCH 012/121] feat: added PTR_LOAD ir instruction --- src/ir/instructions.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 79c094c..35f4947 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -16,6 +16,7 @@ typedef enum { S_ALLOC, PTR_SET, + PTR_LOAD } IR_INSTRUCTION_CODES; From 445a6c00f04340aab50b2880e786e8c6f95de3d6 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:41:09 +0100 Subject: [PATCH 013/121] feat: added IADD ir instruction --- src/ir/instructions.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 35f4947..842a998 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -16,7 +16,9 @@ typedef enum { S_ALLOC, PTR_SET, - PTR_LOAD + PTR_LOAD, + + IADD } IR_INSTRUCTION_CODES; From 6606e88d9dfafcd04fed9c630547cb3afa575428 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:41:26 +0100 Subject: [PATCH 014/121] feat: added ISUB ir instruction --- src/ir/instructions.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 842a998..d565ee3 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -18,7 +18,8 @@ typedef enum { PTR_SET, PTR_LOAD, - IADD + IADD, + ISUB } IR_INSTRUCTION_CODES; From 639185b0d45a30138351cb22f7d749769cdd341a Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:41:53 +0100 Subject: [PATCH 015/121] feat: added IMUL ir instruction --- src/ir/instructions.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index d565ee3..8581c81 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -19,7 +19,8 @@ typedef enum { PTR_LOAD, IADD, - ISUB + ISUB, + IMUL } IR_INSTRUCTION_CODES; From d4e574da7b4f2b1334c4953586c946a59051a8f3 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:43:30 +0100 Subject: [PATCH 016/121] feat: added IDIV ir instruction --- src/ir/instructions.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 8581c81..04321cd 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -20,7 +20,8 @@ typedef enum { IADD, ISUB, - IMUL + IMUL, + IDIV } IR_INSTRUCTION_CODES; From 42cf34205b7d92e5b77b4da67e9a0859a29405e0 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:45:01 +0100 Subject: [PATCH 017/121] feat: added ICMP --- src/ir/instructions.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 04321cd..0fc340d 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -21,7 +21,9 @@ typedef enum { IADD, ISUB, IMUL, - IDIV + IDIV, + + ICOMP } IR_INSTRUCTION_CODES; From 657cd905ea71b9af145fc0d390af99f536730ce9 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:45:22 +0100 Subject: [PATCH 018/121] feat: added ICMP_H --- src/ir/instructions.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 0fc340d..d2b9b0c 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -23,7 +23,8 @@ typedef enum { IMUL, IDIV, - ICOMP + ICMP, + ICMP_H, } IR_INSTRUCTION_CODES; From b9a4da6660591e6b3564e143296a17dd6625dc2e Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:45:37 +0100 Subject: [PATCH 019/121] feat: added ICMP_L --- src/ir/instructions.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index d2b9b0c..0dd55ee 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -25,6 +25,7 @@ typedef enum { ICMP, ICMP_H, + ICMP_L, } IR_INSTRUCTION_CODES; From 1f4a3238597e0a930e40e19d3d997011748e1a5a Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:48:33 +0100 Subject: [PATCH 020/121] feat: added PRM_PUSH --- src/ir/instructions.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 0dd55ee..9c524f5 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -27,6 +27,8 @@ typedef enum { ICMP_H, ICMP_L, + PRM_PUSH + } IR_INSTRUCTION_CODES; #endif \ No newline at end of file From ad1f511d52618d65519753ea086b821c3a1c8291 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:49:34 +0100 Subject: [PATCH 021/121] feat: added CALL --- src/ir/instructions.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 9c524f5..4a0e48d 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -27,7 +27,8 @@ typedef enum { ICMP_H, ICMP_L, - PRM_PUSH + PRM_PUSH, + CALL } IR_INSTRUCTION_CODES; From 717d721ed2b89ac29e6d0e4727164bde759280d1 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:49:46 +0100 Subject: [PATCH 022/121] feat: added RET --- src/ir/instructions.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 4a0e48d..ef6447e 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -28,7 +28,8 @@ typedef enum { ICMP_L, PRM_PUSH, - CALL + CALL, + RET } IR_INSTRUCTION_CODES; From 9c6f8555b8416144a2a7ae9f3feb8775233bd540 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:52:56 +0100 Subject: [PATCH 023/121] feat: added docs for block swapping instructions --- src/ir/instructions.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index ef6447e..833eda4 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -10,10 +10,28 @@ */ typedef enum { + /** + * Swaps the current IR block. + * @param block the new IR block index in the context (function for instance). + */ BLOCK_SWAP, + + /** + * Swaps the current IR block if a condition is met. + * @param block the new IR block index in the context (function for instance). + * @param cond the condition to match. + */ COND_BLOCK_SWAP, + + /** + * Swaps the current IR block depending on a condition result. + * @param trueBlock the new IR block index in the context (function for instance) if the condition is met. + * @param falseBlock the new IR block index in the context (function for instance) if the condition isn't met. + * @param cond the condition. + */ LOGICAL_BLOCK_SWAP, + S_ALLOC, PTR_SET, PTR_LOAD, From 2f4fb5328d4ef1a7861b7f9fb752389679d61315 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:56:25 +0100 Subject: [PATCH 024/121] feat: added docs for memory instructions --- src/ir/instructions.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 833eda4..b686abb 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -31,9 +31,25 @@ typedef enum { */ LOGICAL_BLOCK_SWAP, - + /** + * Allocates a set amount of bits in the stack. + * @param ptr the pointer that is going to be allocated. + * @param size the size of the pointer. + */ S_ALLOC, + + /** + * Sets the value of the pointer's address to the provided element. + * @param val the new value (an integer for now). + * @param ptr the pointer containing the target address. + */ PTR_SET, + + /** + * Loads the values of a specific address into a variable. + * @param var the output variable. + * @param ptr the pointer containing the target address. + */ PTR_LOAD, IADD, From 3f5d8c1726f7c2a9214d757985d222696b893280 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 15:59:10 +0100 Subject: [PATCH 025/121] feat: added docs for artimethrics --- src/ir/instructions.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index b686abb..9c1509e 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -52,11 +52,40 @@ typedef enum { */ PTR_LOAD, + + /** + * Adds two 32 bit integers together. + * @param output the output variable of the result. + * @param i1 the first integer. + * @param i2 the second integer. + */ IADD, + + /** + * Subtracts two 32 bit integers together. + * @param output the output variable of the result. + * @param i1 the first integer. + * @param i2 the second integer. + */ ISUB, + + /** + * Multiplies two 32 bit integers together. + * @param output the output variable of the result. + * @param i1 the first integer. + * @param i2 the second integer. + */ IMUL, + + /** + * Divides two 32 bit integers together. + * @param output the output variable of the result. + * @param i1 the first integer. + * @param i2 the second integer. + */ IDIV, + ICMP, ICMP_H, ICMP_L, From 552e3bf8489ec38c99a6971492652cc692e9a67e Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 16:03:24 +0100 Subject: [PATCH 026/121] feat: added docs for comparing instructions --- src/ir/instructions.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 9c1509e..be4d117 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -85,11 +85,31 @@ typedef enum { */ IDIV, - + /** + * Compares two 32 bit integers to check if they are equal. + * @param out the output variable containing the result. + * @param i1 the first integer. + * @param i2 the second integer. + */ ICMP, + + /** + * Compares two 32 bit integers to check if the first one is higher than the second one. + * @param out the output variable containing the result. + * @param i1 the first integer. + * @param i2 the second integer. + */ ICMP_H, + + /** + * Compares two 32 bit integers to check if the first one is higher or equal to the second one. + * @param out the output variable containing the result. + * @param i1 the first integer. + * @param i2 the second integer. + */ ICMP_L, + PRM_PUSH, CALL, RET From 639af9a26b74cd64dd6ba2668792b28523b3484b Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 16:05:25 +0100 Subject: [PATCH 027/121] feat: finished adding docs to instructions --- src/ir/instructions.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index be4d117..9e6e9c1 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -109,9 +109,23 @@ typedef enum { */ ICMP_L, - + /** + * Moves a variable value into the parameter registers. + * @param var the variable holding the value. + * @param index the index of the parameter to register to. + */ PRM_PUSH, + + /** + * Calls a function. + * @param funcName the functionName. + */ CALL, + + /** + * Returns from a function. + * @param value the outputValue. + */ RET } IR_INSTRUCTION_CODES; From cfabebd601285244acf109c6e86e17afa31eaf65 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 16:06:08 +0100 Subject: [PATCH 028/121] feat: added RET_PUSH and removed argument of RET --- src/ir/instructions.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 9e6e9c1..c9e457e 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -116,6 +116,12 @@ typedef enum { */ PRM_PUSH, + /** + * Moves a variable value into the return value register. + * @param var the variable holding the value. + */ + RET_PUSH, + /** * Calls a function. * @param funcName the functionName. @@ -124,7 +130,6 @@ typedef enum { /** * Returns from a function. - * @param value the outputValue. */ RET From 2968c71be1ec70e5a7f8f46d8bff607ddb60e73f Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 16:14:41 +0100 Subject: [PATCH 029/121] feat: added enum in appendInstruction --- src/ir/instructions.h | 2 +- src/ir/ir.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index c9e457e..76c06f7 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -133,6 +133,6 @@ typedef enum { */ RET -} IR_INSTRUCTION_CODES; +} IR_INSTRUCTION_CODE; #endif \ No newline at end of file diff --git a/src/ir/ir.h b/src/ir/ir.h index f585547..dadddea 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -5,6 +5,7 @@ #ifndef IR2_H #define IR2_H +#include "./instructions.h" #include "./structs.h" /** @@ -14,6 +15,6 @@ * @param params the parameters of the operation. * @param paramsCount the count of the parameters of the operation. */ -void appendInstruction(IR_BASIC_BLOCK block, unsigned char opCode, void* params, int paramsCount); +void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, void* params, int paramsCount); #endif \ No newline at end of file From 5af741cfe2bd995e51caefe9515b3a3956270a6e Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 16:18:01 +0100 Subject: [PATCH 030/121] feat: added parseFunction in header --- src/ir/ir.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ir/ir.h b/src/ir/ir.h index dadddea..296fe2a 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -17,4 +17,10 @@ */ void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, void* params, int paramsCount); +/** + * Parses a AST function into IR. + * @param node the AST node representing the function. + */ +IR_FUNCTION parseFunction(AST_NODE* node); + #endif \ No newline at end of file From c3b912ca04941d4faf7c9db8d6b7b90f70ad58af Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 16:25:12 +0100 Subject: [PATCH 031/121] feat: added implementation of appendInstruction --- src/ir/ir.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/ir/ir.c diff --git a/src/ir/ir.c b/src/ir/ir.c new file mode 100644 index 0000000..94ca751 --- /dev/null +++ b/src/ir/ir.c @@ -0,0 +1,24 @@ +/** + * The Intermediate Representation of Quickfall Code. + */ + +#include "./instructions.h" +#include "./structs.h" + +/** + * Appends an IR instruction into the basic block. + * @parma block the IR basic block. + * @param opCode the operation code of the instruction. + * @param params the parameters of the operation. + * @param paramsCount the count of the parameters of the operation. + */ +void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, void* params, int paramsCount) { + IR_INSTRUCTION instruction = {0}; + + instruction.opCode = code; + instruction.params = params; + instruction.paramCount = paramsCount; + + block.instructions[block.instructionCount] = instruction; + block.instructionCount++; +} \ No newline at end of file From 6340c63e03e16b6bc7ae4fdc73c17cca26665fd8 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 25 Dec 2024 16:25:45 +0100 Subject: [PATCH 032/121] fix: missing include --- src/ir/ir.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ir/ir.h b/src/ir/ir.h index 296fe2a..446e342 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -8,6 +8,8 @@ #include "./instructions.h" #include "./structs.h" +#include "../../parser/ast.h" + /** * Appends an IR instruction into the basic block. * @parma block the IR basic block. From 01b46b99813698b73a091ed236b0f99919909618 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 12:47:27 +0100 Subject: [PATCH 033/121] feat: added basic AST -> IR function (variable dec) --- src/ir/ir.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++- src/ir/ir.h | 2 +- src/ir/structs.h | 3 ++- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/ir/ir.c b/src/ir/ir.c index 94ca751..145ea37 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -2,9 +2,14 @@ * The Intermediate Representation of Quickfall Code. */ +#include +#include + #include "./instructions.h" #include "./structs.h" +#include "../../parser/ast.h" + /** * Appends an IR instruction into the basic block. * @parma block the IR basic block. @@ -12,13 +17,72 @@ * @param params the parameters of the operation. * @param paramsCount the count of the parameters of the operation. */ -void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, void* params, int paramsCount) { +void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned char params[], int paramsCount) { IR_INSTRUCTION instruction = {0}; instruction.opCode = code; instruction.params = params; instruction.paramCount = paramsCount; + + if(block.instructions == NULL) { + block.instructions = malloc(sizeof(IR_INSTRUCTION) * 20); + block.allocatedSize = 20; + } + block.instructions[block.instructionCount] = instruction; block.instructionCount++; +} + +/** + * Parses a AST function into IR. + * @param node the AST node representing the function. + */ +IR_FUNCTION parseFunction(AST_NODE* node) { + IR_FUNCTION func = {0}; + func.blocks = malloc(sizeof(IR_BASIC_BLOCK)); + func.blockCount++; + + func.blocks[0].instructions = NULL; + func.blocks[0].instructionCount = 0; + + while(node != NULL) { + + switch(node->type) { + case AST_VARIABLE_DECLARATION: + // For now let's just say that every variable is 32 bits. + //todo: change this when typing gets added. + + int size = strlen(node->left->value) + sizeof(int); + + unsigned char* ptr = malloc(size); + + int i = 0; + char c; + + while(c = *node->left->value++) { + ptr[i] = c; + ++i; + + if(c == '\0') break; + } + + ptr[i] = (32 >> 24) & 0xFF; + ptr[i + 1] = (32 >> 16) & 0xFF; + ptr[i + 2] = (32 >> 8) & 0xFF; + ptr[i + 3] = 32 & 0xFF; + + appendInstruction(func.blocks[0], S_ALLOC, ptr, size); + + ptr = malloc(sizeof(int)); + ((int*)ptr)[0] = atoi(node->right->value); + + appendInstruction(func.blocks[0], PTR_SET, ptr, sizeof(int)); + break; + } + + node = node->next; + } + + return func; } \ No newline at end of file diff --git a/src/ir/ir.h b/src/ir/ir.h index 446e342..67adca0 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -17,7 +17,7 @@ * @param params the parameters of the operation. * @param paramsCount the count of the parameters of the operation. */ -void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, void* params, int paramsCount); +void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned char params[], int paramsCount); /** * Parses a AST function into IR. diff --git a/src/ir/structs.h b/src/ir/structs.h index 6882255..02c498d 100644 --- a/src/ir/structs.h +++ b/src/ir/structs.h @@ -12,7 +12,7 @@ typedef struct { unsigned char opCode; - void* params; + unsigned char* params; int paramCount; } IR_INSTRUCTION; @@ -24,6 +24,7 @@ typedef struct { IR_INSTRUCTION* instructions; int instructionCount; + int allocatedSize; } IR_BASIC_BLOCK; From c42919b6718a46ee69a600d89ed439430000019f Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 13:27:55 +0100 Subject: [PATCH 034/121] feat: ast sucks so we remaking it --- src/parser/ast.c | 23 ---- src/parser/ast.h | 61 ---------- src/parser/asts/functions.c | 215 ------------------------------------ src/parser/asts/functions.h | 47 -------- src/parser/asts/math.c | 40 ------- src/parser/asts/math.h | 17 --- src/parser/asts/variables.c | 72 ------------ src/parser/asts/variables.h | 26 ----- src/parser/parser.c | 77 ------------- src/parser/parser.h | 19 ---- 10 files changed, 597 deletions(-) delete mode 100644 src/parser/ast.c delete mode 100644 src/parser/ast.h delete mode 100644 src/parser/asts/functions.c delete mode 100644 src/parser/asts/functions.h delete mode 100644 src/parser/asts/math.c delete mode 100644 src/parser/asts/math.h delete mode 100644 src/parser/asts/variables.c delete mode 100644 src/parser/asts/variables.h delete mode 100644 src/parser/parser.c delete mode 100644 src/parser/parser.h diff --git a/src/parser/ast.c b/src/parser/ast.c deleted file mode 100644 index 4ccbfeb..0000000 --- a/src/parser/ast.c +++ /dev/null @@ -1,23 +0,0 @@ -/** - * The AST Nodes in Quickfall. - */ - -#include - -#include "./ast.h" - -/** - * Creates a new AST Node. - * @param type the AST type of the node. - */ -AST_NODE* createASTNode(AST_NODE_TYPE type) { - AST_NODE* node = malloc(sizeof(AST_NODE)); - - node->valueSize = 0; - node->type = type; - node->left = NULL; - node->right = NULL; - node->next = NULL; - - return node; -} diff --git a/src/parser/ast.h b/src/parser/ast.h deleted file mode 100644 index 7d4c7b1..0000000 --- a/src/parser/ast.h +++ /dev/null @@ -1,61 +0,0 @@ -/** - * The header file of AST nodes in Quickfall. - */ - -#ifndef AST_H -#define AST_H - -/** - * The type of AST Node(s). - */ -typedef enum { - - AST_ROOT, // A root of an AST tree, can either represent the first node of the tree or a function body. - - AST_TYPE, // Represents a datatype precision in an AST Node. - AST_VARIABLE_NAME, // Represents a variable / parameter name precision in an AST Node. - AST_VARIABLE_VALUE, - - AST_VARIABLE_DECLARATION, - AST_VARIABLE_REFERENCE, - - AST_ASM_FUNCTION_DECLARATION, - - AST_FUNCTION_DECLARATION, - AST_FUNCTION_HEADER, - - AST_FUNCTION_INVOKE, - - AST_FUNCTION_ROOT, - - AST_MATH_OPERATOR, - AST_MATH_OPERATION, - AST_MATH_OP_HEADER, - - AST_PARAMETER // A function parameter AST Node, used in function declaration. -} AST_NODE_TYPE; - -/** - * An AST Node. Has a tree-ish structure. - */ -typedef struct AST_NODE { - - struct AST_NODE* left; - struct AST_NODE* right; - struct AST_NODE* next; - - AST_NODE_TYPE type; - - int valueSize; - char* value; - int endingIndex; // The index which the parsing ended - -} AST_NODE; - -/** - * Creates a new AST Node. - * @param type the AST type of the node. - */ -AST_NODE* createASTNode(AST_NODE_TYPE type); - -#endif diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c deleted file mode 100644 index d70a500..0000000 --- a/src/parser/asts/functions.c +++ /dev/null @@ -1,215 +0,0 @@ -/** - * Function-related AST parsing. - */ - -#include -#include - -#include "./variables.h" - -#include "../parser.h" -#include "../ast.h" - -#include "../../lexer/tokens.h" -#include "../../lexer/lexer.h" - -#include "../../utils/logging.c" - -/** - * Parse the parameters from a function definition (for example). - * @param result the lexer result. - * @param index the starting index of the parsing. - */ -AST_NODE* parseParameters(LEXER_RESULT result, int index) { - - AST_NODE* root = createASTNode(AST_PARAMETER); - AST_NODE* current = root; - - int stack = 0; - - for(; index < result.size + 1; ++index) { - TOKEN t = result.tokens[index]; - - switch(t.type) { - case COMMA: - if(stack == 0) { - return NULL; - } - - stack = 0; - current->next = createASTNode(AST_PARAMETER); - current = current->next; - break; - case NONE: - case KEYWORD: - if(stack == 2) { - return NULL; - } - - TOKEN next = result.tokens[index + 1]; - - if(next.type == NONE || next.type == KEYWORD) { - current->left = createASTNode(AST_TYPE); - current->left->value = next.value; - } - else { - current->right = createASTNode(AST_VARIABLE_NAME); - current->right->value = t.value; - } - - stack++; - break; - case PAREN_CLOSE: - root->endingIndex = index; - return root; - case PAREN_OPEN: - continue; - default: - printf("Type: %d", t.type); - return NULL; - - } - } -} - -/** - * Parses the arguments passed during a function call (for example). - * @param result the result of the lexer. - * @param index the starting index of the parsing. - */ -AST_NODE* parseArguments(LEXER_RESULT result, int index) { - AST_NODE* root = NULL; - AST_NODE* current = root; - - for(; index < result.size + 1; ++index) { - TOKEN t = result.tokens[index]; - - if(t.type == PAREN_CLOSE) { - return root; - } - - AST_NODE* arg = parseVariableValue(result, index); - - if(arg == NULL) return NULL; - - index = arg->endingIndex; - - if(root == NULL) { - root = arg; - current = root; - } - else { - current->next = arg; - } - } - - return NULL; -} - -AST_NODE* parseFunctionDeclaration(LEXER_RESULT result, int index) { - - AST_NODE* node = createASTNode(AST_FUNCTION_DECLARATION); - node->left = createASTNode(AST_FUNCTION_HEADER); - - if(result.tokens[index].type != KEYWORD) { - return NULL; - } - - int off = 1; - - switch(result.tokens[index + 1].type) { - case KEYWORD: - node->left->value = result.tokens[index].value; - node->left->right = createASTNode(AST_VARIABLE_NAME); - node->left->right->value = result.tokens[index + 1].value; - ++off; - break; - case PAREN_OPEN: - node->left->value = "void"; - node->left->right = createASTNode(AST_VARIABLE_NAME); - node->left->right->value = result.tokens[index].value; - break; - default: - return NULL; - } - - AST_NODE* params = parseParameters(result, index + off); - - if(params == NULL) return NULL; - - node->left->left = params; - - node->right = parseNodes(result, params->endingIndex, AST_FUNCTION_ROOT); - - node->endingIndex = node->right->endingIndex; - - return node; -} - -AST_NODE* parseASMFunctionDeclaration(LEXER_RESULT result, int index) { - AST_NODE* node = createASTNode(AST_ASM_FUNCTION_DECLARATION); - - node->left = createASTNode(AST_FUNCTION_HEADER); - - if(result.tokens[index + 1].type != KEYWORD) { - return NULL; - } - - node->left->right = createASTNode(AST_VARIABLE_NAME); - node->left->right->value = result.tokens[index + 1].value; - - AST_NODE* params = parseParameters(result, index + 2); - - if(params == NULL) { - return NULL; - } - - node->left->left = params; - - index = params->endingIndex + 2; - - int buffSize = 32; - int buffIndex = 0; - uint8_t* buff = malloc(sizeof(uint8_t) * buffSize); - - for(; index <= result.size; ++index) { - TOKEN t = result.tokens[index]; - - if(t.type == BRACKETS_CLOSE) { - break; - } - - if(t.type != NUMBER) { - return NULL; - } - - buff[buffIndex] = strtol(t.value, NULL, 16); - buffIndex++; - } - - node->endingIndex = index; - - buff = realloc(buff, sizeof(uint8_t) * buffIndex); - - node->valueSize = buffIndex; - node->value = buff; - - return node; -} - -AST_NODE* parseFunctionInvoke(LEXER_RESULT result, int index) { - AST_NODE* node = createASTNode(AST_FUNCTION_INVOKE); - - node->value = result.tokens[index].value; - - AST_NODE* args = parseArguments(result, index + 2); - - node->endingIndex = index; - - if(args != NULL) { - node->right = args; - node->endingIndex = args->endingIndex; - } - - return node; -} diff --git a/src/parser/asts/functions.h b/src/parser/asts/functions.h deleted file mode 100644 index e30cf77..0000000 --- a/src/parser/asts/functions.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Function-related AST parsing. - */ - -#include "../../lexer/lexer.h" - -#include "../ast.h" - -#ifndef AST_FUNC_H -#define AST_FUNC_H - -/** - * Parse the parameters from a function defintition (for example). - * @param result the lexer result. - * @param index the starting index of the parsing. - */ -AST_NODE* parseParameters(LEXER_RESULT result, int index); - -/** - * Parses the arguments from a function call (for example). - * @param result the lexer result. - * @param index the starting index of the parsing. - */ -AST_NODE* parseArguments(LEXER_RESULT result, int index); - -/** - * Parses a function declaration. - * @param result the lexer result. - * @param index the starting index of the parsing. - */ -AST_NODE* parseFunctionDeclaration(LEXER_RESULT result, int index); - -/** - * Parses an ASM function declaration. - * @param result the lexer result. - * @param index the starting index of the parsing. - */ -AST_NODE* parseASMFunctionDeclaration(LEXER_RESULT result, int index); - -/** - * Parses an function invocation. - * @param result the lexer result. - * @param index the starting index of the parsing. - */ -AST_NODE* parseFunctionInvoke(LEXER_RESULT result, int index); - -#endif diff --git a/src/parser/asts/math.c b/src/parser/asts/math.c deleted file mode 100644 index b68ac3b..0000000 --- a/src/parser/asts/math.c +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Math ASTs for the Quickfall parser. - */ - -#include - -#include "../../lexer/lexer.h" - -#include "../ast.h" -#include "./variables.h" - -/** - * Parses the mathematical operation. - * @param result the lexer result. - * @param index the starting index. - */ -AST_NODE* parseMathematicalOpNode(LEXER_RESULT result, int index) { - AST_NODE* node = createASTNode(AST_MATH_OPERATION); - node->left = createASTNode(AST_MATH_OP_HEADER); - - node->left->left = createASTNode(AST_VARIABLE_NAME); - node->left->left->value = result.tokens[index].value; - - node->left->right = createASTNode(AST_MATH_OPERATOR); - - node->left->right->value = malloc(2); - node->left->right->value[0] = result.tokens[index + 1].value[0]; - node->left->right->value[1] = '\0'; - - node->value = malloc(1); - - if(result.size >= index + 2 && result.tokens[index + 2].type == DECLARE) { - node->value[0] = '1'; - } - - node->right = parseVariableValue(result, index + 2); - node->endingIndex = node->right->endingIndex; - - return node; -} diff --git a/src/parser/asts/math.h b/src/parser/asts/math.h deleted file mode 100644 index f69c66b..0000000 --- a/src/parser/asts/math.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Math ASTs for the Quickfall parser. - */ - -#include "../ast.h" - -#ifndef AST_MATH_H -#define AST_MATH_H - -/** - * Parses the mathematical operation. - * @param result the lexer result. - * @param index the starting index. - */ -AST_NODE* parseMathematicalOpNode(LEXER_RESULT, int index); - -#endif \ No newline at end of file diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c deleted file mode 100644 index 5241d31..0000000 --- a/src/parser/asts/variables.c +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Variable-related AST parsing. - */ - -#include "../../lexer/lexer.h" -#include "../../lexer/tokens.h" - -#include "./math.h" - -#include "../ast.h" - -AST_NODE* parseVariableValue(LEXER_RESULT result, int index) { - TOKEN t = result.tokens[index]; - - if(t.type == NUMBER || t.type == STRING || t.type == BOOLEAN_VALUE) { - AST_NODE* node = createASTNode(AST_VARIABLE_VALUE); - node->endingIndex = index; - node->left = createASTNode(AST_TYPE); - - switch(t.type) { - case NUMBER: - if(result.size >= index + 1 && result.tokens[index + 1].type == MATH_OP) return parseMathematicalOpNode(result, index); - - node->left->value = "n"; - - case STRING: - node->left->value = "s"; - break; - default: - node->left->value = "b"; - - } - - node->value = t.value; - return node; - } - - if(t.type == KEYWORD) { - AST_NODE* node = createASTNode(AST_VARIABLE_REFERENCE); - node->endingIndex = index + 1; - node->value = t.value; - - return node; - } - -} - -/** - * Parses a variable declaration. - * @param result the lexer result. - * @param index the starting index. - */ -AST_NODE* parseVariableDeclaration(LEXER_RESULT result, int index) { - AST_NODE* node = createASTNode(AST_VARIABLE_DECLARATION); - - if(result.tokens[index].type == VAR) { - node->value = "none"; - } - else { - node->value = result.tokens[index].value; - } - - node->left = createASTNode(AST_VARIABLE_NAME); - node->left->value = result.tokens[index + 1].value; - - node->right = parseVariableValue(result, index + 3); - - if(node->right != NULL) node->endingIndex = node->right->endingIndex; - else node->endingIndex = index + 2; - - return node; -} diff --git a/src/parser/asts/variables.h b/src/parser/asts/variables.h deleted file mode 100644 index e05205b..0000000 --- a/src/parser/asts/variables.h +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Variable-related AST parsing. - */ - -#include "../ast.h" - -#include "../../lexer/lexer.h" - -#ifndef AST_VARIABLES_H -#define AST_VARIABLES_H - -/** - * Parses a variable value. - * @param result the lexer result. - * @param index the starting index. - */ -AST_NODE* parseVariableValue(LEXER_RESULT result, int index); - -/** - * Parses a variable declaration. - * @param result the lexer result. - * @param index the starting index. - */ -AST_NODE* parseVariableDeclaration(LEXER_RESULT result, int index); - -#endif diff --git a/src/parser/parser.c b/src/parser/parser.c deleted file mode 100644 index 5c3f26d..0000000 --- a/src/parser/parser.c +++ /dev/null @@ -1,77 +0,0 @@ -/** - * The parser of Quickfall. - */ - -#include - -#include "../lexer/tokens.h" -#include "../lexer/lexer.h" - -#include "./ast.h" - -#include "./asts/variables.h" -#include "./asts/functions.h" - -/** - * Parses the lexer tokens into nodes starting from the index. - * @param result the LexerResult provided by the lexer. - * @param index the starting index. - */ -AST_NODE* parseNodes(LEXER_RESULT result, int index, AST_NODE_TYPE type) { - AST_NODE* root = createASTNode(type); - AST_NODE* current = root; - - for(; index <= result.size; ++index) { - TOKEN t = result.tokens[index]; - - 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 + 1); - if(node != NULL) { - current->next = node; - current = node; - index = node->endingIndex; - } - - break; - case VAR: - node = parseVariableDeclaration(result, index); - if(node != NULL) { - current->next = node; - current = node; - index = node->endingIndex; - } - break; - case ASM_FUNCTION: - node = parseASMFunctionDeclaration(result, index); - - if(node != NULL) { - current->next = node; - current = node; - index = node->endingIndex; - } - break; - case KEYWORD: - if(result.tokens[index + 1].type == PAREN_OPEN) { - node = parseFunctionInvoke(result, index); - - if(node != NULL) { - current->next = node; - current = node; - index = node->endingIndex; - } - } - - } - } - - return root; -} diff --git a/src/parser/parser.h b/src/parser/parser.h deleted file mode 100644 index 1759e02..0000000 --- a/src/parser/parser.h +++ /dev/null @@ -1,19 +0,0 @@ -/** - * The parser of Quickfall. - */ - -#include "../lexer/lexer.h" -#include "./ast.h" - -#ifndef PARSER_2_H -#define PARSER_2_H - -/** - * Parses the lexer tokens into nodes starting from the index. - * @param result the LexerResult provided by the lexer. - * @param index the starting index. - * @param type the AST node type to return. - */ -AST_NODE* parseNodes(LEXER_RESULT result, int index, AST_NODE_TYPE type); - -#endif From 8a9b1d91d510d9035237d55f500d1275eadcf7ce Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 13:32:17 +0100 Subject: [PATCH 035/121] feat: added parser header and AST_ROOT_TREE struct --- src/parser/parser.h | 8 ++++++++ src/parser/structs/tree.h | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/parser/parser.h create mode 100644 src/parser/structs/tree.h diff --git a/src/parser/parser.h b/src/parser/parser.h new file mode 100644 index 0000000..bdcca1e --- /dev/null +++ b/src/parser/parser.h @@ -0,0 +1,8 @@ +/** + * The Quickfall Parser & AST Generator. + */ + +#ifndef PARSER_H +#define PARSER_H + +#endif \ No newline at end of file diff --git a/src/parser/structs/tree.h b/src/parser/structs/tree.h new file mode 100644 index 0000000..caac937 --- /dev/null +++ b/src/parser/structs/tree.h @@ -0,0 +1,15 @@ +#ifndef TREE_STRUCT_H +#define TREE_STRUCT_H + +/** + * The main AST root tree structure. + */ +typedef struct { + unsigned char type; + + void* next; // Refers to the next node in the tree. + void* last; // Refers to the furthest / last node in the tree. + +} AST_ROOT_TREE; + +#endif \ No newline at end of file From 4e06b34c86920b42b3488c06d7ac04e2576e7ad1 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 13:37:06 +0100 Subject: [PATCH 036/121] feat: added function dec struct --- src/parser/structs/functions.h | 21 +++++++++++++++++++++ src/parser/structs/tree.h | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/parser/structs/functions.h diff --git a/src/parser/structs/functions.h b/src/parser/structs/functions.h new file mode 100644 index 0000000..268e947 --- /dev/null +++ b/src/parser/structs/functions.h @@ -0,0 +1,21 @@ +/** + * AST structure definitions for function related ASTs. + */ + +#ifndef FUNCTIONS_STRUCT_H +#define FUNCTIONS_STRUCT_H + +/** + * The function declaration structure. + */ +typedef struct { + unsigned char type; // The type of the AST node (unused.) + + char* funcName; + unsigned char* returnType; + + void* body; + +} AST_FUNCTION_DEC; + +#endif \ No newline at end of file diff --git a/src/parser/structs/tree.h b/src/parser/structs/tree.h index caac937..29ab20e 100644 --- a/src/parser/structs/tree.h +++ b/src/parser/structs/tree.h @@ -1,3 +1,7 @@ +/** + * AST structure definitions for Tree related ASTs. + */ + #ifndef TREE_STRUCT_H #define TREE_STRUCT_H @@ -5,7 +9,7 @@ * The main AST root tree structure. */ typedef struct { - unsigned char type; + unsigned char type; // The type of the AST node type (unused). void* next; // Refers to the next node in the tree. void* last; // Refers to the furthest / last node in the tree. From 80c2014d6fff2d0670ead0da8e4001cb902f83ff Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 13:38:28 +0100 Subject: [PATCH 037/121] feat: added AST_TREE_BRANCH struct --- src/parser/structs/tree.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/parser/structs/tree.h b/src/parser/structs/tree.h index 29ab20e..c25a4a2 100644 --- a/src/parser/structs/tree.h +++ b/src/parser/structs/tree.h @@ -16,4 +16,14 @@ typedef struct { } AST_ROOT_TREE; + +/** + * The "base" type of each AST structure. Is used to cast to gather next branch. + */ +typedef struct { + unsigned char type; // The type of the AST node type (unused.) + void* next; // The next node in the tree. + +} AST_TREE_BRANCH; + #endif \ No newline at end of file From f72ffff90546ed724e741c2660596078ec4ab084 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 13:38:56 +0100 Subject: [PATCH 038/121] feat: corrected structs to align with AST_TREE_BRANCh --- src/parser/structs/functions.h | 1 + src/parser/structs/tree.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/parser/structs/functions.h b/src/parser/structs/functions.h index 268e947..4987a3a 100644 --- a/src/parser/structs/functions.h +++ b/src/parser/structs/functions.h @@ -10,6 +10,7 @@ */ typedef struct { unsigned char type; // The type of the AST node (unused.) + void* next; char* funcName; unsigned char* returnType; diff --git a/src/parser/structs/tree.h b/src/parser/structs/tree.h index c25a4a2..42b5582 100644 --- a/src/parser/structs/tree.h +++ b/src/parser/structs/tree.h @@ -10,8 +10,8 @@ */ typedef struct { unsigned char type; // The type of the AST node type (unused). - void* next; // Refers to the next node in the tree. + void* last; // Refers to the furthest / last node in the tree. } AST_ROOT_TREE; From e83bd1670c71606cab845b89fb7b6c2b63fbf195 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 13:40:27 +0100 Subject: [PATCH 039/121] feat: added AST_ASM_FUNCTION_DEC --- src/parser/structs/functions.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/parser/structs/functions.h b/src/parser/structs/functions.h index 4987a3a..bff42a8 100644 --- a/src/parser/structs/functions.h +++ b/src/parser/structs/functions.h @@ -19,4 +19,19 @@ typedef struct { } AST_FUNCTION_DEC; + +/** + * The ASM function declaration structure. + */ +typedef struct { + unsigned char type; // The type of the AST node (unused.) + void* next; + + char* funcName; + + unsigned char* buff; // The content buffer of the function. + int buffIndex; // The size of buff. + +} AST_ASM_FUNCTION_DEC; + #endif \ No newline at end of file From 739c9614b8855c8530be84e236d7240dc7140528 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 13:43:40 +0100 Subject: [PATCH 040/121] feat: added AST_PARAMETER struct --- src/parser/structs/functions.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/parser/structs/functions.h b/src/parser/structs/functions.h index bff42a8..3934062 100644 --- a/src/parser/structs/functions.h +++ b/src/parser/structs/functions.h @@ -5,6 +5,17 @@ #ifndef FUNCTIONS_STRUCT_H #define FUNCTIONS_STRUCT_H +/** + * The function parameter structure. + */ +typedef struct { + + unsigned char* type; // The datatype in bytes. + char* name; // The name of the parameter + +} AST_PARAMETER; + + /** * The function declaration structure. */ @@ -15,6 +26,9 @@ typedef struct { char* funcName; unsigned char* returnType; + AST_PARAMETER* parameters; + int parameterIndex; + void* body; } AST_FUNCTION_DEC; @@ -29,6 +43,9 @@ typedef struct { char* funcName; + AST_PARAMETER* parameters; + int parameterIndex; + unsigned char* buff; // The content buffer of the function. int buffIndex; // The size of buff. From 787a8b193a9e98cf1237711b14609d176d49c6f2 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 13:48:33 +0100 Subject: [PATCH 041/121] feat: added AST_FUNCTION_INVOKE struct --- src/parser/structs/functions.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/parser/structs/functions.h b/src/parser/structs/functions.h index 3934062..1b735f3 100644 --- a/src/parser/structs/functions.h +++ b/src/parser/structs/functions.h @@ -51,4 +51,16 @@ typedef struct { } AST_ASM_FUNCTION_DEC; + +/** + * The function invocation structure. + */ +typedef struct { + unsigned char type; + void* next; + + void* arguments; + int argumentIndex; +} AST_FUNCTION_INVOKE; + #endif \ No newline at end of file From 868cfbba4c62044b47d36e3280641184460d1ff7 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 14:25:37 +0100 Subject: [PATCH 042/121] feat: added AST_VARIABLE_DEC struct --- src/parser/parser.h | 8 ++++++++ src/parser/structs/variables.h | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/parser/structs/variables.h diff --git a/src/parser/parser.h b/src/parser/parser.h index bdcca1e..c0ef9bc 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -2,7 +2,15 @@ * The Quickfall Parser & AST Generator. */ +#include "./structs/tree.h" + +#include "../lexer/lexer.h" + #ifndef PARSER_H #define PARSER_H +AST_ROOT_TREE parseRoot(LEXER_RESULT result) { + AST_ROOT_TREE tree = {0}; +} + #endif \ No newline at end of file diff --git a/src/parser/structs/variables.h b/src/parser/structs/variables.h new file mode 100644 index 0000000..629fdcc --- /dev/null +++ b/src/parser/structs/variables.h @@ -0,0 +1,21 @@ +/** + * AST structure definitions for variable related ASTs. + */ + +#ifndef VARIABLES_STRUCT_H +#define VARIABLES_STRUCT_H + +/** + * The variable declaration structure. + */ +typedef struct { + unsigned char astType; + void* next; + + unsigned char* type; + char* name; + + unsigned char* value; +} AST_VARIABLE_DEC; + +#endif \ No newline at end of file From fd3a82b70baa34bab4d8cc26613337c55bdb8a80 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 15:51:00 +0100 Subject: [PATCH 043/121] feat: added parseRoot in header --- src/parser/parser.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/parser/parser.h b/src/parser/parser.h index c0ef9bc..a1b7e7f 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -9,8 +9,10 @@ #ifndef PARSER_H #define PARSER_H -AST_ROOT_TREE parseRoot(LEXER_RESULT result) { - AST_ROOT_TREE tree = {0}; -} +/** + * Parses the Lexer result into an AST root. + * @param result the Lexer result. + */ +AST_ROOT_TREE parseRoot(LEXER_RESULT result); #endif \ No newline at end of file From 9257bf5050078644cf7007bd3c69c705418f311c Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 15:54:01 +0100 Subject: [PATCH 044/121] feat: added parseVariableDeclaration in header --- src/parser/asts/variables.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/parser/asts/variables.h diff --git a/src/parser/asts/variables.h b/src/parser/asts/variables.h new file mode 100644 index 0000000..2552900 --- /dev/null +++ b/src/parser/asts/variables.h @@ -0,0 +1,19 @@ +/** + * Parsing for variable related ASTs. + */ + +#include "../structs/variables.h" + +#include "../../lexer/lexer.h" + +#ifndef VARIABLES_ASTS_H +#define VARIABLES_ASTS_H + +/** + * Parses a variable declaration. + * @param result the Lexer result. + * @param index the index where the parsing needs to start. + */ +AST_VARIABLE_DEC parseVariableDeclaration(LEXER_RESULT result, int index); + +#endif \ No newline at end of file From 3b91f18242b111495020159a5e77eb1cf755bea9 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 15:59:07 +0100 Subject: [PATCH 045/121] feat: added AST_VARIABLE_MOD struct --- src/parser/structs/variables.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/parser/structs/variables.h b/src/parser/structs/variables.h index 629fdcc..97b939f 100644 --- a/src/parser/structs/variables.h +++ b/src/parser/structs/variables.h @@ -18,4 +18,16 @@ typedef struct { unsigned char* value; } AST_VARIABLE_DEC; + +/** + * The variable modification structure. + */ +typedef struct { + unsigned char type; + void* next; + + char* name; + unsigned char* value; +} AST_VARIABLE_MOD; + #endif \ No newline at end of file From 8759b26b3c6c6c6ee081ce4f19515d0225e056fa Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 16:00:32 +0100 Subject: [PATCH 046/121] feat: added parseVariableModification in header --- src/parser/asts/variables.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/parser/asts/variables.h b/src/parser/asts/variables.h index 2552900..81bd890 100644 --- a/src/parser/asts/variables.h +++ b/src/parser/asts/variables.h @@ -16,4 +16,11 @@ */ AST_VARIABLE_DEC parseVariableDeclaration(LEXER_RESULT result, int index); +/** + * Parses a variable modification. + * @param result the Lexer result. + * @param index the index where the parsing needs to start. + */ +AST_VARIABLE_MOD parseVariableModification(LEXER_RESULT result, int index); + #endif \ No newline at end of file From 0cebed9bbfc9dec8b9bf90997f802aa2d40aee65 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 16:40:10 +0100 Subject: [PATCH 047/121] feat: added TYPE_INT32 token --- src/lexer/lexer.c | 3 +++ src/lexer/tokens.h | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index ca4c82d..446c27c 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -109,6 +109,9 @@ LEXER_RESULT runLexer(char* string, int size) { else if(strcmp(buff, "var") == 0) { pushToken(&result, VAR); } + else if(strcmp(buff, "int32") == 0) { + pushToken(&result, TYPE_INT32); + } else { pushToken(&result, KEYWORD); result.tokens[result.size - 1].value = buff; diff --git a/src/lexer/tokens.h b/src/lexer/tokens.h index 3917555..e21bdce 100644 --- a/src/lexer/tokens.h +++ b/src/lexer/tokens.h @@ -22,7 +22,9 @@ typedef enum { DECLARE, USE, NONE, - MATH_OP + MATH_OP, + + TYPE_INT32 } TOKEN_TYPE; /** From f0a4046d081e9441dc8159c93d1abe5e83fb704c Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 16:45:20 +0100 Subject: [PATCH 048/121] feat: added variable declaration parsing --- src/parser/ast.h | 14 +++++++++++ src/parser/asts/variables.c | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/parser/ast.h create mode 100644 src/parser/asts/variables.c diff --git a/src/parser/ast.h b/src/parser/ast.h new file mode 100644 index 0000000..4b6ea77 --- /dev/null +++ b/src/parser/ast.h @@ -0,0 +1,14 @@ +/** + * The AST of Quickfall. + */ + +#ifndef AST_H +#define AST_H + +typedef enum { + + AST_TYPE_VARIABLE_DECLARATION + +} AST_TYPE; + +#endif \ No newline at end of file diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c new file mode 100644 index 0000000..1000605 --- /dev/null +++ b/src/parser/asts/variables.c @@ -0,0 +1,48 @@ +/** + * Parsing for variable related ASTs. + */ + +#include + +#include "../structs/variables.h" + +#include "../ast.h" + +#include "../../lexer/lexer.h" + +/** + * Parses a variable declaration. + * @param result the Lexer result. + * @param index the index where the parsing needs to start. + */ +AST_VARIABLE_DEC parseVariableDeclaration(LEXER_RESULT result, int index) { + AST_VARIABLE_DEC var = {0}; + var.astType = AST_TYPE_VARIABLE_DECLARATION; + + switch(result.tokens[index].type) { + case TYPE_INT32: + var.type[0] = 0x01; + break; + case VAR: + var.type[0] = 0x00; + break; + default: + printf("Error: Disallowed token as variable type! Defaulting to untyped variable!\n"); + var.type[0] = 0x00; + break; + } + + if(result.tokens[index + 1].type != KEYWORD) { + printf("Error: Excepted a keyword for variable name!\n"); + return var; + } + + var.name = result.tokens[index + 1].value; + + + if(result.tokens[index + 2].type == DECLARE) { + //todo: add value parsing if the next token is "=" + } + + return var; +} \ No newline at end of file From 065091ee0fa872e1f3bd897942a432ad883433e5 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 16:47:48 +0100 Subject: [PATCH 049/121] feat: added AST_VALUE struct --- src/parser/structs/values.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/parser/structs/values.h diff --git a/src/parser/structs/values.h b/src/parser/structs/values.h new file mode 100644 index 0000000..e683327 --- /dev/null +++ b/src/parser/structs/values.h @@ -0,0 +1,17 @@ +/** + * AST structure definitions for value related ASTs. + */ + +#ifndef VALUES_STRUCTURES_H +#define VALUES_STRUCTURES_H + +/** + * The AST structure of a value. + */ +typedef struct { + unsigned char astType; + + void* value; +} AST_VALUE; + +#endif \ No newline at end of file From 875e051a71dc3015906af0b37cd78ef36e81acce Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 16:49:30 +0100 Subject: [PATCH 050/121] feat: added parseValue in header --- src/parser/asts/values.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/parser/asts/values.h diff --git a/src/parser/asts/values.h b/src/parser/asts/values.h new file mode 100644 index 0000000..4e3cedf --- /dev/null +++ b/src/parser/asts/values.h @@ -0,0 +1,19 @@ +/** + * Parsing for value related ASTs. + */ + +#include "../structs/values.h" + +#include "../../lexer/lexer.h" + +#ifndef VALUES_AST_H +#define VALUES_AST_H + +/** + * Parses an AST value on the lexer result. + * @param result the Lexer result. + * @param index the index of the start of the parsing. + */ +AST_VALUE parseValue(LEXER_RESULT result, int index); + +#endif \ No newline at end of file From bc7681ac4559c69c5ad89f8fe40d9ac66d9b19e7 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 16:53:14 +0100 Subject: [PATCH 051/121] feat: added parseValue --- src/parser/asts/values.c | 31 +++++++++++++++++++++++++++++++ src/parser/structs/values.h | 1 + 2 files changed, 32 insertions(+) create mode 100644 src/parser/asts/values.c diff --git a/src/parser/asts/values.c b/src/parser/asts/values.c new file mode 100644 index 0000000..9668a2c --- /dev/null +++ b/src/parser/asts/values.c @@ -0,0 +1,31 @@ +/** + * Parsing for value related ASTs. + */ + +#include + +#include "../structs/values.h" + +#include "../../lexer/lexer.h" + +/** + * Parses an AST value on the lexer result. + * @param result the Lexer result. + * @param index the index of the start of the parsing. + */ +AST_VALUE parseValue(LEXER_RESULT result, int index) { + AST_VALUE value = {0}; + + switch(result.tokens[index].type) { + case NUMBER: + value.valueType = 0x01; //i32 + break; + default: + printf("Error: Couldn't parse token %d as a value!\n", result.tokens[index].type); + return value; + } + + value.value = result.tokens[index].value; + + return value; +} \ No newline at end of file diff --git a/src/parser/structs/values.h b/src/parser/structs/values.h index e683327..6779a0a 100644 --- a/src/parser/structs/values.h +++ b/src/parser/structs/values.h @@ -11,6 +11,7 @@ typedef struct { unsigned char astType; + unsigned char* valueType; void* value; } AST_VALUE; From cc41fa2905ce0c72514ad3c86e612ecec26fae7f Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:00:12 +0100 Subject: [PATCH 052/121] feat: added parseValueGroup --- src/parser/asts/values.c | 30 ++++++++++++++++++++++++++---- src/parser/asts/values.h | 9 ++++++++- src/parser/asts/variables.c | 20 ++++++++++---------- src/parser/asts/variables.h | 4 ++-- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/parser/asts/values.c b/src/parser/asts/values.c index 9668a2c..966cf74 100644 --- a/src/parser/asts/values.c +++ b/src/parser/asts/values.c @@ -2,6 +2,7 @@ * Parsing for value related ASTs. */ +#include #include #include "../structs/values.h" @@ -13,19 +14,40 @@ * @param result the Lexer result. * @param index the index of the start of the parsing. */ -AST_VALUE parseValue(LEXER_RESULT result, int index) { - AST_VALUE value = {0}; +AST_VALUE* parseValue(LEXER_RESULT result, int index) { + AST_VALUE* value = malloc(sizeof(AST_VALUE)); switch(result.tokens[index].type) { case NUMBER: - value.valueType = 0x01; //i32 + value->valueType = 0x01; //i32 break; default: printf("Error: Couldn't parse token %d as a value!\n", result.tokens[index].type); return value; } - value.value = result.tokens[index].value; + value->value = result.tokens[index].value; return value; +} + + +/** + * Parses an actual expression as value instead of just one token. + * @param result the Lexer result. + * @param index the index of the start of the parsing. + */ +void* parseValueGroup(LEXER_RESULT result, int index) { + switch(result.tokens[index].type) { + case NUMBER: + if(result.tokens[index + 1].type == MATH_OP) { + //todo: parse math op. + } + + return parseValue(result, index); + break; + default: + printf("Error: couldn't parse value token group!\n"); + return NULL; + } } \ No newline at end of file diff --git a/src/parser/asts/values.h b/src/parser/asts/values.h index 4e3cedf..f404bb8 100644 --- a/src/parser/asts/values.h +++ b/src/parser/asts/values.h @@ -14,6 +14,13 @@ * @param result the Lexer result. * @param index the index of the start of the parsing. */ -AST_VALUE parseValue(LEXER_RESULT result, int index); +AST_VALUE* parseValue(LEXER_RESULT result, int index); + +/** + * Parses an actual expression as value instead of just one token. + * @param result the Lexer result. + * @param index the index of the start of the parsing. + */ +void* parseValueGroup(LEXER_RESULT result, int index); #endif \ No newline at end of file diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c index 1000605..6e11a44 100644 --- a/src/parser/asts/variables.c +++ b/src/parser/asts/variables.c @@ -2,6 +2,7 @@ * Parsing for variable related ASTs. */ +#include #include #include "../structs/variables.h" @@ -15,29 +16,28 @@ * @param result the Lexer result. * @param index the index where the parsing needs to start. */ -AST_VARIABLE_DEC parseVariableDeclaration(LEXER_RESULT result, int index) { - AST_VARIABLE_DEC var = {0}; - var.astType = AST_TYPE_VARIABLE_DECLARATION; +AST_VARIABLE_DEC* parseVariableDeclaration(LEXER_RESULT result, int index) { + AST_VARIABLE_DEC* var = malloc(sizeof(AST_VARIABLE_DEC)); + var->astType = AST_TYPE_VARIABLE_DECLARATION; switch(result.tokens[index].type) { case TYPE_INT32: - var.type[0] = 0x01; + var->type[0] = 0x01; break; case VAR: - var.type[0] = 0x00; + var->type[0] = 0x00; break; default: - printf("Error: Disallowed token as variable type! Defaulting to untyped variable!\n"); - var.type[0] = 0x00; - break; + printf("Error: Disallowed token as variable type!\n"); + return NULL; } if(result.tokens[index + 1].type != KEYWORD) { printf("Error: Excepted a keyword for variable name!\n"); - return var; + return NULL; } - var.name = result.tokens[index + 1].value; + var->name = result.tokens[index + 1].value; if(result.tokens[index + 2].type == DECLARE) { diff --git a/src/parser/asts/variables.h b/src/parser/asts/variables.h index 81bd890..47dc016 100644 --- a/src/parser/asts/variables.h +++ b/src/parser/asts/variables.h @@ -14,13 +14,13 @@ * @param result the Lexer result. * @param index the index where the parsing needs to start. */ -AST_VARIABLE_DEC parseVariableDeclaration(LEXER_RESULT result, int index); +AST_VARIABLE_DEC* parseVariableDeclaration(LEXER_RESULT result, int index); /** * Parses a variable modification. * @param result the Lexer result. * @param index the index where the parsing needs to start. */ -AST_VARIABLE_MOD parseVariableModification(LEXER_RESULT result, int index); +AST_VARIABLE_MOD* parseVariableModification(LEXER_RESULT result, int index); #endif \ No newline at end of file From 06925b0ca816aaa1a8e04383f7f23484a3e92e3b Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:01:53 +0100 Subject: [PATCH 053/121] feat: added value parsing in variable dec --- src/parser/asts/variables.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c index 6e11a44..15e9b27 100644 --- a/src/parser/asts/variables.c +++ b/src/parser/asts/variables.c @@ -6,6 +6,7 @@ #include #include "../structs/variables.h" +#include "./values.h" #include "../ast.h" @@ -41,7 +42,14 @@ AST_VARIABLE_DEC* parseVariableDeclaration(LEXER_RESULT result, int index) { if(result.tokens[index + 2].type == DECLARE) { - //todo: add value parsing if the next token is "=" + void* value = parseValueGroup(result, index + 3); + + if(value == NULL) { + printf("Error: Couldn't parse variable value group!\n"); + return NULL; + } + + var->value = value; } return var; From 2e11704f90b00399f08c476c1c102a5ef4630aba Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:05:18 +0100 Subject: [PATCH 054/121] feat: added parseVariableModification --- src/parser/ast.h | 4 +++- src/parser/asts/values.c | 4 ++++ src/parser/asts/variables.c | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/parser/ast.h b/src/parser/ast.h index 4b6ea77..db3a281 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -7,7 +7,9 @@ typedef enum { - AST_TYPE_VARIABLE_DECLARATION + AST_TYPE_VARIABLE_DECLARATION, + AST_TYPE_VARIABLE_MODIFICATION, + AST_TYPE_VALUE } AST_TYPE; diff --git a/src/parser/asts/values.c b/src/parser/asts/values.c index 966cf74..0a33e2d 100644 --- a/src/parser/asts/values.c +++ b/src/parser/asts/values.c @@ -7,6 +7,8 @@ #include "../structs/values.h" +#include "../ast.h" + #include "../../lexer/lexer.h" /** @@ -16,6 +18,8 @@ */ AST_VALUE* parseValue(LEXER_RESULT result, int index) { AST_VALUE* value = malloc(sizeof(AST_VALUE)); + value->astType = AST_TYPE_VALUE; + switch(result.tokens[index].type) { case NUMBER: diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c index 15e9b27..49795c8 100644 --- a/src/parser/asts/variables.c +++ b/src/parser/asts/variables.c @@ -53,4 +53,28 @@ AST_VARIABLE_DEC* parseVariableDeclaration(LEXER_RESULT result, int index) { } return var; +} + + +/** + * Parses a variable modification. + * @param result the Lexer result. + * @param index the index where the parsing needs to start. + */ +AST_VARIABLE_MOD* parseVariableModification(LEXER_RESULT result, int index) { + AST_VARIABLE_MOD* mod = malloc(sizeof(AST_VARIABLE_MOD)); + mod->type = AST_TYPE_VARIABLE_MODIFICATION; + + mod->name = result.tokens[index].value; + + void* value = parseValueGroup(result, index + 2); + + if(value == NULL) { + printf("Error: Couldn't parse variable value group in redefinition!\n"); + return NULL; + } + + mod->value = value; + + return mod; } \ No newline at end of file From cb3f081a0b9b42aee1e4b5287c3d006ce22a666e Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:15:40 +0100 Subject: [PATCH 055/121] feat: added base of parseRoot --- src/parser/ast.h | 2 ++ src/parser/parser.c | 72 +++++++++++++++++++++++++++++++++++++++ src/parser/parser.h | 4 ++- src/parser/structs/tree.h | 12 ------- 4 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 src/parser/parser.c diff --git a/src/parser/ast.h b/src/parser/ast.h index db3a281..4c5a94b 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -7,6 +7,8 @@ typedef enum { + AST_TYPE_ROOT, + AST_TYPE_VARIABLE_DECLARATION, AST_TYPE_VARIABLE_MODIFICATION, AST_TYPE_VALUE diff --git a/src/parser/parser.c b/src/parser/parser.c new file mode 100644 index 0000000..4a04be0 --- /dev/null +++ b/src/parser/parser.c @@ -0,0 +1,72 @@ +/** + * The Quickfall Parser & AST Generator. + */ + +#include + +#include "./ast.h" + +#include "../lexer/lexer.h" + +#include "./structs/tree.h" +#include "./structs/variables.h" + +#include "./asts/variables.h" + +/** + * Parses the Lexer result into an AST root. + * @param result the Lexer result. + * @param type the output AST type. + */ +void* parseRoot(LEXER_RESULT result, AST_TYPE type) { + AST_TREE_BRANCH* root = NULL; + AST_TREE_BRANCH* curr = NULL; + + for(int i = 0; i < result.size; ++i) { + TOKEN t = result.tokens[i]; + + switch(t.type) { + case VAR: + void* node = parseVariableDeclaration(result, i); + append(curr, root, node); + break; + case KEYWORD: + if(result.tokens[i + 1].type == KEYWORD) { + void* node = parseVariableDeclaration(result, i); + append(curr, root, node); + break; + } + + if(result.tokens[i + 1].type == DECLARE) { + void* node = parseVariableModification(result, i); + append(curr, root, node); + break; + } + + default: + printf("Error: Unexcepted token %d!\n", t.type); + break; + } + } + + return root; +} + +/** + * Appends a new node in the AST tree. + * @param curr the current AST node. + * @param root the root of the AST tree. + * @param node the node to add to the tree. + */ +inline void append(AST_TREE_BRANCH* curr, AST_TREE_BRANCH* root, void* node) { + if(node == NULL) return; + + if(curr == NULL) { + root = node; + curr = root; + } + else { + curr->next = node; + curr = node; + } +} \ No newline at end of file diff --git a/src/parser/parser.h b/src/parser/parser.h index a1b7e7f..ebd312d 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -2,6 +2,7 @@ * The Quickfall Parser & AST Generator. */ +#include "./ast.h" #include "./structs/tree.h" #include "../lexer/lexer.h" @@ -12,7 +13,8 @@ /** * Parses the Lexer result into an AST root. * @param result the Lexer result. + * @param type the output AST type. */ -AST_ROOT_TREE parseRoot(LEXER_RESULT result); +void* parseRoot(LEXER_RESULT result, AST_TYPE type); #endif \ No newline at end of file diff --git a/src/parser/structs/tree.h b/src/parser/structs/tree.h index 42b5582..18721c0 100644 --- a/src/parser/structs/tree.h +++ b/src/parser/structs/tree.h @@ -5,18 +5,6 @@ #ifndef TREE_STRUCT_H #define TREE_STRUCT_H -/** - * The main AST root tree structure. - */ -typedef struct { - unsigned char type; // The type of the AST node type (unused). - void* next; // Refers to the next node in the tree. - - void* last; // Refers to the furthest / last node in the tree. - -} AST_ROOT_TREE; - - /** * The "base" type of each AST structure. Is used to cast to gather next branch. */ From 0f7e8caf8c74bdacf413396f706d77b0cd69d1c3 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:16:39 +0100 Subject: [PATCH 056/121] feat: added endingIndex in ASt_TREE_BRANCH --- src/parser/structs/tree.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/parser/structs/tree.h b/src/parser/structs/tree.h index 18721c0..8e64ee0 100644 --- a/src/parser/structs/tree.h +++ b/src/parser/structs/tree.h @@ -11,6 +11,7 @@ typedef struct { unsigned char type; // The type of the AST node type (unused.) void* next; // The next node in the tree. + int endingIndex; // The index where the parsing of said branch ended. } AST_TREE_BRANCH; From abccb1b57af1ca03622955c9e80fb5172f7334cf Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:17:41 +0100 Subject: [PATCH 057/121] feat: added endingIndexes on target structures --- src/parser/structs/functions.h | 3 +++ src/parser/structs/variables.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/parser/structs/functions.h b/src/parser/structs/functions.h index 1b735f3..99b2a7a 100644 --- a/src/parser/structs/functions.h +++ b/src/parser/structs/functions.h @@ -22,6 +22,7 @@ typedef struct { typedef struct { unsigned char type; // The type of the AST node (unused.) void* next; + int endingIndex; char* funcName; unsigned char* returnType; @@ -40,6 +41,7 @@ typedef struct { typedef struct { unsigned char type; // The type of the AST node (unused.) void* next; + int endingIndex; char* funcName; @@ -58,6 +60,7 @@ typedef struct { typedef struct { unsigned char type; void* next; + int endingIndex; void* arguments; int argumentIndex; diff --git a/src/parser/structs/variables.h b/src/parser/structs/variables.h index 97b939f..fabbd5e 100644 --- a/src/parser/structs/variables.h +++ b/src/parser/structs/variables.h @@ -11,6 +11,7 @@ typedef struct { unsigned char astType; void* next; + int endingIndex; unsigned char* type; char* name; @@ -25,6 +26,7 @@ typedef struct { typedef struct { unsigned char type; void* next; + int endingIndex; char* name; unsigned char* value; From e60747778935b33b9178c89897ee0abc87184afd Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:19:25 +0100 Subject: [PATCH 058/121] feat: made structures a bit cleaner --- src/parser/structs/functions.h | 6 +++--- src/parser/structs/tree.h | 2 +- src/parser/structs/values.h | 2 +- src/parser/structs/variables.h | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/parser/structs/functions.h b/src/parser/structs/functions.h index 99b2a7a..1c3031c 100644 --- a/src/parser/structs/functions.h +++ b/src/parser/structs/functions.h @@ -8,7 +8,7 @@ /** * The function parameter structure. */ -typedef struct { +typedef struct AST_PARAMETER { unsigned char* type; // The datatype in bytes. char* name; // The name of the parameter @@ -19,7 +19,7 @@ typedef struct { /** * The function declaration structure. */ -typedef struct { +typedef struct AST_FUNCTION_DEF { unsigned char type; // The type of the AST node (unused.) void* next; int endingIndex; @@ -38,7 +38,7 @@ typedef struct { /** * The ASM function declaration structure. */ -typedef struct { +typedef struct AST_ASM_FUNCTION_DEF { unsigned char type; // The type of the AST node (unused.) void* next; int endingIndex; diff --git a/src/parser/structs/tree.h b/src/parser/structs/tree.h index 8e64ee0..72ee7bf 100644 --- a/src/parser/structs/tree.h +++ b/src/parser/structs/tree.h @@ -8,7 +8,7 @@ /** * The "base" type of each AST structure. Is used to cast to gather next branch. */ -typedef struct { +typedef struct AST_TREE_BRANCH { unsigned char type; // The type of the AST node type (unused.) void* next; // The next node in the tree. int endingIndex; // The index where the parsing of said branch ended. diff --git a/src/parser/structs/values.h b/src/parser/structs/values.h index 6779a0a..be8d155 100644 --- a/src/parser/structs/values.h +++ b/src/parser/structs/values.h @@ -8,7 +8,7 @@ /** * The AST structure of a value. */ -typedef struct { +typedef struct AST_VALUE { unsigned char astType; unsigned char* valueType; diff --git a/src/parser/structs/variables.h b/src/parser/structs/variables.h index fabbd5e..cc6cc67 100644 --- a/src/parser/structs/variables.h +++ b/src/parser/structs/variables.h @@ -8,7 +8,7 @@ /** * The variable declaration structure. */ -typedef struct { +typedef struct AST_VARIABLE_DEC { unsigned char astType; void* next; int endingIndex; @@ -23,7 +23,7 @@ typedef struct { /** * The variable modification structure. */ -typedef struct { +typedef struct AST_VARIABLE_MOD { unsigned char type; void* next; int endingIndex; From 0d11dc60e2e0f331909458f8bbd2e69a19e078fb Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:20:58 +0100 Subject: [PATCH 059/121] feat: added handling for brackets close when type == AST_FUNC_ROOT --- src/parser/ast.h | 1 + src/parser/parser.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/parser/ast.h b/src/parser/ast.h index 4c5a94b..b39a2cd 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -8,6 +8,7 @@ typedef enum { AST_TYPE_ROOT, + AST_TYPE_FUNC_ROOT, AST_TYPE_VARIABLE_DECLARATION, AST_TYPE_VARIABLE_MODIFICATION, diff --git a/src/parser/parser.c b/src/parser/parser.c index 4a04be0..672c02a 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -43,6 +43,10 @@ void* parseRoot(LEXER_RESULT result, AST_TYPE type) { break; } + case BRACKETS_CLOSE: + if(type == AST_TYPE_FUNC_ROOT) return root; + break; + default: printf("Error: Unexcepted token %d!\n", t.type); break; From 5868c738a61585d25b9a1cd2dd3a7c4f324b8ab9 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:23:16 +0100 Subject: [PATCH 060/121] feat: added parseFunctionDeclaration in header --- src/parser/asts/functions.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/parser/asts/functions.h diff --git a/src/parser/asts/functions.h b/src/parser/asts/functions.h new file mode 100644 index 0000000..ff06c40 --- /dev/null +++ b/src/parser/asts/functions.h @@ -0,0 +1,19 @@ +/** + * Parsing for function related ASTs. + */ + +#include "../structs/functions.h" + +#include "../../lexer/lexer.h" + +#ifndef FUNCTIONS_AST_H +#define FUNCTIONS_AST_H + +/** + * Parses a function declaration into AST. + * @param result the Lexer result. + * @param index the index of the start of the parsing. + */ +AST_FUNCTION_DEC parseFunctionDeclaration(LEXER_RESULT result, int index); + +#endif \ No newline at end of file From f0f4799f69fdc7df2ad48f7bcb79c8fba6c8bd6e Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:30:08 +0100 Subject: [PATCH 061/121] feat: added basic parseFunctionDeclaration --- src/parser/ast.h | 2 ++ src/parser/asts/functions.c | 52 +++++++++++++++++++++++++++++++++++++ src/parser/asts/functions.h | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/parser/asts/functions.c diff --git a/src/parser/ast.h b/src/parser/ast.h index b39a2cd..e966dac 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -10,6 +10,8 @@ typedef enum { AST_TYPE_ROOT, AST_TYPE_FUNC_ROOT, + AST_TYPE_FUNCTION_DECLARATION, + AST_TYPE_VARIABLE_DECLARATION, AST_TYPE_VARIABLE_MODIFICATION, AST_TYPE_VALUE diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c new file mode 100644 index 0000000..05d19c4 --- /dev/null +++ b/src/parser/asts/functions.c @@ -0,0 +1,52 @@ +/** + * Parsing for function related ASTs. + */ + +#include +#include + +#include "../structs/functions.h" + +#include "../ast.h" + +#include "../../lexer/lexer.h" + +/** + * Parses a function declaration into AST. + * @param result the Lexer result. + * @param index the index of the start of the parsing. + */ +AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { + AST_FUNCTION_DEC* func = malloc(sizeof(AST_FUNCTION_DEC)); + + func->type = AST_TYPE_FUNCTION_DECLARATION; + + switch(result.tokens[index + 2].type) { + case PAREN_OPEN: + if(result.tokens[index + 1].type != KEYWORD) { + printf("Error: Excepted a keyword as function name!\n"); + return NULL; + } + + func->funcName = result.tokens[index + 0].value; + func->returnType = 0x00; + break; + case KEYWORD: + if(result.tokens[index + 1].type != TYPE_INT32) { + printf("Error: Invalid type as function return type!\n"); + return NULL; + } + + func->funcName = result.tokens[index + 2].value; + func->returnType = 0x01; + break; + + default: + printf("Error: couldn't parse function declaration!\n"); + return NULL; + } + + func->endingIndex = index + 2; + + return func; +} diff --git a/src/parser/asts/functions.h b/src/parser/asts/functions.h index ff06c40..e486531 100644 --- a/src/parser/asts/functions.h +++ b/src/parser/asts/functions.h @@ -14,6 +14,6 @@ * @param result the Lexer result. * @param index the index of the start of the parsing. */ -AST_FUNCTION_DEC parseFunctionDeclaration(LEXER_RESULT result, int index); +AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index); #endif \ No newline at end of file From 57e38fd79990fddceba1bff1d0c2771073d2eb02 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 17:33:25 +0100 Subject: [PATCH 062/121] feat: added missing ending indexes --- src/parser/asts/values.c | 1 + src/parser/asts/variables.c | 5 +++++ src/parser/structs/values.h | 2 ++ 3 files changed, 8 insertions(+) diff --git a/src/parser/asts/values.c b/src/parser/asts/values.c index 0a33e2d..0661a9c 100644 --- a/src/parser/asts/values.c +++ b/src/parser/asts/values.c @@ -30,6 +30,7 @@ AST_VALUE* parseValue(LEXER_RESULT result, int index) { return value; } + value->endingIndex = index; value->value = result.tokens[index].value; return value; diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c index 49795c8..037e46c 100644 --- a/src/parser/asts/variables.c +++ b/src/parser/asts/variables.c @@ -6,6 +6,8 @@ #include #include "../structs/variables.h" +#include "../structs/tree.h" + #include "./values.h" #include "../ast.h" @@ -49,6 +51,8 @@ AST_VARIABLE_DEC* parseVariableDeclaration(LEXER_RESULT result, int index) { return NULL; } + var->endingIndex = ((AST_TREE_BRANCH*)value)->endingIndex; + var->value = value; } @@ -75,6 +79,7 @@ AST_VARIABLE_MOD* parseVariableModification(LEXER_RESULT result, int index) { } mod->value = value; + mod->endingIndex = ((AST_TREE_BRANCH*)value)->endingIndex; return mod; } \ No newline at end of file diff --git a/src/parser/structs/values.h b/src/parser/structs/values.h index be8d155..48dc56f 100644 --- a/src/parser/structs/values.h +++ b/src/parser/structs/values.h @@ -10,6 +10,8 @@ */ typedef struct AST_VALUE { unsigned char astType; + void* next; + int endingIndex; unsigned char* valueType; void* value; From ddb61dff2b1cb575c84166918ea01dd13189267e Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 22:25:58 +0100 Subject: [PATCH 063/121] feat: added argument parsing --- src/parser/asts/functions.c | 60 +++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 05d19c4..73fcf90 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -21,6 +21,8 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { func->type = AST_TYPE_FUNCTION_DECLARATION; + int offset; + switch(result.tokens[index + 2].type) { case PAREN_OPEN: if(result.tokens[index + 1].type != KEYWORD) { @@ -28,8 +30,9 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { return NULL; } - func->funcName = result.tokens[index + 0].value; + func->funcName = result.tokens[index + 1].value; func->returnType = 0x00; + offset = 3; break; case KEYWORD: if(result.tokens[index + 1].type != TYPE_INT32) { @@ -39,6 +42,7 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { func->funcName = result.tokens[index + 2].value; func->returnType = 0x01; + offset = 4; break; default: @@ -46,7 +50,57 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { return NULL; } - func->endingIndex = index + 2; + index += offset; + + int stack = 0; + + int allocated = 10; + func->parameterIndex = 0; + func->parameters = malloc(sizeof(AST_PARAMETER) * allocated); + + for(; index < result.size; ++index) { + TOKEN t = result.tokens[index]; + + switch(t.type) { + case TYPE_INT32: + if(result.tokens[index + 1].type != KEYWORD) { + printf("Error: Excepted keyword as parameter name!\n"); + return NULL; + } + + func->parameters[func->parameterIndex].name = result.tokens[index + 1].value; + func->parameters[func->parameterIndex].type = 0x01; // i32 - return func; + func->parameterIndex++; + if(func->parameterIndex > allocated) { + allocated *= 1.25; + func->parameters = realloc(func->parameters, sizeof(AST_PARAMETER), allocated); + } + break; + case KEYWORD: + if(result.tokens[index + 1].type != COMMA) { + printf("Error: Excepted comma after parameter!\n"); + return NULL; + } + + if(result.tokens[index - 1].type == COMMA) { + func->parameters[func->parameterIndex].name = result.tokens[index].value; + func->parameterIndex++; + if(func->parameterIndex > allocated) { + allocated *= 1.25; + func->parameters = realloc(func->parameters, sizeof(AST_PARAMETER), allocated); + } + } + + break; + + case PAREN_CLOSE: + func->endingIndex = index; + return func; + + default: + printf("Error: Disallowed token type %d in parameters!\n", t.type); + return NULL; + } + } } From 7e7a27cb300719c31850f42291b5df4148ecb2e0 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 22:28:24 +0100 Subject: [PATCH 064/121] feat: made the two function declaration structs castable --- src/parser/structs/functions.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/structs/functions.h b/src/parser/structs/functions.h index 1c3031c..3ecced9 100644 --- a/src/parser/structs/functions.h +++ b/src/parser/structs/functions.h @@ -25,11 +25,11 @@ typedef struct AST_FUNCTION_DEF { int endingIndex; char* funcName; - unsigned char* returnType; AST_PARAMETER* parameters; int parameterIndex; + unsigned char* returnType; void* body; } AST_FUNCTION_DEC; From b049088d9f1cef4c61213bb05855e8fc841c0983 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 22:29:28 +0100 Subject: [PATCH 065/121] feat: added parseParameters in header --- src/parser/asts/functions.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/parser/asts/functions.h b/src/parser/asts/functions.h index e486531..18f979b 100644 --- a/src/parser/asts/functions.h +++ b/src/parser/asts/functions.h @@ -16,4 +16,11 @@ */ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index); +/** + * Parses the parameters of a function into AST. + * @param result the Lexer result. + * @param index the index of the start of the parsing. + */ +void parseFunctionParameters(void* func, LEXER_RESULT result, int index); + #endif \ No newline at end of file From 8b91178710a34c9e5b5b211668fb58fcdd0cecf8 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 22:32:18 +0100 Subject: [PATCH 066/121] feat: added parseFunctionParameters' --- src/parser/asts/functions.c | 55 +++++++++++++++++++++++++++++++++++++ src/parser/asts/functions.h | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 73fcf90..75cc4a9 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -104,3 +104,58 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { } } } + +/** + * Parses the parameters of a function into AST. + * @param result the Lexer result. + * @param index the index of the start of the parsing. + */ +void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, int index) { + int allocated = 10; + + for(; index < result.size; ++index) { + TOKEN t = result.tokens[index]; + + switch(t.type) { + case TYPE_INT32: + if(result.tokens[index + 1].type != KEYWORD) { + printf("Error: Excepted keyword as parameter name!\n"); + return; + } + + func->parameters[func->parameterIndex].name = result.tokens[index + 1].value; + func->parameters[func->parameterIndex].type = 0x01; // i32 + + func->parameterIndex++; + if(func->parameterIndex > allocated) { + allocated *= 1.25; + func->parameters = realloc(func->parameters, sizeof(AST_PARAMETER), allocated); + } + break; + case KEYWORD: + if(result.tokens[index + 1].type != COMMA) { + printf("Error: Excepted comma after parameter!\n"); + return; + } + + if(result.tokens[index - 1].type == COMMA) { + func->parameters[func->parameterIndex].name = result.tokens[index].value; + func->parameterIndex++; + if(func->parameterIndex > allocated) { + allocated *= 1.25; + func->parameters = realloc(func->parameters, sizeof(AST_PARAMETER), allocated); + } + } + + break; + + case PAREN_CLOSE: + func->endingIndex = index; + return; + + default: + printf("Error: Disallowed token type %d in parameters!\n", t.type); + return; + } + } +} \ No newline at end of file diff --git a/src/parser/asts/functions.h b/src/parser/asts/functions.h index 18f979b..ce6563c 100644 --- a/src/parser/asts/functions.h +++ b/src/parser/asts/functions.h @@ -21,6 +21,6 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index); * @param result the Lexer result. * @param index the index of the start of the parsing. */ -void parseFunctionParameters(void* func, LEXER_RESULT result, int index); +void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, int index); #endif \ No newline at end of file From 9b2ecf56d1edddf18fb2bf8edf4b43358be8fb58 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 22:32:49 +0100 Subject: [PATCH 067/121] feat: made parseFunctionParameters to reduce overhead --- src/parser/asts/functions.c | 2 +- src/parser/asts/functions.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 75cc4a9..07e1520 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -110,7 +110,7 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { * @param result the Lexer result. * @param index the index of the start of the parsing. */ -void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, int index) { +inline void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, int index) { int allocated = 10; for(; index < result.size; ++index) { diff --git a/src/parser/asts/functions.h b/src/parser/asts/functions.h index ce6563c..5adede7 100644 --- a/src/parser/asts/functions.h +++ b/src/parser/asts/functions.h @@ -21,6 +21,6 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index); * @param result the Lexer result. * @param index the index of the start of the parsing. */ -void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, int index); +inline void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, int index); #endif \ No newline at end of file From 508c18c043b84a6f6363c04a4e500eef3ea1641b Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 22:33:31 +0100 Subject: [PATCH 068/121] feat: made parseFunctionDeclaration use parseFunctionParameters --- src/parser/asts/functions.c | 52 ++----------------------------------- 1 file changed, 2 insertions(+), 50 deletions(-) diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 07e1520..35f0aae 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -52,57 +52,9 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { index += offset; - int stack = 0; + parseFunctionParameters(func, result, index); - int allocated = 10; - func->parameterIndex = 0; - func->parameters = malloc(sizeof(AST_PARAMETER) * allocated); - - for(; index < result.size; ++index) { - TOKEN t = result.tokens[index]; - - switch(t.type) { - case TYPE_INT32: - if(result.tokens[index + 1].type != KEYWORD) { - printf("Error: Excepted keyword as parameter name!\n"); - return NULL; - } - - func->parameters[func->parameterIndex].name = result.tokens[index + 1].value; - func->parameters[func->parameterIndex].type = 0x01; // i32 - - func->parameterIndex++; - if(func->parameterIndex > allocated) { - allocated *= 1.25; - func->parameters = realloc(func->parameters, sizeof(AST_PARAMETER), allocated); - } - break; - case KEYWORD: - if(result.tokens[index + 1].type != COMMA) { - printf("Error: Excepted comma after parameter!\n"); - return NULL; - } - - if(result.tokens[index - 1].type == COMMA) { - func->parameters[func->parameterIndex].name = result.tokens[index].value; - func->parameterIndex++; - if(func->parameterIndex > allocated) { - allocated *= 1.25; - func->parameters = realloc(func->parameters, sizeof(AST_PARAMETER), allocated); - } - } - - break; - - case PAREN_CLOSE: - func->endingIndex = index; - return func; - - default: - printf("Error: Disallowed token type %d in parameters!\n", t.type); - return NULL; - } - } + return func; } /** From 48ee07673d79d742bbc4efb1c6d7033d40117fbd Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 22:34:51 +0100 Subject: [PATCH 069/121] feat: added parseASMFunctionDeclaration in header --- src/parser/asts/functions.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/parser/asts/functions.h b/src/parser/asts/functions.h index 5adede7..3ae257a 100644 --- a/src/parser/asts/functions.h +++ b/src/parser/asts/functions.h @@ -16,6 +16,13 @@ */ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index); +/** + * Parses an ASM function declaration into AST. + * @param result the Lexer result. + * @param index the index of the start of the parsing. + */ +AST_ASM_FUNCTION_DEC* parseASMFunctionDeclaration(LEXER_RESULT result, int index); + /** * Parses the parameters of a function into AST. * @param result the Lexer result. From 68ac28db566677e3f9b1a5e0bdb7d550ece709c5 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 26 Dec 2024 22:39:43 +0100 Subject: [PATCH 070/121] feat: added parseASMFunctionDeclaration --- src/parser/ast.h | 1 + src/parser/asts/functions.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/parser/ast.h b/src/parser/ast.h index e966dac..666587c 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -11,6 +11,7 @@ typedef enum { AST_TYPE_FUNC_ROOT, AST_TYPE_FUNCTION_DECLARATION, + AST_TYPE_ASM_FUNCTION_DECLARATION, AST_TYPE_VARIABLE_DECLARATION, AST_TYPE_VARIABLE_MODIFICATION, diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 35f0aae..b710fd7 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -57,6 +57,39 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { return func; } + +/** + * Parses an ASM function declaration into AST. + * @param result the Lexer result. + * @param index the index of the start of the parsing. + */ +AST_ASM_FUNCTION_DEC* parseASMFunctionDeclaration(LEXER_RESULT result, int index) { + AST_ASM_FUNCTION_DEC* func = malloc(sizeof(AST_ASM_FUNCTION_DEC)); + + if(result.tokens[index + 1].type != KEYWORD) { + printf("Error: Excepted keyword as ASM function name!\n"); + return NULL; + } + + func->type = AST_TYPE_ASM_FUNCTION_DECLARATION; + func->funcName = result.tokens[index + 1].value; + + parseFunctionParameters(func, result, index + 3); + + index = func->endingIndex; + + if(result.tokens[index + 1].type != BRACKETS_OPEN || result.tokens[index + 2].type != STRING || result.tokens[index + 3].type != BRACKETS_CLOSE) { + printf("Error: Badly made ASM function body!\n"); + return NULL; + } + + func->buff = result.tokens[index + 2].value; + func->buffIndex = strlen(func->buff); + + return func; +} + + /** * Parses the parameters of a function into AST. * @param result the Lexer result. From 45415f2ab0eecb1ab2ced6e376235b7129c964ee Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 28 Dec 2024 12:51:58 +0100 Subject: [PATCH 071/121] feat: added function body parsing --- src/parser/asts/functions.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index b710fd7..9816a11 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -7,6 +7,7 @@ #include "../structs/functions.h" +#include "../parser.h" #include "../ast.h" #include "../../lexer/lexer.h" @@ -54,6 +55,21 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { parseFunctionParameters(func, result, index); + if(result.tokens[func->endingIndex + 1].type != BRACKETS_OPEN) { + printf("Error: Excepted function body!\n"); + return NULL; + } + + void* root = parseRoot(result, func->endingIndex + 2, AST_TYPE_FUNC_ROOT); + + if(root == NULL) { + printf("Error: couldn't parse function body!\n"); + return NULL; + } + + func->body = root; + func->endingIndex = ((AST_TREE_BRANCH*)root)->endingIndex; + return func; } From 3f857bb246899817376b93ab895449c415fdc89f Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 28 Dec 2024 13:25:34 +0100 Subject: [PATCH 072/121] feat: updated ir function parsing --- src/ir/ir.c | 45 ++++++--------------------------------------- src/ir/ir.h | 4 +++- 2 files changed, 9 insertions(+), 40 deletions(-) diff --git a/src/ir/ir.c b/src/ir/ir.c index 145ea37..b65284f 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -8,6 +8,9 @@ #include "./instructions.h" #include "./structs.h" +#include "../../parser/structs/tree.h" +#include "../../parser/structs/functions.h" + #include "../../parser/ast.h" /** @@ -38,51 +41,15 @@ void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned * Parses a AST function into IR. * @param node the AST node representing the function. */ -IR_FUNCTION parseFunction(AST_NODE* node) { +IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { IR_FUNCTION func = {0}; func.blocks = malloc(sizeof(IR_BASIC_BLOCK)); func.blockCount++; + func.funcName = node->funcName; + func.blocks[0].instructions = NULL; func.blocks[0].instructionCount = 0; - while(node != NULL) { - - switch(node->type) { - case AST_VARIABLE_DECLARATION: - // For now let's just say that every variable is 32 bits. - //todo: change this when typing gets added. - - int size = strlen(node->left->value) + sizeof(int); - - unsigned char* ptr = malloc(size); - - int i = 0; - char c; - - while(c = *node->left->value++) { - ptr[i] = c; - ++i; - - if(c == '\0') break; - } - - ptr[i] = (32 >> 24) & 0xFF; - ptr[i + 1] = (32 >> 16) & 0xFF; - ptr[i + 2] = (32 >> 8) & 0xFF; - ptr[i + 3] = 32 & 0xFF; - - appendInstruction(func.blocks[0], S_ALLOC, ptr, size); - - ptr = malloc(sizeof(int)); - ((int*)ptr)[0] = atoi(node->right->value); - - appendInstruction(func.blocks[0], PTR_SET, ptr, sizeof(int)); - break; - } - - node = node->next; - } - return func; } \ No newline at end of file diff --git a/src/ir/ir.h b/src/ir/ir.h index 67adca0..0319d7c 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -8,6 +8,8 @@ #include "./instructions.h" #include "./structs.h" +#include "../../parser/structs/functions.h" + #include "../../parser/ast.h" /** @@ -23,6 +25,6 @@ void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned * Parses a AST function into IR. * @param node the AST node representing the function. */ -IR_FUNCTION parseFunction(AST_NODE* node); +IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node); #endif \ No newline at end of file From 8893f0c7b2ad8ef3e74d3cd12efbe64e49598bea Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 28 Dec 2024 13:34:04 +0100 Subject: [PATCH 073/121] feat: added S_ALLOC ir instruction for variable dec in function IR --- src/ir/ir.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/ir/ir.c b/src/ir/ir.c index b65284f..d11d9da 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -10,6 +10,7 @@ #include "../../parser/structs/tree.h" #include "../../parser/structs/functions.h" +#include "../../parser/structs/variables.h" #include "../../parser/ast.h" @@ -51,5 +52,42 @@ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { func.blocks[0].instructions = NULL; func.blocks[0].instructionCount = 0; + //todo: move this to another function when finished + while(node->body != NULL) { + AST_TREE_BRANCH* b = (AST_TREE_BRANCH*) node->body; + + switch(b->type) { + case AST_TYPE_VARIABLE_DECLARATION: + AST_VARIABLE_DEC* var = (AST_VARIABLE_DEC*) b; + + int size = 0; + if(var->type == 0x01) size = 32; // int32 + + int paramsSize = 4 * strlen(var->name); + unsigned char* params = malloc(paramsSize); + + params[0] = (32 >> 24) & 0xFF; + params[1] = (32 >> 16) & 0xFF; + params[2] = (32 >> 8) & 0xFF; + params[3] = 32 & 0xFF; + + int i = 0; + char c; + + while(c = *var->name++) { + params[4 + i] = c; + + if(c == '\0') break; + ++i; + } + + appendInstruction(func.blocks[0], S_ALLOC, params, paramsSize); + break; + + } + + node->body = b->next; + } + return func; } \ No newline at end of file From 3cee5745776e1e3cc70fbdca73e46e87f7c9bc11 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 28 Dec 2024 13:44:24 +0100 Subject: [PATCH 074/121] feat: added variable value in IR --- src/ir/ir.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/ir/ir.c b/src/ir/ir.c index d11d9da..24ba35a 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -11,6 +11,7 @@ #include "../../parser/structs/tree.h" #include "../../parser/structs/functions.h" #include "../../parser/structs/variables.h" +#include "../../parser/structs/values.h" #include "../../parser/ast.h" @@ -66,10 +67,10 @@ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { int paramsSize = 4 * strlen(var->name); unsigned char* params = malloc(paramsSize); - params[0] = (32 >> 24) & 0xFF; - params[1] = (32 >> 16) & 0xFF; - params[2] = (32 >> 8) & 0xFF; - params[3] = 32 & 0xFF; + params[0] = (size >> 24) & 0xFF; + params[1] = (size >> 16) & 0xFF; + params[2] = (size >> 8) & 0xFF; + params[3] = size & 0xFF; int i = 0; char c; @@ -82,6 +83,34 @@ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { } appendInstruction(func.blocks[0], S_ALLOC, params, paramsSize); + + if(var->value != NULL) { + if(((AST_TREE_BRANCH*)var->value)->type == AST_TYPE_VALUE) { + AST_VALUE* val = (AST_VALUE*)var->value; + + if(val->valueType != var->type) { + printf("Error: the variable value doesn't match the variable type!\n"); + return; + } + + int size; + + if(var->type == 0x01) { // i32 + size = 4; + params = malloc(4); + + int num = atoi(val->value); + + params[0] = (num >> 24) & 0xFF; + params[1] = (num >> 16) & 0xFF; + params[2] = (num >> 8) & 0xFF; + params[3] = num & 0xFF; + } + + appendInstruction(func.blocks[0], PTR_SET, params, size); + } + } + break; } From aabc44253bc863c83bf397d0d2bf903cd240880b Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 29 Dec 2024 15:24:38 +0100 Subject: [PATCH 075/121] feat: added parseVariableDeclaration in header --- src/ir/ir.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ir/ir.h b/src/ir/ir.h index 0319d7c..2af4e40 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -8,6 +8,7 @@ #include "./instructions.h" #include "./structs.h" +#include "../../parser/structs/variables.h" #include "../../parser/structs/functions.h" #include "../../parser/ast.h" @@ -21,6 +22,13 @@ */ void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned char params[], int paramsCount); +/** + * Parses a variable declaration. + * @param block the IR basic block to append to. + * @param node the AST node representing the variable. + */ +inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* node); + /** * Parses a AST function into IR. * @param node the AST node representing the function. From a4137f48a4bda28ffd10016fffec172ac4ba016e Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 29 Dec 2024 15:33:24 +0100 Subject: [PATCH 076/121] feat: started moving the ir parsing into thier respective categories --- src/ir/ir.c | 31 +++++++++++++++++++++++++++++++ src/ir/irs/variables.h | 20 ++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/ir/irs/variables.h diff --git a/src/ir/ir.c b/src/ir/ir.c index 24ba35a..4a8b1f2 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -39,6 +39,37 @@ void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned block.instructionCount++; } +/** + * Parses a variable declaration. + * @param block the IR basic block to append to. + * @param node the AST node representing the variable. + */ +inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* node) { + int allocSize = 0; + + if(node->type == 0x01) allocSize = 32; // int32 + + int paramsSize = 4 + strlen(node->name); + unsigned char* params = malloc(paramsSize); + + int i; + char c; + + while(c = *node->name++) { + params[i] = c; + + if(c == '\0') break; + ++i; + } + + params[i + 1] = (allocSize >> 24) & 0xFF; + params[i + 2] = (allocSize >> 16) & 0xFF; + params[i + 3] = (allocSize >> 8) & 0xFF; + params[i + 4] = allocSize & 0xFF; + + appendInstruction(block, S_ALLOC, params, paramsSize); +} + /** * Parses a AST function into IR. * @param node the AST node representing the function. diff --git a/src/ir/irs/variables.h b/src/ir/irs/variables.h new file mode 100644 index 0000000..f5f8ee2 --- /dev/null +++ b/src/ir/irs/variables.h @@ -0,0 +1,20 @@ +/** + * IR for variable related. + */ + +#include "../../parser/structs/variables.h" + +#include "../ir.h" + +#ifndef IR_VARIABLES_H +#define IR_VARIABLES_H + +/** + * Parses a variable declaration. + * @param block the IR basic block to append to. + * @param node the AST node representing the variable. + */ +inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* node); + + +#endif \ No newline at end of file From b6895cdc605fc7a89b3150234463ee0ebab2ff77 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 30 Dec 2024 14:59:27 +0100 Subject: [PATCH 077/121] feat: added parseValue in header --- src/ir/irs/values.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/ir/irs/values.h diff --git a/src/ir/irs/values.h b/src/ir/irs/values.h new file mode 100644 index 0000000..a93e6d8 --- /dev/null +++ b/src/ir/irs/values.h @@ -0,0 +1,16 @@ +/** + * IR for value related. + */ + +#ifndef IR_VALUES_H +#define IR_VALUES_H + +/** + * Parses the value into the buffer. + * @param buff the byte buffer. + * @param startIndex the starting index of the buffer. + * @param value the value to parse. + */ +void parseValue(unsigned char* buff, int startIndex, void* value); + +#endif \ No newline at end of file From d470b5a2cb702cc08ea0ff7d94c81c23f6514db9 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 30 Dec 2024 15:04:14 +0100 Subject: [PATCH 078/121] feat: added parseValue impl --- src/ir/irs/values.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/ir/irs/values.c diff --git a/src/ir/irs/values.c b/src/ir/irs/values.c new file mode 100644 index 0000000..48c772c --- /dev/null +++ b/src/ir/irs/values.c @@ -0,0 +1,31 @@ +/** + * IR for value related. + */ + +#include + +#include "../../parser/ast.h" + +#include "../../parser/structs/values.h" +#include "../../parser/structs/tree.h" + +/** + * Parses the value into the buffer. + * @param buff the byte buffer. + * @param startIndex the starting index of the buffer. + * @param value the value to parse. + */ +void parseValue(unsigned char* buff, int startIndex, void* value) { + if(((AST_TREE_BRANCH*)value)->type == AST_TYPE_VALUE) { + AST_VALUE* val = (AST_VALUE*) value; + + if(val->valueType == 0x01) { // int32 + int num = atoi(val->value); + + buff[startIndex + 1] = (num >> 24) & 0xFF; + buff[startIndex + 2] = (num >> 16) & 0xFF; + buff[startIndex + 3] = (num >> 8) & 0xFF; + buff[startIndex + 4] = num & 0xFF; + } + } +} \ No newline at end of file From 1a1916285686d8054a39be368c2ef5b609745859 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 30 Dec 2024 15:05:10 +0100 Subject: [PATCH 079/121] feat: made parseValue return end index --- src/ir/irs/values.c | 6 +++++- src/ir/irs/values.h | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ir/irs/values.c b/src/ir/irs/values.c index 48c772c..9b436fc 100644 --- a/src/ir/irs/values.c +++ b/src/ir/irs/values.c @@ -15,7 +15,7 @@ * @param startIndex the starting index of the buffer. * @param value the value to parse. */ -void parseValue(unsigned char* buff, int startIndex, void* value) { +int parseValue(unsigned char* buff, int startIndex, void* value) { if(((AST_TREE_BRANCH*)value)->type == AST_TYPE_VALUE) { AST_VALUE* val = (AST_VALUE*) value; @@ -26,6 +26,10 @@ void parseValue(unsigned char* buff, int startIndex, void* value) { buff[startIndex + 2] = (num >> 16) & 0xFF; buff[startIndex + 3] = (num >> 8) & 0xFF; buff[startIndex + 4] = num & 0xFF; + + return startIndex + 4; } } + + return startIndex; } \ No newline at end of file diff --git a/src/ir/irs/values.h b/src/ir/irs/values.h index a93e6d8..1148f5e 100644 --- a/src/ir/irs/values.h +++ b/src/ir/irs/values.h @@ -11,6 +11,6 @@ * @param startIndex the starting index of the buffer. * @param value the value to parse. */ -void parseValue(unsigned char* buff, int startIndex, void* value); +int parseValue(unsigned char* buff, int startIndex, void* value); #endif \ No newline at end of file From dc220e5b5ed49c7ec17b42e1cb8e37633b0f42c1 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 30 Dec 2024 15:08:05 +0100 Subject: [PATCH 080/121] feat: implemented parseVariableDeclaration in new irs file --- src/ir/irs/variables.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/ir/irs/variables.c diff --git a/src/ir/irs/variables.c b/src/ir/irs/variables.c new file mode 100644 index 0000000..af3756b --- /dev/null +++ b/src/ir/irs/variables.c @@ -0,0 +1,38 @@ +/** + * IR for variable related. + */ + +#include "../../parser/structs/variables.h" + +#include "../ir.h" + +/** + * Parses a variable declaration. + * @param block the IR basic block to append to. + * @param node the AST node representing the variable. + */ +inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* node) { + int allocSize = 0; + + if(node->type == 0x01) allocSize = 32; // int32 + + int paramsSize = 4 + strlen(node->name); + unsigned char* params = malloc(paramsSize); + + int i; + char c; + + while(c = *node->name++) { + params[i] = c; + + if(c == '\0') break; + ++i; + } + + params[i + 1] = (allocSize >> 24) & 0xFF; + params[i + 2] = (allocSize >> 16) & 0xFF; + params[i + 3] = (allocSize >> 8) & 0xFF; + params[i + 4] = allocSize & 0xFF; + + appendInstruction(block, S_ALLOC, params, paramsSize); +} \ No newline at end of file From c61f206cc885168e317a9ebb3e7d181b3ac46474 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 30 Dec 2024 15:11:57 +0100 Subject: [PATCH 081/121] feat: added getValueSize header --- src/ir/irs/values.h | 6 ++++++ src/ir/irs/variables.c | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/ir/irs/values.h b/src/ir/irs/values.h index 1148f5e..f4ef8bf 100644 --- a/src/ir/irs/values.h +++ b/src/ir/irs/values.h @@ -13,4 +13,10 @@ */ int parseValue(unsigned char* buff, int startIndex, void* value); +/** + * Gets the value size for a certain type for a parameter. + * @param type the type's byte indentifier. + */ +inline int getValueSize(unsigned char type); + #endif \ No newline at end of file diff --git a/src/ir/irs/variables.c b/src/ir/irs/variables.c index af3756b..04d8933 100644 --- a/src/ir/irs/variables.c +++ b/src/ir/irs/variables.c @@ -35,4 +35,8 @@ inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* nod params[i + 4] = allocSize & 0xFF; appendInstruction(block, S_ALLOC, params, paramsSize); + + if(node->value != NULL) { + + } } \ No newline at end of file From d2ba9e03ebe2655f2939358c7335fd7182f03752 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 30 Dec 2024 15:12:45 +0100 Subject: [PATCH 082/121] feat: added getValueSize --- src/ir/irs/values.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ir/irs/values.c b/src/ir/irs/values.c index 9b436fc..0dd1e9f 100644 --- a/src/ir/irs/values.c +++ b/src/ir/irs/values.c @@ -32,4 +32,13 @@ int parseValue(unsigned char* buff, int startIndex, void* value) { } return startIndex; -} \ No newline at end of file +} + +/** + * Gets the value size for a certain type for a parameter. + * @param type the type's byte indentifier. + */ +inline int getValueSize(unsigned char type) { + if(type == 0x01) return 4; // int32 type -> 4 bytes + return 0; +} From cfaced2a6d94579daf2c48169fff3302b9d1eed5 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 30 Dec 2024 15:17:33 +0100 Subject: [PATCH 083/121] feat: added value handling in parseVariableDeclaration --- src/ir/irs/variables.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/ir/irs/variables.c b/src/ir/irs/variables.c index 04d8933..f30d93d 100644 --- a/src/ir/irs/variables.c +++ b/src/ir/irs/variables.c @@ -4,6 +4,8 @@ #include "../../parser/structs/variables.h" +#include "./values.h" + #include "../ir.h" /** @@ -19,6 +21,8 @@ inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* nod int paramsSize = 4 + strlen(node->name); unsigned char* params = malloc(paramsSize); + char* name = node->name; + int i; char c; @@ -37,6 +41,19 @@ inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* nod appendInstruction(block, S_ALLOC, params, paramsSize); if(node->value != NULL) { + paramsSize = strlen(name) + getValueSize(node->type); + params = malloc(paramsSize); + i = 0; + while(c = *name++) { + params[i] = c; + + if(c == '\0') break; + ++i; + } + + parseValue(params, i, node->value); + + appendInstruction(block, PTR_SET, params, paramsSize); } } \ No newline at end of file From 26452633309778bb774ba4f0fc62452107aa9867 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 30 Dec 2024 15:19:12 +0100 Subject: [PATCH 084/121] fix: remove broken includes for now --- src/compiler/compiler.c | 6 ------ src/compiler/compiler.h | 3 --- 2 files changed, 9 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index a37f77a..0ffd2b5 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -7,12 +7,6 @@ #include #include "./compiler.h" -#include "./ir.h" - -#include "../parser/ast.h" - -#include "../utils/hash.h" -#include "../utils/hashmap.h" #include "./pe/pe.h" diff --git a/src/compiler/compiler.h b/src/compiler/compiler.h index d998191..c0e36ea 100644 --- a/src/compiler/compiler.h +++ b/src/compiler/compiler.h @@ -5,9 +5,6 @@ #ifndef COMPILER_H #define COMPILER_H -#include "../utils/hashmap.h" -#include "../parser/ast.h" - /** * Compiles the Context tree to an executable named the provided name. * @param ctx the IR context. From 2d7580441d7db25779da276d76f8c64e9c420b44 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 30 Dec 2024 15:32:38 +0100 Subject: [PATCH 085/121] fix: compiling fixes --- src/cli/main.c | 15 +++------------ src/ir/ir.c | 13 +++++++------ src/ir/ir.h | 6 +++--- src/ir/irs/values.c | 2 +- src/ir/irs/variables.c | 7 +++++-- src/parser/asts/functions.c | 18 +++++++++++------- src/parser/asts/functions.h | 2 +- src/parser/asts/values.c | 6 +++--- src/parser/asts/values.h | 2 +- src/parser/parser.c | 9 ++++++--- src/parser/parser.h | 11 ++++++++++- 11 files changed, 51 insertions(+), 40 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index 3943aa1..096bbd2 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -18,7 +18,8 @@ #include "../compiler/compiler.h" #include "../compiler/pe/pe.h" -#include "../compiler/ir.h" + +#include "../ir/ir.h" #include "../utils/logging.c" @@ -114,17 +115,7 @@ int main(int argc, char* argv[]) { fclose(fptr); LEXER_RESULT result = runLexer(buff, size); - AST_NODE* root = parseNodes(result, 0, AST_ROOT); - - IR_CTX* ctx = makeContext(root); - - 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"); - compile(ctx, fptr); + void* root = parseRoot(result, 0, AST_TYPE_ROOT); break; case 'v': diff --git a/src/ir/ir.c b/src/ir/ir.c index 4a8b1f2..0bd246c 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -2,18 +2,19 @@ * The Intermediate Representation of Quickfall Code. */ +#include #include #include #include "./instructions.h" #include "./structs.h" -#include "../../parser/structs/tree.h" -#include "../../parser/structs/functions.h" -#include "../../parser/structs/variables.h" -#include "../../parser/structs/values.h" +#include "../parser/structs/tree.h" +#include "../parser/structs/functions.h" +#include "../parser/structs/variables.h" +#include "../parser/structs/values.h" -#include "../../parser/ast.h" +#include "../parser/ast.h" /** * Appends an IR instruction into the basic block. @@ -121,7 +122,7 @@ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { if(val->valueType != var->type) { printf("Error: the variable value doesn't match the variable type!\n"); - return; + return func; } int size; diff --git a/src/ir/ir.h b/src/ir/ir.h index 2af4e40..229ac4a 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -8,10 +8,10 @@ #include "./instructions.h" #include "./structs.h" -#include "../../parser/structs/variables.h" -#include "../../parser/structs/functions.h" +#include "../parser/structs/variables.h" +#include "../parser/structs/functions.h" -#include "../../parser/ast.h" +#include "../parser/ast.h" /** * Appends an IR instruction into the basic block. diff --git a/src/ir/irs/values.c b/src/ir/irs/values.c index 0dd1e9f..ab7e153 100644 --- a/src/ir/irs/values.c +++ b/src/ir/irs/values.c @@ -2,7 +2,7 @@ * IR for value related. */ -#include +#include #include "../../parser/ast.h" diff --git a/src/ir/irs/variables.c b/src/ir/irs/variables.c index f30d93d..2b27b69 100644 --- a/src/ir/irs/variables.c +++ b/src/ir/irs/variables.c @@ -2,6 +2,9 @@ * IR for variable related. */ +#include +#include + #include "../../parser/structs/variables.h" #include "./values.h" @@ -16,7 +19,7 @@ inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* node) { int allocSize = 0; - if(node->type == 0x01) allocSize = 32; // int32 + if(node->type[0] == 0x01) allocSize = 32; // int32 int paramsSize = 4 + strlen(node->name); unsigned char* params = malloc(paramsSize); @@ -41,7 +44,7 @@ inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* nod appendInstruction(block, S_ALLOC, params, paramsSize); if(node->value != NULL) { - paramsSize = strlen(name) + getValueSize(node->type); + paramsSize = strlen(name) + getValueSize(node->type[0]); params = malloc(paramsSize); i = 0; diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 9816a11..b3a3c29 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -2,9 +2,12 @@ * Parsing for function related ASTs. */ +#include #include #include +#include "./functions.h" + #include "../structs/functions.h" #include "../parser.h" @@ -32,7 +35,7 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { } func->funcName = result.tokens[index + 1].value; - func->returnType = 0x00; + func->returnType[0] = 0x00; offset = 3; break; case KEYWORD: @@ -42,7 +45,7 @@ AST_FUNCTION_DEC* parseFunctionDeclaration(LEXER_RESULT result, int index) { } func->funcName = result.tokens[index + 2].value; - func->returnType = 0x01; + func->returnType[0] = 0x01; offset = 4; break; @@ -90,7 +93,7 @@ AST_ASM_FUNCTION_DEC* parseASMFunctionDeclaration(LEXER_RESULT result, int index func->type = AST_TYPE_ASM_FUNCTION_DECLARATION; func->funcName = result.tokens[index + 1].value; - parseFunctionParameters(func, result, index + 3); + parseFunctionParameters((AST_FUNCTION_DEC*)func, result, index + 3); index = func->endingIndex; @@ -111,7 +114,7 @@ AST_ASM_FUNCTION_DEC* parseASMFunctionDeclaration(LEXER_RESULT result, int index * @param result the Lexer result. * @param index the index of the start of the parsing. */ -inline void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, int index) { +void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, int index) { int allocated = 10; for(; index < result.size; ++index) { @@ -125,12 +128,12 @@ inline void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, } func->parameters[func->parameterIndex].name = result.tokens[index + 1].value; - func->parameters[func->parameterIndex].type = 0x01; // i32 + func->parameters[func->parameterIndex].type[0] = 0x01; // i32 func->parameterIndex++; if(func->parameterIndex > allocated) { allocated *= 1.25; - func->parameters = realloc(func->parameters, sizeof(AST_PARAMETER), allocated); + func->parameters = realloc(func->parameters, sizeof(AST_PARAMETER) * allocated); } break; case KEYWORD: @@ -141,10 +144,11 @@ inline void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, if(result.tokens[index - 1].type == COMMA) { func->parameters[func->parameterIndex].name = result.tokens[index].value; + func->parameters[func->parameterIndex].type[0] = 0x00; func->parameterIndex++; if(func->parameterIndex > allocated) { allocated *= 1.25; - func->parameters = realloc(func->parameters, sizeof(AST_PARAMETER), allocated); + func->parameters = realloc(func->parameters, sizeof(AST_PARAMETER) * allocated); } } diff --git a/src/parser/asts/functions.h b/src/parser/asts/functions.h index 3ae257a..22884ce 100644 --- a/src/parser/asts/functions.h +++ b/src/parser/asts/functions.h @@ -28,6 +28,6 @@ AST_ASM_FUNCTION_DEC* parseASMFunctionDeclaration(LEXER_RESULT result, int index * @param result the Lexer result. * @param index the index of the start of the parsing. */ -inline void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, int index); +void parseFunctionParameters(AST_FUNCTION_DEC* func, LEXER_RESULT result, int index); #endif \ No newline at end of file diff --git a/src/parser/asts/values.c b/src/parser/asts/values.c index 0661a9c..1048a08 100644 --- a/src/parser/asts/values.c +++ b/src/parser/asts/values.c @@ -16,14 +16,14 @@ * @param result the Lexer result. * @param index the index of the start of the parsing. */ -AST_VALUE* parseValue(LEXER_RESULT result, int index) { +AST_VALUE* parseASTValue(LEXER_RESULT result, int index) { AST_VALUE* value = malloc(sizeof(AST_VALUE)); value->astType = AST_TYPE_VALUE; switch(result.tokens[index].type) { case NUMBER: - value->valueType = 0x01; //i32 + value->valueType[0] = 0x01; //i32 break; default: printf("Error: Couldn't parse token %d as a value!\n", result.tokens[index].type); @@ -49,7 +49,7 @@ void* parseValueGroup(LEXER_RESULT result, int index) { //todo: parse math op. } - return parseValue(result, index); + return parseASTValue(result, index); break; default: printf("Error: couldn't parse value token group!\n"); diff --git a/src/parser/asts/values.h b/src/parser/asts/values.h index f404bb8..0c96b4e 100644 --- a/src/parser/asts/values.h +++ b/src/parser/asts/values.h @@ -14,7 +14,7 @@ * @param result the Lexer result. * @param index the index of the start of the parsing. */ -AST_VALUE* parseValue(LEXER_RESULT result, int index); +AST_VALUE* parseASTValue(LEXER_RESULT result, int index); /** * Parses an actual expression as value instead of just one token. diff --git a/src/parser/parser.c b/src/parser/parser.c index 672c02a..1f0850d 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -4,6 +4,8 @@ #include +#include "./parser.h" + #include "./ast.h" #include "../lexer/lexer.h" @@ -16,13 +18,14 @@ /** * Parses the Lexer result into an AST root. * @param result the Lexer result. + * @param startingIndex the starting Index. * @param type the output AST type. */ -void* parseRoot(LEXER_RESULT result, AST_TYPE type) { +void* parseRoot(LEXER_RESULT result, int startingIndex, AST_TYPE type) { AST_TREE_BRANCH* root = NULL; AST_TREE_BRANCH* curr = NULL; - for(int i = 0; i < result.size; ++i) { + for(int i = startingIndex; i < result.size; ++i) { TOKEN t = result.tokens[i]; switch(t.type) { @@ -62,7 +65,7 @@ void* parseRoot(LEXER_RESULT result, AST_TYPE type) { * @param root the root of the AST tree. * @param node the node to add to the tree. */ -inline void append(AST_TREE_BRANCH* curr, AST_TREE_BRANCH* root, void* node) { +void append(AST_TREE_BRANCH* curr, AST_TREE_BRANCH* root, void* node) { if(node == NULL) return; if(curr == NULL) { diff --git a/src/parser/parser.h b/src/parser/parser.h index ebd312d..605bba4 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -13,8 +13,17 @@ /** * Parses the Lexer result into an AST root. * @param result the Lexer result. + * @param startIndex the starting Index. * @param type the output AST type. */ -void* parseRoot(LEXER_RESULT result, AST_TYPE type); +void* parseRoot(LEXER_RESULT result, int startingIndex, AST_TYPE type); + +/** + * Appends a new node in the AST tree. + * @param curr the current AST node. + * @param root the root of the AST tree. + * @param node the node to add to the tree. + */ +void append(AST_TREE_BRANCH* curr, AST_TREE_BRANCH* root, void* node); #endif \ No newline at end of file From bd95525ad42a66aea622e3f158ba2741efeb87a5 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 30 Dec 2024 16:35:47 +0100 Subject: [PATCH 086/121] feat: finished moving parseVariableDeclaration --- src/ir/ir.c | 34 +++------------------------------- src/ir/ir.h | 7 ------- src/parser/asts/values.c | 1 - 3 files changed, 3 insertions(+), 39 deletions(-) diff --git a/src/ir/ir.c b/src/ir/ir.c index 0bd246c..f0a7677 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -14,6 +14,9 @@ #include "../parser/structs/variables.h" #include "../parser/structs/values.h" +#include "./irs/variables.h" +#include "./irs/values.h" + #include "../parser/ast.h" /** @@ -40,37 +43,6 @@ void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned block.instructionCount++; } -/** - * Parses a variable declaration. - * @param block the IR basic block to append to. - * @param node the AST node representing the variable. - */ -inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* node) { - int allocSize = 0; - - if(node->type == 0x01) allocSize = 32; // int32 - - int paramsSize = 4 + strlen(node->name); - unsigned char* params = malloc(paramsSize); - - int i; - char c; - - while(c = *node->name++) { - params[i] = c; - - if(c == '\0') break; - ++i; - } - - params[i + 1] = (allocSize >> 24) & 0xFF; - params[i + 2] = (allocSize >> 16) & 0xFF; - params[i + 3] = (allocSize >> 8) & 0xFF; - params[i + 4] = allocSize & 0xFF; - - appendInstruction(block, S_ALLOC, params, paramsSize); -} - /** * Parses a AST function into IR. * @param node the AST node representing the function. diff --git a/src/ir/ir.h b/src/ir/ir.h index 229ac4a..59192e5 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -22,13 +22,6 @@ */ void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned char params[], int paramsCount); -/** - * Parses a variable declaration. - * @param block the IR basic block to append to. - * @param node the AST node representing the variable. - */ -inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* node); - /** * Parses a AST function into IR. * @param node the AST node representing the function. diff --git a/src/parser/asts/values.c b/src/parser/asts/values.c index 1048a08..7ca5b58 100644 --- a/src/parser/asts/values.c +++ b/src/parser/asts/values.c @@ -36,7 +36,6 @@ AST_VALUE* parseASTValue(LEXER_RESULT result, int index) { return value; } - /** * Parses an actual expression as value instead of just one token. * @param result the Lexer result. From b785c4f237114a7cb52495d0cb83bfa9b2f27f30 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 1 Jan 2025 15:49:01 +0100 Subject: [PATCH 087/121] feat: moved variable parsing of parseFunction to parseVariableDeclaration --- src/ir/ir.c | 52 +--------------------------------------------------- 1 file changed, 1 insertion(+), 51 deletions(-) diff --git a/src/ir/ir.c b/src/ir/ir.c index f0a7677..17ac3e5 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -63,58 +63,8 @@ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { switch(b->type) { case AST_TYPE_VARIABLE_DECLARATION: - AST_VARIABLE_DEC* var = (AST_VARIABLE_DEC*) b; + parseVariableDeclaration(func.blocks[0], node); - int size = 0; - if(var->type == 0x01) size = 32; // int32 - - int paramsSize = 4 * strlen(var->name); - unsigned char* params = malloc(paramsSize); - - params[0] = (size >> 24) & 0xFF; - params[1] = (size >> 16) & 0xFF; - params[2] = (size >> 8) & 0xFF; - params[3] = size & 0xFF; - - int i = 0; - char c; - - while(c = *var->name++) { - params[4 + i] = c; - - if(c == '\0') break; - ++i; - } - - appendInstruction(func.blocks[0], S_ALLOC, params, paramsSize); - - if(var->value != NULL) { - if(((AST_TREE_BRANCH*)var->value)->type == AST_TYPE_VALUE) { - AST_VALUE* val = (AST_VALUE*)var->value; - - if(val->valueType != var->type) { - printf("Error: the variable value doesn't match the variable type!\n"); - return func; - } - - int size; - - if(var->type == 0x01) { // i32 - size = 4; - params = malloc(4); - - int num = atoi(val->value); - - params[0] = (num >> 24) & 0xFF; - params[1] = (num >> 16) & 0xFF; - params[2] = (num >> 8) & 0xFF; - params[3] = num & 0xFF; - } - - appendInstruction(func.blocks[0], PTR_SET, params, size); - } - } - break; } From afb44cfac7fca8a5289cc2c2c1f50faf871cf7a0 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 1 Jan 2025 16:50:33 +0100 Subject: [PATCH 088/121] feat: removal of invalid and unused stdl for now --- lib/winx86-64/lowlevel.qfall | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 lib/winx86-64/lowlevel.qfall diff --git a/lib/winx86-64/lowlevel.qfall b/lib/winx86-64/lowlevel.qfall deleted file mode 100644 index 304a7c5..0000000 --- a/lib/winx86-64/lowlevel.qfall +++ /dev/null @@ -1,30 +0,0 @@ -/** - * The quickfall std::lowlevel for Windows x86-64 architecture. - */ - -/** - * Marks a specific amount of bytes of the stack as used. - * @param the size to mark (in bytes). - */ -func salloc(int size) {} - -/** - * Marks a specific amount of bytes of the stack as used. - * @param size the size to mark (in bytes). - */ -func sfree(int size) {} - -/** - * Puts a value of the provided type into a stack section. - * @param type the Assembly type of the value. - * @param value the value. - */ -func sput(? type, ? value) {} - -/** - * Moves a section register value into another register. - * @param section the Assembly section where the origin value is stored. - * @param originRegister the register to grab the origin value from. - * @param destRegister the register where the value needs to be moved. - */ -func mov(str section, str originRegister, str destRegister) {} From 776de97fe7820c0dbb912a88e9fb2e484b5fa7dd Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 1 Jan 2025 16:53:39 +0100 Subject: [PATCH 089/121] chore: made IR_INSTRUCTION_CODE enum typedef cleaner --- src/ir/instructions.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 76c06f7..8edd590 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -8,7 +8,7 @@ /** * The instruction codes of IR. */ -typedef enum { +typedef enum IR_INSTRUCTION_CODE { /** * Swaps the current IR block. From 3eb98d2bd93537177319de0c4216202d2998fe47 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 1 Jan 2025 16:56:12 +0100 Subject: [PATCH 090/121] chore: made IR structures typedef cleaner --- src/ir/parser.h | 5 +++++ src/ir/structs.h | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 src/ir/parser.h diff --git a/src/ir/parser.h b/src/ir/parser.h new file mode 100644 index 0000000..5b674f4 --- /dev/null +++ b/src/ir/parser.h @@ -0,0 +1,5 @@ +#ifndef IR_PARSER_H +#define IR_PARSER_H + + +#endif \ No newline at end of file diff --git a/src/ir/structs.h b/src/ir/structs.h index 02c498d..2d4d7d4 100644 --- a/src/ir/structs.h +++ b/src/ir/structs.h @@ -8,7 +8,7 @@ /** * An IR instruction. */ -typedef struct { +typedef struct IR_INSTRUCTION { unsigned char opCode; @@ -20,7 +20,7 @@ typedef struct { /** * An IR basic block. */ -typedef struct { +typedef struct IR_BASIC_BLOCK { IR_INSTRUCTION* instructions; int instructionCount; @@ -31,7 +31,7 @@ typedef struct { /** * An IR function. */ -typedef struct { +typedef struct IR_FUNCTION { char* funcName; From 6b4b87cf1e2e78d82bd7f7b226a9f0be1648f3b8 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 1 Jan 2025 17:05:16 +0100 Subject: [PATCH 091/121] feat: added QASM parser header --- src/ir/parser.h | 5 ----- src/parser/structs/functions.h | 2 +- src/qasm/parser.h | 16 ++++++++++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) delete mode 100644 src/ir/parser.h create mode 100644 src/qasm/parser.h diff --git a/src/ir/parser.h b/src/ir/parser.h deleted file mode 100644 index 5b674f4..0000000 --- a/src/ir/parser.h +++ /dev/null @@ -1,5 +0,0 @@ -#ifndef IR_PARSER_H -#define IR_PARSER_H - - -#endif \ No newline at end of file diff --git a/src/parser/structs/functions.h b/src/parser/structs/functions.h index 3ecced9..9218d6d 100644 --- a/src/parser/structs/functions.h +++ b/src/parser/structs/functions.h @@ -48,7 +48,7 @@ typedef struct AST_ASM_FUNCTION_DEF { AST_PARAMETER* parameters; int parameterIndex; - unsigned char* buff; // The content buffer of the function. + char* buff; // The content buffer of the function. int buffIndex; // The size of buff. } AST_ASM_FUNCTION_DEC; diff --git a/src/qasm/parser.h b/src/qasm/parser.h new file mode 100644 index 0000000..4a0ddfe --- /dev/null +++ b/src/qasm/parser.h @@ -0,0 +1,16 @@ +/** + * The parser of QuickAssembly. The inline assembly of Quickfall. + */ + +#include "../parser/structs/functions.h" +#include "../ir/structs.h" + +#ifndef QASM_PARSER_H +#define QASM_PARSER_H + +/** + * Parses QuickAssembly instructions. + */ +void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size); + +#endif \ No newline at end of file From 8efe8899c84e4068b5c42c820f7e2d0426937c11 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 1 Jan 2025 17:08:37 +0100 Subject: [PATCH 092/121] feat: started working on QASM parsing --- src/qasm/parser.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/qasm/parser.c diff --git a/src/qasm/parser.c b/src/qasm/parser.c new file mode 100644 index 0000000..006d2a2 --- /dev/null +++ b/src/qasm/parser.c @@ -0,0 +1,30 @@ +/** + * The parser of QuickAssembly. The inline assembly of Quickfall. + */ + +#include + +#include "../parser/structs/functions.h" +#include "../ir/structs.h" + +/** + * Parses QuickAssembly instructions. + */ +void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size) { + char* wordBuff = malloc(32); + int wordBuffIndex = 0; + + for(int i = 0; i < size; ++i) { + char c = buff[i]; + + if(c == '\0') break; + + if(c == '\n') { + //todo: add singular instuction parsing. + } + else { + wordBuff[wordBuffIndex] = c; + ++wordBuffIndex; + } + } +} From 9663cc85a98913cbb415f9c75e028daf48cf3e5d Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 1 Jan 2025 17:10:52 +0100 Subject: [PATCH 093/121] feat: parseInstruction in header --- src/qasm/parser.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/qasm/parser.h b/src/qasm/parser.h index 4a0ddfe..6f85de3 100644 --- a/src/qasm/parser.h +++ b/src/qasm/parser.h @@ -13,4 +13,9 @@ */ void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size); +/** + * Parses a QuickAssembly instruction. + */ +inline IR_INSTRUCTION parseInstruction(char* buff, int size); + #endif \ No newline at end of file From 86c933102cafee00854944c7542e047e7c49efdb Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 1 Jan 2025 17:17:28 +0100 Subject: [PATCH 094/121] feat: added pushInstruction --- src/ir/ir.c | 25 +++++++++++++++++-------- src/ir/ir.h | 7 +++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/ir/ir.c b/src/ir/ir.c index 17ac3e5..a06de9e 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -19,6 +19,22 @@ #include "../parser/ast.h" +/** + * Appends an IR instruction into the basic block. + * @param block the IR basic block. + * @param instruction the instruction. + */ +inline void pushInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION instruction) { + if(block.instructions == NULL) { + block.instructions = malloc(sizeof(IR_INSTRUCTION) * 20); + block.allocatedSize = 20; + } + + block.instructions[block.instructionCount] = instruction; + block.instructionCount++; +} + + /** * Appends an IR instruction into the basic block. * @parma block the IR basic block. @@ -33,14 +49,7 @@ void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned instruction.params = params; instruction.paramCount = paramsCount; - - if(block.instructions == NULL) { - block.instructions = malloc(sizeof(IR_INSTRUCTION) * 20); - block.allocatedSize = 20; - } - - block.instructions[block.instructionCount] = instruction; - block.instructionCount++; + pushInstruction(block, instruction); } /** diff --git a/src/ir/ir.h b/src/ir/ir.h index 59192e5..0ac250e 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -13,6 +13,13 @@ #include "../parser/ast.h" +/** + * Appends an IR instruction into the basic block. + * @param block the IR basic block. + * @param instruction the instruction. + */ +inline void pushInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION instruction); + /** * Appends an IR instruction into the basic block. * @parma block the IR basic block. From 8f58c84709ad99a79ae717ce99eebb163bf99a91 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 1 Jan 2025 17:31:55 +0100 Subject: [PATCH 095/121] feat: added space seperator QASM parsing --- src/qasm/parser.c | 48 +++++++++++++++++++++++++++++++++++++++-------- src/qasm/parser.h | 2 +- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/qasm/parser.c b/src/qasm/parser.c index 006d2a2..4d6e651 100644 --- a/src/qasm/parser.c +++ b/src/qasm/parser.c @@ -4,27 +4,59 @@ #include +#include "./parser.h" + #include "../parser/structs/functions.h" + #include "../ir/structs.h" +#include "../ir/ir.h" /** * Parses QuickAssembly instructions. */ void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size) { - char* wordBuff = malloc(32); - int wordBuffIndex = 0; + char** buff = malloc(sizeof(char*) * 10); + int buffIndex = 0; + + int secIndex = 0; + + // Creating the buffers. + for(int i = 0; i < 10; ++i) { + buff[i] = malloc(32); + } for(int i = 0; i < size; ++i) { char c = buff[i]; - if(c == '\0') break; - + if(c == '\0') return; + if(c == '\n') { - //todo: add singular instuction parsing. + IR_INSTRUCTION* instruction = parseInstruction(buff, buffIndex); + + if(instruction != NULL) { + printf("Error: Coudln't parse QuickAssembly instruction named %s!\n", buff[0]); + } + else { + pushInstruction(func.blocks[0], *instruction); //todo: change ptr->obj to ptr + } + + secIndex = 0; + buffIndex = 0; + } + else if(c == ' ') { + ((char*)buff[buffIndex])[secIndex] = '\0'; + buffIndex++; } else { - wordBuff[wordBuffIndex] = c; - ++wordBuffIndex; - } + ((char*)buff[buffIndex])[secIndex] = c; + secIndex++; + } } } + +/** + * Parses a QuickAssembly instruction. + */ +inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { + +} \ No newline at end of file diff --git a/src/qasm/parser.h b/src/qasm/parser.h index 6f85de3..88b58e7 100644 --- a/src/qasm/parser.h +++ b/src/qasm/parser.h @@ -16,6 +16,6 @@ void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size); /** * Parses a QuickAssembly instruction. */ -inline IR_INSTRUCTION parseInstruction(char* buff, int size); +inline IR_INSTRUCTION* parseInstruction(char* buff, int size); #endif \ No newline at end of file From dc7a289df817b528d5c99658ecea5d12aea1683b Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 1 Jan 2025 17:43:41 +0100 Subject: [PATCH 096/121] feat: added instruction hash -> code type --- src/qasm/parser.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/qasm/parser.c b/src/qasm/parser.c index 4d6e651..22f44ba 100644 --- a/src/qasm/parser.c +++ b/src/qasm/parser.c @@ -10,6 +10,9 @@ #include "../ir/structs.h" #include "../ir/ir.h" +#include "../ir/instructions.h" + +#include "../utils/hash.h" /** * Parses QuickAssembly instructions. @@ -58,5 +61,65 @@ void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size) { * Parses a QuickAssembly instruction. */ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { + IR_INSTRUCTION* instruction = malloc(sizeof(IR_INSTRUCTION)); + + int instructionHash = hashstr(buff[0]); + // Determines the instruction type based on the string hash. + switch(instructionHash) { + case 2985: + instruction->opCode = BLOCK_SWAP; + break; + case 2987: + instruction->opCode = COND_BLOCK_SWAP; + break; + case 3275: + instruction->opCode = LOGICAL_BLOCK_SWAP; + break; + case 1798: + instruction->opCode = S_ALLOC; + break; + case 2887: + instruction->opCode = PTR_SET; + break; + case 2472: + instruction->opCode = PTR_LOAD; + break; + case 452: + instruction->opCode = IADD; + break; + case 1508: + instruction->opCode = ISUB; + break; + case 1636: + instruction->opCode = IMUL; + break; + case 1284: + instruction->opCode = IDIV; + break; + case 1188: + instruction->opCode = ICMP; + break; + case 1350: + instruction->opCode = ICMP_H; + break; + case 1478: + instruction->opCode = ICMP_L; + break; + case 3272: + instruction->opCode = PRM_PUSH; + break; + case 3144: + instruction->opCode = RET_PUSH; + break; + case 772: + instruction->opCode = CALL; + break; + case 1283: + instruction->opCode = RET; + break; + default: + printf("Error: Unknown instruction %s!\n", buff[0]); + return NULL; + } } \ No newline at end of file From ff1355571b426e4d77c6e75664107b50913a332c Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 2 Jan 2025 14:57:46 +0100 Subject: [PATCH 097/121] feat: moved qasm parser into parser dir --- src/qasm/{ => parser}/parser.c | 10 +++++----- src/qasm/{ => parser}/parser.h | 4 ++-- src/qasm/qasm.h | 10 ++++++++++ 3 files changed, 17 insertions(+), 7 deletions(-) rename src/qasm/{ => parser}/parser.c (94%) rename src/qasm/{ => parser}/parser.h (82%) create mode 100644 src/qasm/qasm.h diff --git a/src/qasm/parser.c b/src/qasm/parser/parser.c similarity index 94% rename from src/qasm/parser.c rename to src/qasm/parser/parser.c index 22f44ba..ebd70dc 100644 --- a/src/qasm/parser.c +++ b/src/qasm/parser/parser.c @@ -6,13 +6,13 @@ #include "./parser.h" -#include "../parser/structs/functions.h" +#include "../../parser/structs/functions.h" -#include "../ir/structs.h" -#include "../ir/ir.h" -#include "../ir/instructions.h" +#include "../../ir/structs.h" +#include "../../ir/ir.h" +#include "../../ir/instructions.h" -#include "../utils/hash.h" +#include "../../utils/hash.h" /** * Parses QuickAssembly instructions. diff --git a/src/qasm/parser.h b/src/qasm/parser/parser.h similarity index 82% rename from src/qasm/parser.h rename to src/qasm/parser/parser.h index 88b58e7..7e43ce6 100644 --- a/src/qasm/parser.h +++ b/src/qasm/parser/parser.h @@ -2,8 +2,8 @@ * The parser of QuickAssembly. The inline assembly of Quickfall. */ -#include "../parser/structs/functions.h" -#include "../ir/structs.h" +#include "../../parser/structs/functions.h" +#include "../../ir/structs.h" #ifndef QASM_PARSER_H #define QASM_PARSER_H diff --git a/src/qasm/qasm.h b/src/qasm/qasm.h new file mode 100644 index 0000000..d4ed063 --- /dev/null +++ b/src/qasm/qasm.h @@ -0,0 +1,10 @@ +/** + * The main file of QuickAssembly (QASm). + */ + +#ifndef QASM_H +#define QASM_H + + + +#endif \ No newline at end of file From 1537c155d938fa775f07ede02c514a6effee8c06 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 2 Jan 2025 15:01:16 +0100 Subject: [PATCH 098/121] feat: added parseInt32 --- src/qasm/parser/values.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/qasm/parser/values.h diff --git a/src/qasm/parser/values.h b/src/qasm/parser/values.h new file mode 100644 index 0000000..94382b9 --- /dev/null +++ b/src/qasm/parser/values.h @@ -0,0 +1,16 @@ +/** + * QuickAssembly Value types parsing. + */ + +#ifndef QASM_PARSER_VALUES_H +#define QASM_PARSER_VALUES_H + +/** + * Parses a 32-bit integer into bytes. + * @param buff the buff to append the bytes to. + * @param startIndex the starting index of where to append the bytes. + * @param str the string containing the integer. + */ +void parseInt32(unsigned char* buff, int startIndex, char* str); + +#endif \ No newline at end of file From 521b367dfc6e4d1a2454becf306acbd51f82a29a Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 2 Jan 2025 15:03:07 +0100 Subject: [PATCH 099/121] feat: added parseInt32 impl --- src/qasm/parser/values.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/qasm/parser/values.c diff --git a/src/qasm/parser/values.c b/src/qasm/parser/values.c new file mode 100644 index 0000000..5d87afb --- /dev/null +++ b/src/qasm/parser/values.c @@ -0,0 +1,20 @@ +/** + * QuickAssembly Value types parsing. + */ + +#include + +/** + * Parses a 32-bit integer into bytes. + * @param buff the buff to append the bytes to. + * @param startIndex the starting index of where to append the bytes. + * @param str the string containing the integer. + */ +void parseInt32(unsigned char* buff, int startIndex, char* str) { + int i = atoi(str); + + buff[startIndex] = (i >> 24) & 0xFF; + buff[startIndex + 1] = (i >> 16) & 0xFF; + buff[startIndex + 2] = (i >> 8) & 0xFF; + buff[startIndex + 3] = i & 0xFF; +} \ No newline at end of file From 566f9f588edc575006fd2450196e38427b16e892 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 2 Jan 2025 15:08:59 +0100 Subject: [PATCH 100/121] feat: parseVariableName --- src/qasm/parser/values.c | 24 ++++++++++++++++++++++++ src/qasm/parser/values.h | 9 +++++++++ 2 files changed, 33 insertions(+) diff --git a/src/qasm/parser/values.c b/src/qasm/parser/values.c index 5d87afb..edd1b17 100644 --- a/src/qasm/parser/values.c +++ b/src/qasm/parser/values.c @@ -2,6 +2,7 @@ * QuickAssembly Value types parsing. */ +#include #include /** @@ -17,4 +18,27 @@ void parseInt32(unsigned char* buff, int startIndex, char* str) { buff[startIndex + 1] = (i >> 16) & 0xFF; buff[startIndex + 2] = (i >> 8) & 0xFF; buff[startIndex + 3] = i & 0xFF; +} + + +/** + * Parses a variable name. + * @param buff the buff to append the bytes to. + * @param startIndex the starting index of where to append the bytes. + * @param str the string containing the variable name. + */ +void parseVariableName(unsigned char* buff, int startIndex, char* str) { + if(str[0] != '%') { + printf("Error: Variable names must start with %%! Got %s\n", str); + return; + } + + --startIndex; + + int i = 1; + + while(str[i] != '\0') { + buff[startIndex + i] = str[i]; + ++i; + } } \ No newline at end of file diff --git a/src/qasm/parser/values.h b/src/qasm/parser/values.h index 94382b9..541c6ab 100644 --- a/src/qasm/parser/values.h +++ b/src/qasm/parser/values.h @@ -13,4 +13,13 @@ */ void parseInt32(unsigned char* buff, int startIndex, char* str); + +/** + * Parses a variable name. + * @param buff the buff to append the bytes to. + * @param startIndex the starting index of where to append the bytes. + * @param str the string containing the variable name. + */ +void parseVariableName(unsigned char* buff, int startIndex, char* str); + #endif \ No newline at end of file From 195a6d151cfd74b501c63222fd7f4f305b43df78 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 2 Jan 2025 15:12:19 +0100 Subject: [PATCH 101/121] feat: added param parsing for BLOCK_SWAP and COND_BLOCK_SWAP --- src/qasm/parser/parser.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/qasm/parser/parser.c b/src/qasm/parser/parser.c index ebd70dc..c2ee343 100644 --- a/src/qasm/parser/parser.c +++ b/src/qasm/parser/parser.c @@ -6,6 +6,8 @@ #include "./parser.h" +#include "./values.h" + #include "../../parser/structs/functions.h" #include "../../ir/structs.h" @@ -65,13 +67,27 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { int instructionHash = hashstr(buff[0]); + unsigned char* buff; + // Determines the instruction type based on the string hash. switch(instructionHash) { case 2985: instruction->opCode = BLOCK_SWAP; + buff = malloc(4); + parseInt32(buff, 0, buff[1]); + + instruction->params = buff; + instruction->paramCount = 4; break; case 2987: instruction->opCode = COND_BLOCK_SWAP; + int size = 4 + strlen(buff[2]) - 1; + buff = malloc(size); + parseInt32(buff, 0, buff[1]); + parseVariableName(buff, 5, buff[2]); + + instruction->params = buff; + instruction->paramCount = size; break; case 3275: instruction->opCode = LOGICAL_BLOCK_SWAP; From eff013a9121471161881c36bb2d773b1e4ad61c6 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 2 Jan 2025 15:14:48 +0100 Subject: [PATCH 102/121] feat: added param parsing for LOGICAL_BLOCK_SWAP --- src/qasm/parser/parser.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/qasm/parser/parser.c b/src/qasm/parser/parser.c index c2ee343..bfb5434 100644 --- a/src/qasm/parser/parser.c +++ b/src/qasm/parser/parser.c @@ -67,30 +67,38 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { int instructionHash = hashstr(buff[0]); - unsigned char* buff; + unsigned char* b; // Determines the instruction type based on the string hash. switch(instructionHash) { case 2985: instruction->opCode = BLOCK_SWAP; - buff = malloc(4); - parseInt32(buff, 0, buff[1]); + b = malloc(4); + parseInt32(b, 0, buff[1]); - instruction->params = buff; + instruction->params = b; instruction->paramCount = 4; break; case 2987: instruction->opCode = COND_BLOCK_SWAP; int size = 4 + strlen(buff[2]) - 1; - buff = malloc(size); - parseInt32(buff, 0, buff[1]); - parseVariableName(buff, 5, buff[2]); + b = malloc(size); + parseInt32(b, 0, buff[1]); + parseVariableName(b, 5, buff[2]); - instruction->params = buff; + instruction->params = b; instruction->paramCount = size; break; case 3275: instruction->opCode = LOGICAL_BLOCK_SWAP; + int size = 8 + strlen(buff[3]) - 1; + b = malloc(size); + parseInt32(b, 0, buff[1]); + parseInt32(b, 5, buff[2]); + parseVariableName(b, 10, buff[3]); + + instruction->params = b; + instruction->paramCount = size; break; case 1798: instruction->opCode = S_ALLOC; From ad1f1c1aff34bdbb4781a57c775b73c43e461de3 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 2 Jan 2025 15:17:16 +0100 Subject: [PATCH 103/121] feat: changed param order --- src/ir/instructions.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ir/instructions.h b/src/ir/instructions.h index 8edd590..0286b9d 100644 --- a/src/ir/instructions.h +++ b/src/ir/instructions.h @@ -33,8 +33,8 @@ typedef enum IR_INSTRUCTION_CODE { /** * Allocates a set amount of bits in the stack. - * @param ptr the pointer that is going to be allocated. * @param size the size of the pointer. + * @param ptr the pointer that is going to be allocated. */ S_ALLOC, From 4c90852e2aa5e8fe75b0d1b1576f99b8d2bb1645 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 2 Jan 2025 15:18:16 +0100 Subject: [PATCH 104/121] feat: added param parsing for S_ALLOC --- src/qasm/parser/parser.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/qasm/parser/parser.c b/src/qasm/parser/parser.c index bfb5434..cc7b6af 100644 --- a/src/qasm/parser/parser.c +++ b/src/qasm/parser/parser.c @@ -102,6 +102,13 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 1798: instruction->opCode = S_ALLOC; + int size = 4 + strlen(buff[2]) - 1; + b = malloc(size); + parseInt32(b, 0, buff[1]); + parseVariableName(b, 5, buff[2]); + + instruction->params = b; + instruction->paramCount = size; break; case 2887: instruction->opCode = PTR_SET; From a3b84cd257b8857e624d4ffb723febbf355863f8 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 2 Jan 2025 15:25:01 +0100 Subject: [PATCH 105/121] feat: added PTR_SET param parsing --- src/qasm/parser/parser.c | 7 +++++++ src/qasm/parser/values.c | 4 +++- src/qasm/parser/values.h | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/qasm/parser/parser.c b/src/qasm/parser/parser.c index cc7b6af..f59a557 100644 --- a/src/qasm/parser/parser.c +++ b/src/qasm/parser/parser.c @@ -112,6 +112,13 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 2887: instruction->opCode = PTR_SET; + int size = 4 + strlen(buff[2]) - 1; + b = malloc(size); + parseInt32(b, 0, buff[1]); + parseVariableName(b, 5, buff[2]); + + instruction->params = b; + instruction->paramCount = size; break; case 2472: instruction->opCode = PTR_LOAD; diff --git a/src/qasm/parser/values.c b/src/qasm/parser/values.c index edd1b17..f68356f 100644 --- a/src/qasm/parser/values.c +++ b/src/qasm/parser/values.c @@ -27,7 +27,7 @@ void parseInt32(unsigned char* buff, int startIndex, char* str) { * @param startIndex the starting index of where to append the bytes. * @param str the string containing the variable name. */ -void parseVariableName(unsigned char* buff, int startIndex, char* str) { +int parseVariableName(unsigned char* buff, int startIndex, char* str) { if(str[0] != '%') { printf("Error: Variable names must start with %%! Got %s\n", str); return; @@ -41,4 +41,6 @@ void parseVariableName(unsigned char* buff, int startIndex, char* str) { buff[startIndex + i] = str[i]; ++i; } + + return i + startIndex; } \ No newline at end of file diff --git a/src/qasm/parser/values.h b/src/qasm/parser/values.h index 541c6ab..8fd68f6 100644 --- a/src/qasm/parser/values.h +++ b/src/qasm/parser/values.h @@ -20,6 +20,6 @@ void parseInt32(unsigned char* buff, int startIndex, char* str); * @param startIndex the starting index of where to append the bytes. * @param str the string containing the variable name. */ -void parseVariableName(unsigned char* buff, int startIndex, char* str); +int parseVariableName(unsigned char* buff, int startIndex, char* str); #endif \ No newline at end of file From 95ed677b7548478141deed42d6ef25749f4bbbae Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 4 Jan 2025 00:41:48 +0100 Subject: [PATCH 106/121] feat: added parsing for the rest of instructions --- src/qasm/parser/parser.c | 84 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/qasm/parser/parser.c b/src/qasm/parser/parser.c index f59a557..709e739 100644 --- a/src/qasm/parser/parser.c +++ b/src/qasm/parser/parser.c @@ -122,39 +122,123 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 2472: instruction->opCode = PTR_LOAD; + int size = strlen(buff[1]) + strlen(buff[2]) - 2; + b = malloc(size); + int i = parseVariableName(b, 0, buff[1]); + parseVariableName(b, i + 1, buff[2]); + + instruction->params = b; + instruction->paramCount = size; break; case 452: instruction->opCode = IADD; + int size = strlen(buff[1]) + 7; + b = malloc(size); + int i = parseVariableName(b, 0, buff[1]); + parseInt32(b, i + 1, buff[2]); + parseInt32(b, i + 6, buff[3]); + + instruction->params = b; + instruction->paramCount = size; break; case 1508: instruction->opCode = ISUB; + int size = strlen(buff[1]) + 7; + b = malloc(size); + int i = parseVariableName(b, 0, buff[1]); + parseInt32(b, i + 1, buff[2]); + parseInt32(b, i + 6, buff[3]); + + instruction->params = b; + instruction->paramCount = size; break; case 1636: instruction->opCode = IMUL; + int size = strlen(buff[1]) + 7; + b = malloc(size); + int i = parseVariableName(b, 0, buff[1]); + parseInt32(b, i + 1, buff[2]); + parseInt32(b, i + 6, buff[3]); + + instruction->params = b; + instruction->paramCount = size; break; case 1284: instruction->opCode = IDIV; + int size = strlen(buff[1]) + 7; + b = malloc(size); + int i = parseVariableName(b, 0, buff[1]); + parseInt32(b, i + 1, buff[2]); + parseInt32(b, i + 6, buff[3]); + + instruction->params = b; + instruction->paramCount = size; break; case 1188: instruction->opCode = ICMP; + int size = strlen(buff[1]) + 7; + b = malloc(size); + int i = parseVariableName(b, 0, buff[1]); + parseInt32(b, i + 1, buff[2]); + parseInt32(b, i + 6, buff[3]); + + instruction->params = b; + instruction->paramCount = size; break; case 1350: instruction->opCode = ICMP_H; + int size = strlen(buff[1]) + 7; + b = malloc(size); + int i = parseVariableName(b, 0, buff[1]); + parseInt32(b, i + 1, buff[2]); + parseInt32(b, i + 6, buff[3]); + + instruction->params = b; + instruction->paramCount = size; break; case 1478: instruction->opCode = ICMP_L; + int size = strlen(buff[1]) + 7; + b = malloc(size); + int i = parseVariableName(b, 0, buff[1]); + parseInt32(b, i + 1, buff[2]); + parseInt32(b, i + 6, buff[3]); + + instruction->params = b; + instruction->paramCount = size; break; case 3272: instruction->opCode = PRM_PUSH; + int size = strlen(buff[1]) + 3; + b = malloc(size); + int i = parseVariableName(b, 0, buff[1]); + parseInt32(b, i + 1, buff[2]); + + instruction->params = b; + instruction->paramCount = size; break; case 3144: instruction->opCode = RET_PUSH; + int size = strlen(buff[1]) - 1; + b = malloc(size); + parseVariableName(b, 0, buff[1]); + + instruction->params = b; + instruction->paramCount = size; break; case 772: instruction->opCode = CALL; + int size = strlen(buff[1]); + b = malloc(size); + parseVariableName(b, 0, buff[1]); + + instruction->params = b; + instruction->paramCount = size; break; case 1283: instruction->opCode = RET; + instruction->params = NULL; + instruction->paramCount = 0; break; default: printf("Error: Unknown instruction %s!\n", buff[0]); From 19176791e187868439f443cdf238d73506f3f7ff Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 4 Jan 2025 01:31:57 +0100 Subject: [PATCH 107/121] fix: fixed qasm related issues --- src/ir/ir.c | 32 +++++++++++----------- src/ir/ir.h | 4 +-- src/ir/irs/variables.c | 2 +- src/ir/irs/variables.h | 2 +- src/ir/structs.h | 4 +-- src/qasm/parser/parser.c | 57 +++++++++++++++++++++------------------- src/qasm/parser/parser.h | 4 +-- src/qasm/parser/values.c | 3 ++- 8 files changed, 57 insertions(+), 51 deletions(-) diff --git a/src/ir/ir.c b/src/ir/ir.c index a06de9e..d5f80ea 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -24,14 +24,14 @@ * @param block the IR basic block. * @param instruction the instruction. */ -inline void pushInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION instruction) { - if(block.instructions == NULL) { - block.instructions = malloc(sizeof(IR_INSTRUCTION) * 20); - block.allocatedSize = 20; +void pushInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION* instruction) { + if(block->allocatedSize == 0) { + block->instructions = malloc(sizeof(IR_INSTRUCTION*) * 20); + block->allocatedSize = 20; } - block.instructions[block.instructionCount] = instruction; - block.instructionCount++; + block->instructions[block->instructionCount] = instruction; + block->instructionCount++; } @@ -42,12 +42,12 @@ inline void pushInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION instruction) { * @param params the parameters of the operation. * @param paramsCount the count of the parameters of the operation. */ -void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned char params[], int paramsCount) { - IR_INSTRUCTION instruction = {0}; +void appendInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION_CODE code, unsigned char params[], int paramsCount) { + IR_INSTRUCTION* instruction = malloc(sizeof(IR_INSTRUCTION)); - instruction.opCode = code; - instruction.params = params; - instruction.paramCount = paramsCount; + instruction->opCode = code; + instruction->params = params; + instruction->paramCount = paramsCount; pushInstruction(block, instruction); } @@ -58,13 +58,15 @@ void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned */ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { IR_FUNCTION func = {0}; - func.blocks = malloc(sizeof(IR_BASIC_BLOCK)); + func.blocks = malloc(sizeof(IR_BASIC_BLOCK*)); func.blockCount++; func.funcName = node->funcName; - func.blocks[0].instructions = NULL; - func.blocks[0].instructionCount = 0; + func.blocks[0] = malloc(sizeof(IR_BASIC_BLOCK)); + + func.blocks[0]->instructions = NULL; + func.blocks[0]->instructionCount = 0; //todo: move this to another function when finished while(node->body != NULL) { @@ -72,7 +74,7 @@ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { switch(b->type) { case AST_TYPE_VARIABLE_DECLARATION: - parseVariableDeclaration(func.blocks[0], node); + parseVariableDeclaration(func.blocks[0], (AST_VARIABLE_DEC*)b); break; diff --git a/src/ir/ir.h b/src/ir/ir.h index 0ac250e..39a7fff 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -18,7 +18,7 @@ * @param block the IR basic block. * @param instruction the instruction. */ -inline void pushInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION instruction); +void pushInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION* instruction); /** * Appends an IR instruction into the basic block. @@ -27,7 +27,7 @@ inline void pushInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION instruction); * @param params the parameters of the operation. * @param paramsCount the count of the parameters of the operation. */ -void appendInstruction(IR_BASIC_BLOCK block, IR_INSTRUCTION_CODE code, unsigned char params[], int paramsCount); +void appendInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION_CODE code, unsigned char params[], int paramsCount); /** * Parses a AST function into IR. diff --git a/src/ir/irs/variables.c b/src/ir/irs/variables.c index 2b27b69..b2469de 100644 --- a/src/ir/irs/variables.c +++ b/src/ir/irs/variables.c @@ -16,7 +16,7 @@ * @param block the IR basic block to append to. * @param node the AST node representing the variable. */ -inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* node) { +inline void parseVariableDeclaration(IR_BASIC_BLOCK* block, AST_VARIABLE_DEC* node) { int allocSize = 0; if(node->type[0] == 0x01) allocSize = 32; // int32 diff --git a/src/ir/irs/variables.h b/src/ir/irs/variables.h index f5f8ee2..f62a50c 100644 --- a/src/ir/irs/variables.h +++ b/src/ir/irs/variables.h @@ -14,7 +14,7 @@ * @param block the IR basic block to append to. * @param node the AST node representing the variable. */ -inline void parseVariableDeclaration(IR_BASIC_BLOCK block, AST_VARIABLE_DEC* node); +inline void parseVariableDeclaration(IR_BASIC_BLOCK* block, AST_VARIABLE_DEC* node); #endif \ No newline at end of file diff --git a/src/ir/structs.h b/src/ir/structs.h index 2d4d7d4..7516037 100644 --- a/src/ir/structs.h +++ b/src/ir/structs.h @@ -22,7 +22,7 @@ typedef struct IR_INSTRUCTION { */ typedef struct IR_BASIC_BLOCK { - IR_INSTRUCTION* instructions; + IR_INSTRUCTION** instructions; int instructionCount; int allocatedSize; @@ -35,7 +35,7 @@ typedef struct IR_FUNCTION { char* funcName; - IR_BASIC_BLOCK* blocks; + IR_BASIC_BLOCK** blocks; int blockCount; } IR_FUNCTION; diff --git a/src/qasm/parser/parser.c b/src/qasm/parser/parser.c index 709e739..e02d736 100644 --- a/src/qasm/parser/parser.c +++ b/src/qasm/parser/parser.c @@ -2,6 +2,8 @@ * The parser of QuickAssembly. The inline assembly of Quickfall. */ +#include +#include #include #include "./parser.h" @@ -19,7 +21,7 @@ /** * Parses QuickAssembly instructions. */ -void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size) { +void parseQAsmInstructions(IR_FUNCTION* func, char* buffer, int size) { char** buff = malloc(sizeof(char*) * 10); int buffIndex = 0; @@ -31,18 +33,18 @@ void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size) { } for(int i = 0; i < size; ++i) { - char c = buff[i]; + char c = buffer[i]; if(c == '\0') return; if(c == '\n') { IR_INSTRUCTION* instruction = parseInstruction(buff, buffIndex); - if(instruction != NULL) { + if(instruction == NULL) { printf("Error: Coudln't parse QuickAssembly instruction named %s!\n", buff[0]); } else { - pushInstruction(func.blocks[0], *instruction); //todo: change ptr->obj to ptr + pushInstruction(func->blocks[0], instruction); } secIndex = 0; @@ -50,6 +52,7 @@ void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size) { } else if(c == ' ') { ((char*)buff[buffIndex])[secIndex] = '\0'; + secIndex = 0; buffIndex++; } else { @@ -62,7 +65,7 @@ void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size) { /** * Parses a QuickAssembly instruction. */ -inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { +IR_INSTRUCTION* parseInstruction(char** buff, int bufferSize) { IR_INSTRUCTION* instruction = malloc(sizeof(IR_INSTRUCTION)); int instructionHash = hashstr(buff[0]); @@ -91,7 +94,7 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 3275: instruction->opCode = LOGICAL_BLOCK_SWAP; - int size = 8 + strlen(buff[3]) - 1; + size = 8 + strlen(buff[3]) - 1; b = malloc(size); parseInt32(b, 0, buff[1]); parseInt32(b, 5, buff[2]); @@ -102,7 +105,7 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 1798: instruction->opCode = S_ALLOC; - int size = 4 + strlen(buff[2]) - 1; + size = 4 + strlen(buff[2]) - 1; b = malloc(size); parseInt32(b, 0, buff[1]); parseVariableName(b, 5, buff[2]); @@ -112,7 +115,7 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 2887: instruction->opCode = PTR_SET; - int size = 4 + strlen(buff[2]) - 1; + size = 4 + strlen(buff[2]) - 1; b = malloc(size); parseInt32(b, 0, buff[1]); parseVariableName(b, 5, buff[2]); @@ -122,7 +125,7 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 2472: instruction->opCode = PTR_LOAD; - int size = strlen(buff[1]) + strlen(buff[2]) - 2; + size = strlen(buff[1]) + strlen(buff[2]) - 2; b = malloc(size); int i = parseVariableName(b, 0, buff[1]); parseVariableName(b, i + 1, buff[2]); @@ -132,9 +135,9 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 452: instruction->opCode = IADD; - int size = strlen(buff[1]) + 7; + size = strlen(buff[1]) + 7; b = malloc(size); - int i = parseVariableName(b, 0, buff[1]); + i = parseVariableName(b, 0, buff[1]); parseInt32(b, i + 1, buff[2]); parseInt32(b, i + 6, buff[3]); @@ -143,9 +146,9 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 1508: instruction->opCode = ISUB; - int size = strlen(buff[1]) + 7; + size = strlen(buff[1]) + 7; b = malloc(size); - int i = parseVariableName(b, 0, buff[1]); + i = parseVariableName(b, 0, buff[1]); parseInt32(b, i + 1, buff[2]); parseInt32(b, i + 6, buff[3]); @@ -154,9 +157,9 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 1636: instruction->opCode = IMUL; - int size = strlen(buff[1]) + 7; + size = strlen(buff[1]) + 7; b = malloc(size); - int i = parseVariableName(b, 0, buff[1]); + i = parseVariableName(b, 0, buff[1]); parseInt32(b, i + 1, buff[2]); parseInt32(b, i + 6, buff[3]); @@ -165,9 +168,9 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 1284: instruction->opCode = IDIV; - int size = strlen(buff[1]) + 7; + size = strlen(buff[1]) + 7; b = malloc(size); - int i = parseVariableName(b, 0, buff[1]); + i = parseVariableName(b, 0, buff[1]); parseInt32(b, i + 1, buff[2]); parseInt32(b, i + 6, buff[3]); @@ -176,9 +179,9 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 1188: instruction->opCode = ICMP; - int size = strlen(buff[1]) + 7; + size = strlen(buff[1]) + 7; b = malloc(size); - int i = parseVariableName(b, 0, buff[1]); + i = parseVariableName(b, 0, buff[1]); parseInt32(b, i + 1, buff[2]); parseInt32(b, i + 6, buff[3]); @@ -187,9 +190,9 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 1350: instruction->opCode = ICMP_H; - int size = strlen(buff[1]) + 7; + size = strlen(buff[1]) + 7; b = malloc(size); - int i = parseVariableName(b, 0, buff[1]); + i = parseVariableName(b, 0, buff[1]); parseInt32(b, i + 1, buff[2]); parseInt32(b, i + 6, buff[3]); @@ -198,9 +201,9 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 1478: instruction->opCode = ICMP_L; - int size = strlen(buff[1]) + 7; + size = strlen(buff[1]) + 7; b = malloc(size); - int i = parseVariableName(b, 0, buff[1]); + i = parseVariableName(b, 0, buff[1]); parseInt32(b, i + 1, buff[2]); parseInt32(b, i + 6, buff[3]); @@ -209,9 +212,9 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 3272: instruction->opCode = PRM_PUSH; - int size = strlen(buff[1]) + 3; + size = strlen(buff[1]) + 3; b = malloc(size); - int i = parseVariableName(b, 0, buff[1]); + i = parseVariableName(b, 0, buff[1]); parseInt32(b, i + 1, buff[2]); instruction->params = b; @@ -219,7 +222,7 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 3144: instruction->opCode = RET_PUSH; - int size = strlen(buff[1]) - 1; + size = strlen(buff[1]) - 1; b = malloc(size); parseVariableName(b, 0, buff[1]); @@ -228,7 +231,7 @@ inline IR_INSTRUCTION* parseInstruction(char** buff, int size) { break; case 772: instruction->opCode = CALL; - int size = strlen(buff[1]); + size = strlen(buff[1]); b = malloc(size); parseVariableName(b, 0, buff[1]); diff --git a/src/qasm/parser/parser.h b/src/qasm/parser/parser.h index 7e43ce6..e74ebc3 100644 --- a/src/qasm/parser/parser.h +++ b/src/qasm/parser/parser.h @@ -11,11 +11,11 @@ /** * Parses QuickAssembly instructions. */ -void parseQAsmInstructions(IR_FUNCTION func, char* buff, int size); +void parseQAsmInstructions(IR_FUNCTION* func, char* buff, int size); /** * Parses a QuickAssembly instruction. */ -inline IR_INSTRUCTION* parseInstruction(char* buff, int size); +IR_INSTRUCTION* parseInstruction(char** buff, int size); #endif \ No newline at end of file diff --git a/src/qasm/parser/values.c b/src/qasm/parser/values.c index f68356f..d6da305 100644 --- a/src/qasm/parser/values.c +++ b/src/qasm/parser/values.c @@ -2,6 +2,7 @@ * QuickAssembly Value types parsing. */ +#include #include #include @@ -30,7 +31,7 @@ void parseInt32(unsigned char* buff, int startIndex, char* str) { int parseVariableName(unsigned char* buff, int startIndex, char* str) { if(str[0] != '%') { printf("Error: Variable names must start with %%! Got %s\n", str); - return; + return -1; } --startIndex; From 8b0e3e9b8b8036a7970c67b849821eeb7961cf87 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 4 Jan 2025 17:43:41 +0100 Subject: [PATCH 108/121] feat: moved parseFunction into the individual ir file --- src/ir/ir.c | 34 ------------------------------- src/ir/ir.h | 6 ------ src/ir/irs/functions.c | 45 ++++++++++++++++++++++++++++++++++++++++++ src/ir/irs/functions.h | 18 +++++++++++++++++ 4 files changed, 63 insertions(+), 40 deletions(-) create mode 100644 src/ir/irs/functions.c create mode 100644 src/ir/irs/functions.h diff --git a/src/ir/ir.c b/src/ir/ir.c index d5f80ea..3aa23ba 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -50,38 +50,4 @@ void appendInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION_CODE code, unsigned instruction->paramCount = paramsCount; pushInstruction(block, instruction); -} - -/** - * Parses a AST function into IR. - * @param node the AST node representing the function. - */ -IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { - IR_FUNCTION func = {0}; - func.blocks = malloc(sizeof(IR_BASIC_BLOCK*)); - func.blockCount++; - - func.funcName = node->funcName; - - func.blocks[0] = malloc(sizeof(IR_BASIC_BLOCK)); - - func.blocks[0]->instructions = NULL; - func.blocks[0]->instructionCount = 0; - - //todo: move this to another function when finished - while(node->body != NULL) { - AST_TREE_BRANCH* b = (AST_TREE_BRANCH*) node->body; - - switch(b->type) { - case AST_TYPE_VARIABLE_DECLARATION: - parseVariableDeclaration(func.blocks[0], (AST_VARIABLE_DEC*)b); - - break; - - } - - node->body = b->next; - } - - return func; } \ No newline at end of file diff --git a/src/ir/ir.h b/src/ir/ir.h index 39a7fff..c9c8551 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -29,10 +29,4 @@ void pushInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION* instruction); */ void appendInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION_CODE code, unsigned char params[], int paramsCount); -/** - * Parses a AST function into IR. - * @param node the AST node representing the function. - */ -IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node); - #endif \ No newline at end of file diff --git a/src/ir/irs/functions.c b/src/ir/irs/functions.c new file mode 100644 index 0000000..deed758 --- /dev/null +++ b/src/ir/irs/functions.c @@ -0,0 +1,45 @@ +/** + * IR for function related. + */ + +#include "../structs.h" + +#include "../../parser/structs/functions.h" +#include "../../parser/structs/variables.h" +#include "../../parser/structs/tree.h" + +#include "../../parser/ast.h" + +/** + * Parses a AST function into IR. + * @param node the AST node representing the function. + */ +IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { + IR_FUNCTION func = {0}; + func.blocks = malloc(sizeof(IR_BASIC_BLOCK*)); + func.blockCount++; + + func.funcName = node->funcName; + + func.blocks[0] = malloc(sizeof(IR_BASIC_BLOCK)); + + func.blocks[0]->instructions = NULL; + func.blocks[0]->instructionCount = 0; + + //todo: move this to another function when finished + while(node->body != NULL) { + AST_TREE_BRANCH* b = (AST_TREE_BRANCH*) node->body; + + switch(b->type) { + case AST_TYPE_VARIABLE_DECLARATION: + parseVariableDeclaration(func.blocks[0], (AST_VARIABLE_DEC*)b); + + break; + + } + + node->body = b->next; + } + + return func; +} \ No newline at end of file diff --git a/src/ir/irs/functions.h b/src/ir/irs/functions.h new file mode 100644 index 0000000..a04eb66 --- /dev/null +++ b/src/ir/irs/functions.h @@ -0,0 +1,18 @@ +/** + * IR for function related. + */ + +#include "../structs.h" +#include "../../parser/structs/functions.h" + + +#ifndef IR_FUNCTIONS_H +#define IR_FUNCTIONS_H + +/** + * Parses a AST function into IR. + * @param node the AST node representing the function. + */ +IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node); + +#endif \ No newline at end of file From 67a001e610dccd8f7da85f97beca464bc135855d Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 4 Jan 2025 17:44:40 +0100 Subject: [PATCH 109/121] fix: added missing imports --- src/ir/irs/functions.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ir/irs/functions.c b/src/ir/irs/functions.c index deed758..ee60984 100644 --- a/src/ir/irs/functions.c +++ b/src/ir/irs/functions.c @@ -2,6 +2,8 @@ * IR for function related. */ +#include + #include "../structs.h" #include "../../parser/structs/functions.h" @@ -10,6 +12,8 @@ #include "../../parser/ast.h" +#include "./variables.h" + /** * Parses a AST function into IR. * @param node the AST node representing the function. From 78b761315544f935cb08c2acb0ef6739a828083f Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 4 Jan 2025 17:46:00 +0100 Subject: [PATCH 110/121] feat: added parseASMFunction in header --- src/ir/irs/functions.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ir/irs/functions.h b/src/ir/irs/functions.h index a04eb66..0ca1d9c 100644 --- a/src/ir/irs/functions.h +++ b/src/ir/irs/functions.h @@ -15,4 +15,10 @@ */ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node); +/** + * Parses a AST Asm function into IR. + * @param node the AST node representing the Asm function. + */ +IR_FUNCTION parseASMFunction(AST_ASM_FUNCTION_DEC* node); + #endif \ No newline at end of file From ba2127f3a981467a89ccabeaa070773643bd0b88 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 4 Jan 2025 17:48:22 +0100 Subject: [PATCH 111/121] feat: added parseASMFunction impl --- src/ir/irs/functions.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/ir/irs/functions.c b/src/ir/irs/functions.c index ee60984..35a5654 100644 --- a/src/ir/irs/functions.c +++ b/src/ir/irs/functions.c @@ -4,6 +4,8 @@ #include +#include "../../qasm/parser/parser.h" + #include "../structs.h" #include "../../parser/structs/functions.h" @@ -45,5 +47,22 @@ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { node->body = b->next; } + return func; +} + +/** + * Parses a AST Asm function into IR. + * @param node the AST node representing the Asm function. + */ +IR_FUNCTION parseASMFunction(AST_ASM_FUNCTION_DEC* node) { + IR_FUNCTION func = {0}; + func.blocks = malloc(sizeof(IR_BASIC_BLOCK*)); + + func.blocks[0] = malloc(sizeof(IR_BASIC_BLOCK)); + func.blocks[0]->instructions = NULL; + func.blocks[0]->instructionCount = 0; + + parseQAsmInstructions(func, node->buff, node->buffIndex); + return func; } \ No newline at end of file From 0773a9968f422bb52fd493493dbac24a3a14eccf Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 4 Jan 2025 17:50:07 +0100 Subject: [PATCH 112/121] feat: changed IR_FUNCTION -> IR_FUNCTION* ptr --- src/ir/irs/functions.c | 30 +++++++++++++++--------------- src/ir/irs/functions.h | 4 ++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/ir/irs/functions.c b/src/ir/irs/functions.c index 35a5654..cab8867 100644 --- a/src/ir/irs/functions.c +++ b/src/ir/irs/functions.c @@ -20,17 +20,17 @@ * Parses a AST function into IR. * @param node the AST node representing the function. */ -IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { - IR_FUNCTION func = {0}; - func.blocks = malloc(sizeof(IR_BASIC_BLOCK*)); - func.blockCount++; +IR_FUNCTION* parseFunction(AST_FUNCTION_DEC* node) { + IR_FUNCTION* func = malloc(sizeof(IR_FUNCTION)); + func->blocks = malloc(sizeof(IR_BASIC_BLOCK*)); + func->blockCount++; - func.funcName = node->funcName; + func->funcName = node->funcName; - func.blocks[0] = malloc(sizeof(IR_BASIC_BLOCK)); + func->blocks[0] = malloc(sizeof(IR_BASIC_BLOCK)); - func.blocks[0]->instructions = NULL; - func.blocks[0]->instructionCount = 0; + func->blocks[0]->instructions = NULL; + func->blocks[0]->instructionCount = 0; //todo: move this to another function when finished while(node->body != NULL) { @@ -38,7 +38,7 @@ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { switch(b->type) { case AST_TYPE_VARIABLE_DECLARATION: - parseVariableDeclaration(func.blocks[0], (AST_VARIABLE_DEC*)b); + parseVariableDeclaration(func->blocks[0], (AST_VARIABLE_DEC*)b); break; @@ -54,13 +54,13 @@ IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node) { * Parses a AST Asm function into IR. * @param node the AST node representing the Asm function. */ -IR_FUNCTION parseASMFunction(AST_ASM_FUNCTION_DEC* node) { - IR_FUNCTION func = {0}; - func.blocks = malloc(sizeof(IR_BASIC_BLOCK*)); +IR_FUNCTION* parseASMFunction(AST_ASM_FUNCTION_DEC* node) { + IR_FUNCTION* func = malloc(sizeof(IR_FUNCTION)); + func->blocks = malloc(sizeof(IR_BASIC_BLOCK*)); - func.blocks[0] = malloc(sizeof(IR_BASIC_BLOCK)); - func.blocks[0]->instructions = NULL; - func.blocks[0]->instructionCount = 0; + func->blocks[0] = malloc(sizeof(IR_BASIC_BLOCK)); + func->blocks[0]->instructions = NULL; + func->blocks[0]->instructionCount = 0; parseQAsmInstructions(func, node->buff, node->buffIndex); diff --git a/src/ir/irs/functions.h b/src/ir/irs/functions.h index 0ca1d9c..93af5d9 100644 --- a/src/ir/irs/functions.h +++ b/src/ir/irs/functions.h @@ -13,12 +13,12 @@ * Parses a AST function into IR. * @param node the AST node representing the function. */ -IR_FUNCTION parseFunction(AST_FUNCTION_DEC* node); +IR_FUNCTION* parseFunction(AST_FUNCTION_DEC* node); /** * Parses a AST Asm function into IR. * @param node the AST node representing the Asm function. */ -IR_FUNCTION parseASMFunction(AST_ASM_FUNCTION_DEC* node); +IR_FUNCTION* parseASMFunction(AST_ASM_FUNCTION_DEC* node); #endif \ No newline at end of file From eb119661b890b6397f7b998b6e98bf656baabbce Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 4 Jan 2025 20:48:00 +0100 Subject: [PATCH 113/121] feat: added parameters in IR_FUNCTION struct --- src/cli/main.c | 6 ++++-- src/ir/structs.h | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index 096bbd2..4f1dd75 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -6,6 +6,8 @@ * quickfall help - Shows the help command. */ +#include "../../debug/debugger.h" + #include #include #include @@ -41,8 +43,8 @@ void showCommandEntry(char* commandName, char* description, int argumentCount, c void showHelpMessage() { printf("\n%sQuickfall%s - The programming language.\n\nCommands:\n", TEXT_CYAN, RESET); - char** arguments = malloc(5 * 24); - char** argumentDescriptions = malloc(5 * 256); + char** arguments = malloc(4); + char** argumentDescriptions = malloc(125); arguments[0] = "-p"; argumentDescriptions[0] = "Determines the targeted platform. Defaults to the current platform."; diff --git a/src/ir/structs.h b/src/ir/structs.h index 7516037..ca09846 100644 --- a/src/ir/structs.h +++ b/src/ir/structs.h @@ -2,6 +2,8 @@ * Quickfall IR Structures. */ +#include "../utils/hashmap.h" + #ifndef IR_STRUCTS_H #define IR_STRUCTS_H @@ -38,6 +40,10 @@ typedef struct IR_FUNCTION { IR_BASIC_BLOCK** blocks; int blockCount; + unsigned char* paramTypes; + int parameterCount; + } IR_FUNCTION; + #endif \ No newline at end of file From 0161ce09d40923b5be5a65e50fbb08341008a188 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 5 Jan 2025 15:07:50 +0100 Subject: [PATCH 114/121] feat: changed qasm blocks to be global rather than function definitive --- src/ir/irs/functions.c | 58 +++++++++++++++++++++++++--------------- src/ir/irs/functions.h | 4 +-- src/ir/structs.h | 17 +++++++----- src/qasm/parser/parser.c | 4 +-- src/qasm/parser/parser.h | 2 +- 5 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/ir/irs/functions.c b/src/ir/irs/functions.c index cab8867..e0ccbe9 100644 --- a/src/ir/irs/functions.c +++ b/src/ir/irs/functions.c @@ -16,51 +16,65 @@ #include "./variables.h" +#include "../../utils/hash.h" +#include "../../utils/hashmap.h" + /** * Parses a AST function into IR. * @param node the AST node representing the function. */ -IR_FUNCTION* parseFunction(AST_FUNCTION_DEC* node) { +void parseFunction(IR_OUTPUT* out, AST_FUNCTION_DEC* node) { + int hash = strhash(node->funcName); IR_FUNCTION* func = malloc(sizeof(IR_FUNCTION)); - func->blocks = malloc(sizeof(IR_BASIC_BLOCK*)); - func->blockCount++; - func->funcName = node->funcName; + func->startBlock = out->blockCount; + func->typeCount = node->parameterIndex; - func->blocks[0] = malloc(sizeof(IR_BASIC_BLOCK)); + func->types = malloc(func->typeCount + 1); - func->blocks[0]->instructions = NULL; - func->blocks[0]->instructionCount = 0; + if(node->returnType != NULL) func->types[0] = node->returnType[0]; + else func->types[0] == 0x00; - //todo: move this to another function when finished - while(node->body != NULL) { - AST_TREE_BRANCH* b = (AST_TREE_BRANCH*) node->body; + for(int i = 0; i < func->typeCount; ++i) { + func->types[i + 1] = node->parameters[i].type; + } + + hashPut(out->map, hash, func); + + out->blocks[out->blockCount] = malloc(sizeof(IR_BASIC_BLOCK)); + out->blocks[out->blockCount]->instructions = NULL; + out->blocks[out->blockCount]->instructionCount = 0; + out->blocks[out->blockCount]->allocatedSize = 0; - switch(b->type) { + while(node->body != NULL) { + AST_TREE_BRANCH* branch = (AST_TREE_BRANCH*) node->body; + switch(branch->type) { case AST_TYPE_VARIABLE_DECLARATION: - parseVariableDeclaration(func->blocks[0], (AST_VARIABLE_DEC*)b); - + parseVariableDeclaration(out->blocks[out->blockCount], (AST_VARIABLE_DEC*)branch); break; - } - - node->body = b->next; + node->body = branch->next; } - return func; } /** * Parses a AST Asm function into IR. * @param node the AST node representing the Asm function. */ -IR_FUNCTION* parseASMFunction(AST_ASM_FUNCTION_DEC* node) { +void parseASMFunction(IR_OUTPUT* out, AST_ASM_FUNCTION_DEC* node) { IR_FUNCTION* func = malloc(sizeof(IR_FUNCTION)); - func->blocks = malloc(sizeof(IR_BASIC_BLOCK*)); + func->startBlock = out->blockCount; + + func->types = malloc(1); + func->types[0] = 0x00; + func->typeCount = 1; + + hashPut(out->map, hashstr(node->funcName), func); - func->blocks[0] = malloc(sizeof(IR_BASIC_BLOCK)); - func->blocks[0]->instructions = NULL; - func->blocks[0]->instructionCount = 0; + out->blocks[out->blockCount] = malloc(sizeof(IR_BASIC_BLOCK)); + out->blocks[out->blockCount]->instructions = NULL; + out->blocks[out->blockCount]->instructionCount = 0; parseQAsmInstructions(func, node->buff, node->buffIndex); diff --git a/src/ir/irs/functions.h b/src/ir/irs/functions.h index 93af5d9..5bdb6a7 100644 --- a/src/ir/irs/functions.h +++ b/src/ir/irs/functions.h @@ -13,12 +13,12 @@ * Parses a AST function into IR. * @param node the AST node representing the function. */ -IR_FUNCTION* parseFunction(AST_FUNCTION_DEC* node); +void parseFunction(IR_OUTPUT* out, AST_FUNCTION_DEC* node); /** * Parses a AST Asm function into IR. * @param node the AST node representing the Asm function. */ -IR_FUNCTION* parseASMFunction(AST_ASM_FUNCTION_DEC* node); +void parseASMFunction(IR_OUTPUT* out, AST_ASM_FUNCTION_DEC* node); #endif \ No newline at end of file diff --git a/src/ir/structs.h b/src/ir/structs.h index ca09846..2e8136d 100644 --- a/src/ir/structs.h +++ b/src/ir/structs.h @@ -35,15 +35,20 @@ typedef struct IR_BASIC_BLOCK { */ typedef struct IR_FUNCTION { - char* funcName; + int startBlock; - IR_BASIC_BLOCK** blocks; - int blockCount; - - unsigned char* paramTypes; - int parameterCount; + unsigned char* types; + int typeCount; } IR_FUNCTION; +typedef struct IR_OUTPUT { + + IR_BASIC_BLOCK** blocks; + int blockCount; + int allocatedBlockCount; + + struct Hashmap* map; +} IR_OUTPUT; #endif \ No newline at end of file diff --git a/src/qasm/parser/parser.c b/src/qasm/parser/parser.c index e02d736..4bc814e 100644 --- a/src/qasm/parser/parser.c +++ b/src/qasm/parser/parser.c @@ -21,7 +21,7 @@ /** * Parses QuickAssembly instructions. */ -void parseQAsmInstructions(IR_FUNCTION* func, char* buffer, int size) { +void parseQAsmInstructions(IR_BASIC_BLOCK* block, char* buffer, int size) { char** buff = malloc(sizeof(char*) * 10); int buffIndex = 0; @@ -44,7 +44,7 @@ void parseQAsmInstructions(IR_FUNCTION* func, char* buffer, int size) { printf("Error: Coudln't parse QuickAssembly instruction named %s!\n", buff[0]); } else { - pushInstruction(func->blocks[0], instruction); + pushInstruction(block, instruction); } secIndex = 0; diff --git a/src/qasm/parser/parser.h b/src/qasm/parser/parser.h index e74ebc3..8c61b49 100644 --- a/src/qasm/parser/parser.h +++ b/src/qasm/parser/parser.h @@ -11,7 +11,7 @@ /** * Parses QuickAssembly instructions. */ -void parseQAsmInstructions(IR_FUNCTION* func, char* buff, int size); +void parseQAsmInstructions(IR_BASIC_BLOCK* block, char* buff, int size); /** * Parses a QuickAssembly instruction. From dc3abb049c7db152e3e4a5342eb2b4f61a2ad2b3 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 5 Jan 2025 15:10:37 +0100 Subject: [PATCH 115/121] feat: added parseIR in header --- src/ir/ir.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ir/ir.h b/src/ir/ir.h index c9c8551..613cd8c 100644 --- a/src/ir/ir.h +++ b/src/ir/ir.h @@ -10,6 +10,7 @@ #include "../parser/structs/variables.h" #include "../parser/structs/functions.h" +#include "../parser/structs/tree.h" #include "../parser/ast.h" @@ -29,4 +30,9 @@ void pushInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION* instruction); */ void appendInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION_CODE code, unsigned char params[], int paramsCount); +/** + * Converts the AST tree into IR. + */ +IR_OUTPUT* parseIR(AST_TREE_BRANCH* node); + #endif \ No newline at end of file From 3ba79514672e75097ce34259a346d9c1877210f7 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 8 Jan 2025 14:51:38 +0100 Subject: [PATCH 116/121] feat: added parseIR impl --- src/ir/ir.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/ir/ir.c b/src/ir/ir.c index 3aa23ba..b4cf328 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -16,6 +16,7 @@ #include "./irs/variables.h" #include "./irs/values.h" +#include "./irs/functions.h" #include "../parser/ast.h" @@ -50,4 +51,27 @@ void appendInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION_CODE code, unsigned instruction->paramCount = paramsCount; pushInstruction(block, instruction); +} + + +/** + * Converts the AST tree into IR. + */ +IR_OUTPUT* parseIR(AST_TREE_BRANCH* node) { + IR_OUTPUT* out = malloc(sizeof(IR_OUTPUT)); + + while(node != NULL) { + switch(node->type) { + case AST_TYPE_FUNCTION_DECLARATION: + parseFunction(out, (AST_FUNCTION_DEC*)node); + break; + case AST_TYPE_ASM_FUNCTION_DECLARATION: + parseASMFunction(out, (AST_ASM_FUNCTION_DEC*)node); + break; + case AST_TYPE_VARIABLE_DECLARATION: + parseVariableDeclaration(out->blocks[0], (AST_VARIABLE_DEC*)node); + break; + } + node = node->next; + } } \ No newline at end of file From 9b4ddc840285302f04b75a65a550212496cf6815 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 8 Jan 2025 14:52:13 +0100 Subject: [PATCH 117/121] fix: added forgotten return statement --- src/ir/ir.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ir/ir.c b/src/ir/ir.c index b4cf328..0e66735 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -74,4 +74,6 @@ IR_OUTPUT* parseIR(AST_TREE_BRANCH* node) { } node = node->next; } + + return out; } \ No newline at end of file From 3a9e827dac1f96204d01e8641b999ca918c5aaf2 Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 8 Jan 2025 15:27:25 +0100 Subject: [PATCH 118/121] feat: initialized out --- src/ir/ir.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ir/ir.c b/src/ir/ir.c index 0e66735..063f06a 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -59,6 +59,11 @@ void appendInstruction(IR_BASIC_BLOCK* block, IR_INSTRUCTION_CODE code, unsigned */ IR_OUTPUT* parseIR(AST_TREE_BRANCH* node) { IR_OUTPUT* out = malloc(sizeof(IR_OUTPUT)); + + out->allocatedBlockCount = 0; + out->blockCount = 0; + out->blocks = NULL; + out->map = malloc(sizeof(struct Hashmap)); while(node != NULL) { switch(node->type) { From 80419b0356dbeeccddf9936b93e8f76e171affac Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 8 Jan 2025 15:29:05 +0100 Subject: [PATCH 119/121] fix: removed unused return statement --- src/ir/irs/functions.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ir/irs/functions.c b/src/ir/irs/functions.c index e0ccbe9..b7c6325 100644 --- a/src/ir/irs/functions.c +++ b/src/ir/irs/functions.c @@ -77,6 +77,4 @@ void parseASMFunction(IR_OUTPUT* out, AST_ASM_FUNCTION_DEC* node) { out->blocks[out->blockCount]->instructionCount = 0; parseQAsmInstructions(func, node->buff, node->buffIndex); - - return func; } \ No newline at end of file From 21b9c17f62215a940548876cbe5741f7ed1097de Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 8 Jan 2025 15:31:48 +0100 Subject: [PATCH 120/121] feat: freed some char buffers --- src/qasm/parser/parser.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/qasm/parser/parser.c b/src/qasm/parser/parser.c index 4bc814e..e404256 100644 --- a/src/qasm/parser/parser.c +++ b/src/qasm/parser/parser.c @@ -60,6 +60,12 @@ void parseQAsmInstructions(IR_BASIC_BLOCK* block, char* buffer, int size) { secIndex++; } } + + for(int i = 0; i < 10; ++i) { + free(buff[i]); + } + + free(buff); } /** From 8be6ef1d682a708a0309b5af666fe2a138e1670d Mon Sep 17 00:00:00 2001 From: Zffu Date: Wed, 8 Jan 2025 15:32:59 +0100 Subject: [PATCH 121/121] feat: removed debugger call --- src/cli/main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index 4f1dd75..fb5b8dc 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -6,8 +6,6 @@ * quickfall help - Shows the help command. */ -#include "../../debug/debugger.h" - #include #include #include