diff options
author | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2025-04-26 01:21:12 +0330 |
---|---|---|
committer | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2025-04-26 01:21:12 +0330 |
commit | 85bfc724dd2bdaa2259512c1b8ab21f7dfeca8f1 (patch) | |
tree | 0b9075ba9bf70aca9cdf36e5d909b161460b19a8 /src | |
parent | 6edcfb23ffd49e937395b710f7c6213b2c0d93cf (diff) |
clean up
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/ast-tree.c | 46 | ||||
-rw-r--r-- | src/compiler/ast-tree.h | 3 | ||||
-rw-r--r-- | src/compiler/lexer.c | 23 | ||||
-rw-r--r-- | src/compiler/lexer.h | 12 | ||||
-rw-r--r-- | src/compiler/parser.c | 8 | ||||
-rw-r--r-- | src/compiler/parser.h | 2 | ||||
-rw-r--r-- | src/runner/runner.c | 4 | ||||
-rw-r--r-- | src/runner/runner.h | 1 | ||||
-rw-r--r-- | src/utils/file.c | 17 | ||||
-rw-r--r-- | src/utils/log.c | 13 | ||||
-rw-r--r-- | src/utils/log.h | 1 | ||||
-rw-r--r-- | src/utils/memory.c | 2 | ||||
-rw-r--r-- | src/utils/memory.h | 2 | ||||
-rw-r--r-- | src/utils/string.c | 38 | ||||
-rw-r--r-- | src/utils/string.h | 7 | ||||
-rw-r--r-- | src/utils/type.h | 18 |
16 files changed, 96 insertions, 101 deletions
diff --git a/src/compiler/ast-tree.c b/src/compiler/ast-tree.c index 965fca8..205a45f 100644 --- a/src/compiler/ast-tree.c +++ b/src/compiler/ast-tree.c @@ -8,11 +8,6 @@ #include "utils/string.h" #include "utils/time.h" #include "utils/type.h" -#include <stddef.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> AstTree AST_TREE_TYPE_TYPE = { .token = AST_TREE_TOKEN_TYPE_TYPE, @@ -1319,7 +1314,7 @@ AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots #endif ) { for (size_t i = 0; i < roots->size; ++i) { - if (strcmp(roots->data[i]->filePath, filePath) == 0) { + if (strEquals(roots->data[i]->filePath, filePath)) { free(filePath); return roots->data[i]; } @@ -1679,8 +1674,8 @@ bool pushVariable(AstTreeHelper *helper, AstTreeVariables *variables, char *var_end = variables->data[j]->name_end; if (variable->name_end - variable->name_begin == var_end - var_begin && - strncmp(var_begin, variable->name_begin, - variable->name_end - variable->name_begin) == 0) { + strnEquals(var_begin, variable->name_begin, + variable->name_end - variable->name_begin)) { printError(variable->name_begin, variable->name_end, "Variable exists"); return false; } @@ -2196,8 +2191,10 @@ AstTree *astTreeParseIdentifier(ParserNode *parserNode, AstTreeHelper *helper) { AstTree *astTreeParseValue(ParserNode *parserNode, AstTreeToken token, size_t metadata_size, AstTree *type) { - void *metadata = a404m_malloc(metadata_size); - memcpy(metadata, parserNode->metadata, metadata_size); + u8 *metadata = a404m_malloc(metadata_size); + for (size_t i = 0; i < metadata_size; ++i) { + metadata[i] = ((u8 *)parserNode->metadata)[i]; + } return newAstTree(token, metadata, type, parserNode->str_begin, parserNode->str_end); @@ -3146,7 +3143,7 @@ AstTree *makeTypeOf(AstTree *value) { for (size_t i = 0; i < struc->variables.size; ++i) { AstTreeVariable *member = struc->variables.data[i]; const size_t member_size = member->name_end - member->name_begin; - if (member_size == size && strncmp(member->name_begin, str, size)) { + if (member_size == size && strnEquals(member->name_begin, str, size)) { return copyAstTree(member->type); } } @@ -4167,7 +4164,7 @@ bool setTypesFunctionCall(AstTree *tree, AstTreeSetTypesHelper _helper) { for (size_t j = 0; j < function->arguments_size; ++j) { AstTreeTypeFunctionArgument arg = function->arguments[j]; if ((size_t)(arg.name_end - arg.name_begin) == param_name_size && - strncmp(arg.name_begin, param.nameBegin, param_name_size) == 0) { + strnEquals(arg.name_begin, param.nameBegin, param_name_size)) { initedArguments[j] = param; goto END_OF_NAMED_FOR; } @@ -4226,7 +4223,7 @@ bool setTypesVariable(AstTree *tree, AstTreeSetTypesHelper helper, const char *var_str = var->name_begin; const size_t var_str_size = var->name_end - var->name_begin; - if (var_str_size != str_size || strncmp(var_str, str, str_size) != 0) { + if (var_str_size != str_size || !strnEquals(var_str, str, str_size)) { continue; } @@ -4244,7 +4241,7 @@ bool setTypesVariable(AstTree *tree, AstTreeSetTypesHelper helper, const char *var_str = var->name_begin; const size_t var_str_size = var->name_end - var->name_begin; - if (var_str_size != str_size || strncmp(var_str, str, str_size) != 0) { + if (var_str_size != str_size || !strnEquals(var_str, str, str_size)) { continue; } @@ -4272,8 +4269,7 @@ bool setTypesVariable(AstTree *tree, AstTreeSetTypesHelper helper, for (size_t j = 0; j < function->arguments_size; ++j) { AstTreeTypeFunctionArgument arg = function->arguments[j]; if ((size_t)(arg.name_end - arg.name_begin) == param_name_size && - strncmp(arg.name_begin, param.nameBegin, param_name_size) == - 0) { + strnEquals(arg.name_begin, param.nameBegin, param_name_size)) { if (!typeIsEqual(arg.type, param.value->type)) { goto CONTINUE_OUTER; } @@ -4701,8 +4697,8 @@ bool setTypesOperatorAccess(AstTree *tree, AstTreeSetTypesHelper helper) { const char *str = metadata->member.name.begin; const char LENGTH_STR[] = "length"; - const size_t LENGTH_STR_SIZE = strlen(LENGTH_STR); - if (LENGTH_STR_SIZE == size && strncmp(LENGTH_STR, str, size) == 0) { + const size_t LENGTH_STR_SIZE = strLength(LENGTH_STR); + if (LENGTH_STR_SIZE == size && strnEquals(LENGTH_STR, str, size)) { metadata->member.index = 0; tree->type = copyAstTree(&AST_TREE_U64_TYPE); return true; @@ -4719,7 +4715,7 @@ bool setTypesOperatorAccess(AstTree *tree, AstTreeSetTypesHelper helper) { for (size_t i = 0; i < struc->variables.size; ++i) { AstTreeVariable *member = struc->variables.data[i]; const size_t member_size = member->name_end - member->name_begin; - if (member_size == size && strncmp(member->name_begin, str, size) == 0) { + if (member_size == size && strnEquals(member->name_begin, str, size)) { metadata->member.index = i; tree->type = copyAstTree(member->type); return true; @@ -4769,11 +4765,11 @@ bool setTypesBuiltin(AstTree *tree, AstTreeSetTypesHelper helper, return false; } } else if (param_name_size == FROM_STR_SIZE && - strncmp(param.nameBegin, FROM_STR, FROM_STR_SIZE) == 0 && + strnEquals(param.nameBegin, FROM_STR, FROM_STR_SIZE) && from == NULL) { from = param.value; } else if (param_name_size == TO_STR_SIZE && - strncmp(param.nameBegin, TO_STR, TO_STR_SIZE) == 0 && + strnEquals(param.nameBegin, TO_STR, TO_STR_SIZE) && to == NULL) { to = param.value; } else { @@ -4841,8 +4837,8 @@ bool setTypesBuiltin(AstTree *tree, AstTreeSetTypesHelper helper, return false; } } else if (param_name_size == VARIABLE_STR_SIZE && - strncmp(param.nameBegin, VARIABLE_STR, VARIABLE_STR_SIZE) == - 0 && + strnEquals(param.nameBegin, VARIABLE_STR, + VARIABLE_STR_SIZE) && variable == NULL) { variable = param.value; } else { @@ -4902,8 +4898,8 @@ bool setTypesBuiltin(AstTree *tree, AstTreeSetTypesHelper helper, return false; } } else if (param_name_size == VARIABLE_STR_SIZE && - strncmp(param.nameBegin, VARIABLE_STR, VARIABLE_STR_SIZE) == - 0 && + strnEquals(param.nameBegin, VARIABLE_STR, + VARIABLE_STR_SIZE) && file == NULL) { file = param.value; } else { diff --git a/src/compiler/ast-tree.h b/src/compiler/ast-tree.h index cb7c301..0e101a8 100644 --- a/src/compiler/ast-tree.h +++ b/src/compiler/ast-tree.h @@ -1,9 +1,6 @@ #pragma once #include "compiler/parser.h" -#include <stddef.h> -#include <stdint.h> -#include <sys/types.h> #include <time.h> typedef enum AstTreeToken { diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index b4e00b3..1c7e546 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -4,9 +4,6 @@ #include "utils/memory.h" #include "utils/string.h" -#include <stdint.h> -#include <stdio.h> - const char *LEXER_TOKEN_STRINGS[] = { "LEXER_TOKEN_SYMBOL_CLOSE_CURLY_BRACKET", @@ -99,12 +96,12 @@ const char *LEXER_TOKEN_STRINGS[] = { "LEXER_TOKEN_NONE", }; -const char *LEXER_SYMBOL_STRINGS[] = { +static const char *LEXER_SYMBOL_STRINGS[] = { ";", "(", ")", "{", "}", "->", ":", "=", "+=", "-=", "*=", "/=", "%=", ",", "+", "-", "*", "/", "%", "==", "!=", ">", ">=", "<", "<=", "&", ".*", ".", "!", "&&", "||", "[", "]", }; -const LexerToken LEXER_SYMBOL_TOKENS[] = { +static const LexerToken LEXER_SYMBOL_TOKENS[] = { LEXER_TOKEN_SYMBOL_EOL, LEXER_TOKEN_SYMBOL_OPEN_PARENTHESIS, LEXER_TOKEN_SYMBOL_CLOSE_PARENTHESIS, @@ -139,10 +136,10 @@ const LexerToken LEXER_SYMBOL_TOKENS[] = { LEXER_TOKEN_SYMBOL_OPEN_BRACKET, LEXER_TOKEN_SYMBOL_CLOSE_BRACKET, }; -const size_t LEXER_SYMBOL_SIZE = +static const size_t LEXER_SYMBOL_SIZE = sizeof(LEXER_SYMBOL_TOKENS) / sizeof(*LEXER_SYMBOL_TOKENS); -const char *LEXER_KEYWORD_STRINGS[] = { +static const char *LEXER_KEYWORD_STRINGS[] = { "type", "void", "i8", "u8", "i16", "u16", "i32", "u32", "i64", "u64", #ifdef FLOAT_16_SUPPORT @@ -151,7 +148,7 @@ const char *LEXER_KEYWORD_STRINGS[] = { "f32", "f64", "f128", "bool", "putc", "return", "true", "false", "if", "else", "while", "comptime", "null", "struct", "undefined", "code", }; -const LexerToken LEXER_KEYWORD_TOKENS[] = { +static const LexerToken LEXER_KEYWORD_TOKENS[] = { LEXER_TOKEN_KEYWORD_TYPE, LEXER_TOKEN_KEYWORD_VOID, LEXER_TOKEN_KEYWORD_I8, LEXER_TOKEN_KEYWORD_U8, LEXER_TOKEN_KEYWORD_I16, LEXER_TOKEN_KEYWORD_U16, @@ -169,7 +166,7 @@ const LexerToken LEXER_KEYWORD_TOKENS[] = { LEXER_TOKEN_KEYWORD_NULL, LEXER_TOKEN_KEYWORD_STRUCT, LEXER_TOKEN_KEYWORD_UNDEFINED, LEXER_TOKEN_KEYWORD_CODE, }; -const size_t LEXER_KEYWORD_SIZE = +static const size_t LEXER_KEYWORD_SIZE = sizeof(LEXER_KEYWORD_TOKENS) / sizeof(*LEXER_KEYWORD_TOKENS); const LexerNodeArray LEXER_NODE_ARRAY_ERROR = { @@ -481,11 +478,5 @@ bool isSpace(char c) { } bool isString(char c) { - switch (c) { - case '\'': - case '\"': - return true; - default: - return false; - } + return c == '\'' || c == '\"'; } diff --git a/src/compiler/lexer.h b/src/compiler/lexer.h index d1d746b..ca765d8 100644 --- a/src/compiler/lexer.h +++ b/src/compiler/lexer.h @@ -113,21 +113,11 @@ typedef enum LexerToken : u8 { extern const char *LEXER_TOKEN_STRINGS[]; -extern const char *LEXER_SYMBOL_STRINGS[]; -extern const LexerToken LEXER_SYMBOL_TOKENS[]; -extern const size_t LEXER_SYMBOL_SIZE; - -extern const char *LEXER_KEYWORD_STRINGS[]; -extern const LexerToken LEXER_KEYWORD_TOKENS[]; -extern const size_t LEXER_KEYWORD_SIZE; - -struct ParserNode; - typedef struct LexerNode { char *str_begin; char *str_end; - LexerToken token; struct ParserNode *parserNode; + LexerToken token; } LexerNode; typedef struct LexerNodeArray { diff --git a/src/compiler/parser.c b/src/compiler/parser.c index 32c37c9..19e0009 100644 --- a/src/compiler/parser.c +++ b/src/compiler/parser.c @@ -6,11 +6,6 @@ #include "utils/memory.h" #include "utils/string.h" #include "utils/time.h" -#include <stddef.h> -#include <stdint.h> -#include <stdio.h> -#include <stdlib.h> -#include <time.h> const char *PARSER_TOKEN_STRINGS[] = { "PARSER_TOKEN_ROOT", @@ -763,8 +758,7 @@ bool parserNodeArray(LexerNode *begin, LexerNode *end, ParserNode *parent) { parsedNodes_data = a404m_realloc( parsedNodes_data, sizeof(*parsedNodes_data) * parsedNodes_capacity); } - parsedNodes_data[parsedNodes_size] = pNode; - parsedNodes_size += 1; + parsedNodes_data[parsedNodes_size++] = pNode; } ParserNodeArray *parsedNodes = a404m_malloc(sizeof(*parsedNodes)); diff --git a/src/compiler/parser.h b/src/compiler/parser.h index f37715d..f7d1954 100644 --- a/src/compiler/parser.h +++ b/src/compiler/parser.h @@ -2,8 +2,6 @@ #include "compiler/lexer.h" #include "utils/type.h" -#include <stddef.h> -#include <stdint.h> #include <time.h> typedef enum ParserToken { diff --git a/src/runner/runner.c b/src/runner/runner.c index 2e8c347..f33f056 100644 --- a/src/runner/runner.c +++ b/src/runner/runner.c @@ -2,8 +2,8 @@ #include "compiler/ast-tree.h" #include "utils/log.h" #include "utils/memory.h" +#include "utils/string.h" #include <stdio.h> -#include <string.h> #define doOperation(op0, op1, operator, originalType, type) \ *((originalType *)(op0)->metadata) = \ @@ -53,7 +53,7 @@ bool runAstTree(AstTreeRoots roots) { AstTreeVariable *variable = root->variables.data[i]; size_t name_size = variable->name_end - variable->name_begin; if (name_size == MAIN_STR_SIZE && - strncmp(variable->name_begin, MAIN_STR, MAIN_STR_SIZE) == 0 && + strnEquals(variable->name_begin, MAIN_STR, MAIN_STR_SIZE) && variable->type->token == AST_TREE_TOKEN_TYPE_FUNCTION) { if (mainVariable != NULL) { printLog("Too many main variables"); diff --git a/src/runner/runner.h b/src/runner/runner.h index 12ee241..4de6fbe 100644 --- a/src/runner/runner.h +++ b/src/runner/runner.h @@ -1,7 +1,6 @@ #pragma once #include "compiler/ast-tree.h" -#include <stdint.h> void runnerVariableSetValue(AstTreeVariable *variable, AstTree *value); void runnerVariableSetValueWihtoutConstCheck(AstTreeVariable *variable, diff --git a/src/utils/file.c b/src/utils/file.c index 55a438c..61165d2 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -2,8 +2,8 @@ #include "utils/log.h" #include "utils/memory.h" +#include "utils/string.h" #include <stdio.h> -#include <string.h> size_t fileCodes_capacity = 0; char **fileCodes = NULL; @@ -36,10 +36,13 @@ void filePush(const char *filePath, char *code) { fileCodes_names = a404m_realloc( fileCodes_names, fileCodes_capacity * sizeof(*fileCodes_names)); } + size_t filePath_length = strLength(filePath); fileCodes[fileCodes_length] = code; fileCodes_names[fileCodes_length] = - a404m_malloc((strlen(filePath) + 1) * sizeof(**fileCodes_names)); - strcpy(fileCodes_names[fileCodes_length], filePath); + a404m_malloc((filePath_length + 1) * sizeof(**fileCodes_names)); + for (size_t i = 0; i < filePath_length; ++i) { + fileCodes_names[fileCodes_length][i] = filePath[i]; + } fileCodes_length += 1; } @@ -73,7 +76,7 @@ char *readWholeFile(const char *filePath) { size_t getFileIndex(const char *filePath) { for (size_t i = 0; i < fileCodes_length; ++i) { const char *str = fileCodes_names[i]; - if (strcmp(filePath, str) == 0) { + if (strEquals(filePath, str)) { return i; } } @@ -89,9 +92,11 @@ char *joinToPathOf(const char *original, const char *file) { } } char *result = - a404m_malloc((result_size + 1 + strlen(file) + 1) * sizeof(*result)); + a404m_malloc((result_size + 1 + strLength(file) + 1) * sizeof(*result)); - strncpy(result, original, result_size); + for (size_t i = 0; i < result_size; ++i) { + result[i] = original[i]; + } result[result_size++] = '/'; for (size_t i = 0; file[i] != '\0'; ++i) { diff --git a/src/utils/log.c b/src/utils/log.c index 898517d..cab0c48 100644 --- a/src/utils/log.c +++ b/src/utils/log.c @@ -1,10 +1,9 @@ #include "log.h" #include "utils/file.h" +#include "utils/string.h" #include <stdarg.h> -#include <stdint.h> #include <stdio.h> -#include <string.h> void _printLogBack(const char *format, const char *file, int line, ...) { va_list args; @@ -26,7 +25,7 @@ void _printErrorWarningBack(const char *file, int line, char *begin, char *end, size_t file_index = SIZE_MAX; for (size_t i = 0; i < fileCodes_length; ++i) { char *fileCode_begin = fileCodes[i]; - char *fileCode_end = fileCodes[i] + strlen(fileCodes[i]); + char *fileCode_end = fileCodes[i] + strLength(fileCodes[i]); if (begin >= fileCode_begin && end <= fileCode_end) { file_index = i; break; @@ -70,11 +69,11 @@ void _printErrorWarningBack(const char *file, int line, char *begin, char *end, for (char *iter = file_line_begin; iter < file_line_end; ++iter) { if (iter == begin) { - fprintf(stderr, "%s", secondColor); + fputs(secondColor, stderr); } else if (iter == end) { - fprintf(stderr, "%s", FIRST_COLOR); + fputs(FIRST_COLOR, stderr); } - fprintf(stderr, "%c", *iter); + fputc(*iter, stderr); } - fprintf(stderr, "\e[0m\n"); + fputs("\e[0m\n", stderr); } diff --git a/src/utils/log.h b/src/utils/log.h index bcc7aa1..913fd34 100644 --- a/src/utils/log.h +++ b/src/utils/log.h @@ -1,6 +1,5 @@ #pragma once -#include "utils/type.h" #include <stdlib.h> #ifndef __FILE_NAME__ diff --git a/src/utils/memory.c b/src/utils/memory.c index bf3de92..0c9740c 100644 --- a/src/utils/memory.c +++ b/src/utils/memory.c @@ -10,7 +10,7 @@ void *a404m_malloc(size_t size) { } } -void *a404m_realloc(void *restrict pointer, size_t size) { +void *a404m_realloc(void *pointer, size_t size) { if (size == 0) { free(pointer); return NULL; diff --git a/src/utils/memory.h b/src/utils/memory.h index 29bd3db..132fd2b 100644 --- a/src/utils/memory.h +++ b/src/utils/memory.h @@ -3,5 +3,5 @@ #include <stddef.h> extern void *a404m_malloc(size_t size); -extern void *a404m_realloc(void *restrict pointer, size_t size); +extern void *a404m_realloc(void *pointer, size_t size); extern size_t a404m_malloc_usable_size(void *pointer); diff --git a/src/utils/string.c b/src/utils/string.c index 55d2c3e..55e6236 100644 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -2,15 +2,37 @@ #include "memory.h" #include "utils/type.h" -#include <stdint.h> -#include <stdio.h> -#include <string.h> +size_t strLength(const char *str) { + size_t i = 0; + for (; str[i] != '\0'; ++i) + ; + return i; +} + +bool strEquals(const char *left, const char *right) { + size_t i = 0; + for (; left[i] != '\0' && right[i] != '\0'; ++i) { + if (left[i] != right[i]) { + return false; + } + } + return left[i] == right[i]; +} + +bool strnEquals(const char *left, const char *right, size_t len) { + for (size_t i = 0; i < len; ++i) { + if (left[i] != right[i]) { + return false; + } + } + return true; +} size_t searchInStringArray(const char *array[], size_t array_size, const char *str, size_t str_size) { for (size_t i = 0; i < array_size; ++i) { - const size_t el_size = strlen(array[i]); - if (el_size == str_size && strncmp(array[i], str, str_size) == 0) { + const size_t el_size = strLength(array[i]); + if (el_size == str_size && strnEquals(array[i], str, str_size)) { return i; } } @@ -87,8 +109,10 @@ char *u64ToString(u64 value) { } char *strClone(const char *str) { - const size_t str_length = strlen(str) + 1; + const size_t str_length = strLength(str) + 1; char *result = a404m_malloc(str_length * sizeof(*result)); - strncpy(result, str, str_length); + for (size_t i = 0; i < str_length; ++i) { + result[i] = str[i]; + } return result; } diff --git a/src/utils/string.h b/src/utils/string.h index a5a0319..56f6825 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -4,11 +4,16 @@ #include <stddef.h> #include <stdint.h> +size_t strLength(const char *str); + +bool strEquals(const char *left, const char *right); +bool strnEquals(const char *left, const char *right, size_t len); + size_t searchInStringArray(const char *array[], size_t array_size, const char *str, size_t str_size); u64 decimalToU64(char *str_begin, char *str_end, bool *success); f128 numberToFloat(char *str_begin, char *str_end, bool *success); -char* u64ToString(u64 value); +char *u64ToString(u64 value); char *strClone(const char *str); diff --git a/src/utils/type.h b/src/utils/type.h index 4c61bc3..3a0e59e 100644 --- a/src/utils/type.h +++ b/src/utils/type.h @@ -1,20 +1,18 @@ #pragma once -#include <stdint.h> - #ifdef __FLT16_MIN__ #define FLOAT_16_SUPPORT #endif -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; -typedef int64_t i64; +typedef char i8; +typedef short i16; +typedef int i32; +typedef long long i64; -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long long u64; #ifdef FLOAT_16_SUPPORT typedef _Float16 f16; |