From c64d8217a598364fcb48ed965edc7a138f2f6750 Mon Sep 17 00:00:00 2001 From: A404M Date: Fri, 6 Jun 2025 02:54:20 +0330 Subject: cleaning import --- code/file.felan | 2 +- code/main.felan | 2 + src/compiler/ast-tree.c | 226 +++++++++++++++++++----------------------------- src/compiler/ast-tree.h | 15 +++- 4 files changed, 105 insertions(+), 140 deletions(-) diff --git a/code/file.felan b/code/file.felan index 64661b5..311a5ce 100644 --- a/code/file.felan +++ b/code/file.felan @@ -1,3 +1,3 @@ foo :: () -> void { - putc 'a'; + @putc('a'); }; diff --git a/code/main.felan b/code/main.felan index b570c08..36cddd9 100644 --- a/code/main.felan +++ b/code/main.felan @@ -1,4 +1,5 @@ @import("basic.felan"); +file :: @import("file.felan"); t :: (comptime formatter : string) macro -> string { i := 0; @@ -26,6 +27,7 @@ t :: (comptime formatter : string) macro -> string { }; main :: () -> void { + file.foo(); a := '2'; // @insert("a = '3';a = '5';"); s :: t("hello"); 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); -- cgit v1.2.3