Fix improperly prompting about unused local in another modules, fix full recompile, Text editor: restructure

This commit is contained in:
Krzosa Karol
2024-06-09 12:04:38 +02:00
parent b08c7c9ce6
commit 39cb9cb4b4
22 changed files with 86 additions and 54 deletions

View File

@@ -8,4 +8,4 @@ if not exist build\bld.exe (
) )
rem ubuntu run ./build.sh rem ubuntu run ./build.sh
build\bld.exe --tests text_editor build\bld.exe

View File

@@ -37,12 +37,12 @@ bool add_instrumentation() {
} }
LC_String code = LC_GenerateUnityBuild(); LC_String code = LC_GenerateUnityBuild();
LC_LangEnd(lang);
S8_String path = "examples/add_instrumentation/add_instrumentation.c"; S8_String path = "examples/add_instrumentation/add_instrumentation.c";
OS_MakeDir("examples"); OS_MakeDir("examples");
OS_MakeDir("examples/add_instrumentation"); OS_MakeDir("examples/add_instrumentation");
OS_WriteFile(path, code); OS_WriteFile(path, code);
LC_LangEnd(lang);
if (!UseCL) return true; if (!UseCL) return true;
S8_String cmd = Fmt("cl %.*s -Zi -std:c11 -nologo -FC -Fd:examples/add_instrumentation/a.pdb -Fe:examples/add_instrumentation/add_instrumentation.exe %.*s", S8_Expand(path), S8_Expand(RaylibLIB)); S8_String cmd = Fmt("cl %.*s -Zi -std:c11 -nologo -FC -Fd:examples/add_instrumentation/a.pdb -Fe:examples/add_instrumentation/add_instrumentation.exe %.*s", S8_Expand(path), S8_Expand(RaylibLIB));

View File

@@ -34,10 +34,9 @@ bool add_source_location_macro() {
} }
LC_String code = LC_GenerateUnityBuild(); LC_String code = LC_GenerateUnityBuild();
LC_LangEnd(lang);
OS_MakeDir("examples/add_source_location_macro"); OS_MakeDir("examples/add_source_location_macro");
OS_WriteFile("examples/add_source_location_macro/add_source_location_macro.c", code); OS_WriteFile("examples/add_source_location_macro/add_source_location_macro.c", code);
LC_LangEnd(lang);
return true; return true;
} }

View File

