aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-06-06 02:54:20 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-06-06 02:54:20 +0330
commitc64d8217a598364fcb48ed965edc7a138f2f6750 (patch)
tree61c550306ad8bb86199d328fe0b2a8d772577993 /src
parentc506c3458a07485169cc38ce27d5cf21601136c2 (diff)
cleaning import
Diffstat (limited to 'src')
-rw-r--r--src/compiler/ast-tree.c226
-rw-r--r--src/compiler/ast-tree.h15
2 files changed, 102 insertions, 139 deletions
diff --git a/src/compiler/ast-tree.c b/src/compiler/ast-tree.c
index 2b79e14..65e7a4d 100644
--- a/src/compiler/ast-tree.c
+++ b/src/compiler/ast-tree.c
@@ -1892,76 +1892,13 @@ AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots
for (size_t i = 0; i < root->trees.size; ++i) {
AstTree *tree = root->trees.data[i];
- if (tree->token == AST_TREE_TOKEN_FUNCTION_CALL) {
- AstTreeFunctionCall *tree_metadata = tree->metadata;
- AstTree *operand = tree_metadata->function;
- if (operand->token == AST_TREE_TOKEN_BUILTIN_IMPORT) {
- AstTreeSetTypesHelper helper = {
- .lookingType = NULL,
- .dependencies.data = NULL,
- .dependencies.size = 0,
- .variables = root->variables,
- .root = root,
- .loops = NULL,
- .loops_size = 0,
- .scope = NULL,
- .isInScope = false,
- };
- if (!setAllTypes(tree, helper, NULL, NULL)) {
- goto RETURN_ERROR;
- }
- AstTree *parameter = tree_metadata->parameters[0].value;
- if (!isConst(parameter)) {
- printError(parameter->str_begin, parameter->str_end,
- "Is not constant");
- goto RETURN_ERROR;
- }
- parameter = getValue(parameter, true, helper.scope);
- if (parameter == NULL) {
- goto RETURN_ERROR;
- }
-
- AstTreeBracket *type_metadata = a404m_malloc(sizeof(*type_metadata));
- type_metadata->operand = &AST_TREE_U8_TYPE;
-
- type_metadata->parameters.size = 0;
- type_metadata->parameters.data =
- a404m_malloc(0 * sizeof(*type_metadata->parameters.data));
-
- AstTree *type = newAstTree(AST_TREE_TOKEN_TYPE_ARRAY, type_metadata,
- &AST_TREE_TYPE_TYPE, NULL, NULL);
-
- if (!typeIsEqual(type, parameter->type, helper.scope)) {
- printError(parameter->str_begin, parameter->str_end,
- "Type mismatch (must be a []u8 aka string)");
- goto RETURN_ERROR;
- }
-
- char *str = u8ArrayToCString(parameter);
- astTreeDelete(parameter);
-
- const size_t imports_size =
- a404m_malloc_usable_size(root->imports) / sizeof(*root->imports);
- if (imports_size == root->imports_size) {
- root->imports = a404m_realloc(root->imports,
- (imports_size + imports_size / 2 + 1) *
- sizeof(*root->imports));
- }
-
- AstTreeRoot *import = getAstTreeRoot(joinToPathOf(filePath, str), roots,
- lexingTime, parsingTime);
- free(str);
-
- if (import == NULL) {
- goto RETURN_ERROR;
- }
-
- root->imports[root->imports_size].root = import;
- root->imports[root->imports_size].visible = true;
- root->imports_size += 1;
-
- astTreeDelete(type);
- }
+ if (!astTreeDoImport(roots, root, tree, NULL
+#ifdef PRINT_STATISTICS
+ ,
+ lexingTime, parsingTime
+#endif
+ )) {
+ goto RETURN_ERROR;
}
}
@@ -1971,95 +1908,112 @@ AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots
continue;
}
AstTree *tree = variable->value;
- if (tree->token == AST_TREE_TOKEN_FUNCTION_CALL) {
- AstTreeFunctionCall *tree_metadata = tree->metadata;
- AstTree *operand = tree_metadata->function;
- if (operand->token == AST_TREE_TOKEN_BUILTIN_IMPORT) {
- AstTreeSetTypesHelper helper = {
- .lookingType = NULL,
- .dependencies.data = NULL,
- .dependencies.size = 0,
- .variables = root->variables,
- .root = root,
- .loops = NULL,
- .loops_size = 0,
- .scope = NULL,
- .isInScope = false,
- };
- if (!setAllTypes(tree, helper, NULL, NULL)) {
- goto RETURN_ERROR;
- }
- AstTree *parameter = tree_metadata->parameters[0].value;
- if (!isConst(parameter)) {
- printError(parameter->str_begin, parameter->str_end,
- "Is not constant");
- goto RETURN_ERROR;
- }
- parameter = getValue(parameter, true, helper.scope);
- if (parameter == NULL) {
- goto RETURN_ERROR;
- }
+ if (!astTreeDoImport(roots, root, tree, variable
+#ifdef PRINT_STATISTICS
+ ,
+ lexingTime, parsingTime
+#endif
+ )) {
+ goto RETURN_ERROR;
+ }
+ }
- AstTreeBracket *type_metadata = a404m_malloc(sizeof(*type_metadata));
- type_metadata->operand = &AST_TREE_U8_TYPE;
+ return root;
- type_metadata->parameters.size = 0;
- type_metadata->parameters.data =
- a404m_malloc(0 * sizeof(*type_metadata->parameters.data));
+RETURN_ERROR:
+ return NULL;
+}
- AstTree *type = newAstTree(AST_TREE_TOKEN_TYPE_ARRAY, type_metadata,
- &AST_TREE_TYPE_TYPE, NULL, NULL);
+bool astTreeDoImport(AstTreeRoots *roots, AstTreeRoot *root, AstTree *tree,
+ AstTreeVariable *variable
+#ifdef PRINT_STATISTICS
+ ,
+ Time *lexingTime, Time *parsingTime
+#endif
+) {
+ if (tree->token == AST_TREE_TOKEN_FUNCTION_CALL) {
+ AstTreeFunctionCall *tree_metadata = tree->metadata;
+ AstTree *operand = tree_metadata->function;
+ if (operand->token == AST_TREE_TOKEN_BUILTIN_IMPORT) {
+ AstTreeSetTypesHelper helper = {
+ .lookingType = NULL,
+ .dependencies.data = NULL,
+ .dependencies.size = 0,
+ .variables = root->variables,
+ .root = root,
+ .loops = NULL,
+ .loops_size = 0,
+ .scope = NULL,
+ .isInScope = false,
+ };
+ if (!setAllTypes(tree, helper, NULL, NULL)) {
+ return false;
+ }
+ AstTree *parameter = tree_metadata->parameters[0].value;
+ if (!isConst(parameter)) {
+ printError(parameter->str_begin, parameter->str_end, "Is not constant");
+ return false;
+ }
+ parameter = getValue(parameter, true, helper.scope);
+ if (parameter == NULL) {
+ return false;
+ }
- if (!typeIsEqual(type, parameter->type, helper.scope)) {
- printError(parameter->str_begin, parameter->str_end,
- "Type mismatch (must be a []u8 aka string)");
- astTreeDelete(type);
- goto RETURN_ERROR;
- }
+ AstTree *type = makeStringType();
+ if (!typeIsEqual(type, parameter->type, helper.scope)) {
+ printError(parameter->str_begin, parameter->str_end,
+ "Type mismatch (must be a []u8 aka string)");
astTreeDelete(type);
+ return false;
+ }
+ astTreeDelete(type);
- char *str = u8ArrayToCString(parameter);
- astTreeDelete(parameter);
+ char *str = u8ArrayToCString(parameter);
+ astTreeDelete(parameter);
- const size_t imports_size =
- a404m_malloc_usable_size(root->imports) / sizeof(*root->imports);
- if (imports_size == root->imports_size) {
- root->imports = a404m_realloc(root->imports,
- (imports_size + imports_size / 2 + 1) *
- sizeof(*root->imports));
- }
+ const size_t imports_size =
+ a404m_malloc_usable_size(root->imports) / sizeof(*root->imports);
+ if (imports_size == root->imports_size) {
+ root->imports =
+ a404m_realloc(root->imports, (imports_size + imports_size / 2 + 1) *
+ sizeof(*root->imports));
+ }
- AstTreeRoot *import = getAstTreeRoot(joinToPathOf(filePath, str), roots,
- lexingTime, parsingTime);
- free(str);
+ AstTreeRoot *import =
+ getAstTreeRoot(joinToPathOf(root->filePath, str), roots
+#ifdef PRINT_STATISTICS
+ ,
+ lexingTime, parsingTime
+#endif
+ );
+ free(str);
- if (import == NULL) {
- goto RETURN_ERROR;
- }
+ if (import == NULL) {
+ return false;
+ }
+ root->imports[root->imports_size].root = import;
+
+ if (variable != NULL) {
+ root->imports[root->imports_size].visible = false;
AstTreeNamespace *value_metadata =
a404m_malloc(sizeof(*value_metadata));
value_metadata->importedIndex = root->imports_size;
- root->imports[root->imports_size].root = import;
- root->imports[root->imports_size].visible = false;
- root->imports_size += 1;
-
AstTree *oldValue = variable->value;
+
variable->value =
newAstTree(AST_TREE_TOKEN_VALUE_NAMESPACE, value_metadata,
copyAstTree(&AST_TREE_NAMESPACE_TYPE),
oldValue->str_begin, oldValue->str_end);
-
astTreeDelete(oldValue);
+ } else {
+ root->imports[root->imports_size].visible = true;
}
+ root->imports_size += 1;
}
}
-
- return root;
-
-RETURN_ERROR:
- return NULL;
+ return true;
}
AstTreeRoot *makeAstRoot(const ParserNode *parsedRoot, char *filePath) {
diff --git a/src/compiler/ast-tree.h b/src/compiler/ast-tree.h
index 7a88f28..cf868fd 100644
--- a/src/compiler/ast-tree.h
+++ b/src/compiler/ast-tree.h
@@ -408,6 +408,13 @@ AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots
Time *lexingTime, Time *parsingTime
#endif
);
+bool astTreeDoImport(AstTreeRoots *roots, AstTreeRoot *root, AstTree *tree,
+ AstTreeVariable *variable
+#ifdef PRINT_STATISTICS
+ ,
+ Time *lexingTime, Time *parsingTime
+#endif
+);
AstTreeRoot *makeAstRoot(const ParserNode *parsedRoot, char *filePath);
bool pushVariable(AstTreeVariables *variables, AstTreeVariable *variable);
@@ -459,12 +466,14 @@ AstTree *makeTypeOf(AstTree *value);
AstTree *makeTypeOfFunction(AstTreeFunction *function, const char *str_begin,
const char *str_end);
bool typeIsEqual(AstTree *type0, AstTree *type1, AstTreeScope *scope);
-bool typeIsEqualBack(const AstTree *type0, const AstTree *type1,AstTreeScope *scope);
+bool typeIsEqualBack(const AstTree *type0, const AstTree *type1,
+ AstTreeScope *scope);
AstTree *getValue(AstTree *tree, bool copy, AstTreeScope *scope);
bool isIntType(AstTree *type);
bool isFloatType(AstTree *type);
-bool isEqual(AstTree *left, AstTree *right,AstTreeScope *scope);
-bool isEqualVariable(AstTreeVariable *left, AstTreeVariable *right,AstTreeScope *scope);
+bool isEqual(AstTree *left, AstTree *right, AstTreeScope *scope);
+bool isEqualVariable(AstTreeVariable *left, AstTreeVariable *right,
+ AstTreeScope *scope);
void allOfVariablesWithImport(AstTreeVariables *variables, AstTreeRoot *root,
AstTreeRoots *checkedRoots);