From dc26ae0b913ff49ecbf0f653b4baffa7a37f2303 Mon Sep 17 00:00:00 2001 From: A404M Date: Fri, 25 Apr 2025 22:22:40 +0330 Subject: add better time printing --- src/compiler/ast-tree.c | 45 ++++++++++++++++++++++++++++++++++----------- src/compiler/ast-tree.h | 15 +++++++++++++-- src/compiler/lexer.c | 4 ++-- src/compiler/parser.c | 34 ++++++++++++++++++++++++---------- src/compiler/parser.h | 10 ++++++++-- 5 files changed, 81 insertions(+), 27 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/ast-tree.c b/src/compiler/ast-tree.c index 2201b4a..47b3d54 100644 --- a/src/compiler/ast-tree.c +++ b/src/compiler/ast-tree.c @@ -6,11 +6,13 @@ #include "utils/log.h" #include "utils/memory.h" #include "utils/string.h" +#include "utils/time.h" #include "utils/type.h" #include #include #include #include +#include AstTree AST_TREE_TYPE_TYPE = { .token = AST_TREE_TOKEN_TYPE_TYPE, @@ -859,11 +861,13 @@ void astTreeRootsDestroy(AstTreeRoots roots) { AstTree *newAstTree(AstTreeToken token, void *metadata, AstTree *type, char *str_begin, char *str_end) { AstTree *result = a404m_malloc(sizeof(*result)); - result->token = token; - result->metadata = metadata; - result->type = type; - result->str_begin = str_begin; - result->str_end = str_end; + *result = (AstTree){ + .token = token, + .metadata = metadata, + .type = type, + .str_begin = str_begin, + .str_end = str_end, + }; return result; } @@ -1277,13 +1281,20 @@ AstTreeVariables copyAstTreeVariables(AstTreeVariables variables, return result; } -AstTreeRoots makeAstTree(const char *filePath) { +AstTreeRoots makeAstTree(const char *filePath +#ifdef PRINT_STATISTICS + , + struct timespec *lexingTime, + struct timespec *parsingTime +#endif +) { AstTreeRoots roots = { .data = a404m_malloc(0 * sizeof(*roots.data)), .size = 0, }; - if (getAstTreeRoot(strClone(filePath), &roots) == NULL) { + if (getAstTreeRoot(strClone(filePath), &roots, lexingTime, parsingTime) == + NULL) { goto RETURN_ERROR; } @@ -1300,7 +1311,13 @@ RETURN_ERROR: return AST_TREE_ROOTS_ERROR; } -AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots) { +AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots +#ifdef PRINT_STATISTICS + , + struct timespec *lexingTime, + struct timespec *parsingTime +#endif +) { for (size_t i = 0; i < roots->size; ++i) { if (strcmp(roots->data[i]->filePath, filePath) == 0) { free(filePath); @@ -1308,10 +1325,16 @@ AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots) { } } - ParserNode *parserNode = parserFromPath(filePath); + struct timespec start, end; + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); + + ParserNode *parserNode = parserFromPath(filePath, lexingTime); if (parserNode == NULL) { goto RETURN_ERROR; } + + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); + *parsingTime = time_add(*parsingTime, time_diff(end, start)); AstTreeRoot *root = makeAstRoot(parserNode, filePath); parserNodeDelete(parserNode); if (root == NULL) { @@ -1381,8 +1404,8 @@ AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots) { (imports_size + imports_size / 2 + 1) * sizeof(*root->imports)); } - AstTreeRoot *import = - getAstTreeRoot(joinToPathOf(filePath, str), roots); + AstTreeRoot *import = getAstTreeRoot(joinToPathOf(filePath, str), + roots, lexingTime, parsingTime); free(str); if (import == NULL) { diff --git a/src/compiler/ast-tree.h b/src/compiler/ast-tree.h index 68b8ec6..cb7c301 100644 --- a/src/compiler/ast-tree.h +++ b/src/compiler/ast-tree.h @@ -4,6 +4,7 @@ #include #include #include +#include typedef enum AstTreeToken { AST_TREE_TOKEN_FUNCTION, @@ -284,8 +285,18 @@ AstTreeVariables copyAstTreeVariables(AstTreeVariables variables, AstTreeVariables newVariables[], size_t variables_size); -AstTreeRoots makeAstTree(const char *filePath); -AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots); +AstTreeRoots makeAstTree(const char *filePath +#ifdef PRINT_STATISTICS + , + struct timespec *lexingTime, struct timespec* parsingTime +#endif +); +AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots +#ifdef PRINT_STATISTICS + , + struct timespec *lexingTime, struct timespec *parsingTime +#endif + ); AstTreeRoot *makeAstRoot(ParserNode *parsedRoot, char *filePath); bool pushVariable(AstTreeHelper *helper, AstTreeVariables *variables, diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index 8cf0cc3..b4e00b3 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -308,7 +308,7 @@ RETURN_SUCCESS: return result; } -void lexerPushClear(LexerNodeArray *array, size_t *array_size, char *iter, +inline __attribute__((always_inline)) void lexerPushClear(LexerNodeArray *array, size_t *array_size, char *iter, char **node_str_begin, LexerToken *node_token, LexerToken token) { switch (*node_token) { @@ -480,7 +480,7 @@ bool isSpace(char c) { } } -extern bool isString(char c) { +bool isString(char c) { switch (c) { case '\'': case '\"': diff --git a/src/compiler/parser.c b/src/compiler/parser.c index 4c67988..32c37c9 100644 --- a/src/compiler/parser.c +++ b/src/compiler/parser.c @@ -5,10 +5,12 @@ #include "utils/log.h" #include "utils/memory.h" #include "utils/string.h" +#include "utils/time.h" #include #include #include #include +#include const char *PARSER_TOKEN_STRINGS[] = { "PARSER_TOKEN_ROOT", @@ -665,15 +667,24 @@ RETURN_SUCCESS: ParserNode *newParserNode(ParserToken token, char *str_begin, char *str_end, void *metadata, ParserNode *parent) { ParserNode *parserNode = a404m_malloc(sizeof(*parserNode)); - parserNode->token = token; - parserNode->str_begin = str_begin; - parserNode->str_end = str_end; - parserNode->metadata = metadata; - parserNode->parent = parent; + *parserNode = (ParserNode){ + .token = token, + .str_begin = str_begin, + .str_end = str_end, + .metadata = metadata, + .parent = parent, + }; return parserNode; } -ParserNode *parserFromPath(const char *filePath) { +ParserNode *parserFromPath(const char *filePath +#ifdef PRINT_STATISTICS + , + struct timespec *lexingTime +#endif +) { + struct timespec start, end; + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); char *code = readWholeFile(filePath); if (code == NULL) { return NULL; @@ -683,6 +694,8 @@ ParserNode *parserFromPath(const char *filePath) { if (lexerNodeArrayIsError(lexed)) { return NULL; } + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); + *lexingTime = time_add(*lexingTime, time_diff(end, start)); ParserNode *root = parser(lexed); lexerNodeArrayDestroy(lexed); @@ -740,7 +753,8 @@ bool parserNodeArray(LexerNode *begin, LexerNode *end, ParserNode *parent) { } } */ - if(parsedNodes_size != 0 && parsedNodes_data[parsedNodes_size-1] == pNode){ + if (parsedNodes_size != 0 && + parsedNodes_data[parsedNodes_size - 1] == pNode) { continue; } @@ -1293,7 +1307,7 @@ ParserNode *parserParenthesis(LexerNode *closing, LexerNode *begin, parserNode->str_end = closing->str_end; if (parserNodeArray(opening + 1, closing, parserNode)) { - ParserNodeFunctionCall *metadata = malloc(sizeof(*metadata)); + ParserNodeFunctionCall *metadata = a404m_malloc(sizeof(*metadata)); metadata->function = before; metadata->params = parserNode->metadata; parserNode->metadata = metadata; @@ -1360,7 +1374,7 @@ ParserNode *parserBracketsRight(LexerNode *closing, LexerNode *begin, parserNode->str_end = closing->str_end; if (parserNodeArray(opening + 1, closing, parserNode)) { - ParserNodeBracketMetadata *metadata = malloc(sizeof(*metadata)); + ParserNodeBracketMetadata *metadata = a404m_malloc(sizeof(*metadata)); metadata->operand = before; metadata->params = parserNode->metadata; parserNode->metadata = metadata; @@ -1419,7 +1433,7 @@ ParserNode *parserBracketsLeft(LexerNode *closing, LexerNode *begin, parserNode->str_end = after->str_end; if (parserNodeArray(opening + 1, closing, parserNode)) { - ParserNodeBracketMetadata *metadata = malloc(sizeof(*metadata)); + ParserNodeBracketMetadata *metadata = a404m_malloc(sizeof(*metadata)); metadata->operand = after; metadata->params = parserNode->metadata; parserNode->metadata = metadata; diff --git a/src/compiler/parser.h b/src/compiler/parser.h index 0ee59a6..f37715d 100644 --- a/src/compiler/parser.h +++ b/src/compiler/parser.h @@ -4,6 +4,7 @@ #include "utils/type.h" #include #include +#include typedef enum ParserToken { PARSER_TOKEN_ROOT, @@ -178,7 +179,12 @@ void parserNodePrint(const ParserNode *node, int indent); #endif void parserNodeDelete(ParserNode *node); -ParserNode *parserFromPath(const char *filePath); +ParserNode *parserFromPath(const char *filePath +#ifdef PRINT_STATISTICS + , + struct timespec *lexingTime +#endif +); ParserNode *parser(LexerNodeArray lexed); bool parserNodeArray(LexerNode *begin, LexerNode *end, ParserNode *parent); @@ -240,4 +246,4 @@ bool isExpression(ParserNode *node); bool isType(ParserNode *node); bool isValue(ParserNode *node); -char escapeChar(char *begin,char *end, bool *success); +char escapeChar(char *begin, char *end, bool *success); -- cgit v1.2.3