@@ -16,10 +16,10 @@ bool hello_world() {
OS_MakeDir("examples/hello_world"); OS_MakeDir("examples/hello_world");
LC_ParseAndResolve(name); LC_ParseAndResolve(name);
LC_String code = LC_GenerateUnityBuild(); LC_String code = LC_GenerateUnityBuild();
LC_LangEnd(lang);
S8_String path = "examples/hello_world/hello_world.c"; S8_String path = "examples/hello_world/hello_world.c";
OS_WriteFile(path, code); OS_WriteFile(path, code);
LC_LangEnd(lang);
if (UseCL) { if (UseCL) {
S8_String cmd = Fmt("cl %.*s -nologo -FC -Fd:examples/hello_world/hello_world.pdb -Fe:examples/hello_world/hello_world.exe", S8_Expand(path)); S8_String cmd = Fmt("cl %.*s -nologo -FC -Fd:examples/hello_world/hello_world.pdb -Fe:examples/hello_world/hello_world.exe", S8_Expand(path));
if (Run(cmd) != 0) return false; if (Run(cmd) != 0) return false;

View File

@@ -6,9 +6,9 @@ bool text_editor() {
defer { LC_LangEnd(lang); }; defer { LC_LangEnd(lang); };
LC_RegisterPackageDir("../pkgs"); LC_RegisterPackageDir("../pkgs");
LC_RegisterPackageDir("../examples"); LC_RegisterPackageDir("../examples/text_editor");
LC_Intern name = LC_ILit("text_editor"); LC_Intern name = LC_ILit("entry_point");
LC_ParseAndResolve(name); LC_ParseAndResolve(name);
if (L->errors) return false; if (L->errors) return false;

View File

@@ -1,3 +1,6 @@
import "raylib";
import "std_types";
ClampInt :: proc(val: int, min: int, max: int): int { ClampInt :: proc(val: int, min: int, max: int): int {
result := val; result := val;
if (val < min) result = min; if (val < min) result = min;

View File

@@ -12,6 +12,9 @@ import "raylib";
import "std_types"; import "std_types";
import "libc"; import "libc";
import "core";
import TE "text_editor";
InvalidCodepath :: proc() { InvalidCodepath :: proc() {
assert(:*char("invalid codepath") == :*char("")); assert(:*char("invalid codepath") == :*char(""));
} }
@@ -81,12 +84,13 @@ main :: proc(): int {
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
if sandbox_chosen == SANDBOX_TEXT_EDITOR { if sandbox_chosen == SANDBOX_TEXT_EDITOR {
UpdateTextEditor(screen_rect, font, font_size, font_spacing); TE.UpdateTextEditor(screen_rect, font, font_size, font_spacing);
} else if sandbox_chosen == SANDBOX_PROTOTYPE { } else if sandbox_chosen == SANDBOX_PROTOTYPE {
UpdatePrototype(screen_rect); UpdatePrototype(screen_rect);
} }
DrawRect(top_bar_original, LIGHTGRAY);
TE.DrawRect(top_bar_original, LIGHTGRAY);
DrawRectangleRoundedLines(Rect2PToRectangle(button1), 0.3, 12, 2, BLACK); DrawRectangleRoundedLines(Rect2PToRectangle(button1), 0.3, 12, 2, BLACK);
if button1_hover DrawRectangleRounded(Rect2PToRectangle(button1), 0.3, 12, WHITE); if button1_hover DrawRectangleRounded(Rect2PToRectangle(button1), 0.3, 12, WHITE);
@@ -102,3 +106,4 @@ main :: proc(): int {
CloseWindow(); CloseWindow();
return 0; return 0;
} }

View File

@@ -1,3 +1,9 @@
import "std_types";
import "raylib";
import "libc";
import "core";
TeInited := false; TeInited := false;
TeFontSize: float = 14; TeFontSize: float = 14;
TeFontSpacing: float = 1; TeFontSpacing: float = 1;
@@ -91,7 +97,6 @@ UpdateAndDrawWindow :: proc(w: *Window, font: Font, font_size: float) {
} }
} }
if w == FocusedWindow { if w == FocusedWindow {
if IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT) { if IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT) {
if IsKeyDown(KEY_LEFT_SHIFT) { if IsKeyDown(KEY_LEFT_SHIFT) {

View File

@@ -1045,8 +1045,8 @@ LC_FUNCTION void LC_ResolveProcBodiesPass(void); // The Parse an
LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(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_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass // These three functions are used to implement LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass
LC_FUNCTION LC_Map LC_CountDeclRefs(LC_Arena *arena); LC_FUNCTION void LC_CountDeclRefs(LC_Arena *arena, LC_Map *map, LC_AST *ast);
LC_FUNCTION void LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits); LC_FUNCTION LC_Decl *LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits);
LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits); LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits);
// Notes // Notes
@@ -9891,20 +9891,16 @@ LC_FUNCTION void WalkAndCountDeclRefs(LC_ASTWalker *ctx, LC_AST *n) {
} }
} }
LC_FUNCTION LC_Map LC_CountDeclRefs(LC_Arena *arena) { LC_FUNCTION void LC_CountDeclRefs(LC_Arena *arena, LC_Map *map, LC_AST *ast) {
LC_Map map = {arena};
LC_MapReserve(&map, 512);
LC_AST *package = LC_GetPackageByName(L->first_package);
LC_ASTWalker walker = LC_GetDefaultWalker(arena, WalkAndCountDeclRefs); LC_ASTWalker walker = LC_GetDefaultWalker(arena, WalkAndCountDeclRefs);
walker.user_data = (void *)&map; walker.user_data = (void *)map;
walker.visit_notes = true; walker.visit_notes = true;
LC_WalkAST(&walker, package); LC_WalkAST(&walker, ast);
return map;
} }
LC_FUNCTION void LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits) { LC_FUNCTION LC_Decl *LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits) {
LC_Decl *first_removed_decl = NULL;
LC_Decl *last_removed_decl = NULL;
for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) { for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) {
for (LC_Decl *decl = it->ast->apackage.ext->first_ordered; decl;) { for (LC_Decl *decl = it->ast->apackage.ext->first_ordered; decl;) {
intptr_t ref_count = (intptr_t)LC_MapGetP(map_of_visits, decl); intptr_t ref_count = (intptr_t)LC_MapGetP(map_of_visits, decl);
@@ -9913,9 +9909,11 @@ LC_FUNCTION void LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits) {
decl = decl->next; decl = decl->next;
if (ref_count == 0 && remove->foreign_name != LC_ILit("main")) { if (ref_count == 0 && remove->foreign_name != LC_ILit("main")) {
LC_DLLRemove(it->ast->apackage.ext->first_ordered, it->ast->apackage.ext->last_ordered, remove); LC_DLLRemove(it->ast->apackage.ext->first_ordered, it->ast->apackage.ext->last_ordered, remove);
LC_DLLAdd(first_removed_decl, last_removed_decl, remove);
} }
} }
} }
return first_removed_decl;
} }
LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits) { LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits) {
@@ -9939,9 +9937,16 @@ LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(void) {
if (L->errors) return; if (L->errors) return;
LC_TempArena check = LC_BeginTemp(L->arena); LC_TempArena check = LC_BeginTemp(L->arena);
LC_Map map = LC_CountDeclRefs(check.arena); LC_AST *package = LC_GetPackageByName(L->first_package);
LC_Map map = {check.arena};
LC_MapReserve(&map, 512);
LC_CountDeclRefs(check.arena, &map, package);
LC_Decl *first_removed_decl = LC_RemoveUnreferencedGlobalDeclsPass(&map);
for (LC_Decl *it = first_removed_decl; it; it = it->next) {
LC_CountDeclRefs(check.arena, &map, it->ast);
}
LC_ErrorOnUnreferencedLocalsPass(&map); LC_ErrorOnUnreferencedLocalsPass(&map);
LC_RemoveUnreferencedGlobalDeclsPass(&map);
LC_EndTemp(check); LC_EndTemp(check);
} }

