summaryrefslogtreecommitdiff
path: root/src/compiler/ast-tree.c
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-04-25 22:22:40 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-04-25 22:22:40 +0330
commitdc26ae0b913ff49ecbf0f653b4baffa7a37f2303 (patch)
tree155ab12a41b09f3852a7a7cf10f0fabf6ab467f4 /src/compiler/ast-tree.c
parent833fbb09640e46f7e8968a1a724baf25f2a219fc (diff)
add better time printing
Diffstat (limited to 'src/compiler/ast-tree.c')
-rw-r--r--src/compiler/ast-tree.c45
1 files changed, 34 insertions, 11 deletions
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 <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,
@@ -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) {