Move package data

This commit is contained in:
Krzosa Karol
2024-04-14 09:35:43 +02:00
parent 34f83e444f
commit 36f0fac82f
10 changed files with 51 additions and 50 deletions

View File

@@ -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 // that is going to be imported into every other package, for now we use only the current package
For(array_of_to_gen) { 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 *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); LC_DLLAdd(file->afile.fdecl, file->afile.ldecl, ast);
} }

View File

@@ -78,11 +78,11 @@ bool sandbox() {
// //
LC_AST *package = LC_GetPackageByName(name); LC_AST *package = LC_GetPackageByName(name);
LC_AST *dynamic_array_file = NULL; 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); S8_String path = S8_MakeFromChar((char *)it->afile.x->file);
if (S8_EndsWith(path, "dynamic_array.lc")) { if (S8_EndsWith(path, "dynamic_array.lc")) {
dynamic_array_file = it; 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; break;
} }
} }
@@ -94,7 +94,7 @@ bool sandbox() {
walker.user_data = (void *)⁢ walker.user_data = (void *)⁢
LC_WalkAST(&walker, new_array_file); 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); LC_OrderAndResolveTopLevelDecls(name);

View File

@@ -70,7 +70,7 @@ void VerifyCopy_Walk(LC_ASTWalker *ctx, LC_AST *n) {
void VerifyASTCopy(LC_ASTRefList packages) { void VerifyASTCopy(LC_ASTRefList packages) {
for (LC_ASTRef *it = packages.first; it; it = it->next) { 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_TempArena c = LC_BeginTemp(L->arena);
LC_AST *copy = LC_CopyAST(L->arena, file); LC_AST *copy = LC_CopyAST(L->arena, file);

View File

@@ -57,7 +57,7 @@ LC_FUNCTION void LC_WalkAST(LC_ASTWalker *ctx, LC_AST *n) {
case LC_ASTKind_StmtContinue: break; case LC_ASTKind_StmtContinue: break;
case LC_ASTKind_Package: { 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; } break;
case LC_ASTKind_File: { case LC_ASTKind_File: {

View File

@@ -606,7 +606,7 @@ LC_FUNCTION void LC_GenCVarFDecl(LC_Decl *decl) {
LC_FUNCTION void LC_GenCHeader(LC_AST *package) { LC_FUNCTION void LC_GenCHeader(LC_AST *package) {
// C notes // C notes
LC_ASTFor(file, package->apackage.ext->ffile) { LC_ASTFor(file, package->apackage.ffile) {
LC_ASTFor(it, file->afile.fdecl) { LC_ASTFor(it, file->afile.fdecl) {
if (it->kind != LC_ASTKind_DeclNote) continue; if (it->kind != LC_ASTKind_DeclNote) continue;

View File

@@ -373,13 +373,15 @@ struct LC_ASTFile {
LC_Token *doc_comment; LC_Token *doc_comment;
}; };
// To minimize package ast size, we want all nodes to be equal in size, // This extension thing is only to minimize package ast size!
// a lot of things are easier then and we can loop through all asts for free etc. // 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; typedef struct LC_ASTPackageExt LC_ASTPackageExt;
struct LC_ASTPackageExt { struct LC_ASTPackageExt {
LC_StringList injected_filepaths; // to sidestep regular file finding, implement single file packages etc. LC_StringList injected_filepaths; // to sidestep regular file finding, implement single file packages etc.
LC_AST *ffile; LC_Token *doc_comment;
LC_AST *lfile; LC_DeclState state;
LC_String path;
// These are resolved later: // These are resolved later:
// @todo: add foreign name? // @todo: add foreign name?
@@ -389,10 +391,9 @@ struct LC_ASTPackageExt {
}; };
struct LC_ASTPackage { struct LC_ASTPackage {
LC_DeclState state; LC_AST *ffile;
LC_AST *lfile;
LC_Intern name; LC_Intern name;
LC_String path;
LC_Token *doc_comment;
LC_ASTPackageExt *ext; LC_ASTPackageExt *ext;
}; };

View File

@@ -72,7 +72,7 @@ LC_FUNCTION bool LC_PackageNameDuplicate(LC_Intern name) {
LC_FUNCTION void LC_AddPackageToList(LC_AST *n) { LC_FUNCTION void LC_AddPackageToList(LC_AST *n) {
LC_Intern name = n->apackage.name; LC_Intern name = n->apackage.name;
if (LC_PackageNameDuplicate(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; L->errors += 1;
return; return;
} }
@@ -89,7 +89,7 @@ LC_FUNCTION LC_AST *LC_RegisterPackage(LC_String path) {
LC_AST *n = LC_CreateAST(NULL, LC_ASTKind_Package); LC_AST *n = LC_CreateAST(NULL, LC_ASTKind_Package);
n->apackage.ext = LC_PushStruct(L->arena, LC_ASTPackageExt); n->apackage.ext = LC_PushStruct(L->arena, LC_ASTPackageExt);
n->apackage.name = LC_MakePackageNameFromPath(path); n->apackage.name = LC_MakePackageNameFromPath(path);
n->apackage.path = path; n->apackage.ext->path = path;
LC_AddPackageToList(n); LC_AddPackageToList(n);
return 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_ASSERT(package, package->kind == LC_ASTKind_Package);
LC_ASTRefList refs = {0}; LC_ASTRefList refs = {0};
LC_ASTFor(file, package->apackage.ext->ffile) { LC_ASTFor(file, package->apackage.ffile) {
LC_ASTFor(import, file->afile.fimport) { LC_ASTFor(import, file->afile.fimport) {
LC_AST *found = LC_FindImportInRefList(&refs, import->gimport.path); LC_AST *found = LC_FindImportInRefList(&refs, import->gimport.path);
if (found) { 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); LC_String path = LC_Format(L->arena, "%.*s/%s", LC_Expand(s), (char *)name);
if (LC_IsDir(L->arena, path)) { if (LC_IsDir(L->arena, path)) {
if (result != NULL) { 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; L->errors += 1;
break; break;
} }
@@ -184,10 +184,10 @@ LC_FUNCTION void LC_ParsePackage(LC_AST *n) {
LC_StringList files = n->apackage.ext->injected_filepaths; LC_StringList files = n->apackage.ext->injected_filepaths;
if (files.node_count == 0) { 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) { if (files.first == NULL) {
LC_SendErrorMessagef(NULL, NULL, "no valid .lc files in '%.*s'", LC_Expand(n->apackage.path)); LC_SendErrorMessagef(NULL, NULL, "no valid .lc files in '%.*s'", LC_Expand(n->apackage.ext->path));
n->apackage.state = LC_DeclState_Error; n->apackage.ext->state = LC_DeclState_Error;
L->errors += 1; L->errors += 1;
return; 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); LC_AST *ast_file = LC_ParseFile(n, file.path.str, file.content.str, file.line);
if (!ast_file) { if (!ast_file) {
n->apackage.state = LC_DeclState_Error; n->apackage.ext->state = LC_DeclState_Error;
return; return;
} }
} }
@@ -224,13 +224,13 @@ LC_FUNCTION void LC_ParsePackagesUsingRegistry(LC_Intern name) {
LC_FUNCTION void LC_BuildIfPass(void) { LC_FUNCTION void LC_BuildIfPass(void) {
LC_ASTFor(n, L->fpackage) { 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 *next = fit->next;
LC_AST *build_if = LC_HasNote(fit, L->ibuild_if); LC_AST *build_if = LC_HasNote(fit, L->ibuild_if);
if (build_if) { if (build_if) {
if (!LC_ResolveBuildIf(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); LC_AddASTToRefList(&L->discarded, fit);
fit = next; fit = next;
continue; continue;
@@ -269,23 +269,23 @@ LC_FUNCTION void LC_AddOrderedPackageToRefList(LC_AST *n) {
// an aggregate. It's just a number. // an aggregate. It's just a number.
LC_FUNCTION LC_AST *LC_OrderPackagesAndBasicResolve(LC_AST *pos, LC_Intern name) { LC_FUNCTION LC_AST *LC_OrderPackagesAndBasicResolve(LC_AST *pos, LC_Intern name) {
LC_AST *n = LC_GetPackageByName(name); LC_AST *n = LC_GetPackageByName(name);
if (n->apackage.state == LC_DeclState_Error) { if (n->apackage.ext->state == LC_DeclState_Error) {
return NULL; 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 // This function can be called multiple times, I assume user might
// want to use type information to generate something. Pattern: // want to use type information to generate something. Pattern:
// typecheck -> generate -> typecheck is expected! // typecheck -> generate -> typecheck is expected!
LC_PackageDecls(n); LC_PackageDecls(n);
return 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); LC_ReportASTError(pos, "circular import '%s'", name);
n->apackage.state = LC_DeclState_Error; n->apackage.ext->state = LC_DeclState_Error;
return NULL; return NULL;
} }
LC_ASSERT(pos, n->apackage.state == LC_DeclState_Unresolved); LC_ASSERT(pos, n->apackage.ext->state == LC_DeclState_Unresolved);
n->apackage.state = LC_DeclState_Resolving; n->apackage.ext->state = LC_DeclState_Resolving;
LC_Operand op = LC_ImportPackage(NULL, n, L->builtin_package); LC_Operand op = LC_ImportPackage(NULL, n, L->builtin_package);
LC_ASSERT(pos, !LC_IsError(op)); 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) { for (LC_ASTRef *it = refs.first; it; it = it->next) {
LC_AST *import = LC_OrderPackagesAndBasicResolve(it->ast, it->ast->gimport.path); LC_AST *import = LC_OrderPackagesAndBasicResolve(it->ast, it->ast->gimport.path);
if (!import) { if (!import) {
n->apackage.state = LC_DeclState_Error; n->apackage.ext->state = LC_DeclState_Error;
wrong_import += 1; wrong_import += 1;
continue; continue;
} }
LC_Operand op = LC_ImportPackage(it->ast, n, import); LC_Operand op = LC_ImportPackage(it->ast, n, import);
if (LC_IsError(op)) { if (LC_IsError(op)) {
n->apackage.state = LC_DeclState_Error; n->apackage.ext->state = LC_DeclState_Error;
wrong_import += 1; wrong_import += 1;
continue; continue;
} }
@@ -316,7 +316,7 @@ LC_FUNCTION LC_AST *LC_OrderPackagesAndBasicResolve(LC_AST *pos, LC_Intern name)
LC_PackageDecls(n); LC_PackageDecls(n);
LC_AddOrderedPackageToRefList(n); LC_AddOrderedPackageToRefList(n);
n->apackage.state = LC_DeclState_Resolved; n->apackage.ext->state = LC_DeclState_Resolved;
return n; 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 // 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) { for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) {
LC_AST *package = it->ast; 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); LC_ResolveIncompleteTypes(package);
} }
} }
@@ -339,7 +339,7 @@ LC_FUNCTION void LC_ResolveAllProcBodies(void) {
// the list. // the list.
for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) { for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) {
LC_AST *package = it->ast; 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); LC_ResolveProcBodies(package);
} }
} }
@@ -390,7 +390,7 @@ LC_FUNCTION void LC_AddSingleFilePackage(LC_Intern name, LC_String path) {
LC_AST *n = LC_CreateAST(0, LC_ASTKind_Package); LC_AST *n = LC_CreateAST(0, LC_ASTKind_Package);
n->apackage.ext = LC_PushStruct(L->arena, LC_ASTPackageExt); n->apackage.ext = LC_PushStruct(L->arena, LC_ASTPackageExt);
n->apackage.name = name; n->apackage.name = name;
n->apackage.path = path; n->apackage.ext->path = path;
LC_AddNode(L->arena, &n->apackage.ext->injected_filepaths, path); LC_AddNode(L->arena, &n->apackage.ext->injected_filepaths, path);
LC_AddPackageToList(n); LC_AddPackageToList(n);
} }

View File

@@ -955,7 +955,7 @@ LC_FUNCTION LC_AST *LC_ParseImport(void) {
LC_FUNCTION void LC_AddFileToPackage(LC_AST *pkg, LC_AST *f) { LC_FUNCTION void LC_AddFileToPackage(LC_AST *pkg, LC_AST *f) {
f->afile.package = pkg; 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) { 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) {
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); 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.doc_comment = package_doc_comment; package->apackage.ext->doc_comment = package_doc_comment;
LC_AddFileToPackage(package, n); LC_AddFileToPackage(package, n);
} }

View File

@@ -83,7 +83,7 @@ LC_FUNCTION char *LC_GenLCAggName(LC_Type *t) {
LC_FUNCTION void LC_GenLCNode(LC_AST *n) { LC_FUNCTION void LC_GenLCNode(LC_AST *n) {
switch (n->kind) { switch (n->kind) {
case LC_ASTKind_Package: { case LC_ASTKind_Package: {
LC_ASTFor(it, n->apackage.ext->ffile) { LC_ASTFor(it, n->apackage.ffile) {
LC_GenLCNode(it); LC_GenLCNode(it);
} }
} break; } break;

View File

@@ -35,7 +35,7 @@ LC_FUNCTION void LC_PackageDecls(LC_AST *package) {
LC_PUSH_PACKAGE(package); LC_PUSH_PACKAGE(package);
// Register top level declarations // Register top level declarations
LC_ASTFor(file, package->apackage.ext->ffile) { LC_ASTFor(file, package->apackage.ffile) {
LC_ASTFor(import, file->afile.fimport) { LC_ASTFor(import, file->afile.fimport) {
if (import->gimport.resolved == false) LC_ReportASTError(import, "internal compiler error: unresolved import got into typechecking stage"); 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 // Resolve declarations by name
LC_ASTFor(file, package->apackage.ext->ffile) { LC_ASTFor(file, package->apackage.ffile) {
LC_ResolveDeclsFromFile(file); LC_ResolveDeclsFromFile(file);
} }
@@ -53,7 +53,7 @@ LC_FUNCTION void LC_PackageDecls(LC_AST *package) {
LC_FUNCTION void LC_ResolveProcBodies(LC_AST *package) { LC_FUNCTION void LC_ResolveProcBodies(LC_AST *package) {
LC_PUSH_PACKAGE(package); LC_PUSH_PACKAGE(package);
LC_ASTFor(file, package->apackage.ext->ffile) { LC_ASTFor(file, package->apackage.ffile) {
LC_ASTFor(n, file->afile.fdecl) { LC_ASTFor(n, file->afile.fdecl) {
if (n->kind == LC_ASTKind_DeclNote) continue; 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_FUNCTION void LC_ResolveIncompleteTypes(LC_AST *package) {
LC_PUSH_PACKAGE(package); LC_PUSH_PACKAGE(package);
LC_ASTFor(file, package->apackage.ext->ffile) { LC_ASTFor(file, package->apackage.ffile) {
LC_ASTFor(n, file->afile.fdecl) { LC_ASTFor(n, file->afile.fdecl) {
if (n->kind == LC_ASTKind_DeclNote) continue; if (n->kind == LC_ASTKind_DeclNote) continue;