View File

@@ -16,20 +16,16 @@ LC_FUNCTION void WalkAndCountDeclRefs(LC_ASTWalker *ctx, LC_AST *n) {
} }
} }
LC_FUNCTION LC_Map LC_CountDeclRefs(LC_Arena *arena) { LC_FUNCTION void LC_CountDeclRefs(LC_Arena *arena, LC_Map *map, LC_AST *ast) {
LC_Map map = {arena};
LC_MapReserve(&map, 512);
LC_AST *package = LC_GetPackageByName(L->first_package);
LC_ASTWalker walker = LC_GetDefaultWalker(arena, WalkAndCountDeclRefs); LC_ASTWalker walker = LC_GetDefaultWalker(arena, WalkAndCountDeclRefs);
walker.user_data = (void *)&map; walker.user_data = (void *)map;
walker.visit_notes = true; walker.visit_notes = true;
LC_WalkAST(&walker, package); LC_WalkAST(&walker, ast);
return map;
} }
LC_FUNCTION void LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits) { LC_FUNCTION LC_Decl *LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits) {
LC_Decl *first_removed_decl = NULL;
LC_Decl *last_removed_decl = NULL;
for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) { for (LC_ASTRef *it = L->ordered_packages.first; it; it = it->next) {
for (LC_Decl *decl = it->ast->apackage.ext->first_ordered; decl;) { for (LC_Decl *decl = it->ast->apackage.ext->first_ordered; decl;) {
intptr_t ref_count = (intptr_t)LC_MapGetP(map_of_visits, decl); intptr_t ref_count = (intptr_t)LC_MapGetP(map_of_visits, decl);
@@ -38,9 +34,11 @@ LC_FUNCTION void LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits) {
decl = decl->next; decl = decl->next;
if (ref_count == 0 && remove->foreign_name != LC_ILit("main")) { if (ref_count == 0 && remove->foreign_name != LC_ILit("main")) {
LC_DLLRemove(it->ast->apackage.ext->first_ordered, it->ast->apackage.ext->last_ordered, remove); LC_DLLRemove(it->ast->apackage.ext->first_ordered, it->ast->apackage.ext->last_ordered, remove);
LC_DLLAdd(first_removed_decl, last_removed_decl, remove);
} }
} }
} }
return first_removed_decl;
} }
LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits) { LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits) {
@@ -64,9 +62,16 @@ LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(void) {
if (L->errors) return; if (L->errors) return;
LC_TempArena check = LC_BeginTemp(L->arena); LC_TempArena check = LC_BeginTemp(L->arena);
LC_Map map = LC_CountDeclRefs(check.arena); LC_AST *package = LC_GetPackageByName(L->first_package);
LC_Map map = {check.arena};
LC_MapReserve(&map, 512);
LC_CountDeclRefs(check.arena, &map, package);
LC_Decl *first_removed_decl = LC_RemoveUnreferencedGlobalDeclsPass(&map);
for (LC_Decl *it = first_removed_decl; it; it = it->next) {
LC_CountDeclRefs(check.arena, &map, it->ast);
}
LC_ErrorOnUnreferencedLocalsPass(&map); LC_ErrorOnUnreferencedLocalsPass(&map);
LC_RemoveUnreferencedGlobalDeclsPass(&map);
LC_EndTemp(check); LC_EndTemp(check);
} }

