From c3a0ab7693e62d4865891922ba4d3ed6a26086b8 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 22:47:59 +0100 Subject: [PATCH 01/40] feat: started working on v2 of parser --- src/parserv2/ast.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/parserv2/ast.h diff --git a/src/parserv2/ast.h b/src/parserv2/ast.h new file mode 100644 index 0000000..2a0743f --- /dev/null +++ b/src/parserv2/ast.h @@ -0,0 +1,29 @@ +/** + * The header file of AST nodes in Quickfall. + */ + +#ifndef AST_2_H +#define AST_2_H + +/** + * The type of AST Node(s). + */ +enum ASTNodeType { + +} + +/** + * An AST Node. Has a tree-ish structure. + */ +typedef struct ASTNode { + + AST_NODE* left; + AST_NODE* right; + AST_NODE* next; + + enum ASTNodeType type; + char* value; + +} AST_NODE; + +#endif From 1c80d3bf735756ab93df9288e8d9990a237ac5da Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 22:50:01 +0100 Subject: [PATCH 02/40] feat: added createASTNode func in header --- src/parserv2/ast.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/parserv2/ast.h b/src/parserv2/ast.h index 2a0743f..4c880a9 100644 --- a/src/parserv2/ast.h +++ b/src/parserv2/ast.h @@ -26,4 +26,10 @@ typedef struct ASTNode { } AST_NODE; +/** + * Creates a new AST Node. + * @param type the AST type of the node. + */ +AST_NODE* createASTNode(enum ASTNodeType type); + #endif From 878fbb64d5645bcb62d466f99dd4725cdf498ff2 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 22:51:39 +0100 Subject: [PATCH 03/40] feat: createASTNode implementation --- src/parserv2/ast.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/parserv2/ast.c diff --git a/src/parserv2/ast.c b/src/parserv2/ast.c new file mode 100644 index 0000000..8398ae2 --- /dev/null +++ b/src/parserv2/ast.c @@ -0,0 +1,18 @@ +/** + * 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(enum ASTNodeType type) { + AST_NODE* node = malloc(sizeof(AST_NODE)); + node->type = type; + + return node; +} From a4d8982d3ca0b8e07496d113f0ec647c92af4cc4 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 22:55:48 +0100 Subject: [PATCH 04/40] feat: added parser header file --- src/parserv2/parser.c | 5 +++++ src/parserv2/parser.h | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/parserv2/parser.c create mode 100644 src/parserv2/parser.h diff --git a/src/parserv2/parser.c b/src/parserv2/parser.c new file mode 100644 index 0000000..a1ebe39 --- /dev/null +++ b/src/parserv2/parser.c @@ -0,0 +1,5 @@ +/** + * The parser of Quickfall. + */ + + diff --git a/src/parserv2/parser.h b/src/parserv2/parser.h new file mode 100644 index 0000000..1a895c9 --- /dev/null +++ b/src/parserv2/parser.h @@ -0,0 +1,18 @@ +/** + * 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. + */ +AST_NODE* parseNodes(struct LexerResult result, int index); + +#endif From 88712cb15ce2cb6cd41adf932cab60f8836453af Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 22:59:44 +0100 Subject: [PATCH 05/40] feat: started adding asts --- src/parserv2/asts/functions.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/parserv2/asts/functions.h diff --git a/src/parserv2/asts/functions.h b/src/parserv2/asts/functions.h new file mode 100644 index 0000000..297a913 --- /dev/null +++ b/src/parserv2/asts/functions.h @@ -0,0 +1,25 @@ +/** + * Function-related AST parsing. + */ + +#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(struct LexerResult 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(struct LexerResult result, int index); + + +#endif From a2b1f83ef2bf72b38c183fd11e4ddcbd8d82367d Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 23:12:23 +0100 Subject: [PATCH 06/40] feat: added parameter parsing --- src/parserv2/ast.h | 4 ++- src/parserv2/asts/functions.c | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/parserv2/asts/functions.c diff --git a/src/parserv2/ast.h b/src/parserv2/ast.h index 4c880a9..d56f897 100644 --- a/src/parserv2/ast.h +++ b/src/parserv2/ast.h @@ -9,7 +9,9 @@ * The type of AST Node(s). */ enum ASTNodeType { - + AST_TYPE, // Represents a datatype precision in an AST Node. + AST_VARIABLE_NAME, // Represents a variable / parameter name precision in an AST Node. + AST_PARAMETER // A function parameter AST Node, used in function declaration. } /** diff --git a/src/parserv2/asts/functions.c b/src/parserv2/asts/functions.c new file mode 100644 index 0000000..5921051 --- /dev/null +++ b/src/parserv2/asts/functions.c @@ -0,0 +1,60 @@ +/** + * Function-related AST parsing. + */ + +#include "../ast.h" +#include "../../utils/logging.h" + +/** + * 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(struct LexerResult result, int index) { + + AST_NODE* root = createASTNode(AST_PARAMETER); + AST_NODE* current = root; + + int stack = 0; + + for(; index < result.size + 1; ++index) { + struct 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; + } + + struct 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: + if(stack != 0) return root; + return NULL; + default: + return NULL; + + } + } +} From 46f1e1694ec328af522005043862b1a57be32168 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 23:14:51 +0100 Subject: [PATCH 07/40] feat: added variable asts header --- src/parserv2/asts/variables.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/parserv2/asts/variables.h diff --git a/src/parserv2/asts/variables.h b/src/parserv2/asts/variables.h new file mode 100644 index 0000000..58d15c5 --- /dev/null +++ b/src/parserv2/asts/variables.h @@ -0,0 +1,17 @@ +/** + * Variable-related AST parsing. + */ + +#include "../ast.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(struct LexerResult result, int index); + +#endif From f1542ddee640c8e52fe75ac53aa3589791e61583 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 23:22:36 +0100 Subject: [PATCH 08/40] feat: added variable ast parsing --- src/parserv2/ast.h | 2 ++ src/parserv2/asts/variables.c | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/parserv2/asts/variables.c diff --git a/src/parserv2/ast.h b/src/parserv2/ast.h index d56f897..e62e03d 100644 --- a/src/parserv2/ast.h +++ b/src/parserv2/ast.h @@ -11,6 +11,8 @@ enum ASTNodeType { 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_PARAMETER // A function parameter AST Node, used in function declaration. } diff --git a/src/parserv2/asts/variables.c b/src/parserv2/asts/variables.c new file mode 100644 index 0000000..85fb5ad --- /dev/null +++ b/src/parserv2/asts/variables.c @@ -0,0 +1,39 @@ +/** + * Variable-related AST parsing. + */ + +#include "../../lexer/lexer.h" +#include "../../lexer/tokens.h" + +#include "../ast.h" + +AST_NODE* parseVariableValue(struct LexerResult result, int index) { + struct Token t = result.tokens[index]; + + if(t.type == NUMBER || t.type == STRING || t.type == BOOLEAN_VALUE) { + AST_NODE* node = createASTNode(AST_VARIABLE_VALUE); + node->left = createASTNode(AST_TYPE); + + switch(t.type) { + case NUMBER: + node->left->value = "n"; + break; + 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->value = t.value; + + return node; + } +} From 3bd2e7da9dfa50dfe3e5108569244b52bd53887c Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 23:23:38 +0100 Subject: [PATCH 09/40] feat: added ending index to header --- src/parserv2/ast.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/parserv2/ast.h b/src/parserv2/ast.h index e62e03d..75c0be8 100644 --- a/src/parserv2/ast.h +++ b/src/parserv2/ast.h @@ -13,6 +13,8 @@ enum ASTNodeType { AST_VARIABLE_NAME, // Represents a variable / parameter name precision in an AST Node. AST_VARIABLE_VALUE, + AST_VARIABLE_REFERENCE, + AST_PARAMETER // A function parameter AST Node, used in function declaration. } @@ -27,6 +29,7 @@ typedef struct ASTNode { enum ASTNodeType type; char* value; + int endingIndex; // The index which the parsing ended } AST_NODE; From fcdf968bef6ef922357311509cee72e0e60889a9 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 23:24:46 +0100 Subject: [PATCH 10/40] feat: added ending index to the value parsing --- src/parserv2/asts/variables.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/parserv2/asts/variables.c b/src/parserv2/asts/variables.c index 85fb5ad..05e8eb7 100644 --- a/src/parserv2/asts/variables.c +++ b/src/parserv2/asts/variables.c @@ -12,6 +12,7 @@ AST_NODE* parseVariableValue(struct LexerResult result, int index) { if(t.type == NUMBER || t.type == STRING || t.type == BOOLEAN_VALUE) { AST_NODE* node = createASTNode(AST_VARIABLE_VALUE); + node->endingIndex = index + 1; node->left = createASTNode(AST_TYPE); switch(t.type) { @@ -32,6 +33,7 @@ AST_NODE* parseVariableValue(struct LexerResult result, int index) { if(t.type == KEYWORD) { AST_NODE* node = createASTNode(AST_VARIABLE_REFERENCE); + node->endingIndex = index + 1; node->value = t.value; return node; From 7ff2582fa650d2106befbb227d638abbcf26df2a Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 23:26:10 +0100 Subject: [PATCH 11/40] feat: added ending index to the function parameter parsing --- src/parserv2/asts/functions.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/parserv2/asts/functions.c b/src/parserv2/asts/functions.c index 5921051..ee6406d 100644 --- a/src/parserv2/asts/functions.c +++ b/src/parserv2/asts/functions.c @@ -50,7 +50,10 @@ AST_NODE* parseParameters(struct LexerResult result, int index) { stack++; break; case PAREN_CLOSE: - if(stack != 0) return root; + if(stack != 0) { + root->endingIndex = index; + return root; + } return NULL; default: return NULL; From 48c995c62834694e8172fbf6391dbd40c99462e4 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 23:31:10 +0100 Subject: [PATCH 12/40] feat: added function arguments parsing --- src/parserv2/asts/functions.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/parserv2/asts/functions.c b/src/parserv2/asts/functions.c index ee6406d..d880bb9 100644 --- a/src/parserv2/asts/functions.c +++ b/src/parserv2/asts/functions.c @@ -2,6 +2,8 @@ * Function-related AST parsing. */ +#include "./variables.h" + #include "../ast.h" #include "../../utils/logging.h" @@ -61,3 +63,32 @@ AST_NODE* parseParameters(struct LexerResult result, int index) { } } } + +AST_NODE* parseArguments(struct LexerResult result, int index) { + AST_NODE* root = NULL; + AST_NODE* current = root; + + for(; index < result.size + 1; ++index) { + struct 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; +} From 4640361af021d1211996db3e5c53ef6b23b15823 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 23:33:03 +0100 Subject: [PATCH 13/40] docs: added parseArguments impl docs --- src/parserv2/asts/functions.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/parserv2/asts/functions.c b/src/parserv2/asts/functions.c index d880bb9..805d299 100644 --- a/src/parserv2/asts/functions.c +++ b/src/parserv2/asts/functions.c @@ -64,6 +64,11 @@ AST_NODE* parseParameters(struct LexerResult result, int index) { } } +/** + * 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(struct LexerResult result, int index) { AST_NODE* root = NULL; AST_NODE* current = root; From 054c8066e7b6528ce0f30cb73124ead22bf4e5c1 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 5 Dec 2024 23:56:36 +0100 Subject: [PATCH 14/40] feat: added function declaration parsing --- src/parserv2/ast.h | 17 +++++++++----- src/parserv2/asts/functions.c | 42 ++++++++++++++++++++++++++++++++++- src/parserv2/asts/functions.h | 6 +++++ src/parserv2/asts/variables.h | 2 ++ 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/parserv2/ast.h b/src/parserv2/ast.h index 75c0be8..fd68103 100644 --- a/src/parserv2/ast.h +++ b/src/parserv2/ast.h @@ -15,23 +15,28 @@ enum ASTNodeType { AST_VARIABLE_REFERENCE, + AST_FUNCTION_DECLARATION, + AST_FUNCTION_HEADER, + AST_PARAMETER // A function parameter AST Node, used in function declaration. -} +}; /** * An AST Node. Has a tree-ish structure. */ -typedef struct ASTNode { +struct ASTNode { - AST_NODE* left; - AST_NODE* right; - AST_NODE* next; + struct ASTNode* left; + struct ASTNode* right; + struct ASTNode* next; enum ASTNodeType type; char* value; int endingIndex; // The index which the parsing ended -} AST_NODE; +}; + +typedef struct ASTNode AST_NODE; /** * Creates a new AST Node. diff --git a/src/parserv2/asts/functions.c b/src/parserv2/asts/functions.c index 805d299..eb54c94 100644 --- a/src/parserv2/asts/functions.c +++ b/src/parserv2/asts/functions.c @@ -4,8 +4,10 @@ #include "./variables.h" +#include "../parser.h" #include "../ast.h" -#include "../../utils/logging.h" + +#include "../../lexer/lexer.h" /** * Parse the parameters from a function definition (for example). @@ -97,3 +99,41 @@ AST_NODE* parseArguments(struct LexerResult result, int index) { return NULL; } + +AST_NODE* parseFunctionDeclaration(struct LexerResult 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); + + return node; +} diff --git a/src/parserv2/asts/functions.h b/src/parserv2/asts/functions.h index 297a913..0564c80 100644 --- a/src/parserv2/asts/functions.h +++ b/src/parserv2/asts/functions.h @@ -21,5 +21,11 @@ AST_NODE* parseParameters(struct LexerResult result, int index); */ AST_NODE* parseArguments(struct LexerResult result, int index); +/** + * Parses a function declaration. + * @param result the lexer result. + * @param index the starting index of the parsing. + */ +AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index); #endif diff --git a/src/parserv2/asts/variables.h b/src/parserv2/asts/variables.h index 58d15c5..184d9d7 100644 --- a/src/parserv2/asts/variables.h +++ b/src/parserv2/asts/variables.h @@ -4,6 +4,8 @@ #include "../ast.h" +#include "../../lexer/lexer.h" + #ifndef AST_VARIABLES_H #define AST_VARIABLES_H From 3a516b9991b5e21824abb040a5c47c893ffefacb Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Radi0o@users.noreply.github.com> Date: Sat, 7 Dec 2024 17:56:04 +0100 Subject: [PATCH 15/40] feat: added ast node type in parseNodes() --- src/parserv2/parser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parserv2/parser.h b/src/parserv2/parser.h index 1a895c9..df90203 100644 --- a/src/parserv2/parser.h +++ b/src/parserv2/parser.h @@ -13,6 +13,6 @@ * @param result the LexerResult provided by the lexer. * @param index the starting index. */ -AST_NODE* parseNodes(struct LexerResult result, int index); +AST_NODE* parseNodes(struct LexerResult result, int index, enum ASTNodeType); #endif From cdbc5fc2115add15a51994887f214a4658354fed Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Radi0o@users.noreply.github.com> Date: Sat, 7 Dec 2024 17:58:05 +0100 Subject: [PATCH 16/40] feat: added parseNodes impl --- src/parserv2/parser.c | 32 ++++++++++++++++++++++++++++++++ src/parserv2/parser.h | 3 ++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/parserv2/parser.c b/src/parserv2/parser.c index a1ebe39..5c9a04b 100644 --- a/src/parserv2/parser.c +++ b/src/parserv2/parser.c @@ -2,4 +2,36 @@ * The parser of Quickfall. */ +#include "../lexer/lexer.h" +#include "./ast.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(struct LexerResult result, int index, enum ASTNodeType type) { + AST_NODE* root = createASTNode(type); + AST_NODE* current = root; + + for(; index <= result.size; ++i) { + struct Token t = result.tokens[index]; + + switch(t.type) { + case FUNC: + AST_NODE* node = parseFunctionDeclaration(result, index); + if(node != null) { + current->next = node; + current = node; + index = node->endingIndex; + } + + break; + + } + } + + return root; +} diff --git a/src/parserv2/parser.h b/src/parserv2/parser.h index df90203..6a3cc5e 100644 --- a/src/parserv2/parser.h +++ b/src/parserv2/parser.h @@ -12,7 +12,8 @@ * 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(struct LexerResult result, int index, enum ASTNodeType); +AST_NODE* parseNodes(struct LexerResult result, int index, enum ASTNodeType type); #endif From 659ed851e97924b3208a48ffa48c65b09ffe0d17 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Radi0o@users.noreply.github.com> Date: Sat, 7 Dec 2024 17:59:18 +0100 Subject: [PATCH 17/40] feat: added AST_ROOT ast type --- src/parserv2/ast.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/parserv2/ast.h b/src/parserv2/ast.h index fd68103..4d8eb1e 100644 --- a/src/parserv2/ast.h +++ b/src/parserv2/ast.h @@ -9,6 +9,9 @@ * The type of AST Node(s). */ enum ASTNodeType { + + 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, From 5f0cf4b5ab908b8412c65c5b25d972fa58d25cc2 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Radi0o@users.noreply.github.com> Date: Sat, 7 Dec 2024 18:00:47 +0100 Subject: [PATCH 18/40] feat: fixed a few stuff --- src/parserv2/asts/functions.c | 2 +- src/parserv2/parser.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/parserv2/asts/functions.c b/src/parserv2/asts/functions.c index eb54c94..ea60e1d 100644 --- a/src/parserv2/asts/functions.c +++ b/src/parserv2/asts/functions.c @@ -133,7 +133,7 @@ AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index) { node->left->left = params; - node->right = parseNodes(result, params->endingIndex); + node->right = parseNodes(result, params->endingIndex, AST_ROOT); return node; } diff --git a/src/parserv2/parser.c b/src/parserv2/parser.c index 5c9a04b..9cd2599 100644 --- a/src/parserv2/parser.c +++ b/src/parserv2/parser.c @@ -16,13 +16,13 @@ AST_NODE* parseNodes(struct LexerResult result, int index, enum ASTNodeType type AST_NODE* root = createASTNode(type); AST_NODE* current = root; - for(; index <= result.size; ++i) { + for(; index <= result.size; ++index) { struct Token t = result.tokens[index]; switch(t.type) { - case FUNC: + case FUNCTION: AST_NODE* node = parseFunctionDeclaration(result, index); - if(node != null) { + if(node != NULL) { current->next = node; current = node; index = node->endingIndex; From cd04e752d05af4d547cd9d4ca8e839a962632f38 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Radi0o@users.noreply.github.com> Date: Sat, 7 Dec 2024 18:02:57 +0100 Subject: [PATCH 19/40] feat: dropped old parser version as it resulted to compiling errors --- src/parser/ast.c | 19 ---- src/parser/ast.h | 48 -------- src/parser/parser.c | 265 -------------------------------------------- src/parser/parser.h | 15 --- 4 files changed, 347 deletions(-) delete mode 100644 src/parser/ast.c delete mode 100644 src/parser/ast.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 3fdf9f6..0000000 --- a/src/parser/ast.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -#include "./ast.h" - -/** - * Creates an AST node. - */ -struct ASTNode* createASTNode(enum ASTNodeType type) { - struct ASTNode* node = malloc(sizeof(struct ASTNode)); - node->left = NULL; - node->right = NULL; - node->next = NULL; - node->type = type; - node->end = 0; - - memset(node->value, 0, sizeof(node->value)); - - return node; -} diff --git a/src/parser/ast.h b/src/parser/ast.h deleted file mode 100644 index 00e02b6..0000000 --- a/src/parser/ast.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef AST_H -#define AST_H - -/** - * The type of AST nodes - */ -enum ASTNodeType { - AST_VARIABLE_DEF, - AST_FUNCTION_DEF, - AST_FUNCTION_NAME, - AST_FUNCTION_TEMPLATE, - AST_FUNCTION_BODY, - AST_FUNCTION_CALL, - AST_VARIABLE, - AST_PARAMETERS, - AST_PARAM, - AST_PARAM_TYPE, - AST_PARAM_NAME, - AST_GENERIC, - AST_INVOKE_TARGET, - AST_INVOKE_PARAMETERS, - AST_VARIABLE_TYPE, - AST_VARIABLE_META, - AST_VARIABLE_NAME, - AST_VARIABLE_VALUE, - AST_USE_STDL, - AST_STDL_TARGET, - AST_FUNCTION_GENERIC -}; - -/** - * An AST node. - */ -struct ASTNode { - enum ASTNodeType type; - char value[32]; - struct ASTNode* left; - struct ASTNode* right; - struct ASTNode* next; - int end; -}; - -/** - * Creates an AST node. - */ -struct ASTNode* createASTNode(enum ASTNodeType type); - -#endif \ No newline at end of file diff --git a/src/parser/parser.c b/src/parser/parser.c deleted file mode 100644 index 05fba94..0000000 --- a/src/parser/parser.c +++ /dev/null @@ -1,265 +0,0 @@ -/** - * The parser of Quickfall. - */ - -#include "../lexer/tokens.h" -#include "../lexer/lexer.h" -#include "./ast.h" -#include "../utils/logging.c" - -#include -#include - -struct ASTNode* parseParameters(struct LexerResult result, int index); -struct ASTNode* parseFunctionDeclaration(struct LexerResult result, int index); -struct ASTNode* parseExpression(struct LexerResult result, int index, int end, enum ASTNodeType type); - - -/** - * Parses parameters of a function. - */ -struct ASTNode* parseParameters(struct LexerResult result, int index) { - struct ASTNode* root = createASTNode(AST_PARAM); - struct ASTNode* current = root; - int mode = 0; - - for(; index < result.size + 1; ++index) { - struct Token t = result.tokens[index]; - - root->end = index; - - 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) { - 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)); - } - mode++; - break; - case PAREN_CLOSE: - return root; - default: - printf("Didn't except token %d in arguments!\n", t.type); - } - } - - printf("Error: The arguments paren wasn't closed!\n"); - return NULL; -} - -/** - * Parses a function declaration. - */ -struct ASTNode* parseFunctionDeclaration(struct LexerResult result, int index) { - struct ASTNode* node = createASTNode(AST_FUNCTION_DEF); - - if(result.tokens[index + 2].type != PAREN_OPEN) { - printf("Error: Excepted a paren after function name!\n"); - return NULL; - } - - struct ASTNode* parameters = parseParameters(result, index + 3); - - node->left = createASTNode(AST_FUNCTION_TEMPLATE); - node->left->left = createASTNode(AST_FUNCTION_NAME); - memcpy(node->left->left->value, result.tokens[index + 1].value, strlen(result.tokens[index + 1].value)); - node->left->right = parameters; - - if(!parameters) { - printf("Error: Argument parsing went wrong!\n"); - return NULL; - } - - index = parameters->end + 1; - - if(result.tokens[index].type != BRACKETS_OPEN) { - printf("Error: Excepted function body declaration got %d instead!\n", result.tokens[index - 1].type); - return NULL; - } - - index++; - - int start = index; - - for(;index < result.size + 1; ++index) { - struct Token t = result.tokens[index]; - - if(t.type == BRACKETS_CLOSE) { - node->right = parseExpression(result, start, index, AST_FUNCTION_GENERIC); //todo: make a function to remove the need to loop to find the closing point - } - } - - node->end = index; - return node; -} - -struct ASTNode* parseFunctionInvoke(struct LexerResult result, int index) { - struct ASTNode* node = createASTNode(AST_FUNCTION_CALL); - node->left = createASTNode(AST_INVOKE_TARGET); - node->right = createASTNode(AST_INVOKE_PARAMETERS); - struct ASTNode* current = node->right; - memcpy(node->left->value, result.tokens[index].value, strlen(result.tokens[index].value)); - - index += 2; - - int i = 0; - for(; index < result.size + 1; ++index) { - struct Token t = result.tokens[index]; - - if(t.type == PAREN_CLOSE) { - node->end = index; - return node; - } - - if(t.type == COMMA) { - if(i == 0) { - printf("Error: Invoker arguments were passed wrongly!\n"); - return NULL; - } - i = 0; - continue; - } - - struct ASTNode* n = createASTNode(AST_PARAM); - - memcpy(n->value, result.tokens[index].value, strlen(result.tokens[index].value)); - - current->next = n; - - current = n; - i = 1; - } - - node->end = index; - return node; -} - -struct ASTNode* parseVariableDefinition(struct LexerResult result, int index) { - struct ASTNode* node = createASTNode(AST_VARIABLE_DEF); - node->left = createASTNode(AST_VARIABLE_NAME); - - memcpy(node->left->value, result.tokens[index].value, strlen(result.tokens[index].value)); - - 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; - } - - node->right = createASTNode(AST_VARIABLE_VALUE); - memcpy(node->right->value, result.tokens[index + 2].value, strlen(result.tokens[index + 2].value)); - - node->end = index + 2; - return node; -} - -/** - * Parses an expression in the specified range. - */ -struct ASTNode* parseExpression(struct LexerResult result, int index, int end, enum ASTNodeType type) { - struct ASTNode* root = createASTNode(type); - struct ASTNode* current = root; - - for(; index < end; ++index) { - struct Token t = result.tokens[index]; - struct Token next = result.tokens[index + 1]; - - if(t.type == SEMICOLON) { - continue; - } - - if(t.type == FUNCTION) { - if(next.type == KEYWORD) { - struct ASTNode* node = parseFunctionDeclaration(result, index); - - if(node != NULL) { - index = node->end; - current->next = node; - current = node; - } - } - else { - printf("Error: Excepted function name after func!\n"); - } - } - else if(t.type == KEYWORD) { - if(next.type == PAREN_OPEN) { - struct ASTNode* node = parseFunctionInvoke(result, index); - - if(node != NULL) { - index = node->end; - current->next = node; - current = node; - } - } - if(next.type == DECLARE) { - struct ASTNode* node = parseVariableDefinition(result, index); - - if(node != NULL) { - index = node->end; - current->next = node; - current = node; - } - } - } - else if(t.type == USE) { - if(next.type == STRING) { - struct ASTNode* node = createASTNode(AST_USE_STDL); - node->right = createASTNode(AST_STDL_TARGET); - - memcpy(node->right->value, next.value, strlen(next.value)); - - index++; - current->next = node; - current = node; - } - else { - printf("Error: Excepted string litteral after use\n"); - } - } - else { - printf("Error: Unexcepted token %d\n", t.type); - } - } - - return root; -} - -struct ASTNode* runParser(struct LexerResult result) { - return parseExpression(result, 0, result.size, AST_GENERIC); -} diff --git a/src/parser/parser.h b/src/parser/parser.h deleted file mode 100644 index cb1291b..0000000 --- a/src/parser/parser.h +++ /dev/null @@ -1,15 +0,0 @@ -/** - * The parser of Quickfall. - */ - -#ifndef PARSER_H -#define PARSER_H - -#include "../lexer/lexer.h" - -/** - * Runs the parser - */ -struct ASTNode* runParser(struct LexerResult result); - -#endif \ No newline at end of file From a64a31b8cb3794c0d4b58a9d592538788889af89 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Radi0o@users.noreply.github.com> Date: Sat, 7 Dec 2024 18:03:46 +0100 Subject: [PATCH 20/40] feat: removed old compiler version as it resulted to compiling errors --- src/compiler/compiler.c | 54 ----------- src/compiler/compiler.h | 25 ------ src/compiler/platforms/linux.c | 54 ----------- src/compiler/platforms/linux.h | 17 ---- src/compiler/platforms/windowsx86-64.c | 120 ------------------------- src/compiler/platforms/windowsx86-64.h | 13 --- src/compiler/stdl.c | 47 ---------- src/compiler/stdl.h | 17 ---- 8 files changed, 347 deletions(-) delete mode 100644 src/compiler/compiler.c delete mode 100644 src/compiler/compiler.h delete mode 100644 src/compiler/platforms/linux.c delete mode 100644 src/compiler/platforms/linux.h delete mode 100644 src/compiler/platforms/windowsx86-64.c delete mode 100644 src/compiler/platforms/windowsx86-64.h delete mode 100644 src/compiler/stdl.c delete mode 100644 src/compiler/stdl.h diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c deleted file mode 100644 index 4bee57b..0000000 --- a/src/compiler/compiler.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * The compiler of Quickfall. - */ - -#include -#include -#include -#include "../parser/ast.h" -#include "../parser/parser.h" -#include "../lexer/lexer.h" - -#include "./platforms/linux.h" -#include "./platforms/windowsx86-64.h" - -enum Platform { - WINDOWS, - LINUX -}; - -enum Platform platformFromString(char* platform) { - if(strcmp(platform, "win")) return WINDOWS; - else LINUX; -} - -char* compile(struct ASTNode* node, char* platform) { - enum Platform p = platformFromString(platform); - - struct CompilingContext ctx; - ctx.section = 1; - - // Default size buffers for now, todo: use realloc for less memory usage. - ctx.defaultSection = malloc(512); - memset(ctx.defaultSection, 0, 512); - strcat(ctx.defaultSection, WIN_64_DEFAULT_SECTION); // Change this based on the platform - - ctx.sections = malloc(1024); - memset(ctx.sections, 0, 1024); - - ctx.main = malloc(1024); - memset(ctx.main, 0, 1024); - strcat(ctx.main, "\nmain:"); - - win64(ctx, node, 0); //todo: change this based on the platform - - char* buff = malloc(2048); - memset(buff, 0, 2048); - - strcat(buff, ctx.defaultSection); - strcat(buff, ctx.sections); - strcat(buff, ctx.main); - strcat(buff, "\n ret\n"); - - return buff; -} diff --git a/src/compiler/compiler.h b/src/compiler/compiler.h deleted file mode 100644 index f0817b6..0000000 --- a/src/compiler/compiler.h +++ /dev/null @@ -1,25 +0,0 @@ -/** - * The main compiler file of Quickfall. - */ - -#ifndef COMPILER_H -#define COMPILER_H - -#include "../parser/ast.h" - -/** - * The object that gets passed trough the compiling functions. - */ -struct CompilingContext { - char* defaultSection; // the default (.LC0 in x64) section, is used to store variables for now. - char* sections; // the additional sections generated. - char* main; // the main function. - int section; // the current section index. -}; - -/** - * Compiles the AST node tree into Assembly. - */ -char* compile(struct ASTNode* node, char* platform); - -#endif \ No newline at end of file diff --git a/src/compiler/platforms/linux.c b/src/compiler/platforms/linux.c deleted file mode 100644 index 1c319f7..0000000 --- a/src/compiler/platforms/linux.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Linux compiling changes - */ - -#include -#include "../../parser/ast.h" - -/** - * Handles the decSec assembly thing. - */ -void decSec(char* buffer, char* name, char* type, char* val) { - strcat(buffer, "\n "); - strcat(buffer, name); - strcat(buffer, " "); - strcat(buffer, type); - strcat(buffer, " '"); - strcat(buffer, val); - strcat(buffer, "', 0"); -} - -/** - * Handles the movR assembly thing. - */ -void movR(char* buffer, char* name, char *v) { - strcat(buffer, "\n mov "); - strcat(buffer, name); - strcat(buffer, ", "); - strcat(buffer, v); -} - -/** - * Handles the sectG assembly thing. - */ -void sectG(char* buffer, char* name) { - strcat(buffer, "\n global "); - strcat(buffer, name); -} - -/** - * Handles the syscall assembly thing. - */ -void syscall(char* buffer) { - strcat(buffer, "\n int 0x80"); -} - -/** - * Handles the regXOR assembly thing. - */ -void regXOR(char* buffer, char* name, char* name2) { - strcat(buffer, "\n xor "); - strcat(buffer, name); - strcat(buffer, ", "); - strcat(buffer, name2); -} \ No newline at end of file diff --git a/src/compiler/platforms/linux.h b/src/compiler/platforms/linux.h deleted file mode 100644 index cd874f9..0000000 --- a/src/compiler/platforms/linux.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Linux compiling changes - */ - -#ifndef COMPILER_PLATFORM_LINUX_H -#define COMPILER_PLATFORM_LINUX_H - -#include -#include "../../parser/ast.h" - -void decSec(char* buffer, char* name, char* type, char* val); -void movR(char* buffer, char* name, char* v); -void sectG(char* buffer, char* val); -void syscall(char* buffer); -void regXOR(char* buffer, char* name1, char* name2); - -#endif \ No newline at end of file diff --git a/src/compiler/platforms/windowsx86-64.c b/src/compiler/platforms/windowsx86-64.c deleted file mode 100644 index 572aedc..0000000 --- a/src/compiler/platforms/windowsx86-64.c +++ /dev/null @@ -1,120 +0,0 @@ -/** - * Compiling for Windows x86-64 systems. - */ - -#include -#include - -#include "../../parser/ast.h" -#include "../compiler.h" -#include "../stdl.h" - -/** - * Gets the assembly output of the AST Node. - */ -void win64(struct CompilingContext ctx, struct ASTNode* node, int genericState) { - if(node->type == AST_FUNCTION_CALL) { - - char* funcBuff = (genericState == 0 ? ctx.main : ctx.sections); - - // Handles the low level function here for now. - - if(strcmp(node->left->value, "salloc") == 0) { - strcat(funcBuff, "\n subq $"); - strcat(funcBuff, node->right->next->value); - strcat(funcBuff, ", %rsp"); - } - else if(strcmp(node->left->value, "sfree") == 0) { - strcat(funcBuff, "\n addq $"); - strcat(funcBuff, node->right->next->value); - strcat(funcBuff, ", %rsp"); - } - else if(strcmp(node->left->value, "stackPut") == 0) { - strcat(ctx.defaultSection, "\n ."); - strcat(ctx.defaultSection, node->right->next->value); - strcat(ctx.defaultSection, " \""); - strcat(ctx.defaultSection, node->right->next->next->value); - strcat(ctx.defaultSection, "\""); - } - else if(strcmp(node->left->value, "call") == 0) { - strcat(funcBuff, "\n call "); - strcat(funcBuff, node->right->next->value); - } - else if(strcmp(node->left->value, "mov") == 0) { - strcat(funcBuff, "\n leaq ."); - strcat(funcBuff, node->right->next->value); - strcat(funcBuff, "(%"); - strcat(funcBuff, node->right->next->next->value); - strcat(funcBuff, "), %"); - strcat(funcBuff, node->right->next->next->next->value); - } - else { - // If the function isn't an internal, jump to it! - if(node->right->next != NULL) { - int argCount = 0; - while(node->right->next != NULL) { - node->right = node->right->next; - - char b[5] = {""}; - snprintf(b, 5, "%d", ctx.section++); - - strcat(ctx.sections, "\n."); - strcat(ctx.sections, "LC"); - strcat(ctx.sections, b); - strcat(ctx.sections, ":"); - strcat(ctx.sections, "\n .ascii \""); - strcat(ctx.sections, node->right->value); - strcat(ctx.sections, "\\0\""); - - strcat(funcBuff, "\n leaq .LC"); - strcat(funcBuff, b); - strcat(funcBuff, "(%rip), %rax"); - strcat(funcBuff, "\n movq %rax,"); - - switch(argCount) { - case 0: - strcat(funcBuff, "%rcx"); - break; - case 1: - strcat(funcBuff, "%rdx"); - break; - case 2: - strcat(funcBuff, "%r8"); - break; - case 3: - strcat(funcBuff, "%r9"); - break; - } - - argCount++; - } - } - - strcat(funcBuff, "\n jmp ."); - strcat(funcBuff, node->left->value); - } - } - else if(node->type == AST_FUNCTION_DEF) { - strcat(ctx.sections, "\n."); - strcat(ctx.sections, node->left->left->value); - strcat(ctx.sections, ":"); - - win64(ctx, node->right, genericState); // Parses the AST_GENERIC Node. - strcat(ctx.sections, "\n ret\n"); - } - else if(node->type == AST_FUNCTION_GENERIC || node->type == AST_GENERIC) { - struct ASTNode* n = node; - while (n->next != NULL) { - n = n->next; - - win64(ctx, n, (node->type == AST_GENERIC ? 0 : 1)); - } - - } - else if(node->type == AST_USE_STDL) { - loadAndDump(ctx, node->right->value); - } - else { - printf("Error: AST Node of type %d could not be converted to x64 assembly!\n", node->type); - } -} diff --git a/src/compiler/platforms/windowsx86-64.h b/src/compiler/platforms/windowsx86-64.h deleted file mode 100644 index d0e39cf..0000000 --- a/src/compiler/platforms/windowsx86-64.h +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Compiling for Windows x86-64 systems. - */ - -#include "../../parser/ast.h" -#include "../compiler.h" - -#define WIN_64_DEFAULT_SECTION ".LC0:\n .globl main" - -/** - * Gets the Assembly output of the AST node. - */ -void win64(struct CompilingContext ctx, struct ASTNode* node, int genericState); \ No newline at end of file diff --git a/src/compiler/stdl.c b/src/compiler/stdl.c deleted file mode 100644 index e4fbf9b..0000000 --- a/src/compiler/stdl.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * The code in charge of managing the standart library usage in Quickfall. - */ - -#include -#include -#include - -#include "./compiler.h" -#include "./platforms/windowsx86-64.h" - -#include "../lexer/lexer.h" -#include "../parser/parser.h" -#include "../parser/ast.h" - -/** - * Loads a component (a file) of the standart library and dump its contents into the assembly output. - * @param ctx the compiling context of the currently running compile attempt. - * @param component the component name (file name) of the part to load. - */ -void loadAndDump(struct CompilingContext ctx, char* component) { - char fileName[32] = "stdl/"; - strcat(fileName, component); - strcat(fileName, ".qfall"); - - FILE* fptr = fopen(fileName, "r"); - - if(fptr == NULL) { - printf("Error: Could not find STDL component named %s!", component); - return; - } - - fseek(fptr, 0, SEEK_END); - int size = ftell(fptr); - fseek(fptr, 0, SEEK_SET); - - char* buff = malloc(size + 1); - buff[size] = '\0'; - - fread(buff, 1, size, fptr); - fclose(fptr); - - struct LexerResult result = runLexer(buff); - struct ASTNode* node = runParser(result); - - win64(ctx, node, 0); // todo: change this according to the platform selected -} diff --git a/src/compiler/stdl.h b/src/compiler/stdl.h deleted file mode 100644 index 671f225..0000000 --- a/src/compiler/stdl.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * The code in charge of managing the standart library usage in Quickfall. - */ - -#ifndef COMPILER_STDL -#define COMPILER_STDL - -#include "./compiler.h" - -/** - * Loads a component (a file) of the standart library and dump its content into the assembly output. - * @param ctx the compiling context of the currently running compiling. - * @param component the component name (file name) of the part to load. - */ -void loadAndDump(struct CompilingContext ctx, char* component); - -#endif From a429e17f242162790ffe231ca89e91ef3074938d Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Radi0o@users.noreply.github.com> Date: Sat, 7 Dec 2024 18:04:19 +0100 Subject: [PATCH 21/40] feat: renamed the parser and the compiler folders accordingly --- src/{compilerv2 => compiler}/att/att-linux.h | 0 src/{compilerv2 => compiler}/att/att-win.h | 0 src/{compilerv2 => compiler}/compilerv2.c | 0 src/{compilerv2 => compiler}/compilerv2.h | 0 src/{compilerv2 => compiler}/objects.h | 0 src/{parserv2 => parser}/ast.c | 0 src/{parserv2 => parser}/ast.h | 0 src/{parserv2 => parser}/asts/functions.c | 0 src/{parserv2 => parser}/asts/functions.h | 0 src/{parserv2 => parser}/asts/variables.c | 0 src/{parserv2 => parser}/asts/variables.h | 0 src/{parserv2 => parser}/parser.c | 0 src/{parserv2 => parser}/parser.h | 0 13 files changed, 0 insertions(+), 0 deletions(-) rename src/{compilerv2 => compiler}/att/att-linux.h (100%) rename src/{compilerv2 => compiler}/att/att-win.h (100%) rename src/{compilerv2 => compiler}/compilerv2.c (100%) rename src/{compilerv2 => compiler}/compilerv2.h (100%) rename src/{compilerv2 => compiler}/objects.h (100%) rename src/{parserv2 => parser}/ast.c (100%) rename src/{parserv2 => parser}/ast.h (100%) rename src/{parserv2 => parser}/asts/functions.c (100%) rename src/{parserv2 => parser}/asts/functions.h (100%) rename src/{parserv2 => parser}/asts/variables.c (100%) rename src/{parserv2 => parser}/asts/variables.h (100%) rename src/{parserv2 => parser}/parser.c (100%) rename src/{parserv2 => parser}/parser.h (100%) diff --git a/src/compilerv2/att/att-linux.h b/src/compiler/att/att-linux.h similarity index 100% rename from src/compilerv2/att/att-linux.h rename to src/compiler/att/att-linux.h diff --git a/src/compilerv2/att/att-win.h b/src/compiler/att/att-win.h similarity index 100% rename from src/compilerv2/att/att-win.h rename to src/compiler/att/att-win.h diff --git a/src/compilerv2/compilerv2.c b/src/compiler/compilerv2.c similarity index 100% rename from src/compilerv2/compilerv2.c rename to src/compiler/compilerv2.c diff --git a/src/compilerv2/compilerv2.h b/src/compiler/compilerv2.h similarity index 100% rename from src/compilerv2/compilerv2.h rename to src/compiler/compilerv2.h diff --git a/src/compilerv2/objects.h b/src/compiler/objects.h similarity index 100% rename from src/compilerv2/objects.h rename to src/compiler/objects.h diff --git a/src/parserv2/ast.c b/src/parser/ast.c similarity index 100% rename from src/parserv2/ast.c rename to src/parser/ast.c diff --git a/src/parserv2/ast.h b/src/parser/ast.h similarity index 100% rename from src/parserv2/ast.h rename to src/parser/ast.h diff --git a/src/parserv2/asts/functions.c b/src/parser/asts/functions.c similarity index 100% rename from src/parserv2/asts/functions.c rename to src/parser/asts/functions.c diff --git a/src/parserv2/asts/functions.h b/src/parser/asts/functions.h similarity index 100% rename from src/parserv2/asts/functions.h rename to src/parser/asts/functions.h diff --git a/src/parserv2/asts/variables.c b/src/parser/asts/variables.c similarity index 100% rename from src/parserv2/asts/variables.c rename to src/parser/asts/variables.c diff --git a/src/parserv2/asts/variables.h b/src/parser/asts/variables.h similarity index 100% rename from src/parserv2/asts/variables.h rename to src/parser/asts/variables.h diff --git a/src/parserv2/parser.c b/src/parser/parser.c similarity index 100% rename from src/parserv2/parser.c rename to src/parser/parser.c diff --git a/src/parserv2/parser.h b/src/parser/parser.h similarity index 100% rename from src/parserv2/parser.h rename to src/parser/parser.h From 5b0987a822d34f3e9a5e5ebd779809be6df0f1c9 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Radi0o@users.noreply.github.com> Date: Sat, 7 Dec 2024 18:05:15 +0100 Subject: [PATCH 22/40] feat: renamed compiler files accordingly --- src/compiler/{compilerv2.c => compiler.c} | 0 src/compiler/{compilerv2.h => compiler.h} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/compiler/{compilerv2.c => compiler.c} (100%) rename src/compiler/{compilerv2.h => compiler.h} (100%) diff --git a/src/compiler/compilerv2.c b/src/compiler/compiler.c similarity index 100% rename from src/compiler/compilerv2.c rename to src/compiler/compiler.c diff --git a/src/compiler/compilerv2.h b/src/compiler/compiler.h similarity index 100% rename from src/compiler/compilerv2.h rename to src/compiler/compiler.h From f4c194908e5b599787c855a9b0b61ab05b4219e4 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Radi0o@users.noreply.github.com> Date: Sat, 7 Dec 2024 18:10:00 +0100 Subject: [PATCH 23/40] feat: removed compiling logic to rework it with ast changes --- src/cli/main.c | 3 +- src/compiler/compiler.c | 68 ++--------------------------------------- 2 files changed, 4 insertions(+), 67 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index 02c76a1..fa8c7ee 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -13,7 +13,6 @@ #include "../lexer/lexer.h" #include "../parser/parser.h" #include "../compiler/compiler.h" -#include "../compilerv2/compilerv2.h" #include "../utils/logging.c" @@ -110,7 +109,7 @@ int main(int argc, char* argv[]) { fclose(fptr); struct LexerResult result = runLexer(buff); - struct ASTNode* root = runParser(result); + struct ASTNode* root = parseNodes(result, 0, AST_ROOT); struct Context ctx = parseContext(root); diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 92d890f..09b3875 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -6,7 +6,7 @@ #include #include -#include "./compilerv2.h" +#include "./compiler.h" #include "./objects.h" #include "../parser/ast.h" #include "../utils/logging.c" @@ -40,70 +40,8 @@ struct Context parseContext(struct ASTNode* node) { ctx.variableCount = 0; ctx.functionCount = 0; - while(node->next != NULL) { - node = node->next; - switch (node->type) { - case AST_VARIABLE_DEF: - struct Variable* var = malloc(sizeof(struct Variable)); - - var->name = node->left->value; - var->type = node->value; - - if(node->right->type == AST_VARIABLE_VALUE) { - var->value = node->right->value; - } - else { - 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, hash, var); - - ctx.variableCount++; - break; - case AST_FUNCTION_DEF: - struct Function* func = malloc(sizeof(struct Function)); - - func->name = node->left->left->value; - - int c = 0; - - while(node->left->right->next != NULL) { - node->left->right = node->left->right->next; - - func->variables[c].name = node->left->right->right->value; - func->variables[c].type = node->left->right->left->value; - c++; - } - - func->variableCount = c; - - func->body = node->right; - - 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, hash, func); - - ctx.functionCount++; - break; - } - } - + + return ctx; } From 8360e772c993ab0448324c107e3f503a84ce86fb Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 9 Dec 2024 20:03:52 +0100 Subject: [PATCH 24/40] feat: added parseVariableDeclaration --- 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 184d9d7..daf4681 100644 --- a/src/parser/asts/variables.h +++ b/src/parser/asts/variables.h @@ -16,4 +16,11 @@ */ AST_NODE* parseVariableValue(struct LexerResult result, int index); +/** + * Parses a variable declaration. + * @param result the lexer result. + * @param index the starting index. + */ +AST_NODE* parseVariableDeclaration(struct LexerResult result, int index); + #endif From 16c6a5c2801189f1089aa506ee25e87dec50d393 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 9 Dec 2024 20:05:23 +0100 Subject: [PATCH 25/40] feat: added AST_VARIABLE_DECLARATION --- src/parser/ast.h | 1 + src/parser/asts/variables.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/parser/ast.h b/src/parser/ast.h index 4d8eb1e..2278bbd 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -16,6 +16,7 @@ enum ASTNodeType { AST_VARIABLE_NAME, // Represents a variable / parameter name precision in an AST Node. AST_VARIABLE_VALUE, + AST_VARIABLE_DECLARATION AST_VARIABLE_REFERENCE, AST_FUNCTION_DECLARATION, diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c index 05e8eb7..b323fe5 100644 --- a/src/parser/asts/variables.c +++ b/src/parser/asts/variables.c @@ -39,3 +39,7 @@ AST_NODE* parseVariableValue(struct LexerResult result, int index) { return node; } } + +AST_NODE* parseVariableDeclaration(struct LexerResult result, int index) { + AST_NODE* node = createASTNode(AST_VARIABLE_ +} From d2361671aee912224ed0785f9ba48fd71e34a694 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 9 Dec 2024 20:08:32 +0100 Subject: [PATCH 26/40] feat: added variable declaration parsing --- src/parser/asts/variables.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c index b323fe5..3821d79 100644 --- a/src/parser/asts/variables.c +++ b/src/parser/asts/variables.c @@ -40,6 +40,25 @@ AST_NODE* parseVariableValue(struct LexerResult result, int index) { } } +/** + * Parses a variable declaration. + * @param result the lexer result. + * @param index the starting index. + */ AST_NODE* parseVariableDeclaration(struct LexerResult result, int index) { - AST_NODE* node = createASTNode(AST_VARIABLE_ + 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 + 2); + + return node; } From 3268da423bc6d068dbd6f11ced3419d2fae3a092 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 9 Dec 2024 20:11:50 +0100 Subject: [PATCH 27/40] feat: removed duplicate token enum --- src/lexer/tokens.c | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 src/lexer/tokens.c diff --git a/src/lexer/tokens.c b/src/lexer/tokens.c deleted file mode 100644 index f43cf63..0000000 --- a/src/lexer/tokens.c +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Data related to tokens - */ - -#define longestKeywordSize 32 // Pumped up to handle longer values like numbers -#define smallestKeywordSize 4 - -/** - * The token types. - */ -enum TokenType { - FUNCTION = 1, - RETURN = 2, - VAR = 3, - BRACKETS_OPEN = 4, - BRACKETS_CLOSE = 5, - PAREN_OPEN = 6, - PAREN_CLOSE = 7, - ARRAY_OPEN = 8, - ARRAY_CLOSE = 9, - NUMBER = 10, - STRING = 11, - BOOLEAN_VALUE = 12, - NU = 13, - KEYWORD = 14, - SEMICOLON = 15, - COMMA = 16, - DECLARE = 17, - USE = 18, - NONE = 19 -}; From 4c2e238182866bd3382897d705b6957f4654e06b Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 9 Dec 2024 20:12:48 +0100 Subject: [PATCH 28/40] fix: parser enum dec --- src/parser/ast.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/ast.h b/src/parser/ast.h index 2278bbd..2bee7f9 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -16,7 +16,7 @@ enum ASTNodeType { AST_VARIABLE_NAME, // Represents a variable / parameter name precision in an AST Node. AST_VARIABLE_VALUE, - AST_VARIABLE_DECLARATION + AST_VARIABLE_DECLARATION, AST_VARIABLE_REFERENCE, AST_FUNCTION_DECLARATION, From 0a54802df6751ff5245f0bd998621b8d02e75093 Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 9 Dec 2024 21:12:34 +0100 Subject: [PATCH 29/40] feat: added AST_MATH_OPERATION --- src/parser/ast.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/parser/ast.h b/src/parser/ast.h index 2bee7f9..ca25c20 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -21,6 +21,8 @@ enum ASTNodeType { AST_FUNCTION_DECLARATION, AST_FUNCTION_HEADER, + + AST_MATH_OPERATION, AST_PARAMETER // A function parameter AST Node, used in function declaration. }; From b9e5d1db8b874ed869455709ee4f5a122f549738 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 20:31:19 +0100 Subject: [PATCH 30/40] feat: added math asts header --- src/parser/asts/math.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/parser/asts/math.h diff --git a/src/parser/asts/math.h b/src/parser/asts/math.h new file mode 100644 index 0000000..8ac5a73 --- /dev/null +++ b/src/parser/asts/math.h @@ -0,0 +1,13 @@ +/** + * Math ASTs for the Quickfall parser. + */ + +#include "../../ast.h" + +/** + * Parses the mathematical operation. + * @param result the lexer result. + * @param index the starting index. + */ +AST_NODE* parseMathematicalOpNode(struct LexerResult result, int index); + From f1c60b73d37a2e75756ee43da2b2ba81614f1b63 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 20:32:11 +0100 Subject: [PATCH 31/40] feat: added MATH_OP token --- src/lexer/tokens.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lexer/tokens.h b/src/lexer/tokens.h index 4b24b6e..e1cfaa9 100644 --- a/src/lexer/tokens.h +++ b/src/lexer/tokens.h @@ -23,7 +23,8 @@ enum TokenType { COMMA = 16, DECLARE = 17, USE = 18, - NONE = 19 + NONE = 19, + MATH_OP = 20 }; struct KeywordResult { From 1e7bd11722f0cab5bd9271b3986d5cfe53d9ba6b Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 20:33:20 +0100 Subject: [PATCH 32/40] feat: added mathematical operator in lexer --- src/lexer/lexer.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 31cdb93..14274c6 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -116,6 +116,13 @@ struct LexerResult runLexer(char string[]) { pushToken(&result, NONE); result.tokens[result.size - 1].value[0] = '?'; break; + case '+': + case '-': + case '/': + case '*': + pushToken(&result, MATH_OP); + result.tokens[result.size - 1].value[0] = c; + break; } } From 68db05f56b37f0ec23dfbce074d2a0212488646d Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 20:36:44 +0100 Subject: [PATCH 33/40] feat: added AST_MATH_OP_HEADER --- src/parser/ast.h | 1 + src/parser/asts/math.c | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 src/parser/asts/math.c diff --git a/src/parser/ast.h b/src/parser/ast.h index ca25c20..40a0869 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -23,6 +23,7 @@ enum ASTNodeType { AST_FUNCTION_HEADER, AST_MATH_OPERATION, + AST_MATH_OP_HEADER, AST_PARAMETER // A function parameter AST Node, used in function declaration. }; diff --git a/src/parser/asts/math.c b/src/parser/asts/math.c new file mode 100644 index 0000000..fb85f65 --- /dev/null +++ b/src/parser/asts/math.c @@ -0,0 +1,5 @@ +/** + * Math ASTs for the Quickfall parser. + */ + +#include "../../ast.h" From 3c8c46e109f35772cb3e4c28ad73b0bcc501a3f9 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 20:40:27 +0100 Subject: [PATCH 34/40] feat: added full math operation ast parsing --- src/parser/ast.h | 1 + src/parser/asts/math.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/parser/ast.h b/src/parser/ast.h index 40a0869..5df85bd 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -22,6 +22,7 @@ enum ASTNodeType { AST_FUNCTION_DECLARATION, AST_FUNCTION_HEADER, + AST_MATH_OPERATOR, AST_MATH_OPERATION, AST_MATH_OP_HEADER, diff --git a/src/parser/asts/math.c b/src/parser/asts/math.c index fb85f65..0da5874 100644 --- a/src/parser/asts/math.c +++ b/src/parser/asts/math.c @@ -3,3 +3,29 @@ */ #include "../../ast.h" +#include "../../parser.h" + +/** + * Parses the mathematical operation. + * @param result the lexer result. + * @param index the starting index. + */ +AST_NODE* parseMathematicalOpNode(struct LexerResult 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 = result.tokens[index + 1].value[0]; + + if(result.tokens[index + 2].type == DECLARE) { + node->value[0] = '1'; + } + + node->right = parseNodes(result, index + 2); + node->endingIndex = node->right->endingIndex; + + return node; +} From db7afe509e77fab20c40128b60b5f9ff4ddae20a Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 20:47:56 +0100 Subject: [PATCH 35/40] feat: added parser AST test --- tests/parser.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/parser.c diff --git a/tests/parser.c b/tests/parser.c new file mode 100644 index 0000000..14a69aa --- /dev/null +++ b/tests/parser.c @@ -0,0 +1,48 @@ +/** + * A simple parser AST test. + */ + +#include +#include + +#include "../src/lexer/lexer.h" + +#include "../src/parser/parser.h" +#include "../src/parser/ast.h" + +void dumpASTTree(AST_NODE* node, int depth); + +int main(int argc, char* argv[]) { + if(argc < 2) { + printf("Usage: \n"); + return -1; + } + + FILE* fptr = fopen(argv[1], "r"); + + if(fptr == NULL) { + printf("Couldn't open file!\n"); + return -1; + } + + fseek(fptr, 0, SEEK_END); + int size = ftell(fptr); + fseek(fptr, 0, SEEK_SET); + + char* buff = malloc(size + 1); + + fread(buff, 1, size, fptr); + buff[size] = '\0'; + fclose(fptr); + + struct LexerResult result = runLexer(buff); + struct ASTNode* root = parseNodes(result, 0, AST_ROOT); +} + +void dumpASTTree(AST_NODE* root, int depth) { + if(root->left != NULL) dumpASTTree(root->left, depth + 1); + if(root->right != NULL) dumpASTTree(root->right, depth + 1); + if(root->next != NULL) dumpASTTree(root->next, depth); + + printf("AST t: %d, v: %s\n", root->type, root->value); +} From a6514dc5f69f67c1020f1a695a57e781d4b9af0a Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 20:52:06 +0100 Subject: [PATCH 36/40] chore: fixed bench and added test target in Makefile --- Makefile | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 29bec26..332ecfe 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,9 @@ TARGET = quickfall$(TARGET_EXTENSION) # The target of a bench mode compiling. BENCH_TARGET = bench$(TARGET_EXTENSION) +# The target of a testing mode compiling. +TEST_TARGET = test$(TARGET_EXTENSION) + # # Building variables @@ -57,6 +60,7 @@ BENCH_TARGET = bench$(TARGET_EXTENSION) # The sources that are going to be compiled in normal mode. SOURCES = $(wildcard ${SRC_DIR}/**/*.c) $(wildcard ${SRC_DIR}/**/**/*.c) +SOURCES = $(subst src/cli/main.c,,${SOURCES}) BENCH_SOURCES = ${SOURCES} $(wildcard ${BENCH_SRC_DIR}/**/*.c) @@ -66,6 +70,7 @@ BENCH_SOURCES = ${SOURCES} $(wildcard ${BENCH_SRC_DIR}/**/*.c) all: prepare_build $(TARGET) bench: prepare_build $(BENCH_TARGET) +test: prepare_build $(TEST_TARGET) prepare_build: @echo [INFO] Using "${COMPILER}" as a compiler! @@ -77,5 +82,11 @@ prepare_build: @echo [INFO] Starting building logic $(TARGET): - $(COMPILER) $(FLAGS) $(SOURCES) -o $(TARGET) + $(COMPILER) $(FLAGS) $(SOURCES) src/cli/main.c -o $(TARGET) + +$(BENCH_TARGET): + $(COMPILER) $(FLAGS) $(SOURCES) benchmarks/main.c -o $(BENCH_TARGET) + +$(TEST_TARGET): + $(COMPILER) $(FLAGS) $(SOURCES) tests/parser.c -o $(TEST_TARGET) From fa9ef2c31238caae6560878d01dffaae6b400110 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 20:57:20 +0100 Subject: [PATCH 37/40] chore-fix: fixed building --- Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 332ecfe..994709e 100644 --- a/Makefile +++ b/Makefile @@ -59,8 +59,8 @@ TEST_TARGET = test$(TARGET_EXTENSION) # # The sources that are going to be compiled in normal mode. -SOURCES = $(wildcard ${SRC_DIR}/**/*.c) $(wildcard ${SRC_DIR}/**/**/*.c) -SOURCES = $(subst src/cli/main.c,,${SOURCES}) +SRCS = $(wildcard ${SRC_DIR}/**/*.c) $(wildcard ${SRC_DIR}/**/**/*.c) +SOURCES = $(subst src/cli/main.c,,${SRCS}) BENCH_SOURCES = ${SOURCES} $(wildcard ${BENCH_SRC_DIR}/**/*.c) @@ -79,6 +79,8 @@ prepare_build: @echo [INFO] Clearing old builds $(RM) build $(RM) $(TARGET) + $(RM) $(TEST_TARGET) + $(RM) $(BENCH_TARGET) @echo [INFO] Starting building logic $(TARGET): From 9679d45abb0b168f98cb004c7f7146857f7a13b7 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 20:59:28 +0100 Subject: [PATCH 38/40] fix: fixed math ast issues --- src/parser/asts/math.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/parser/asts/math.c b/src/parser/asts/math.c index 0da5874..c470513 100644 --- a/src/parser/asts/math.c +++ b/src/parser/asts/math.c @@ -2,8 +2,8 @@ * Math ASTs for the Quickfall parser. */ -#include "../../ast.h" -#include "../../parser.h" +#include "../ast.h" +#include "./variables.h" /** * Parses the mathematical operation. @@ -18,13 +18,14 @@ AST_NODE* parseMathematicalOpNode(struct LexerResult result, int index) { node->left->left->value = result.tokens[index].value; node->left->right = createASTNode(AST_MATH_OPERATOR); - node->left->right->value = result.tokens[index + 1].value[0]; + node->left->right->value[0] = result.tokens[index + 1].value[0]; + node->left->right->value[1] = '\0'; if(result.tokens[index + 2].type == DECLARE) { node->value[0] = '1'; } - node->right = parseNodes(result, index + 2); + node->right = parseVariableValue(result, index + 2); node->endingIndex = node->right->endingIndex; return node; From 5a08c807ec67b0f855bb248954fbba44ae8b71a9 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 21:54:54 +0100 Subject: [PATCH 39/40] pushed parser --- src/lexer/lexer.c | 4 +++- src/parser/ast.c | 4 ++++ src/parser/asts/math.h | 2 +- src/parser/asts/variables.c | 10 +++++++++- src/parser/parser.c | 15 ++++++++++++++- tests/parser.c | 27 +++++++++++++++++++++------ 6 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 14274c6..b9035b6 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -92,7 +92,9 @@ struct LexerResult runLexer(char string[]) { token.type = NU; } else if(strcmp(word, "use") == 0) { token.type = USE; - } + } else if(strcmp(word, "var") == 0) { + token.type = VAR; + } else { token.type = KEYWORD; } diff --git a/src/parser/ast.c b/src/parser/ast.c index 8398ae2..21dc281 100644 --- a/src/parser/ast.c +++ b/src/parser/ast.c @@ -12,7 +12,11 @@ */ AST_NODE* createASTNode(enum ASTNodeType type) { AST_NODE* node = malloc(sizeof(AST_NODE)); + node->type = type; + node->left = NULL; + node->right = NULL; + node->next = NULL; return node; } diff --git a/src/parser/asts/math.h b/src/parser/asts/math.h index 8ac5a73..13aaac6 100644 --- a/src/parser/asts/math.h +++ b/src/parser/asts/math.h @@ -2,7 +2,7 @@ * Math ASTs for the Quickfall parser. */ -#include "../../ast.h" +#include "../ast.h" /** * Parses the mathematical operation. diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c index 3821d79..2dccc5b 100644 --- a/src/parser/asts/variables.c +++ b/src/parser/asts/variables.c @@ -5,6 +5,8 @@ #include "../../lexer/lexer.h" #include "../../lexer/tokens.h" +#include "./math.h" + #include "../ast.h" AST_NODE* parseVariableValue(struct LexerResult result, int index) { @@ -17,8 +19,10 @@ AST_NODE* parseVariableValue(struct LexerResult result, int index) { 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"; - break; + case STRING: node->left->value = "s"; break; @@ -38,6 +42,7 @@ AST_NODE* parseVariableValue(struct LexerResult result, int index) { return node; } + } /** @@ -60,5 +65,8 @@ AST_NODE* parseVariableDeclaration(struct LexerResult result, int index) { node->right = parseVariableValue(result, index + 2); + if(node->right != NULL) node->endingIndex = node->right->endingIndex; + else node->endingIndex = index + 2; + return node; } diff --git a/src/parser/parser.c b/src/parser/parser.c index 9cd2599..1028f6d 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -2,9 +2,12 @@ * The parser of Quickfall. */ +#include + #include "../lexer/lexer.h" #include "./ast.h" +#include "./asts/variables.h" #include "./asts/functions.h" /** @@ -19,9 +22,11 @@ AST_NODE* parseNodes(struct LexerResult result, int index, enum ASTNodeType type for(; index <= result.size; ++index) { struct Token t = result.tokens[index]; + AST_NODE* node = NULL; + switch(t.type) { case FUNCTION: - AST_NODE* node = parseFunctionDeclaration(result, index); + node = parseFunctionDeclaration(result, index); if(node != NULL) { current->next = node; current = node; @@ -29,6 +34,14 @@ AST_NODE* parseNodes(struct LexerResult result, int index, enum ASTNodeType type } break; + case VAR: + node = parseVariableDeclaration(result, index); + if(node != NULL) { + current->next = node; + current = node; + index = node->endingIndex; + } + break; } } diff --git a/tests/parser.c b/tests/parser.c index 14a69aa..f453916 100644 --- a/tests/parser.c +++ b/tests/parser.c @@ -37,12 +37,27 @@ int main(int argc, char* argv[]) { struct LexerResult result = runLexer(buff); struct ASTNode* root = parseNodes(result, 0, AST_ROOT); -} -void dumpASTTree(AST_NODE* root, int depth) { - if(root->left != NULL) dumpASTTree(root->left, depth + 1); - if(root->right != NULL) dumpASTTree(root->right, depth + 1); - if(root->next != NULL) dumpASTTree(root->next, depth); + dumpASTTree(root, 0); +} - printf("AST t: %d, v: %s\n", root->type, root->value); +char* debug[12] = {"Root", "Type Node", "Variable Name", "Variable Value", "Variable Declaration", "Variable Reference", "Function Declaration", "Function Header", "Math Operator", "Math Operation", "Math Operation Header", "Parameter"}; + +void dumpASTTree(struct ASTNode* node, int depth) { + for(int i = 0; i < depth; ++i) { + printf(" "); + } + printf("AST Node of type (%d)\n", node->type); + + if(node->left != NULL) { + dumpASTTree(node->left, depth + 1); + } + + if(node->right != NULL) { + dumpASTTree(node->right, depth + 1); + } + + if(node->next != NULL) { + dumpASTTree(node->next, depth); + } } From ab518e36e758df58fb2fa48f710eb789ffdb9d46 Mon Sep 17 00:00:00 2001 From: Zffu Date: Tue, 10 Dec 2024 22:11:21 +0100 Subject: [PATCH 40/40] feat: finished true parsing --- src/parser/asts/math.c | 8 +++++++- src/parser/asts/variables.c | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/parser/asts/math.c b/src/parser/asts/math.c index c470513..fada606 100644 --- a/src/parser/asts/math.c +++ b/src/parser/asts/math.c @@ -2,6 +2,8 @@ * Math ASTs for the Quickfall parser. */ +#include + #include "../ast.h" #include "./variables.h" @@ -18,10 +20,14 @@ AST_NODE* parseMathematicalOpNode(struct LexerResult result, int index) { 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'; - if(result.tokens[index + 2].type == DECLARE) { + node->value = malloc(1); + + if(result.size >= index + 2 && result.tokens[index + 2].type == DECLARE) { node->value[0] = '1'; } diff --git a/src/parser/asts/variables.c b/src/parser/asts/variables.c index 2dccc5b..71cb9c3 100644 --- a/src/parser/asts/variables.c +++ b/src/parser/asts/variables.c @@ -14,7 +14,7 @@ AST_NODE* parseVariableValue(struct LexerResult result, int index) { if(t.type == NUMBER || t.type == STRING || t.type == BOOLEAN_VALUE) { AST_NODE* node = createASTNode(AST_VARIABLE_VALUE); - node->endingIndex = index + 1; + node->endingIndex = index; node->left = createASTNode(AST_TYPE); switch(t.type) { @@ -63,7 +63,7 @@ AST_NODE* parseVariableDeclaration(struct LexerResult result, int index) { node->left = createASTNode(AST_VARIABLE_NAME); node->left->value = result.tokens[index + 1].value; - node->right = parseVariableValue(result, index + 2); + node->right = parseVariableValue(result, index + 3); if(node->right != NULL) node->endingIndex = node->right->endingIndex; else node->endingIndex = index + 2;