From 1ed4598ee78c51ad6d0654d731c0ab377bbe4a67 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 14 Dec 2024 15:44:10 +0100 Subject: [PATCH 1/7] feat: added asm function token type in headers --- src/lexer/tokens.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lexer/tokens.h b/src/lexer/tokens.h index a5b7ece..2e99cee 100644 --- a/src/lexer/tokens.h +++ b/src/lexer/tokens.h @@ -2,6 +2,7 @@ #define TOKENS_H enum TokenType { + ASM_FUNCTION, FUNCTION, RETURN, VAR, From e9d79aaf2a06b5fc35d3e83ead72e1a3819a8be0 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 14 Dec 2024 15:45:03 +0100 Subject: [PATCH 2/7] feat: added asm func token in lexer --- src/lexer/lexer.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index f0d8e21..5ba9dc9 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -74,6 +74,9 @@ struct LexerResult runLexer(char* string) { if(strcmp(buff, "func") == 0) { pushToken(&result, FUNCTION); } + else if(strcmp(buff, "asmf" == 0) { + pushToken(&result, ASM_FUNCTION); + } else if(strcmp(buff, "true") == 0 || strcmp(buff, "false") == 0) { pushToken(&result, BOOLEAN_VALUE); result.tokens[result.size - 1].value = buff; From 7ad9ff7319be6d2f81c25ad5b2fac585189a60bf Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 14 Dec 2024 15:45:34 +0100 Subject: [PATCH 3/7] fix: missing paren --- src/lexer/lexer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 5ba9dc9..650d144 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -74,7 +74,7 @@ struct LexerResult runLexer(char* string) { if(strcmp(buff, "func") == 0) { pushToken(&result, FUNCTION); } - else if(strcmp(buff, "asmf" == 0) { + else if(strcmp(buff, "asmf") == 0) { pushToken(&result, ASM_FUNCTION); } else if(strcmp(buff, "true") == 0 || strcmp(buff, "false") == 0) { From bce6dc090c46cd3848168983f05f8afb9c7248b6 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 14 Dec 2024 15:46:25 +0100 Subject: [PATCH 4/7] feat: added AST_ASM_FUNCTION_DECLARATION --- src/parser/ast.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/parser/ast.h b/src/parser/ast.h index 5df85bd..3416964 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -19,6 +19,8 @@ enum ASTNodeType { AST_VARIABLE_DECLARATION, AST_VARIABLE_REFERENCE, + AST_ASM_FUNCTION_DECLARATION, + AST_FUNCTION_DECLARATION, AST_FUNCTION_HEADER, From 5cb07c8514a0c5a9c787f303c6b21f418ea23689 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 14 Dec 2024 15:47:33 +0100 Subject: [PATCH 5/7] feat: added parseASMFunctionDeclaration in header --- src/parser/asts/functions.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/parser/asts/functions.h b/src/parser/asts/functions.h index cf05a1b..025f43e 100644 --- a/src/parser/asts/functions.h +++ b/src/parser/asts/functions.h @@ -30,4 +30,11 @@ AST_NODE* parseArguments(struct LexerResult result, int index); */ AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index); +/** + * Parses an ASM function declaration. + * @param result the lexer result. + * @param index the starting index of the parsing. + */ +AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index); + #endif From 634537b3fc32eec1d2fa28612997b4aba0d28eb6 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 14 Dec 2024 16:02:59 +0100 Subject: [PATCH 6/7] feat: added assembly function parsin --- src/parser/asts/functions.c | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 95cb6b8..4d36dd3 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -138,3 +138,54 @@ AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index) { return node; } + +AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) { + AST_NODE* node = createASTNode(AST_ASM_FUNCTION_DECLARATION); + + node->left = createASTNode(AST_FUNCTION_HEADER); + + if(result.tokens[index].type != KEYWORD) { + return NULL; + } + + AST_NODE* params = parseParameters(result, index + 1); + + if(params == NULL) return NULL; + + node->left->left = params; + + index = params->endingIndex; + + int buffSize = 32; + int buffIndex = 0; + char* buff = malloc(buffSize); + + for(; index <= result.size; ++index) { + struct Token t = result.tokens[index]; + + if(t.type == BRACKETS_CLOSE) { + break; + } + + if(t.type != STRING) { + logError("Disallowed token type in Assembly function! Only allows string!\n"); + return NULL; + } + + char c; + while(c = *t.value++) { + buff[buffIndex] = c; + buffIndex++; + + if(buffIndex >= buffSize) { + buffSize *= 1.5; + buff = realloc(buff, buffSize); + } + } + } + + buff[buffIndex] = '\0'; + + node->value = buff; + return node; +} From 9f48fdfd3801854bc44fc3b9007d529ef02e3ef3 Mon Sep 17 00:00:00 2001 From: Zffu Date: Sat, 14 Dec 2024 16:08:45 +0100 Subject: [PATCH 7/7] feat: added ASM functino parsing --- src/parser/asts/functions.c | 5 ++++- src/parser/parser.c | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 4d36dd3..e9e0fb9 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -2,6 +2,8 @@ * Function-related AST parsing. */ +#include + #include "./variables.h" #include "../parser.h" @@ -10,6 +12,8 @@ #include "../../lexer/tokens.h" #include "../../lexer/lexer.h" +#include "../../utils/logging.c" + /** * Parse the parameters from a function definition (for example). * @param result the lexer result. @@ -168,7 +172,6 @@ AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) { } if(t.type != STRING) { - logError("Disallowed token type in Assembly function! Only allows string!\n"); return NULL; } diff --git a/src/parser/parser.c b/src/parser/parser.c index 6fce88c..15bada7 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -44,7 +44,15 @@ AST_NODE* parseNodes(struct LexerResult result, int index, enum ASTNodeType type index = node->endingIndex; } break; - + case ASM_FUNCTION: + node = parseASMFunctionDeclaration(result, index); + if(node != NULL) { + current->next = node; + current = node; + index = node->endingIndex; + } + break; + } }