View File

@@ -1001,8 +1001,8 @@ LC_FUNCTION void LC_ResolveProcBodiesPass(void); // The Parse an
LC_FUNCTION void LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass(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_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass // These three functions are used to implement LC_FindUnusedLocalsAndRemoveUnusedGlobalDeclsPass
LC_FUNCTION LC_Map LC_CountDeclRefs(LC_Arena *arena); LC_FUNCTION void LC_CountDeclRefs(LC_Arena *arena, LC_Map *map, LC_AST *ast);
LC_FUNCTION void LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits); LC_FUNCTION LC_Decl *LC_RemoveUnreferencedGlobalDeclsPass(LC_Map *map_of_visits);
LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits); LC_FUNCTION void LC_ErrorOnUnreferencedLocalsPass(LC_Map *map_of_visits);
// Notes // Notes

View File

@@ -32,9 +32,9 @@ int main(int argc, char **argv) {
} }
LC_Intern name = LC_InternStrLen(package.str, (int)package.len); LC_Intern name = LC_InternStrLen(package.str, (int)package.len);
LC_ASTRefList packages = LC_ParseAndResolve(name); LC_ParseAndResolve(name);
if (lang->errors) return 1; if (lang->errors) return 1;
LC_String code = LC_GenerateUnityBuild(packages); LC_String code = LC_GenerateUnityBuild();
(void)code; (void)code;
} }

View File

@@ -32,9 +32,9 @@ int main(int argc, char **argv) {
} }
LC_Intern name = LC_InternStrLen(package.str, (int)package.len); LC_Intern name = LC_InternStrLen(package.str, (int)package.len);
LC_ASTRefList packages = LC_ParseAndResolve(name); LC_ParseAndResolve(name);
if (lang->errors) return 1; if (lang->errors) return 1;
LC_String code = LC_GenerateUnityBuild(packages); LC_String code = LC_GenerateUnityBuild();
(void)code; (void)code;
} }

View File

@@ -0,0 +1,5 @@
import b "second";
main :: proc(): int {
return 0;
}

View File

@@ -0,0 +1,5 @@
other_thing :: proc(): int {
a := 0;
return a;
}

View File

@@ -42,9 +42,9 @@ int main(int argc, char **argv) {
S8_For(it, dirs) { LC_RegisterPackageDir(it->string.str); } S8_For(it, dirs) { LC_RegisterPackageDir(it->string.str); }
LC_Intern name = LC_InternStrLen(package.str, (int)package.len); LC_Intern name = LC_InternStrLen(package.str, (int)package.len);
LC_ASTRefList packages = LC_ParseAndResolve(name); LC_ParseAndResolve(name);
if (lang->errors) return 1; if (lang->errors) return 1;
S8_String code = LC_GenerateUnityBuild(packages); S8_String code = LC_GenerateUnityBuild();
OS_WriteFile(S8_Lit("output.c"), code); OS_WriteFile(S8_Lit("output.c"), code);
} }