Improve API

This commit is contained in:
Krzosa Karol
2024-04-14 10:11:17 +02:00
parent 980e9de413
commit 44eb8be1db
22 changed files with 113 additions and 126 deletions

View File

@@ -30,8 +30,8 @@ void TestReadme() {
LC_RegisterPackageDir("../pkgs");
LC_Intern name = LC_ILit("readme_test");
LC_AddSingleFilePackage(name, readme_path);
LC_ResolvePackageByName(name);
// LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls();
LC_ParseAndResolve(name);
// LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass();
}
}
}

View File

@@ -276,16 +276,16 @@ void RunTestFile(TestDesc it) {
LC_AddSingleFilePackage(name, it.absolute_path);
L->first_package = name;
LC_ParsePackagesUsingRegistry(name);
LC_ParsePackagesPass(name);
LC_BuildIfPass();
if (L->errors) {
result = Failed_Parse;
goto end_of_test;
}
LC_OrderAndResolveTopLevelDecls(name);
LC_ResolveAllProcBodies();
LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls();
LC_OrderAndResolveTopLevelPass(name);
LC_ResolveProcBodiesPass();
LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass();
if (L->errors) {
result = Failed_Resolve;
goto end_of_test;
@@ -294,7 +294,7 @@ void RunTestFile(TestDesc it) {
DebugVerifyAST(L->ordered_packages);
OS_MakeDir(it.filename);
code = LC_GenerateUnityBuild(L->ordered_packages);
code = LC_GenerateUnityBuild();
out_path = S8_Format(L->arena, "%.*s/%.*s.c", S8_Expand(it.filename), S8_Expand(it.filename));
OS_WriteFile(out_path, code);
@@ -333,15 +333,15 @@ void RunTestDir(TestDesc it, S8_String package_name) {
LC_Intern first_package = LC_ILit(package_name.str);
LC_RegisterPackageDir(it.absolute_path.str);
LC_RegisterPackageDir("../../pkgs");
LC_ASTRefList packages = LC_ResolvePackageByName(first_package);
LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls();
LC_ParseAndResolve(first_package);
LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass();
if (L->errors != 0) result = Failed_Package;
if (result == OK && T->expected_result == OK) {
DebugVerifyAST(packages);
DebugVerifyAST(L->ordered_packages);
OS_MakeDir(it.filename);
S8_String code = LC_GenerateUnityBuild(packages);
S8_String code = LC_GenerateUnityBuild();
S8_String out_path = S8_Format(L->arena, "%.*s/%.*s.c", S8_Expand(it.filename), S8_Expand(it.filename));
OS_WriteFile(out_path, code);
if (!T->dont_run) result = Compile(it.filename, out_path);
@@ -437,16 +437,16 @@ void RunTests() {
lang->breakpoint_on_error = BreakpointOnError;
LC_LangBegin(lang);
LC_RegisterPackageDir(T->path);
LC_ASTRefList dll_packages = LC_ResolvePackageByName(LC_ILit("dll"));
LC_ASTRefList exe_packages = LC_ResolvePackageByName(LC_ILit("exe"));
result = L->errors >= 1 ? Failed_Package : OK;
LC_ParseAndResolve(LC_ILit("dll"));
LC_ParseAndResolve(LC_ILit("exe"));
result = L->errors >= 1 ? Failed_Package : OK;
if (result == OK) {
DebugVerifyAST(dll_packages);
DebugVerifyAST(L->ordered_packages);
// DebugVerifyAST(exe_packages);
S8_String prev = PushDir("example_ui_and_hot_reloading");
S8_String code = LC_GenerateUnityBuild(exe_packages);
LC_String code = LC_GenerateUnityBuild();
OS_WriteFile("example_ui_and_hot_reloading", code);
OS_SetWorkingDir(prev);
}

View File

@@ -29,7 +29,7 @@ LC_FUNCTION LC_Map LC_CountDeclRefs(LC_Arena *arena) {
return map;
}
LC_FUNCTION void LC_RemoveUnreferencedGlobalDecls(LC_Map *map_of_visits) {
LC_FUNCTION void LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits) {
for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) {
for (LC_Decl *decl = it->ast->apackage.ext->first_ordered; decl;) {
intptr_t ref_count = (intptr_t)LC_MapGetP(map_of_visits, decl);
@@ -43,7 +43,7 @@ LC_FUNCTION void LC_RemoveUnreferencedGlobalDecls(LC_Map *map_of_visits) {
}
}
LC_FUNCTION void LC_ErrorOnUnreferencedLocals(LC_Map *map_of_visits) {
LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits) {
LC_Decl *first = (LC_Decl *)L->decl_arena->memory.data;
for (int i = 0; i < L->decl_count; i += 1) {
LC_Decl *decl = first + i;
@@ -60,13 +60,13 @@ LC_FUNCTION void LC_ErrorOnUnreferencedLocals(LC_Map *map_of_visits) {
}
}
LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls(void) {
LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(void) {
if (L->errors) return;
LC_TempArena check = LC_BeginTemp(L->arena);
LC_Map map = LC_CountDeclRefs(check.arena);
LC_ErrorOnUnreferencedLocals(&map);
LC_RemoveUnreferencedGlobalDecls(&map);
LC_ErrorOnUnreferencedLocalsPass(&map);
LC_RemoveUnreferencedGlobalDeclsPass(&map);
LC_EndTemp(check);
}

View File

@@ -449,6 +449,7 @@ LC_FUNCTION LC_Lex *LC_LexStream(char *file, char *str, int line) {
LC_LexNext(x, t);
if (t->kind == LC_TokenKind_EOF) break;
}
if (L->on_tokens_lexed) L->on_tokens_lexed(x);
return x;
}
@@ -530,6 +531,7 @@ LC_FUNCTION void LC_InternTokens(LC_Lex *x) {
}
}
}
if (L->on_tokens_interned) L->on_tokens_interned(x);
}
#undef LC_IF

View File

@@ -966,7 +966,9 @@ struct LC_Lang {
bool breakpoint_on_error;
bool use_colored_terminal_output;
bool (*on_decl_parsed)(bool discarded, LC_AST *n); // returning 'true' from here indicates that declaration should be discarded
void (*on_tokens_lexed)(LC_Lex *x);
void (*on_tokens_interned)(LC_Lex *x);
void (*on_decl_parsed)(LC_AST *n);
void (*on_expr_parsed)(LC_AST *n);
void (*on_stmt_parsed)(LC_AST *n);
void (*on_typespec_parsed)(LC_AST *n);
@@ -982,7 +984,6 @@ struct LC_Lang {
};
extern LC_THREAD_LOCAL LC_Lang *L;
extern LC_Operand LC_OPNull;
//
// Main @api
@@ -992,23 +993,23 @@ LC_FUNCTION LC_Lang *LC_LangAlloc(void); // This allocates memory for LC_
LC_FUNCTION void LC_LangBegin(LC_Lang *l); // Prepare for compilation: init types, init builtins, set architecture variables stuff like that
LC_FUNCTION void LC_LangEnd(LC_Lang *lang); // Deallocate language memory
LC_FUNCTION void LC_RegisterPackageDir(char *dir); // Add a package search directory
LC_FUNCTION LC_ASTRefList LC_ResolvePackageByName(LC_Intern name); // Fully resolve a package and all it's dependences
LC_FUNCTION LC_String LC_GenerateUnityBuild(LC_ASTRefList packages); // Generate the C program and return as a string
LC_FUNCTION void LC_RegisterPackageDir(char *dir); // Add a package search directory
LC_FUNCTION void LC_ParseAndResolve(LC_Intern name); // Fully resolve a package and all it's dependences
LC_FUNCTION LC_String LC_GenerateUnityBuild(void); // Generate the C program and return as a string
// Smaller passes for AST modification
LC_FUNCTION void LC_ParsePackagesUsingRegistry(LC_Intern name); // These 3 functions are equivalent to LC_ResolvePackageByName,
LC_FUNCTION void LC_OrderAndResolveTopLevelDecls(LC_Intern name); // you can use them to hook into the compilation process - you can modify the AST
LC_FUNCTION void LC_ResolveAllProcBodies(void); // before resolving or use resolved top declarations to generate some code.
// The Parse and Order functions can be called multiple times to accommodate this.
LC_FUNCTION void LC_ParsePackagesPass(LC_Intern name); // These functions are equivalent to LC_ParseAndResolve,
LC_FUNCTION void LC_BuildIfPass(void); // you can use them to hook into the compilation process - you can modify the AST
LC_FUNCTION void LC_OrderAndResolveTopLevelPass(LC_Intern name); // before resolving or use resolved top declarations to generate some code.
LC_FUNCTION void LC_ResolveProcBodiesPass(void); // The Parse and Order functions can be called multiple times to accommodate this.
// Extended pass / optimization
LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls(void); // Extended pass that you can execute once you have resolved all packages
LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(void); // Extended pass that you can execute once you have resolved all packages
// These three functions are used to implement LC_FindUnusedLocalsAndRemoveUnusedGlobalDecls
// These three functions are used to implement LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass
LC_FUNCTION LC_Map LC_CountDeclRefs(LC_Arena *arena);
LC_FUNCTION void LC_RemoveUnreferencedGlobalDecls(LC_Map *map_of_visits);
LC_FUNCTION void LC_ErrorOnUnreferencedLocals(LC_Map *map_of_visits);
LC_FUNCTION void LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits);
LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits);
// Notes
LC_FUNCTION void LC_DeclareNote(LC_Intern intern);
@@ -1231,6 +1232,7 @@ LC_FUNCTION LC_Operand LC_ResolveTypeCast(LC_AST *pos, LC_Operan
LC_FUNCTION LC_Operand LC_ResolveTypeVarDecl(LC_AST *pos, LC_Operand t, LC_Operand v);
LC_FUNCTION LC_Operand LC_ResolveTypeAggregate(LC_AST *pos, LC_Type *type);
extern LC_Operand LC_OPNull;
LC_FUNCTION LC_Operand LC_OPError(void);
LC_FUNCTION LC_Operand LC_OPConstType(LC_Type *type);
LC_FUNCTION LC_Operand LC_OPDecl(LC_Decl *decl);

View File

@@ -205,7 +205,7 @@ LC_FUNCTION void LC_ParsePackage(LC_AST *n) {
}
}
LC_FUNCTION void LC_ParsePackagesUsingRegistry(LC_Intern name) {
LC_FUNCTION void LC_ParsePackagesPass(LC_Intern name) {
LC_AST *n = LC_GetPackageByName(name);
if (!n) {
LC_SendErrorMessagef(NULL, NULL, "no package with name '%s'\n", name);
@@ -218,7 +218,7 @@ LC_FUNCTION void LC_ParsePackagesUsingRegistry(LC_Intern name) {
LC_ParsePackage(n);
LC_ASTRefList imports = LC_GetPackageImports(n);
for (LC_ASTRef *it = imports.first; it; it = it->next) {
LC_ParsePackagesUsingRegistry(it->ast->gimport.path);
LC_ParsePackagesPass(it->ast->gimport.path);
}
}
@@ -320,7 +320,7 @@ LC_FUNCTION LC_AST *LC_OrderPackagesAndBasicResolve(LC_AST *pos, LC_Intern name)
return n;
}
LC_FUNCTION void LC_OrderAndResolveTopLevelDecls(LC_Intern name) {
LC_FUNCTION void LC_OrderAndResolveTopLevelPass(LC_Intern name) {
L->first_package = name;
LC_OrderPackagesAndBasicResolve(NULL, name);
@@ -334,7 +334,7 @@ LC_FUNCTION void LC_OrderAndResolveTopLevelDecls(LC_Intern name) {
}
}
LC_FUNCTION void LC_ResolveAllProcBodies(void) {
LC_FUNCTION void LC_ResolveProcBodiesPass(void) {
// We don't need to check errors, only valid packages should have been put into
// the list.
for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) {
@@ -344,19 +344,18 @@ LC_FUNCTION void LC_ResolveAllProcBodies(void) {
}
}
LC_FUNCTION LC_ASTRefList LC_ResolvePackageByName(LC_Intern name) {
LC_ParsePackagesUsingRegistry(name);
LC_FUNCTION void LC_ParseAndResolve(LC_Intern name) {
LC_ParsePackagesPass(name);
LC_BuildIfPass();
LC_ASTRefList empty = {0};
if (L->errors) return empty;
if (L->errors) return;
LC_OrderAndResolveTopLevelDecls(name);
LC_ResolveAllProcBodies();
return L->ordered_packages;
LC_OrderAndResolveTopLevelPass(name);
LC_ResolveProcBodiesPass();
}
LC_FUNCTION LC_String LC_GenerateUnityBuild(LC_ASTRefList packages) {
LC_FUNCTION LC_String LC_GenerateUnityBuild(void) {
if (L->errors) return LC_MakeEmptyString();
LC_ASTRefList packages = L->ordered_packages;
LC_BeginStringGen(L->arena);

View File

@@ -987,8 +987,8 @@ LC_FUNCTION LC_AST *LC_ParseFileEx(LC_AST *package) {
if (decl->kind == LC_ASTKind_Error) {
LC_EatUntilNextValidDecl();
} else {
if (L->on_decl_parsed) L->on_decl_parsed(false, decl);
LC_DLLAdd(n->afile.fdecl, n->afile.ldecl, decl);
if (L->on_decl_parsed) L->on_decl_parsed(decl);
}
}

View File

@@ -115,7 +115,7 @@ void WASM_EXPORT(test)(void) {
LC_Intern name = LC_ILit("file");
LC_AddSingleFilePackage(name, LC_Lit("file.lc"));
LC_ASTRefList packages = LC_ResolvePackageByName(name);
LC_ASTRefList packages = LC_ParseAndResolve(name);
if (L->errors == 0) {
LC_BeginStringGen(L->arena);