From fd31f685d47a1518c6562fdae344fd337605ed7f Mon Sep 17 00:00:00 2001 From: Zffu Date: Fri, 29 Nov 2024 00:53:58 +0100 Subject: [PATCH 01/38] feat: added empty header --- src/compilerv2/compilerv2.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/compilerv2/compilerv2.h diff --git a/src/compilerv2/compilerv2.h b/src/compilerv2/compilerv2.h new file mode 100644 index 0000000..0cd36df --- /dev/null +++ b/src/compilerv2/compilerv2.h @@ -0,0 +1,15 @@ +/** + * The compiler of Quickfall. + */ + +#ifndef COMPILER_2_H +#define COMPILER_2_H + +/** + * A context is the less abstract way Quickfall represents the code before converting it to assembly. + */ +struct Context { + +}; + +#endif From 27fa9264d1d2d2b85971b933af4aa6fd50220a62 Mon Sep 17 00:00:00 2001 From: Zffu Date: Fri, 29 Nov 2024 00:55:13 +0100 Subject: [PATCH 02/38] feat: added parserContext --- src/compilerv2/compilerv2.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/compilerv2/compilerv2.h b/src/compilerv2/compilerv2.h index 0cd36df..6c7cb6c 100644 --- a/src/compilerv2/compilerv2.h +++ b/src/compilerv2/compilerv2.h @@ -5,6 +5,8 @@ #ifndef COMPILER_2_H #define COMPILER_2_H +#include "../parser/ast.h" + /** * A context is the less abstract way Quickfall represents the code before converting it to assembly. */ @@ -12,4 +14,9 @@ struct Context { }; +/** + * Parses the AST tree into a context. + */ +struct Context parseContext(struct ASTNode* node); + #endif From 75c0271270d7c39bb42e433d6933775e603c4580 Mon Sep 17 00:00:00 2001 From: Zffu Date: Fri, 29 Nov 2024 01:01:07 +0100 Subject: [PATCH 03/38] feat: added compiler objects --- src/compilerv2/objects.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/compilerv2/objects.h diff --git a/src/compilerv2/objects.h b/src/compilerv2/objects.h new file mode 100644 index 0000000..42fbe0a --- /dev/null +++ b/src/compilerv2/objects.h @@ -0,0 +1,30 @@ +/** + * Compiling objects such as functions, variables... + */ + +#ifndef COMPILER_OBJECTS_H +#define COMPILER_OBJECTS_H + +#include "../parser/ast.h" + +struct Variable { + char* name; + char* value; + char* type; + byte type; // 0 = variable, 1 = Func argument, 2 = Function body argument +}; + +struct FunctionBody { + struct Variable* +}; + +struct Function { + char* name; + struct Variable* variables; + int variableCount; + struct ASTNode* body; +}; + + + +#endif From 3b654382272fb1c6ad8bb3146658a2f2dfc472ee Mon Sep 17 00:00:00 2001 From: Zffu Date: Fri, 29 Nov 2024 01:01:49 +0100 Subject: [PATCH 04/38] feat: added variables and functions in context --- src/compilerv2/compilerv2.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/compilerv2/compilerv2.h b/src/compilerv2/compilerv2.h index 6c7cb6c..f771e24 100644 --- a/src/compilerv2/compilerv2.h +++ b/src/compilerv2/compilerv2.h @@ -6,12 +6,16 @@ #define COMPILER_2_H #include "../parser/ast.h" +#include "./objects.h" /** * A context is the less abstract way Quickfall represents the code before converting it to assembly. */ struct Context { - + struct Variable* variables; + struct Function* functions; + int variableCount; + int functionCount; }; /** From b8de72a8cfe867e47214f59c63d4bebb1b977cd0 Mon Sep 17 00:00:00 2001 From: Zffu Date: Fri, 29 Nov 2024 08:55:48 +0100 Subject: [PATCH 05/38] feat: added vars in function body --- src/compilerv2/objects.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compilerv2/objects.h b/src/compilerv2/objects.h index 42fbe0a..9ff9deb 100644 --- a/src/compilerv2/objects.h +++ b/src/compilerv2/objects.h @@ -15,7 +15,8 @@ struct Variable { }; struct FunctionBody { - struct Variable* + struct Variable* vars; + int varCount; }; struct Function { From faf6a34ac7fe2080669fa35eaea5f08afd5726ae Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 30 Nov 2024 00:32:54 +0100 Subject: [PATCH 06/38] feat: removed FunctionBody struct --- src/cli/main.c | 2 +- src/compilerv2/objects.h | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index f86a018..ecce710 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -1,4 +1,4 @@ -/** +** * The file of the Quickfall CLI. * A modern, fast and lightweight programming language. * Usage: diff --git a/src/compilerv2/objects.h b/src/compilerv2/objects.h index 42fbe0a..750b3ff 100644 --- a/src/compilerv2/objects.h +++ b/src/compilerv2/objects.h @@ -14,10 +14,6 @@ struct Variable { byte type; // 0 = variable, 1 = Func argument, 2 = Function body argument }; -struct FunctionBody { - struct Variable* -}; - struct Function { char* name; struct Variable* variables; From c46e124ce1b22b0fe91ea0d6b3e439afcd44a12b Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 30 Nov 2024 00:36:27 +0100 Subject: [PATCH 07/38] reverted git wierd --- ast.s | 42 --- bench.json | 966 ----------------------------------------------- gmon.out | Bin 20664 -> 0 bytes main.c | 5 - main.s | 28 -- output.txt | 9 - stdl/hello.qfall | 5 - t.s | 9 - 8 files changed, 1064 deletions(-) delete mode 100644 ast.s delete mode 100644 bench.json delete mode 100644 gmon.out delete mode 100644 main.c delete mode 100644 main.s delete mode 100644 output.txt delete mode 100644 stdl/hello.qfall delete mode 100644 t.s diff --git a/ast.s b/ast.s deleted file mode 100644 index 162d609..0000000 --- a/ast.s +++ /dev/null @@ -1,42 +0,0 @@ - .file "ast.c" - .text - .globl createASTNode - .def createASTNode; .scl 2; .type 32; .endef - .seh_proc createASTNode -createASTNode: - pushq %rbp - .seh_pushreg %rbp - movq %rsp, %rbp - .seh_setframe %rbp, 0 - subq $48, %rsp - .seh_stackalloc 48 - .seh_endprologue - movl %ecx, 16(%rbp) - movl $72, %ecx - call malloc - movq %rax, -8(%rbp) - movq -8(%rbp), %rax - movq $0, 40(%rax) - movq -8(%rbp), %rax - movq $0, 48(%rax) - movq -8(%rbp), %rax - movq $0, 56(%rax) - movq -8(%rbp), %rax - movl 16(%rbp), %edx - movl %edx, (%rax) - movq -8(%rbp), %rax - movl $0, 64(%rax) - movq -8(%rbp), %rax - addq $4, %rax - movl $32, %r8d - movl $0, %edx - movq %rax, %rcx - call memset - movq -8(%rbp), %rax - addq $48, %rsp - popq %rbp - ret - .seh_endproc - .ident "GCC: (Rev2, Built by MSYS2 project) 14.2.0" - .def malloc; .scl 2; .type 32; .endef - .def memset; .scl 2; .type 32; .endef diff --git a/bench.json b/bench.json deleted file mode 100644 index 60a5091..0000000 --- a/bench.json +++ /dev/null @@ -1,966 +0,0 @@ -{ - "results": [ - { - "command": "quickfall compile examples/helloWorld.qf -p win -o t.s", - "mean": 0.0045328828955223865, - "stddev": 0.0004930933967893705, - "median": 0.004452022, - "user": 0.0006063432835820895, - "system": 0.0008395522388059701, - "min": 0.004054322000000001, - "max": 0.010138122000000001, - "times": [ - 0.004767922000000001, - 0.004694222000000001, - 0.004465122, - 0.004401422, - 0.004348122000000001, - 0.0043571220000000015, - 0.004288722, - 0.004358322000000001, - 0.0045892220000000004, - 0.004314322000000001, - 0.0045122220000000015, - 0.004398822, - 0.004553422000000001, - 0.004465222000000001, - 0.004624122000000001, - 0.004731622000000001, - 0.004225722000000001, - 0.004441922000000001, - 0.0044938220000000015, - 0.004444222000000001, - 0.004319422000000002, - 0.004279522000000001, - 0.004782222000000001, - 0.0045036220000000005, - 0.004277322, - 0.004220322, - 0.0044370220000000005, - 0.004418022000000001, - 0.0043041220000000005, - 0.004576722, - 0.004492122000000001, - 0.004716622, - 0.004373622000000001, - 0.004383722000000001, - 0.004262722, - 0.004474022000000001, - 0.004268122000000001, - 0.004423222000000001, - 0.0042867220000000015, - 0.004259322000000001, - 0.004228622000000001, - 0.004460622000000001, - 0.0043825220000000015, - 0.004457422000000001, - 0.004396622000000001, - 0.005072822000000001, - 0.0055283220000000004, - 0.004759122000000001, - 0.004552722, - 0.004297722, - 0.0044421220000000015, - 0.004175922, - 0.0048588220000000005, - 0.004635422, - 0.0045230220000000015, - 0.004280522, - 0.004448622000000001, - 0.004241122, - 0.004209922000000001, - 0.004703022000000001, - 0.004380322000000001, - 0.004220822000000001, - 0.004410822, - 0.0051582220000000005, - 0.005083922000000001, - 0.004854622000000001, - 0.004439322000000001, - 0.004387022000000001, - 0.004304222, - 0.004152922000000002, - 0.004525122000000001, - 0.004937122, - 0.004461322, - 0.004235322000000001, - 0.004523922000000001, - 0.004474022000000001, - 0.004408422, - 0.004543422, - 0.004467622000000001, - 0.004310922, - 0.004438122000000001, - 0.004599622000000001, - 0.004346322000000001, - 0.004358922000000001, - 0.004590922000000001, - 0.0043051220000000015, - 0.004525322, - 0.004388222000000001, - 0.0054319220000000005, - 0.004941722000000001, - 0.004768822000000001, - 0.004541922, - 0.004686822, - 0.0042645220000000015, - 0.004286522000000001, - 0.004780522000000001, - 0.004329022, - 0.004185222000000001, - 0.004450522, - 0.004315322, - 0.004498022000000001, - 0.004953422000000001, - 0.004597622000000001, - 0.004364522000000001, - 0.004251022, - 0.004531922000000001, - 0.004188622000000001, - 0.004493422, - 0.004909122, - 0.004470522000000001, - 0.004454422000000001, - 0.0045344220000000015, - 0.004421922, - 0.004417622000000001, - 0.004471822, - 0.004302422, - 0.004353122000000001, - 0.004535322000000001, - 0.004485422000000001, - 0.004417322000000001, - 0.004295422, - 0.004279622, - 0.004309722, - 0.004167022000000001, - 0.004260522000000001, - 0.004828222, - 0.0047218220000000005, - 0.004329422000000001, - 0.004086522, - 0.009946822000000001, - 0.004420722, - 0.004447822000000001, - 0.004399222000000001, - 0.004393722000000001, - 0.004394422, - 0.004544022, - 0.004396522, - 0.004441322000000001, - 0.004453122, - 0.004414622, - 0.004619022, - 0.0042559220000000005, - 0.004482822000000001, - 0.004466322, - 0.004288622000000001, - 0.004566322000000001, - 0.0044522220000000005, - 0.004625222, - 0.004386722000000001, - 0.004380122, - 0.004301422000000001, - 0.0045112220000000005, - 0.004267822000000001, - 0.004601222, - 0.004354222000000001, - 0.004894322000000001, - 0.004441922000000001, - 0.004478822, - 0.0041465220000000014, - 0.004557622000000001, - 0.004606622000000001, - 0.004854822, - 0.005107122, - 0.005751722000000001, - 0.004873822000000002, - 0.004565422000000001, - 0.004409722000000001, - 0.004563422000000001, - 0.004608722000000001, - 0.004364022, - 0.0044265220000000004, - 0.004551122000000001, - 0.004374822, - 0.004782922, - 0.004448222, - 0.004391222, - 0.004468222000000001, - 0.004258122000000001, - 0.004506522000000001, - 0.0048040220000000015, - 0.005112322000000001, - 0.005187522, - 0.004597022000000001, - 0.004467422, - 0.004424022000000001, - 0.004458522000000001, - 0.0043333220000000006, - 0.004365222, - 0.0044925220000000005, - 0.004626622, - 0.004500922000000001, - 0.004667522, - 0.004452022, - 0.004300522000000001, - 0.004306322000000001, - 0.004626922, - 0.004353222, - 0.004862722000000002, - 0.0042131220000000014, - 0.004531022000000001, - 0.004612622, - 0.004676022, - 0.0047148220000000005, - 0.004657922, - 0.004689722, - 0.004605622, - 0.004626922, - 0.004376922, - 0.004626122000000002, - 0.004767622000000001, - 0.004519622000000001, - 0.004394822000000001, - 0.004465122, - 0.0042826220000000016, - 0.004436122000000001, - 0.004933222000000001, - 0.004534222000000001, - 0.004494622, - 0.004384922000000001, - 0.004403922000000001, - 0.004324122000000001, - 0.004757422000000001, - 0.0045372220000000005, - 0.004319422000000002, - 0.004425522000000001, - 0.004521922000000001, - 0.004356922000000001, - 0.004810822000000001, - 0.004429422000000001, - 0.004338322, - 0.004630122, - 0.004359222000000001, - 0.004878722, - 0.004568722000000001, - 0.004685422000000002, - 0.004426022, - 0.004373122, - 0.004284022, - 0.004743922000000001, - 0.004548922, - 0.004584322, - 0.0044405220000000006, - 0.0042382220000000016, - 0.004471422000000001, - 0.0046187220000000005, - 0.004455322000000001, - 0.004538022000000001, - 0.004458522000000001, - 0.004278322000000001, - 0.004310122000000001, - 0.004443222, - 0.004387122, - 0.004384022000000001, - 0.004685522000000001, - 0.004241822000000001, - 0.004784722, - 0.0041871220000000015, - 0.0043016220000000015, - 0.0046187220000000005, - 0.004350122000000001, - 0.004469222, - 0.004529022000000001, - 0.004221922000000001, - 0.005071922000000001, - 0.004769122000000001, - 0.004402322, - 0.004207522, - 0.004290822000000001, - 0.004521522, - 0.004808622, - 0.004397622, - 0.004054322000000001, - 0.004337822000000002, - 0.004096122000000001, - 0.004235822, - 0.004368322000000001, - 0.005047822, - 0.004905422000000001, - 0.004433722000000001, - 0.004767422, - 0.004852922000000001, - 0.005105622000000001, - 0.004624422000000001, - 0.0048182220000000005, - 0.010138122000000001, - 0.004443022000000001, - 0.004582722000000001, - 0.004392422, - 0.004255322000000001, - 0.0044703220000000005, - 0.0044716220000000015, - 0.004270322, - 0.005010222, - 0.0045337220000000004, - 0.004413422, - 0.004298822000000001, - 0.004416222000000001, - 0.0050139220000000005, - 0.005719322000000001, - 0.004536522000000001, - 0.004499322, - 0.004203322000000001, - 0.0044487220000000004, - 0.004279222000000001, - 0.004461622, - 0.004487522000000001, - 0.004298022, - 0.004301222, - 0.004327822, - 0.004624822000000001, - 0.0047082220000000015, - 0.004775322, - 0.0041233220000000004, - 0.004417022000000001, - 0.004196522000000001, - 0.0041906220000000015, - 0.0047193220000000015, - 0.004119622, - 0.004712422000000001, - 0.004702922, - 0.004169322000000001, - 0.004239122000000001, - 0.004788522, - 0.004317122000000001, - 0.004508522000000001, - 0.004288722, - 0.0044516220000000006, - 0.0043644220000000015, - 0.004723322, - 0.004154422000000001, - 0.004329022, - 0.004327922000000001, - 0.004360722000000001, - 0.004571922000000001, - 0.0048588220000000005 - ], - "exit_codes": [ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ] - }, - { - "command": "gcc hello.c -o hello.exe", - "mean": 0.023535573515151512, - "stddev": 0.006088903352551694, - "median": 0.022732272000000005, - "user": 0.0005918560606060606, - "system": 0.004261363636363636, - "min": 0.017169022, - "max": 0.066111022, - "times": [ - 0.018458022000000004, - 0.017559222, - 0.018157722, - 0.018543222000000005, - 0.027428822000000005, - 0.029927122000000007, - 0.025775322000000003, - 0.028523922000000007, - 0.021125322000000002, - 0.028213722000000004, - 0.025070622, - 0.023867022, - 0.020004422, - 0.022923022, - 0.025445422000000002, - 0.022343222000000003, - 0.025212922, - 0.024168422000000002, - 0.020532922000000002, - 0.023424222, - 0.017635522, - 0.018662822000000003, - 0.03148352200000001, - 0.020261422, - 0.025201622, - 0.019170422, - 0.022993022000000002, - 0.017774922, - 0.017273422000000004, - 0.024949322000000003, - 0.018007222000000003, - 0.022835322000000005, - 0.017481422000000003, - 0.019549722, - 0.024422322000000003, - 0.020098422000000005, - 0.023204422000000002, - 0.017522522, - 0.030682122000000006, - 0.020048122, - 0.025831622000000005, - 0.018043322, - 0.022212222000000004, - 0.023248722, - 0.027555722000000005, - 0.017652022000000003, - 0.035106922000000006, - 0.026011522000000002, - 0.027596722000000004, - 0.017169022, - 0.025718622000000003, - 0.017334122, - 0.025818422, - 0.017614122000000003, - 0.031263322, - 0.018168522, - 0.032098522000000004, - 0.018063322, - 0.066111022, - 0.03622492200000001, - 0.029114822000000005, - 0.028235022000000005, - 0.024370822, - 0.022247522, - 0.035062722000000004, - 0.025267622000000003, - 0.026846122, - 0.017876022, - 0.021627622, - 0.021486922, - 0.026859522000000004, - 0.020784822, - 0.024037322, - 0.017786222000000004, - 0.034138322000000006, - 0.023325522, - 0.037319122, - 0.020533022000000005, - 0.021188822000000003, - 0.023866522, - 0.033383522000000006, - 0.018424922000000003, - 0.025056122, - 0.017861022000000004, - 0.026992622, - 0.018277122, - 0.022514522000000002, - 0.019421822, - 0.027475622000000005, - 0.019370622000000004, - 0.024608722, - 0.018621422, - 0.025576622, - 0.018377222, - 0.027168122000000003, - 0.022618822000000004, - 0.028364122000000005, - 0.017965522000000005, - 0.025461922000000005, - 0.021772922, - 0.028140922000000006, - 0.017386422, - 0.022629222000000004, - 0.017791522000000004, - 0.017497422, - 0.024573022, - 0.018211722, - 0.021925822000000005, - 0.017463122000000005, - 0.020725722000000002, - 0.027885022000000002, - 0.021153522, - 0.020683222, - 0.017882622, - 0.017716122, - 0.022398622, - 0.022279622, - 0.029056522, - 0.023061222, - 0.024829222000000005, - 0.021738122, - 0.023864822, - 0.018037322, - 0.024420522, - 0.017571522, - 0.027078922000000005, - 0.026546922, - 0.028733122000000007, - 0.019029222, - 0.034093622000000004, - 0.017841422000000003, - 0.028356722 - ], - "exit_codes": [ - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1 - ] - } - ] -} diff --git a/gmon.out b/gmon.out deleted file mode 100644 index 1bf04072b18b7e75920c76cf4e2bfcee6da2d5ff..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20664 zcmeIuu?;|A0EE#~C?$WPFosbKVF0Z}qP393NH&nD6l~#L^X1xC+U>m7q~G5jLo#=D z#=I-b)ZEo~ga82o1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U_?5r|A{z?a diff --git a/main.c b/main.c deleted file mode 100644 index 70a9157..0000000 --- a/main.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -void main() { - printf("Hello World!"); -} \ No newline at end of file diff --git a/main.s b/main.s deleted file mode 100644 index 00c7017..0000000 --- a/main.s +++ /dev/null @@ -1,28 +0,0 @@ - .file "main.c" - .text - .section .rdata,"dr" -.LC0: - .ascii "Hello World!\0" - .text - .globl main - .def main; .scl 2; .type 32; .endef - .seh_proc main -main: - pushq %rbp - .seh_pushreg %rbp - movq %rsp, %rbp - .seh_setframe %rbp, 0 - subq $32, %rsp - .seh_stackalloc 32 - .seh_endprologue - call __main - leaq .LC0(%rip), %rax - movq %rax, %rcx - call __mingw_printf - nop - addq $32, %rsp - popq %rbp - ret - .seh_endproc - .def __main; .scl 2; .type 32; .endef - .ident "GCC: (Rev2, Built by MSYS2 project) 14.2.0" diff --git a/output.txt b/output.txt deleted file mode 100644 index 465a89c..0000000 --- a/output.txt +++ /dev/null @@ -1,9 +0,0 @@ -.LC0: - .globl main - .ascii "Hello World\0" -main: - subq $40, %rsp - leaq .LC0(%rip), %rcx - call printf - addq $40, %rsp - ret diff --git a/stdl/hello.qfall b/stdl/hello.qfall deleted file mode 100644 index a119ae9..0000000 --- a/stdl/hello.qfall +++ /dev/null @@ -1,5 +0,0 @@ -func sayHello(none bob) { - salloc(40); - call(printf); - sfree(40); -} diff --git a/t.s b/t.s deleted file mode 100644 index 465a89c..0000000 --- a/t.s +++ /dev/null @@ -1,9 +0,0 @@ -.LC0: - .globl main - .ascii "Hello World\0" -main: - subq $40, %rsp - leaq .LC0(%rip), %rcx - call printf - addq $40, %rsp - ret From 67133707e28079faa175092b6e2a8cc046d49ed9 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 30 Nov 2024 00:37:16 +0100 Subject: [PATCH 08/38] feat: fixed object header being afftected by git --- src/compilerv2/objects.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/compilerv2/objects.h b/src/compilerv2/objects.h index 041cc39..e568391 100644 --- a/src/compilerv2/objects.h +++ b/src/compilerv2/objects.h @@ -14,14 +14,6 @@ struct Variable { byte type; // 0 = variable, 1 = Func argument, 2 = Function body argument }; -<<<<<<< HEAD -======= -struct FunctionBody { - struct Variable* vars; - int varCount; -}; - ->>>>>>> b8de72a8cfe867e47214f59c63d4bebb1b977cd0 struct Function { char* name; struct Variable* variables; @@ -29,6 +21,4 @@ struct Function { struct ASTNode* body; }; - - #endif From c7a441ce18ff7842fe17607e36d3bf47f204d527 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 00:01:07 +0100 Subject: [PATCH 09/38] feat: added ast function -> compiler func --- src/compilerv2/compilerv2.c | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/compilerv2/compilerv2.c diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c new file mode 100644 index 0000000..e4a550a --- /dev/null +++ b/src/compilerv2/compilerv2.c @@ -0,0 +1,48 @@ +/** + * The compiler of Quickfall. + */ + +#include + +#include "./compilerv2.h" +#include "./objects.h" +#include "../parser/ast.h" + +struct Context parseContext(ASTNode* node) { + struct Context ctx = {0}; + + ctx.variables = malloc(sizeof(struct Variable) * 50); + ctx.functions = malloc(sizeof(struct Functions) * 50); + + ctx.variableCount = 0; + ctx.functionCount = 0; + + while(node->next != NULL) { + node = node->next; + switch (node->type) { + case AST_VARIABLE_DEF: + ctx.variables[ctx.variableCount].name = node->left->value; + ctx.variables[ctx.variableCount].type = "none"; + ctx.variables[ctx.variableCount].value = node->right->value; + ctx.variableCount++; + break; + case AST_FUNCION_DEF: + ctx.functions[ctx.functionCount].name = node->left->left->value; + while(node->left->right->next != NULL) { + node->left->right = node->left->right->next; + + int c = ctx.functions[ctx.functionCount].variableCount; + + ctx.functions[ctx.functionCount].variables[c].name = node->left->right->right->value; + ctx.functions[ctx.functionCount].variables[c].type = node->left->right->left->value; + ctx.functions[ctx.functionCount].variableCount++; + } + + ctx.functions[ctx.functionCount].body = node->right; + ctx.functionCount++; + break; + } + } + +} + From 4666081230acc0c4649dcbc7d29edf7d8db26083 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 00:23:40 +0100 Subject: [PATCH 10/38] chore: remade makefile --- Makefile | 118 +++++++++++++++++++++++++------------------------------ 1 file changed, 54 insertions(+), 64 deletions(-) diff --git a/Makefile b/Makefile index 6e63292..5413a27 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,9 @@ -# Detect operating system and set commands accordingly +# +# +# The Quickfall makefile. Allows to build the Quickfall toolchain. +# +# + ifeq ($(OS),Windows_NT) DETECTED_OS := Windows TARGET_EXTENSION := .exe @@ -15,74 +20,59 @@ else PATHSEP := / endif -# Compiler settings -CFLAGS = -Wall -Wextra -std=c11 -Wimplicit-function-declaration -g +# +# Compiling configuration. +# + +# Which compiler to use (must be equal to the command used to compile) +COMPILER = gcc + +# Flags that are passed to the compiler +FLAGS = -Wall -Wextra -Wimplicit-function-declaration + +# +# Path configuration. +# -# Directories +# The directory containing the code of Quickfall. SRC_DIR = src -CLI_DIR = $(SRC_DIR)/cli -LEXER_DIR = $(SRC_DIR)/lexer -PARSER_DIR = $(SRC_DIR)/parser -COMPILER_DIR = $(SRC_DIR)/compiler -COMPILER_PLATFORMS = $(COMPILER_DIR)/platforms -UTILS_DIR = $(SRC_DIR)/utils -BUILD_DIR = build - -# Source files -SOURCES = $(LEXER_DIR)/lexer.c \ - $(LEXER_DIR)/tokens.c \ - $(PARSER_DIR)/parser.c \ - $(PARSER_DIR)/ast.c \ - $(COMPILER_DIR)/compiler.c \ - $(COMPILER_DIR)/stdl.c \ - $(COMPILER_PLATFORMS)/linux.c \ - $(COMPILER_PLATFORMS)/windowsx86-64.c \ - $(UTILS_DIR)/hashes.c \ - -# Executable name + +# The directory containing the benchmarks of Quickfall. +BENCH_SRC_DIR = benchmarks + +# +# Output definition +# + +# The target of a normal mode compiling. TARGET = quickfall$(TARGET_EXTENSION) + +# The target of a bench mode compiling. BENCH_TARGET = bench$(TARGET_EXTENSION) -# Default target -all: check_commands $(TARGET) -bench: check_commands $(BENCH_TARGET) -OBJECTS = $(SOURCES:%.c=$(BUILD_DIR)/%.o) +# +# Building variables +# + +# The sources that are going to be compiled in normal mode. +SOURCES = $(wildcard ${SRC_DIR}/**/*.c) $(wildcard ${SRC_DIR}/**/**/*.c) + +BENCH_SOURCES = ${SOURCES} $(wildcard ${BENCH_SRC_DIR}/**/*.c) + +# +# Building logic +# -# Check commands target -check_commands: - @echo "Using compiler: gcc" - @echo "Operating System: $(DETECTED_OS)" +all: prepare_build $(TARGET) +bench: prepare_build $(BENCH_TARGET) + +prepare_build: + @echo [INFO] Using "${COMPILER}" as a compiler! + @echo [INFO] Detected current operating system as ${DETECTED_OS} $(CHECK_COMMANDS) - @echo "Sources: $(SOURCES)" - -# Create build directory structure -$(BUILD_DIR): - $(MKDIR) $(BUILD_DIR)$(PATHSEP)benchmarks - $(MKDIR) $(BUILD_DIR)$(PATHSEP)$(SRC_DIR)$(PATHSEP)cli - $(MKDIR) $(BUILD_DIR)$(PATHSEP)$(SRC_DIR)$(PATHSEP)compiler - $(MKDIR) $(BUILD_DIR)$(PATHSEP)$(SRC_DIR)$(PATHSEP)compiler$(PATHSEP)platforms - $(MKDIR) $(BUILD_DIR)$(PATHSEP)$(SRC_DIR)$(PATHSEP)lexer - $(MKDIR) $(BUILD_DIR)$(PATHSEP)$(SRC_DIR)$(PATHSEP)parser - $(MKDIR) $(BUILD_DIR)$(PATHSEP)$(SRC_DIR)$(PATHSEP)utils - -# Compile source files -$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR) - gcc $(CFLAGS) -c $< -o $@ - -# Link object files -$(TARGET): $(OBJECTS) - gcc $(CFLAGS) -c src/cli/main.c -o build/src/cli/main.o - gcc build/src/cli/main.o $(OBJECTS) -g -o $@ - -$(BENCH_TARGET): $(OBJECTS) - gcc $(CFLAGS) -c benchmarks/main.c -o build/benchmarks/main.o - gcc build/benchmarks/main.o $(OBJECTS) -g -o $@ - -# Clean build files -clean: - $(RM) $(BUILD_DIR) - $(RM) $(TARGET) - -# Phony targets -.PHONY: all clean check_commands + @echo [INFO] Starting building logic + +$(TARGET): + $(COMPILER) $(FLAGS) $(SOURCES) -o $(TARGET) + From fb5a7191919f1bda164834be074f864868f362c0 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 00:23:58 +0100 Subject: [PATCH 11/38] fix: some compiling errors --- src/cli/main.c | 2 +- src/compilerv2/compilerv2.c | 6 +++--- src/compilerv2/objects.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index ecce710..521f2c8 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -1,4 +1,4 @@ -** +/* * The file of the Quickfall CLI. * A modern, fast and lightweight programming language. * Usage: diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index e4a550a..ac11fa2 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -8,11 +8,11 @@ #include "./objects.h" #include "../parser/ast.h" -struct Context parseContext(ASTNode* node) { +struct Context parseContext(struct ASTNode* node) { struct Context ctx = {0}; ctx.variables = malloc(sizeof(struct Variable) * 50); - ctx.functions = malloc(sizeof(struct Functions) * 50); + ctx.functions = malloc(sizeof(struct Function) * 50); ctx.variableCount = 0; ctx.functionCount = 0; @@ -26,7 +26,7 @@ struct Context parseContext(ASTNode* node) { ctx.variables[ctx.variableCount].value = node->right->value; ctx.variableCount++; break; - case AST_FUNCION_DEF: + case AST_FUNCTION_DEF: ctx.functions[ctx.functionCount].name = node->left->left->value; while(node->left->right->next != NULL) { node->left->right = node->left->right->next; diff --git a/src/compilerv2/objects.h b/src/compilerv2/objects.h index e568391..9cd808d 100644 --- a/src/compilerv2/objects.h +++ b/src/compilerv2/objects.h @@ -11,7 +11,7 @@ struct Variable { char* name; char* value; char* type; - byte type; // 0 = variable, 1 = Func argument, 2 = Function body argument + unsigned char varType; // 0 = variable, 1 = Func argument, 2 = Function body argument }; struct Function { From 9ce6edcd20e940da0240614001891ce74f5c807b Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 00:34:36 +0100 Subject: [PATCH 12/38] feat: added variable -> variable assignement handling --- src/cli/main.c | 13 ++----------- src/compilerv2/compilerv2.c | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index 521f2c8..32c9ee5 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -13,6 +13,7 @@ #include "../lexer/lexer.h" #include "../parser/parser.h" #include "../compiler/compiler.h" +#include "../compilerv2/compilerv2.h" #include "../utils/logging.c" @@ -111,17 +112,7 @@ int main(int argc, char* argv[]) { struct LexerResult result = runLexer(buff); struct ASTNode* root = runParser(result); - char* output = compile(root, "win"); // todo: change the platform. - - if(output == NULL) { - printf("%sError: Compiling failed! Coudln't gather the output!%s\n", TEXT_RED, RESET); - return -1; - } - - fptr = fopen(outputFile, "w"); - fprintf(fptr, output); - fclose(fptr); - + struct Context ctx = parseContext(root); break; case 'v': diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index ac11fa2..8b3d52a 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -3,10 +3,13 @@ */ #include +#include +#include #include "./compilerv2.h" #include "./objects.h" #include "../parser/ast.h" +#include "../utils/logging.h" struct Context parseContext(struct ASTNode* node) { struct Context ctx = {0}; @@ -23,7 +26,30 @@ struct Context parseContext(struct ASTNode* node) { case AST_VARIABLE_DEF: ctx.variables[ctx.variableCount].name = node->left->value; ctx.variables[ctx.variableCount].type = "none"; - ctx.variables[ctx.variableCount].value = node->right->value; + + if(node->right->type == AST_VARIABLE_VALUE) { + ctx.variables[ctx.variableCount].value = node->right->value; + } + else if(node->right->type == AST_VARIABLE) { + if(node->right->value == ctx.variables[ctx.variableCount].name) { + printf("%sError: Cannot assign a variable's value to itself!\n", TEXT_HRED); + return NULL; + } + + //todo: change this to an hashmap before pr finished. + + for(int i = 0; i < ctx.variableCount; ++i) { + if(strcmp(ctx.variables[i].name, node->right->value) == 0) { + ctx.variables[ctx.variableCount].value = ctx.variables[i].value; + break; + } + } + } + else { + printf("%sError: Dissallowed token type as a variable value!\n", TEXT_HRED); + return NULL; + } + ctx.variableCount++; break; case AST_FUNCTION_DEF: From 27c55060faa9b1984b129dcd5b8edd050d191b77 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 00:35:47 +0100 Subject: [PATCH 13/38] fix: logging path --- src/compilerv2/compilerv2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index 8b3d52a..3da2dcc 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -9,7 +9,7 @@ #include "./compilerv2.h" #include "./objects.h" #include "../parser/ast.h" -#include "../utils/logging.h" +#include "../utils/logging.c" struct Context parseContext(struct ASTNode* node) { struct Context ctx = {0}; From 5e7262f29aefdde86c7d9e563e7bc1759005646e Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 00:40:01 +0100 Subject: [PATCH 14/38] Delete examples/bigBoom.qf --- examples/bigBoom.qf | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 examples/bigBoom.qf diff --git a/examples/bigBoom.qf b/examples/bigBoom.qf deleted file mode 100644 index f595467..0000000 --- a/examples/bigBoom.qf +++ /dev/null @@ -1,2 +0,0 @@ -salloc(40); -sfree(10000000); \ No newline at end of file From aa6941a2cc47d2b3b762562d52540d317ed57759 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 00:40:11 +0100 Subject: [PATCH 15/38] Delete examples/helloStdl.qf --- examples/helloStdl.qf | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 examples/helloStdl.qf diff --git a/examples/helloStdl.qf b/examples/helloStdl.qf deleted file mode 100644 index 71ead87..0000000 --- a/examples/helloStdl.qf +++ /dev/null @@ -1,3 +0,0 @@ -use "hello" - -sayHello("Hello Bob\0") \ No newline at end of file From 3bb61671e7ef7b155514f1e464eff8c99f41b8f3 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 00:51:38 +0100 Subject: [PATCH 16/38] feat: added the null token --- src/compilerv2/compilerv2.c | 4 ++-- src/lexer/lexer.c | 1 + src/lexer/tokens.c | 3 ++- src/lexer/tokens.h | 3 ++- src/parser/parser.c | 2 ++ 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index 3da2dcc..01bfac6 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -33,7 +33,7 @@ struct Context parseContext(struct ASTNode* node) { else if(node->right->type == AST_VARIABLE) { if(node->right->value == ctx.variables[ctx.variableCount].name) { printf("%sError: Cannot assign a variable's value to itself!\n", TEXT_HRED); - return NULL; + return ctx; } //todo: change this to an hashmap before pr finished. @@ -47,7 +47,7 @@ struct Context parseContext(struct ASTNode* node) { } else { printf("%sError: Dissallowed token type as a variable value!\n", TEXT_HRED); - return NULL; + return ctx; } ctx.variableCount++; diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 35830eb..1ae50d3 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -112,6 +112,7 @@ struct LexerResult runLexer(char string[]) { case ';': pushToken(&result, SEMICOLON); break; case ',': pushToken(&result, COMMA); break; case '=': pushToken(&result, DECLARE); break; + case '?': pushToken(&result, NONE); break; } } diff --git a/src/lexer/tokens.c b/src/lexer/tokens.c index 28d8973..f43cf63 100644 --- a/src/lexer/tokens.c +++ b/src/lexer/tokens.c @@ -26,5 +26,6 @@ enum TokenType { SEMICOLON = 15, COMMA = 16, DECLARE = 17, - USE = 18 + USE = 18, + NONE = 19 }; diff --git a/src/lexer/tokens.h b/src/lexer/tokens.h index de42fa4..4b24b6e 100644 --- a/src/lexer/tokens.h +++ b/src/lexer/tokens.h @@ -22,7 +22,8 @@ enum TokenType { SEMICOLON = 15, COMMA = 16, DECLARE = 17, - USE = 18 + USE = 18, + NONE = 19 }; struct KeywordResult { diff --git a/src/parser/parser.c b/src/parser/parser.c index e4a806c..b897a28 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -55,12 +55,14 @@ struct ASTNode* parseParameters(struct LexerResult result, int index) { if(!current->left) { current->left = createASTNode(AST_PARAM_TYPE); memcpy(current->left->value, result.tokens[index].value, strlen(result.tokens[index].value)); + printf("Mode 2 left -> %s\n", current->left->value); mode = 1; } else if(!current->right) { current->right = createASTNode(AST_PARAM_NAME); memcpy(current->right->value, result.tokens[index].value, strlen(result.tokens[index].value)); mode = 2; + printf("Mode 2 -> %s\n", current->right->value); } } From 805659a435bd63668151ed348ba071b5409cecba Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 02:12:08 +0100 Subject: [PATCH 17/38] refactor: refactored how the parameters parsing works to allow for none types --- src/parser/parser.c | 77 ++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 43 deletions(-) diff --git a/src/parser/parser.c b/src/parser/parser.c index b897a28..470cff9 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -23,51 +23,42 @@ struct ASTNode* parseParameters(struct LexerResult result, int index) { for(; index < result.size + 1; ++index) { struct Token t = result.tokens[index]; - - if(t.type == PAREN_CLOSE) { - root->end = index; - return root; - } - - if(t.type != COMMA && t.type != KEYWORD) { - printf("Error: Parameters must be literals! Got %d", t.type); - return NULL; - } - - if(t.type == COMMA) { - if(mode != 2) { - printf("Error: Parameters were not passed correctly!\n"); - return NULL; - } - - struct ASTNode* node = createASTNode(AST_PARAM); - current->next = node; - current = node; - } - - if(!root) { - root = createASTNode(AST_PARAM); - current = root; - } - - root->end = index; - - if(!current->left) { - current->left = createASTNode(AST_PARAM_TYPE); - memcpy(current->left->value, result.tokens[index].value, strlen(result.tokens[index].value)); - printf("Mode 2 left -> %s\n", current->left->value); - mode = 1; - } - else if(!current->right) { - current->right = createASTNode(AST_PARAM_NAME); - memcpy(current->right->value, result.tokens[index].value, strlen(result.tokens[index].value)); - mode = 2; - printf("Mode 2 -> %s\n", current->right->value); - } + + switch (t.type) { + case COMMA: + if (mode == 0) { + printf("Error: Arguments aren't passed properly!\n"); + return NULL; + } + mode = 0; + current->next = createASTNode(AST_PARAM); + current = current->next; + break; + case NONE: + case KEYWORD: + if(mode >= 2) { + printf("Error: Arguments aren't passed properly!\n"); + return NULL; + } + if(result.tokens[index + 1].type == NONE || result.tokens[index + 1].type == KEYWORD) { + current->right = createASTNode(AST_PARAM_TYPE); + memcpy(current->right->value, t.value, strlen(t.value)); + } + else { + current->left = createASTNode(AST_PARAM_NAME); + memcpy(current->left->value, t.value, strlen(t.value)); + } + mode++; + break; + case PAREN_CLOSE: + return root; + default: + printf("Didn't except token %d in arguments!\n", t.type); + } } - printf("Error: The paren wasn't closed!\n"); - return root; + printf("Error: The arguments paren wasn't closed!\n"); + return NULL; } /** From e89029b61cf08510e89a7545009a53ad4440cfd8 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 19:56:55 +0100 Subject: [PATCH 18/38] feat: fixed argument parsing --- src/parser/parser.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/parser/parser.c b/src/parser/parser.c index 470cff9..ca023d3 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -24,6 +24,8 @@ struct ASTNode* parseParameters(struct LexerResult result, int index) { for(; index < result.size + 1; ++index) { struct Token t = result.tokens[index]; + root->end = index; + switch (t.type) { case COMMA: if (mode == 0) { @@ -41,10 +43,12 @@ struct ASTNode* parseParameters(struct LexerResult result, int index) { return NULL; } if(result.tokens[index + 1].type == NONE || result.tokens[index + 1].type == KEYWORD) { + printf("Passed type %s\n", t.value); current->right = createASTNode(AST_PARAM_TYPE); memcpy(current->right->value, t.value, strlen(t.value)); } else { + printf("Passed name %s\n", t.value); current->left = createASTNode(AST_PARAM_NAME); memcpy(current->left->value, t.value, strlen(t.value)); } From 9930412d646bfe567cb2ad4af5b786a2dabea701 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 19:57:20 +0100 Subject: [PATCH 19/38] feat: removed for variable -> variable assignement checking for now --- src/compilerv2/compilerv2.c | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index 01bfac6..41cb4b7 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -25,28 +25,13 @@ struct Context parseContext(struct ASTNode* node) { switch (node->type) { case AST_VARIABLE_DEF: ctx.variables[ctx.variableCount].name = node->left->value; - ctx.variables[ctx.variableCount].type = "none"; + ctx.variables[ctx.variableCount].type = node->right->value; if(node->right->type == AST_VARIABLE_VALUE) { ctx.variables[ctx.variableCount].value = node->right->value; } - else if(node->right->type == AST_VARIABLE) { - if(node->right->value == ctx.variables[ctx.variableCount].name) { - printf("%sError: Cannot assign a variable's value to itself!\n", TEXT_HRED); - return ctx; - } - - //todo: change this to an hashmap before pr finished. - - for(int i = 0; i < ctx.variableCount; ++i) { - if(strcmp(ctx.variables[i].name, node->right->value) == 0) { - ctx.variables[ctx.variableCount].value = ctx.variables[i].value; - break; - } - } - } else { - printf("%sError: Dissallowed token type as a variable value!\n", TEXT_HRED); + printf("%sError: Invalid token type as a variable value!\n", TEXT_HRED); return ctx; } From 1e5ab8ac188cb4051c632a46ba6f0993a4322c57 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 19:57:39 +0100 Subject: [PATCH 20/38] chore: added old build deletion before building --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 5413a27..29bec26 100644 --- a/Makefile +++ b/Makefile @@ -71,6 +71,9 @@ prepare_build: @echo [INFO] Using "${COMPILER}" as a compiler! @echo [INFO] Detected current operating system as ${DETECTED_OS} $(CHECK_COMMANDS) + @echo [INFO] Clearing old builds + $(RM) build + $(RM) $(TARGET) @echo [INFO] Starting building logic $(TARGET): From 3542a3ad66c8cc32a69d173ba9374d6bbd560a37 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 20:04:58 +0100 Subject: [PATCH 21/38] feat: added none type token --- src/lexer/lexer.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 1ae50d3..31cdb93 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -112,7 +112,10 @@ struct LexerResult runLexer(char string[]) { case ';': pushToken(&result, SEMICOLON); break; case ',': pushToken(&result, COMMA); break; case '=': pushToken(&result, DECLARE); break; - case '?': pushToken(&result, NONE); break; + case '?': + pushToken(&result, NONE); + result.tokens[result.size - 1].value[0] = '?'; + break; } } From be6ecadc4041702b48691c3b88faa807bee9ce0a Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 20:05:18 +0100 Subject: [PATCH 22/38] feat: added primitive type detection for variable values --- src/parser/parser.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/parser/parser.c b/src/parser/parser.c index ca023d3..05fba94 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -5,6 +5,8 @@ #include "../lexer/tokens.h" #include "../lexer/lexer.h" #include "./ast.h" +#include "../utils/logging.c" + #include #include @@ -160,6 +162,20 @@ struct ASTNode* parseVariableDefinition(struct LexerResult result, int index) { struct Token val = result.tokens[index + 2]; + switch(val.type) { + case NUMBER: + node->value[0] = 'n'; + break; + case STRING: + node->value[0] = 's'; + break; + case BOOLEAN_VALUE: + node->value[0] = 'b'; + break; + default: + printf("%sWarning: unsupported variable value type! Compiling of this variable will be ignored!%s\n", TEXT_YELLOW, RESET); // This warning will be here until there is no unsupported types + } + if(val.type != KEYWORD && val.type != NUMBER && val.type != STRING && val.type != BOOLEAN_VALUE) { printf("Error: Disallowed token as variable value: %d\n", val.type); return NULL; From 88185cf574d19244d9c18657464f32492821490b Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 20:33:30 +0100 Subject: [PATCH 23/38] feat: added platforms enum --- src/compilerv2/compilerv2.c | 1 + src/compilerv2/compilerv2.h | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index 41cb4b7..89b97b7 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -57,3 +57,4 @@ struct Context parseContext(struct ASTNode* node) { } + diff --git a/src/compilerv2/compilerv2.h b/src/compilerv2/compilerv2.h index f771e24..8e6b583 100644 --- a/src/compilerv2/compilerv2.h +++ b/src/compilerv2/compilerv2.h @@ -8,6 +8,11 @@ #include "../parser/ast.h" #include "./objects.h" +enum Platform { + ATT_WINDOWS, + ATT_LINUX +}; + /** * A context is the less abstract way Quickfall represents the code before converting it to assembly. */ From 3e379bfaa94bd61a2a62a78d4a038540955e488f Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 20:42:10 +0100 Subject: [PATCH 24/38] att-win: added attwin assembly format --- src/compilerv2/att/att-win.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/compilerv2/att/att-win.h diff --git a/src/compilerv2/att/att-win.h b/src/compilerv2/att/att-win.h new file mode 100644 index 0000000..4059687 --- /dev/null +++ b/src/compilerv2/att/att-win.h @@ -0,0 +1,13 @@ +/** + * The Windows-AT & T assembly format. + */ + +/** + * The registries used for function arguments. + */ +char* ATTWIN_ARGUMENT_REGISTRIES[] = {"rcx", "rdx", "r8", "r9"}; + +/** + * The types of different data values that are registered in sections and not the stack. + */ +char* SECTION_TYPES[] = {".ascii"}; From 216310fcb8c3715e7f594003bb8cf8e669b77954 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 20:45:16 +0100 Subject: [PATCH 25/38] feat: added attlinux assembly format --- src/compilerv2/att/att-linux.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/compilerv2/att/att-linux.h diff --git a/src/compilerv2/att/att-linux.h b/src/compilerv2/att/att-linux.h new file mode 100644 index 0000000..fb6645e --- /dev/null +++ b/src/compilerv2/att/att-linux.h @@ -0,0 +1,13 @@ +/** + * The Linux-AT & T assembly format. + */ + +/** + * The registries used for function arguments. + */ +char* ATTLINUX_ARGUMENT_REGISTRIES[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"}; + +/** + * The types of different data values that are registered in sections and not the stack. + */ +char* ATTLINUX_SECTION_TYPES[] = {".string"}; From afc346e0681f251ba8dcf9dd01cd9da6e5e6d609 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 21:20:49 +0100 Subject: [PATCH 26/38] feat: added basic assembly compiler --- src/cli/main.c | 11 ++++ src/compilerv2/att/att-win.h | 2 +- src/compilerv2/compilerv2.c | 101 ++++++++++++++++++++++++++++++++++- src/compilerv2/compilerv2.h | 2 + 4 files changed, 114 insertions(+), 2 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index 32c9ee5..02c76a1 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -114,6 +114,17 @@ int main(int argc, char* argv[]) { struct Context ctx = parseContext(root); + char* output = compileV2(ctx); + + if(output == NULL) { + printf("Error: the compiled output is null! Something went wrong!\n"); + return -1; + } + + fptr = fopen(outputFile, "w"); + fprintf(fptr, output); + fclose(fptr); + break; case 'v': if(strlen(argv[1]) > 1 && strcmp(argv[1], "version") != 0) { diff --git a/src/compilerv2/att/att-win.h b/src/compilerv2/att/att-win.h index 4059687..d7f8a1f 100644 --- a/src/compilerv2/att/att-win.h +++ b/src/compilerv2/att/att-win.h @@ -10,4 +10,4 @@ char* ATTWIN_ARGUMENT_REGISTRIES[] = {"rcx", "rdx", "r8", "r9"}; /** * The types of different data values that are registered in sections and not the stack. */ -char* SECTION_TYPES[] = {".ascii"}; +char* ATTWIN_SECTION_TYPES[] = {".ascii"}; diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index 89b97b7..7e4df8a 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -11,6 +11,15 @@ #include "../parser/ast.h" #include "../utils/logging.c" +#include "./att/att-win.h" +#include "./att/att-linux.h" + +/** + * Assembly format defintions. Will be changed by the architure. + */ +char** ARGUMENT_REGISTRIES = NULL; +char** SECTION_TYPES = NULL; + struct Context parseContext(struct ASTNode* node) { struct Context ctx = {0}; @@ -25,7 +34,7 @@ struct Context parseContext(struct ASTNode* node) { switch (node->type) { case AST_VARIABLE_DEF: ctx.variables[ctx.variableCount].name = node->left->value; - ctx.variables[ctx.variableCount].type = node->right->value; + ctx.variables[ctx.variableCount].type = node->value; if(node->right->type == AST_VARIABLE_VALUE) { ctx.variables[ctx.variableCount].value = node->right->value; @@ -55,6 +64,96 @@ struct Context parseContext(struct ASTNode* node) { } } + return ctx; + } +/** + * Compiles the context down to assembly. + */ +char* compileV2(struct Context context) { + char* firstSection = malloc(1024); + char* sections = malloc(1024); + char* main = malloc(1024); + + firstSection[0] = '\0'; + sections[0] = '\0'; + main[0] = '\0'; + + int sectionIndex = 0; + int stackSize = 0; + + // Platform def + ARGUMENT_REGISTRIES = ATTWIN_ARGUMENT_REGISTRIES; + SECTION_TYPES = ATTWIN_SECTION_TYPES; + + strcat(firstSection, ".LC0:\n .globl main"); + + for(int i = 0; i < context.variableCount; ++i) { + if(context.variables[i].type[0] == 's') { + if(sectionIndex == 0) { + strcat(firstSection, "\n "); + strcat(firstSection, SECTION_TYPES[0]); + strcat(firstSection, " "); + strcat(firstSection, "\""); + strcat(firstSection, context.variables[i].value); + strcat(firstSection, "\""); + } + else { + strcat(sections, ".LC"); + + char secI[5]; + snprintf(secI, 5, "%d", sectionIndex); + + strcat(sections, secI); + strcat(sections, ":\n "); + strcat(sections, SECTION_TYPES[0]); + strcat(sections, " "); + strcat(sections, "\""); + strcat(sections, context.variables[i].value); + strcat(sections, "\""); + } + sectionIndex++; + } + else if(context.variables[i].type[0] = 'n') { + stackSize += 4; + + strcat(main, "\n movq $"); + strcat(main, context.variables[i].value); + strcat(main, ", -"); + + char sI[5]; + snprintf(sI, 5, "%d", stackSize); + + strcat(main, sI); + strcat(main, "(%rsp)"); + } + } + + char* buff = malloc(1024); + + buff[0] = '\0'; + + strcat(buff, firstSection); + strcat(buff, sections); + strcat(buff, "\n\nmain:"); + char size[5]; + snprintf(size, 5, "%d", stackSize); + + if(stackSize > 0) { + strcat(buff, "\n subq $"); + strcat(buff, size); + strcat(buff, ", %rsp"); + } + + strcat(buff, main); + + if(stackSize > 0) { + strcat(buff, "\n addq $"); + strcat(buff, size); + strcat(buff, ", %rsp"); + } + + return buff; +} diff --git a/src/compilerv2/compilerv2.h b/src/compilerv2/compilerv2.h index 8e6b583..6423b50 100644 --- a/src/compilerv2/compilerv2.h +++ b/src/compilerv2/compilerv2.h @@ -28,4 +28,6 @@ struct Context { */ struct Context parseContext(struct ASTNode* node); +char* compileV2(struct Context context); + #endif From c83c1cc1842f524e5f7744bd654403d352dba4f8 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 22:36:15 +0100 Subject: [PATCH 27/38] feat: added string hashing --- src/utils/hash.c | 20 ++++++++++++++++++++ src/utils/hash.h | 9 +++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/utils/hash.c create mode 100644 src/utils/hash.h diff --git a/src/utils/hash.c b/src/utils/hash.c new file mode 100644 index 0000000..a3f33be --- /dev/null +++ b/src/utils/hash.c @@ -0,0 +1,20 @@ +/** + * Hashing related utilities. + */ + +/** + * Hashes a string. + */ +unsigned int hashstr(char* str) { + unsigned int result = 5381; + unsigned char* p; + + p = (unsigned char *)str; + + while(*p != '\0') { + result = (result << 5) + result + *p; + ++p; + } + + return result; +} diff --git a/src/utils/hash.h b/src/utils/hash.h new file mode 100644 index 0000000..50e1340 --- /dev/null +++ b/src/utils/hash.h @@ -0,0 +1,9 @@ +#ifndef HASH_UTILS +#define HASH_UTILS + +/** + * Hashes the string. + */ +unsigned int hashstr(char* string); + +#endif From 49f5f97a8a2fd05019a8663d5fe17e2b83956d27 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sun, 1 Dec 2024 22:58:19 +0100 Subject: [PATCH 28/38] feat: made the string hashing algorithm generate less big numbers --- src/utils/hash.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/utils/hash.c b/src/utils/hash.c index a3f33be..6f5e5d9 100644 --- a/src/utils/hash.c +++ b/src/utils/hash.c @@ -2,17 +2,14 @@ * Hashing related utilities. */ -/** - * Hashes a string. - */ -unsigned int hashstr(char* str) { - unsigned int result = 5381; - unsigned char* p; +#include - p = (unsigned char *)str; +unsigned int hashstr(char* str) { + unsigned int result = 0; + unsigned char* p = (unsigned char*) str; while(*p != '\0') { - result = (result << 5) + result + *p; + result = ((*p - 97) << 5) + result + 1; ++p; } From f3d13193b07d01296182b77310953ca77ffe0edf Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 2 Dec 2024 22:23:00 +0100 Subject: [PATCH 29/38] feat: added hashmap --- src/utils/hashmap.c | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/utils/hashmap.c diff --git a/src/utils/hashmap.c b/src/utils/hashmap.c new file mode 100644 index 0000000..31926e9 --- /dev/null +++ b/src/utils/hashmap.c @@ -0,0 +1,54 @@ +/** + * A simple hashmap implementation. + */ + +#include + +struct Bucket { + void** values; + int bucketSize; +}; + +struct Hashmap { + int bucketSize; + int maxBucketIndex; + + struct Bucket** buckets; +}; + +struct Hashmap* createHashmap(int bucketSize, int maxBucketIndex) { + struct Hashmap* map = malloc(sizeof(struct Hashmap)); + map->bucketSize = bucketSize; + map->maxBucketIndex = maxBucketIndex; + + map->buckets = malloc(sizeof(struct Bucket*) * maxBucketIndex); + + for(int i = 0; i < maxBucketIndex; ++i) { + map->buckets[i] = NULL; + } + + return map; +} + +void hashPut(struct Hashmap* hashmap, int hash, void* value) { + int bucket = hash / hashmap->bucketSize; + + if(hashmap->buckets[bucket] == NULL) { + struct Bucket* b = malloc(sizeof(struct Bucket)); + b->bucketSize = hashmap->bucketSize; + b->values = malloc(sizeof(int*) * b->bucketSize); + + hashmap->buckets[bucket] = b; + } + + hashmap->buckets[bucket]->values[hash % hashmap->bucketSize] = value; +} + +void* hashGet(struct Hashmap* hashmap, int hash) { + int bucket = hash / hashmap->bucketSize; + + if(hashmap->buckets[bucket] == NULL) return NULL; + + return hashmap->buckets[bucket]->values[hash % hashmap->bucketSize]; +} + From dbff628c5020d794c4a775ca74d31a872b71160b Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 3 Dec 2024 23:40:32 +0100 Subject: [PATCH 30/38] feat: added hashmap header fiel --- src/utils/hashmap.c | 12 +----------- src/utils/hashmap.h | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 src/utils/hashmap.h diff --git a/src/utils/hashmap.c b/src/utils/hashmap.c index 31926e9..a6cfb1c 100644 --- a/src/utils/hashmap.c +++ b/src/utils/hashmap.c @@ -4,17 +4,7 @@ #include -struct Bucket { - void** values; - int bucketSize; -}; - -struct Hashmap { - int bucketSize; - int maxBucketIndex; - - struct Bucket** buckets; -}; +#include "./hashmap.h" struct Hashmap* createHashmap(int bucketSize, int maxBucketIndex) { struct Hashmap* map = malloc(sizeof(struct Hashmap)); diff --git a/src/utils/hashmap.h b/src/utils/hashmap.h new file mode 100644 index 0000000..e87e634 --- /dev/null +++ b/src/utils/hashmap.h @@ -0,0 +1,24 @@ +/** + * A simple hashmap implementation. + */ + +#ifndef HASHMAP_H +#define HASHMAP_H + +struct Bucket { + void** values; + int bucketSize; +}; + +struct Hashmap { + int bucketSize; + int maxBucketIndex; + + struct Bucket** buckets; +}; + +struct Hashmap* createHashmap(int bucketSize, int maxBucketIndex); +void hashPut(struct Hashmap* hashmap, int hash, void* value); +void* hashGet(struct Hashmap* hashmap, int hash); + +#endif From ec567bb4cb6b06a23e004a86ba90305958144a97 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 3 Dec 2024 23:42:55 +0100 Subject: [PATCH 31/38] fix: compiling errors due to incorrect hashmap usage' --- src/compilerv2/compilerv2.c | 20 +++++++++++++++++++- src/compilerv2/compilerv2.h | 5 +++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index 7e4df8a..16617b3 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -14,18 +14,32 @@ #include "./att/att-win.h" #include "./att/att-linux.h" +#include "../utils/hashmap.h" +#include "../utils/hash.h" + /** * Assembly format defintions. Will be changed by the architure. */ char** ARGUMENT_REGISTRIES = NULL; char** SECTION_TYPES = NULL; +/** + * The maximum hash the hashmaps can store. + */ +#define MAX_HASH_CAPACITY 256000 + struct Context parseContext(struct ASTNode* node) { struct Context ctx = {0}; - + + int* ptr = NULL; + int ptrSize = sizeof(ptr); //Allows to know the pointer size as it varies between architectures + ctx.variables = malloc(sizeof(struct Variable) * 50); ctx.functions = malloc(sizeof(struct Function) * 50); + ctx.variableHashMap = createHashmap(512, 500); + ctx.functionHashMap = createHashmap(512, 500); + ctx.variableCount = 0; ctx.functionCount = 0; @@ -44,6 +58,10 @@ struct Context parseContext(struct ASTNode* node) { return ctx; } + struct Variable* ptr = NULL; + + hashPut(ctx.variableHashMap, hashstr(node->left->value), ptr); + ctx.variableCount++; break; case AST_FUNCTION_DEF: diff --git a/src/compilerv2/compilerv2.h b/src/compilerv2/compilerv2.h index 6423b50..d082331 100644 --- a/src/compilerv2/compilerv2.h +++ b/src/compilerv2/compilerv2.h @@ -5,6 +5,7 @@ #ifndef COMPILER_2_H #define COMPILER_2_H +#include "../utils/hashmap.h" #include "../parser/ast.h" #include "./objects.h" @@ -21,6 +22,10 @@ struct Context { struct Function* functions; int variableCount; int functionCount; + + // Hashmaps + struct Hashmap* variableHashMap; + struct Hashmap* functionHashMap; }; /** From 89debda5422602147803df6f7eb870e98012ddb8 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 3 Dec 2024 23:46:51 +0100 Subject: [PATCH 32/38] feat: changed variables and functions stored in Context to pointers for a better hashmap usage --- src/compilerv2/compilerv2.c | 38 +++++++++++++++++++++---------------- src/compilerv2/compilerv2.h | 4 ++-- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index 16617b3..f330307 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -34,8 +34,8 @@ struct Context parseContext(struct ASTNode* node) { int* ptr = NULL; int ptrSize = sizeof(ptr); //Allows to know the pointer size as it varies between architectures - ctx.variables = malloc(sizeof(struct Variable) * 50); - ctx.functions = malloc(sizeof(struct Function) * 50); + ctx.variables = malloc(sizeof(struct Variable*) * 50); + ctx.functions = malloc(sizeof(struct Function*) * 50); ctx.variableHashMap = createHashmap(512, 500); ctx.functionHashMap = createHashmap(512, 500); @@ -47,36 +47,42 @@ struct Context parseContext(struct ASTNode* node) { node = node->next; switch (node->type) { case AST_VARIABLE_DEF: - ctx.variables[ctx.variableCount].name = node->left->value; - ctx.variables[ctx.variableCount].type = node->value; - + struct Variable* var = malloc(sizeof(struct Variable)); + + var->name = node->left->value; + var->type = node->value; + if(node->right->type == AST_VARIABLE_VALUE) { - ctx.variables[ctx.variableCount].value = node->right->value; + var->value = node->right->value; } else { - printf("%sError: Invalid token type as a variable value!\n", TEXT_HRED); - return ctx; + printf("%sError: Invalid token type as variable value!%s\n", TEXT_HRED, RESET); } - struct Variable* ptr = NULL; + ctx.variables[ctx.variableCount] = var; - hashPut(ctx.variableHashMap, hashstr(node->left->value), ptr); + hashPut(ctx.variableHashMap, hashstr(node->left->value), var); ctx.variableCount++; break; case AST_FUNCTION_DEF: - ctx.functions[ctx.functionCount].name = node->left->left->value; + struct Function* func = malloc(sizeof(struct Function)); + + func->name = node->left->left->value; while(node->left->right->next != NULL) { node->left->right = node->left->right->next; - int c = ctx.functions[ctx.functionCount].variableCount; + int c = func->variableCount; - ctx.functions[ctx.functionCount].variables[c].name = node->left->right->right->value; - ctx.functions[ctx.functionCount].variables[c].type = node->left->right->left->value; - ctx.functions[ctx.functionCount].variableCount++; + func->variables[c].name = node->left->right->right->value; + func->variables[c].type = node->left->right->left->value; + func->variableCount++; } - ctx.functions[ctx.functionCount].body = node->right; + func->body = node->right; + + ctx.functions[ctx.functionCount] = func; + ctx.functionCount++; break; } diff --git a/src/compilerv2/compilerv2.h b/src/compilerv2/compilerv2.h index d082331..7d26c01 100644 --- a/src/compilerv2/compilerv2.h +++ b/src/compilerv2/compilerv2.h @@ -18,8 +18,8 @@ enum Platform { * A context is the less abstract way Quickfall represents the code before converting it to assembly. */ struct Context { - struct Variable* variables; - struct Function* functions; + struct Variable** variables; + struct Function** functions; int variableCount; int functionCount; From 073321688c6a629d7a9d1eba150c62adcef4f550 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 3 Dec 2024 23:47:52 +0100 Subject: [PATCH 33/38] feat: added functions in hashmap --- src/compilerv2/compilerv2.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index f330307..ff415ee 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -83,6 +83,8 @@ struct Context parseContext(struct ASTNode* node) { ctx.functions[ctx.functionCount] = func; + hashPut(ctx.functionHashmap, hashstr(func->name), func); + ctx.functionCount++; break; } From c870c8af53c0cbb313a412ee479e89b830d6e30c Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 3 Dec 2024 23:51:31 +0100 Subject: [PATCH 34/38] fix: struct -> struct* conversion --- src/compilerv2/compilerv2.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index ff415ee..c3dd87f 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -69,6 +69,7 @@ struct Context parseContext(struct ASTNode* node) { struct Function* func = malloc(sizeof(struct Function)); func->name = node->left->left->value; + while(node->left->right->next != NULL) { node->left->right = node->left->right->next; @@ -83,7 +84,7 @@ struct Context parseContext(struct ASTNode* node) { ctx.functions[ctx.functionCount] = func; - hashPut(ctx.functionHashmap, hashstr(func->name), func); + hashPut(ctx.functionHashMap, hashstr(func->name), func); ctx.functionCount++; break; @@ -116,13 +117,13 @@ char* compileV2(struct Context context) { strcat(firstSection, ".LC0:\n .globl main"); for(int i = 0; i < context.variableCount; ++i) { - if(context.variables[i].type[0] == 's') { + if(context.variables[i]->type[0] == 's') { if(sectionIndex == 0) { strcat(firstSection, "\n "); strcat(firstSection, SECTION_TYPES[0]); strcat(firstSection, " "); strcat(firstSection, "\""); - strcat(firstSection, context.variables[i].value); + strcat(firstSection, context.variables[i]->value); strcat(firstSection, "\""); } else { @@ -136,16 +137,16 @@ char* compileV2(struct Context context) { strcat(sections, SECTION_TYPES[0]); strcat(sections, " "); strcat(sections, "\""); - strcat(sections, context.variables[i].value); + strcat(sections, context.variables[i]->value); strcat(sections, "\""); } sectionIndex++; } - else if(context.variables[i].type[0] = 'n') { + else if(context.variables[i]->type[0] = 'n') { stackSize += 4; strcat(main, "\n movq $"); - strcat(main, context.variables[i].value); + strcat(main, context.variables[i]->value); strcat(main, ", -"); char sI[5]; From 85e78d21db88572dcdcfecf9aa10c4a414d69f4b Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 3 Dec 2024 23:53:23 +0100 Subject: [PATCH 35/38] feat: added already defined variable check --- src/compilerv2/compilerv2.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index c3dd87f..d96c8fe 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -59,9 +59,16 @@ struct Context parseContext(struct ASTNode* node) { printf("%sError: Invalid token type as variable value!%s\n", TEXT_HRED, RESET); } + int hash = hashstr(node->left->value); + + if(hashGet(ctx.variableHashMap, hash) != NULL) { + printf("%sError: Variable %s is already defined!%s\n", TEXT_HRED, var->name, RESET); + return ctx; + } + ctx.variables[ctx.variableCount] = var; - hashPut(ctx.variableHashMap, hashstr(node->left->value), var); + hashPut(ctx.variableHashMap, hash, var); ctx.variableCount++; break; From db5b12c075bb83f223171ff0cc99cd9c9dea595a Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 3 Dec 2024 23:54:42 +0100 Subject: [PATCH 36/38] feat: added already defined function check --- src/compilerv2/compilerv2.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index d96c8fe..5415ef5 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -89,9 +89,16 @@ struct Context parseContext(struct ASTNode* node) { func->body = node->right; + int hash = hashstr(func->name); + + if(hashGet(ctx.functionHashMap, hash) != NULL) { + printf("%sError: Function %s is already defined!%s\n", TEXT_HRED, func->name, RESET); + return ctx; + } + ctx.functions[ctx.functionCount] = func; - hashPut(ctx.functionHashMap, hashstr(func->name), func); + hashPut(ctx.functionHashMap, hash, func); ctx.functionCount++; break; From 165cee233c7ef8d79273afde4a12d9e53db6554b Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 3 Dec 2024 23:55:11 +0100 Subject: [PATCH 37/38] fix: already defined hash variable --- src/compilerv2/compilerv2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index 5415ef5..dc132f9 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -89,7 +89,7 @@ struct Context parseContext(struct ASTNode* node) { func->body = node->right; - int hash = hashstr(func->name); + hash = hashstr(func->name); if(hashGet(ctx.functionHashMap, hash) != NULL) { printf("%sError: Function %s is already defined!%s\n", TEXT_HRED, func->name, RESET); From a0b76465ddb5c51df0940467fb2ff65620831fe2 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 3 Dec 2024 23:57:15 +0100 Subject: [PATCH 38/38] feat: removed useless pointer size checker --- src/compilerv2/compilerv2.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/compilerv2/compilerv2.c b/src/compilerv2/compilerv2.c index dc132f9..322f5b2 100644 --- a/src/compilerv2/compilerv2.c +++ b/src/compilerv2/compilerv2.c @@ -31,9 +31,6 @@ char** SECTION_TYPES = NULL; struct Context parseContext(struct ASTNode* node) { struct Context ctx = {0}; - int* ptr = NULL; - int ptrSize = sizeof(ptr); //Allows to know the pointer size as it varies between architectures - ctx.variables = malloc(sizeof(struct Variable*) * 50); ctx.functions = malloc(sizeof(struct Function*) * 50);