diff --git a/examples/add_dynamic_array_macro/build.cpp b/examples/add_dynamic_array_macro/build.cpp index 676ed5b..ceded66 100644 --- a/examples/add_dynamic_array_macro/build.cpp +++ b/examples/add_dynamic_array_macro/build.cpp @@ -154,7 +154,7 @@ bool add_dynamic_array_macro() { // that is going to be imported into every other package, for now we use only the current package For(array_of_to_gen) { LC_AST *ast = LC_ParseDeclf("ArrayOf%s :: struct { data: *%s; len: int; cap: int; }", (char *)it, (char *)it); - LC_AST *file = package->apackage.ext->ffile; + LC_AST *file = package->apackage.ffile; LC_DLLAdd(file->afile.fdecl, file->afile.ldecl, ast); } diff --git a/examples/sandbox/build.cpp b/examples/sandbox/build.cpp index 1889983..f8ed0dd 100644 --- a/examples/sandbox/build.cpp +++ b/examples/sandbox/build.cpp @@ -78,11 +78,11 @@ bool sandbox() { // LC_AST *package = LC_GetPackageByName(name); LC_AST *dynamic_array_file = NULL; - LC_ASTFor(it, package->apackage.ext->ffile) { + LC_ASTFor(it, package->apackage.ffile) { S8_String path = S8_MakeFromChar((char *)it->afile.x->file); if (S8_EndsWith(path, "dynamic_array.lc")) { dynamic_array_file = it; - DLL_QUEUE_REMOVE(package->apackage.ext->ffile, package->apackage.ext->lfile, it); + DLL_QUEUE_REMOVE(package->apackage.ffile, package->apackage.lfile, it); break; } } @@ -94,7 +94,7 @@ bool sandbox() { walker.user_data = (void *)⁢ LC_WalkAST(&walker, new_array_file); - LC_DLLAdd(package->apackage.ext->ffile, package->apackage.ext->lfile, new_array_file); + LC_DLLAdd(package->apackage.ffile, package->apackage.lfile, new_array_file); } LC_OrderAndResolveTopLevelDecls(name); diff --git a/src/build_file/ast_verify.cpp b/src/build_file/ast_verify.cpp index ef26d1d..d267918 100644 --- a/src/build_file/ast_verify.cpp +++ b/src/build_file/ast_verify.cpp @@ -70,7 +70,7 @@ void VerifyCopy_Walk(LC_ASTWalker *ctx, LC_AST *n) { void VerifyASTCopy(LC_ASTRefList packages) { for (LC_ASTRef *it = packages.first; it; it = it->next) { - LC_ASTFor(file, it->ast->apackage.ext->ffile) { + LC_ASTFor(file, it->ast->apackage.ffile) { LC_TempArena c = LC_BeginTemp(L->arena); LC_AST *copy = LC_CopyAST(L->arena, file); diff --git a/src/compiler/ast_walk.c b/src/compiler/ast_walk.c index 8bfc674..54cd909 100644 --- a/src/compiler/ast_walk.c +++ b/src/compiler/ast_walk.c @@ -57,7 +57,7 @@ LC_FUNCTION void LC_WalkAST(LC_ASTWalker *ctx, LC_AST *n) { case LC_ASTKind_StmtContinue: break; case LC_ASTKind_Package: { - LC_ASTFor(it, n->apackage.ext->ffile) LC_WalkAST(ctx, it); + LC_ASTFor(it, n->apackage.ffile) LC_WalkAST(ctx, it); } break; case LC_ASTKind_File: { diff --git a/src/compiler/genc.c b/src/compiler/genc.c index 94ca01e..969cca0 100644 --- a/src/compiler/genc.c +++ b/src/compiler/genc.c @@ -606,7 +606,7 @@ LC_FUNCTION void LC_GenCVarFDecl(LC_Decl *decl) { LC_FUNCTION void LC_GenCHeader(LC_AST *package) { // C notes - LC_ASTFor(file, package->apackage.ext->ffile) { + LC_ASTFor(file, package->apackage.ffile) { LC_ASTFor(it, file->afile.fdecl) { if (it->kind != LC_ASTKind_DeclNote) continue; diff --git a/src/compiler/lib_compiler.h b/src/compiler/lib_compiler.h index cdcab57..66a5572 100644 --- a/src/compiler/lib_compiler.h +++ b/src/compiler/lib_compiler.h @@ -373,13 +373,15 @@ struct LC_ASTFile { LC_Token *doc_comment; }; -// To minimize package ast size, we want all nodes to be equal in size, -// a lot of things are easier then and we can loop through all asts for free etc. +// This extension thing is only to minimize package ast size! +// I want all nodes to be equal in size, this way a lot of things are +// easier. You can loop through all the ast nodes easily and stuff like that. typedef struct LC_ASTPackageExt LC_ASTPackageExt; struct LC_ASTPackageExt { LC_StringList injected_filepaths; // to sidestep regular file finding, implement single file packages etc. - LC_AST *ffile; - LC_AST *lfile; + LC_Token *doc_comment; + LC_DeclState state; + LC_String path; // These are resolved later: // @todo: add foreign name? @@ -389,10 +391,9 @@ struct LC_ASTPackageExt { }; struct LC_ASTPackage { - LC_DeclState state; + LC_AST *ffile; + LC_AST *lfile; LC_Intern name; - LC_String path; - LC_Token *doc_comment; LC_ASTPackageExt *ext; }; diff --git a/src/compiler/packages.c b/src/compiler/packages.c index 645c5f7..905bc8a 100644 --- a/src/compiler/packages.c +++ b/src/compiler/packages.c @@ -72,7 +72,7 @@ LC_FUNCTION bool LC_PackageNameDuplicate(LC_Intern name) { LC_FUNCTION void LC_AddPackageToList(LC_AST *n) { LC_Intern name = n->apackage.name; if (LC_PackageNameDuplicate(name)) { - LC_SendErrorMessagef(NULL, NULL, "found 2 packages with the same name: '%s' / '%.*s'\n", name, LC_Expand(n->apackage.path)); + LC_SendErrorMessagef(NULL, NULL, "found 2 packages with the same name: '%s' / '%.*s'\n", name, LC_Expand(n->apackage.ext->path)); L->errors += 1; return; } @@ -86,10 +86,10 @@ LC_FUNCTION void LC_AddPackageToList(LC_AST *n) { LC_FUNCTION LC_AST *LC_RegisterPackage(LC_String path) { LC_ASSERT(NULL, path.len != 0); - LC_AST *n = LC_CreateAST(NULL, LC_ASTKind_Package); - n->apackage.ext = LC_PushStruct(L->arena, LC_ASTPackageExt); - n->apackage.name = LC_MakePackageNameFromPath(path); - n->apackage.path = path; + LC_AST *n = LC_CreateAST(NULL, LC_ASTKind_Package); + n->apackage.ext = LC_PushStruct(L->arena, LC_ASTPackageExt); + n->apackage.name = LC_MakePackageNameFromPath(path); + n->apackage.ext->path = path; LC_AddPackageToList(n); return n; } @@ -111,7 +111,7 @@ LC_FUNCTION LC_ASTRefList LC_GetPackageImports(LC_AST *package) { LC_ASSERT(package, package->kind == LC_ASTKind_Package); LC_ASTRefList refs = {0}; - LC_ASTFor(file, package->apackage.ext->ffile) { + LC_ASTFor(file, package->apackage.ffile) { LC_ASTFor(import, file->afile.fimport) { LC_AST *found = LC_FindImportInRefList(&refs, import->gimport.path); if (found) { @@ -145,7 +145,7 @@ LC_FUNCTION LC_AST *LC_GetPackageByName(LC_Intern name) { LC_String path = LC_Format(L->arena, "%.*s/%s", LC_Expand(s), (char *)name); if (LC_IsDir(L->arena, path)) { if (result != NULL) { - LC_SendErrorMessagef(NULL, NULL, "found 2 directories with the same name: '%.*s', '%.*s'\n", LC_Expand(path), LC_Expand(result->apackage.path)); + LC_SendErrorMessagef(NULL, NULL, "found 2 directories with the same name: '%.*s', '%.*s'\n", LC_Expand(path), LC_Expand(result->apackage.ext->path)); L->errors += 1; break; } @@ -184,10 +184,10 @@ LC_FUNCTION void LC_ParsePackage(LC_AST *n) { LC_StringList files = n->apackage.ext->injected_filepaths; if (files.node_count == 0) { - files = LC_ListFilesInPackage(L->arena, n->apackage.path); + files = LC_ListFilesInPackage(L->arena, n->apackage.ext->path); if (files.first == NULL) { - LC_SendErrorMessagef(NULL, NULL, "no valid .lc files in '%.*s'", LC_Expand(n->apackage.path)); - n->apackage.state = LC_DeclState_Error; + LC_SendErrorMessagef(NULL, NULL, "no valid .lc files in '%.*s'", LC_Expand(n->apackage.ext->path)); + n->apackage.ext->state = LC_DeclState_Error; L->errors += 1; return; } @@ -199,7 +199,7 @@ LC_FUNCTION void LC_ParsePackage(LC_AST *n) { LC_AST *ast_file = LC_ParseFile(n, file.path.str, file.content.str, file.line); if (!ast_file) { - n->apackage.state = LC_DeclState_Error; + n->apackage.ext->state = LC_DeclState_Error; return; } } @@ -224,13 +224,13 @@ LC_FUNCTION void LC_ParsePackagesUsingRegistry(LC_Intern name) { LC_FUNCTION void LC_BuildIfPass(void) { LC_ASTFor(n, L->fpackage) { - for (LC_AST *fit = n->apackage.ext->ffile; fit;) { + for (LC_AST *fit = n->apackage.ffile; fit;) { LC_AST *next = fit->next; LC_AST *build_if = LC_HasNote(fit, L->ibuild_if); if (build_if) { if (!LC_ResolveBuildIf(build_if)) { - LC_DLLRemove(n->apackage.ext->ffile, n->apackage.ext->lfile, fit); + LC_DLLRemove(n->apackage.ffile, n->apackage.lfile, fit); LC_AddASTToRefList(&L->discarded, fit); fit = next; continue; @@ -269,23 +269,23 @@ LC_FUNCTION void LC_AddOrderedPackageToRefList(LC_AST *n) { // an aggregate. It's just a number. LC_FUNCTION LC_AST *LC_OrderPackagesAndBasicResolve(LC_AST *pos, LC_Intern name) { LC_AST *n = LC_GetPackageByName(name); - if (n->apackage.state == LC_DeclState_Error) { + if (n->apackage.ext->state == LC_DeclState_Error) { return NULL; } - if (n->apackage.state == LC_DeclState_Resolved) { + if (n->apackage.ext->state == LC_DeclState_Resolved) { // This function can be called multiple times, I assume user might // want to use type information to generate something. Pattern: // typecheck -> generate -> typecheck is expected! LC_PackageDecls(n); return n; } - if (n->apackage.state == LC_DeclState_Resolving) { + if (n->apackage.ext->state == LC_DeclState_Resolving) { LC_ReportASTError(pos, "circular import '%s'", name); - n->apackage.state = LC_DeclState_Error; + n->apackage.ext->state = LC_DeclState_Error; return NULL; } - LC_ASSERT(pos, n->apackage.state == LC_DeclState_Unresolved); - n->apackage.state = LC_DeclState_Resolving; + LC_ASSERT(pos, n->apackage.ext->state == LC_DeclState_Unresolved); + n->apackage.ext->state = LC_DeclState_Resolving; LC_Operand op = LC_ImportPackage(NULL, n, L->builtin_package); LC_ASSERT(pos, !LC_IsError(op)); @@ -299,14 +299,14 @@ LC_FUNCTION LC_AST *LC_OrderPackagesAndBasicResolve(LC_AST *pos, LC_Intern name) for (LC_ASTRef *it = refs.first; it; it = it->next) { LC_AST *import = LC_OrderPackagesAndBasicResolve(it->ast, it->ast->gimport.path); if (!import) { - n->apackage.state = LC_DeclState_Error; + n->apackage.ext->state = LC_DeclState_Error; wrong_import += 1; continue; } LC_Operand op = LC_ImportPackage(it->ast, n, import); if (LC_IsError(op)) { - n->apackage.state = LC_DeclState_Error; + n->apackage.ext->state = LC_DeclState_Error; wrong_import += 1; continue; } @@ -316,7 +316,7 @@ LC_FUNCTION LC_AST *LC_OrderPackagesAndBasicResolve(LC_AST *pos, LC_Intern name) LC_PackageDecls(n); LC_AddOrderedPackageToRefList(n); - n->apackage.state = LC_DeclState_Resolved; + n->apackage.ext->state = LC_DeclState_Resolved; return n; } @@ -329,7 +329,7 @@ LC_FUNCTION void LC_OrderAndResolveTopLevelDecls(LC_Intern name) { // it should still be fine to go forward with this and also proc body analysis for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) { LC_AST *package = it->ast; - LC_ASSERT(package, package->apackage.state == LC_DeclState_Resolved); + LC_ASSERT(package, package->apackage.ext->state == LC_DeclState_Resolved); LC_ResolveIncompleteTypes(package); } } @@ -339,7 +339,7 @@ LC_FUNCTION void LC_ResolveAllProcBodies(void) { // the list. for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) { LC_AST *package = it->ast; - LC_ASSERT(package, package->apackage.state == LC_DeclState_Resolved); + LC_ASSERT(package, package->apackage.ext->state == LC_DeclState_Resolved); LC_ResolveProcBodies(package); } } @@ -387,10 +387,10 @@ LC_FUNCTION LC_String LC_GenerateUnityBuild(LC_ASTRefList packages) { } LC_FUNCTION void LC_AddSingleFilePackage(LC_Intern name, LC_String path) { - LC_AST *n = LC_CreateAST(0, LC_ASTKind_Package); - n->apackage.ext = LC_PushStruct(L->arena, LC_ASTPackageExt); - n->apackage.name = name; - n->apackage.path = path; + LC_AST *n = LC_CreateAST(0, LC_ASTKind_Package); + n->apackage.ext = LC_PushStruct(L->arena, LC_ASTPackageExt); + n->apackage.name = name; + n->apackage.ext->path = path; LC_AddNode(L->arena, &n->apackage.ext->injected_filepaths, path); LC_AddPackageToList(n); } \ No newline at end of file diff --git a/src/compiler/parse.c b/src/compiler/parse.c index cd4fc2c..f1a5f7d 100644 --- a/src/compiler/parse.c +++ b/src/compiler/parse.c @@ -955,7 +955,7 @@ LC_FUNCTION LC_AST *LC_ParseImport(void) { LC_FUNCTION void LC_AddFileToPackage(LC_AST *pkg, LC_AST *f) { f->afile.package = pkg; - LC_DLLAdd(pkg->apackage.ext->ffile, pkg->apackage.ext->lfile, f); + LC_DLLAdd(pkg->apackage.ffile, pkg->apackage.lfile, f); } LC_FUNCTION LC_AST *LC_ParseFileEx(LC_AST *package) { @@ -993,8 +993,8 @@ LC_FUNCTION LC_AST *LC_ParseFileEx(LC_AST *package) { } if (package) { - if (package->apackage.doc_comment) LC_ReportParseError(package_doc_comment, "there are more then 1 package doc comments in %s package", (char *)package->apackage.name); - package->apackage.doc_comment = package_doc_comment; + if (package->apackage.ext->doc_comment) LC_ReportParseError(package_doc_comment, "there are more then 1 package doc comments in %s package", (char *)package->apackage.name); + package->apackage.ext->doc_comment = package_doc_comment; LC_AddFileToPackage(package, n); } diff --git a/src/compiler/printer.c b/src/compiler/printer.c index d933281..457d23c 100644 --- a/src/compiler/printer.c +++ b/src/compiler/printer.c @@ -83,7 +83,7 @@ LC_FUNCTION char *LC_GenLCAggName(LC_Type *t) { LC_FUNCTION void LC_GenLCNode(LC_AST *n) { switch (n->kind) { case LC_ASTKind_Package: { - LC_ASTFor(it, n->apackage.ext->ffile) { + LC_ASTFor(it, n->apackage.ffile) { LC_GenLCNode(it); } } break; diff --git a/src/compiler/resolve.c b/src/compiler/resolve.c index f396887..24e76de 100644 --- a/src/compiler/resolve.c +++ b/src/compiler/resolve.c @@ -35,7 +35,7 @@ LC_FUNCTION void LC_PackageDecls(LC_AST *package) { LC_PUSH_PACKAGE(package); // Register top level declarations - LC_ASTFor(file, package->apackage.ext->ffile) { + LC_ASTFor(file, package->apackage.ffile) { LC_ASTFor(import, file->afile.fimport) { if (import->gimport.resolved == false) LC_ReportASTError(import, "internal compiler error: unresolved import got into typechecking stage"); } @@ -43,7 +43,7 @@ LC_FUNCTION void LC_PackageDecls(LC_AST *package) { } // Resolve declarations by name - LC_ASTFor(file, package->apackage.ext->ffile) { + LC_ASTFor(file, package->apackage.ffile) { LC_ResolveDeclsFromFile(file); } @@ -53,7 +53,7 @@ LC_FUNCTION void LC_PackageDecls(LC_AST *package) { LC_FUNCTION void LC_ResolveProcBodies(LC_AST *package) { LC_PUSH_PACKAGE(package); - LC_ASTFor(file, package->apackage.ext->ffile) { + LC_ASTFor(file, package->apackage.ffile) { LC_ASTFor(n, file->afile.fdecl) { if (n->kind == LC_ASTKind_DeclNote) continue; @@ -71,7 +71,7 @@ LC_FUNCTION void LC_ResolveProcBodies(LC_AST *package) { LC_FUNCTION void LC_ResolveIncompleteTypes(LC_AST *package) { LC_PUSH_PACKAGE(package); - LC_ASTFor(file, package->apackage.ext->ffile) { + LC_ASTFor(file, package->apackage.ffile) { LC_ASTFor(n, file->afile.fdecl) { if (n->kind == LC_ASTKind_DeclNote) continue;