From ff612d00ef22177d9fe2e490ed4d3523d73fb748 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 10 Jun 2024 08:45:58 +0200 Subject: [PATCH] Fix field resolution not resolving constant value properly --- build.bat | 2 +- .../text_editor/entry_point/entry_point.lc | 4 +++ examples/text_editor/entry_point/prototype.lc | 27 +++++++------------ .../text_editor/text_editor/text_editor.lc | 13 +++++---- lib_compiler.h | 21 ++++++++++----- src/compiler/resolve.c | 21 ++++++++++----- tests/const_not_folding/main/test_unused.lc | 5 ++++ tests/const_not_folding/second/second.lc | 1 + tests/defer.txt | 11 ++++++++ 9 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 tests/const_not_folding/main/test_unused.lc create mode 100644 tests/const_not_folding/second/second.lc create mode 100644 tests/defer.txt diff --git a/build.bat b/build.bat index c4f0075..d7b0ada 100644 --- a/build.bat +++ b/build.bat @@ -8,4 +8,4 @@ if not exist build\bld.exe ( ) rem ubuntu run ./build.sh -build\bld.exe --quick +build\bld.exe --tests text_editor diff --git a/examples/text_editor/entry_point/entry_point.lc b/examples/text_editor/entry_point/entry_point.lc index f8ae95c..e5fc801 100644 --- a/examples/text_editor/entry_point/entry_point.lc +++ b/examples/text_editor/entry_point/entry_point.lc @@ -28,6 +28,10 @@ main :: proc(): int { font_spacing: float = 1; font: Font = LoadFontEx("C:/Windows/Fonts/consola.ttf", :int(font_size), nil, 0); + glyph_info: GlyphInfo = GetGlyphInfo(font, 'A'); + size := MeasureTextEx(font, "A", font_size, font_spacing); + TE.Monosize = {:float(glyph_info.image.width), size.y}; + SANDBOX_TEXT_EDITOR :: 1; SANDBOX_PROTOTYPE :: 2; sandbox_chosen := SANDBOX_PROTOTYPE; diff --git a/examples/text_editor/entry_point/prototype.lc b/examples/text_editor/entry_point/prototype.lc index dfcb2d6..0b2640b 100644 --- a/examples/text_editor/entry_point/prototype.lc +++ b/examples/text_editor/entry_point/prototype.lc @@ -3,6 +3,7 @@ Perm: MA_Arena; Transcripts: TranscriptStack; SearchBar: TE.Buffer; +FilenamesBuffer: TE.Buffer; ResultsBuffer: TE.Buffer; UpdatePrototype :: proc(rect: Rect2P, font: Font, font_size: float, font_spacing: float) { @@ -10,8 +11,6 @@ UpdatePrototype :: proc(rect: Rect2P, font: Font, font_size: float, font_spacing Inited = true; for iter := OS_IterateFiles(&Perm, S8("C:/video")); OS_IsValid(iter); OS_Advance(&iter) { if S8_EndsWith(iter.absolute_path, S8(".srt"), true) { - printf("%.*s\n", :int(iter.absolute_path.len), iter.absolute_path.str); - file_content := OS_ReadFile(&Perm, iter.absolute_path); PushTranscript(&Transcripts, { absolute_path = STR(iter.absolute_path), @@ -19,27 +18,21 @@ UpdatePrototype :: proc(rect: Rect2P, font: Font, font_size: float, font_spacing } } + for i := 0; i < Transcripts.len; i += 1 { + TE.ReplaceText(&FilenamesBuffer, {FilenamesBuffer.len, FilenamesBuffer.len}, Transcripts.data[i].absolute_path); + TE.ReplaceText(&FilenamesBuffer, {FilenamesBuffer.len, FilenamesBuffer.len}, "\n"); + } + TE.ReplaceText(&ResultsBuffer, {ResultsBuffer.len, ResultsBuffer.len}, Transcripts.data[0].file_content); + TE.ReplaceText(&SearchBar, {SearchBar.len, SearchBar.len}, ""); + TE.AddWindow({buffer = &SearchBar}); - TE.AddWindow({buffer = &ResultsBuffer}); + TE.AddWindow({buffer = &ResultsBuffer, flags = TE.WindowFlags_DrawScrollbar}); TE.FocusedWindow = &TE.WindowStack[0]; TE.FocusedWindow = nil; } - for key := GetCharPressed(); key; key = GetCharPressed() { - selection_range := :TE.Range{0,0}; - - result: UTF8_Result = UTF32ToUTF8(:u32(key)); - if result.error == 0 { - TE.ReplaceText(&SearchBar, selection_range, {:*char(&result.out_str[0]), result.len}); - } else { - TE.ReplaceText(&SearchBar, selection_range, "?"); - } - } - - - // Te.ComputeWindowRects(rect); - TE.WindowStack[0].rect = CutLeft(&rect, 0.5 * GetRectX(rect)); + TE.WindowStack[0].rect = CutTop(&rect, font_size * 2); TE.WindowStack[1].rect = CutLeft(&rect, 1.0 * GetRectX(rect)); TE.UpdateAndDrawWindows(font, font_size); diff --git a/examples/text_editor/text_editor/text_editor.lc b/examples/text_editor/text_editor/text_editor.lc index c66eba7..1cfd73e 100644 --- a/examples/text_editor/text_editor/text_editor.lc +++ b/examples/text_editor/text_editor/text_editor.lc @@ -14,11 +14,6 @@ WindowStackCount: int; UpdateTextEditor :: proc(rect: Rect2P, font: Font, font_size: float, font_spacing: float) { if !Inited { Inited = true; - - glyph_info: GlyphInfo = GetGlyphInfo(font, 'A'); - size := MeasureTextEx(font, "A", font_size, font_spacing); - Monosize = {:float(glyph_info.image.width), size.y}; - file_content := LoadFileText("C:/Work/language/examples/text_editor/entry_point/entry_point.lc"); AddText(&TeBuffer, {file_content, :int(strlen(file_content))}); UnloadFileText(file_content); @@ -31,8 +26,13 @@ UpdateTextEditor :: proc(rect: Rect2P, font: Font, font_size: float, font_spacin UpdateAndDrawWindows(font, font_size); } +WindowFlags :: typedef u64; +WindowFlags_DrawScrollbar :: 1; + Window :: struct { + flags: WindowFlags; buffer: *Buffer; + cursor: Selection; scroll: Vector2; mouse_scrolling: bool; @@ -324,8 +324,7 @@ UpdateAndDrawWindow :: proc(w: *Window, font: Font, font_size: float) { if (w.scroll.x < 0) w.scroll.x = 0; if (w.scroll.y < 0) w.scroll.y = 0; - adjusted_kind_of_end := buffer_end_wpos.y - GetRectY(text_window_rect) * 0.8; - if (w.scroll.y > adjusted_kind_of_end) w.scroll.y = adjusted_kind_of_end; + if (w.scroll.y > buffer_end_wpos.y) w.scroll.y = buffer_end_wpos.y; // // Mouse diff --git a/lib_compiler.h b/lib_compiler.h index 4f42284..fc9fe78 100644 --- a/lib_compiler.h +++ b/lib_compiler.h @@ -6855,16 +6855,15 @@ LC_FUNCTION LC_Operand LC_ResolveExprEx(LC_AST *n) { } break; case LC_ASTKind_ExprField: { - bool first_part_executed = false; + bool resolved_import = false; LC_Operand op = {0}; if (n->efield.left->kind == LC_ASTKind_ExprIdent) { LC_AST *nf = n->efield.left; LC_Operand LC_PROP_ERROR(op_name, nf, LC_ResolveName(nf, nf->eident.name)); - // LC_Match (Package.) and fold (Package.Other) into just (Other) if (op_name.decl->kind == LC_DeclKind_Import) { - first_part_executed = true; + resolved_import = true; nf->eident.resolved_decl = op_name.decl; nf->type = L->tvoid; @@ -6872,7 +6871,7 @@ LC_FUNCTION LC_Operand LC_ResolveExprEx(LC_AST *n) { } } - if (!first_part_executed) { + if (!resolved_import) { LC_ASTKind left_kind = n->efield.left->kind; LC_PROP_ERROR(op, n, LC_ResolveExpr(n->efield.left)); @@ -6881,10 +6880,18 @@ LC_FUNCTION LC_Operand LC_ResolveExprEx(LC_AST *n) { LC_IF(!LC_IsAggType(type), n->efield.left, "invalid operation, expected aggregate type, '%s' is not an aggregate", LC_GenLCType(type)); LC_PROP_ERROR(op, n, LC_ResolveNameInScope(n, type->decl)); LC_ASSERT(n, op.decl->kind == LC_DeclKind_Var); - result.flags |= LC_OPF_Const; + result.flags |= LC_OPF_LValue | LC_OPF_Const; + result.val = op.decl->val; + } else { + result.val = op.decl->val; + // @copy_paste from ExprIdent + if (op.decl->kind == LC_DeclKind_Const) { + result.flags |= LC_OPF_UTConst | LC_OPF_Const; + SetConstVal(n, result.val); + } else { + result.flags |= LC_OPF_LValue | LC_OPF_Const; + } } - result.flags |= LC_OPF_LValue; - result.val = op.decl->val; } break; case LC_ASTKind_ExprCall: { diff --git a/src/compiler/resolve.c b/src/compiler/resolve.c index 3a84e81..bae77f8 100644 --- a/src/compiler/resolve.c +++ b/src/compiler/resolve.c @@ -690,16 +690,15 @@ LC_FUNCTION LC_Operand LC_ResolveExprEx(LC_AST *n) { } break; case LC_ASTKind_ExprField: { - bool first_part_executed = false; + bool resolved_import = false; LC_Operand op = {0}; if (n->efield.left->kind == LC_ASTKind_ExprIdent) { LC_AST *nf = n->efield.left; LC_Operand LC_PROP_ERROR(op_name, nf, LC_ResolveName(nf, nf->eident.name)); - // LC_Match (Package.) and fold (Package.Other) into just (Other) if (op_name.decl->kind == LC_DeclKind_Import) { - first_part_executed = true; + resolved_import = true; nf->eident.resolved_decl = op_name.decl; nf->type = L->tvoid; @@ -707,7 +706,7 @@ LC_FUNCTION LC_Operand LC_ResolveExprEx(LC_AST *n) { } } - if (!first_part_executed) { + if (!resolved_import) { LC_ASTKind left_kind = n->efield.left->kind; LC_PROP_ERROR(op, n, LC_ResolveExpr(n->efield.left)); @@ -716,10 +715,18 @@ LC_FUNCTION LC_Operand LC_ResolveExprEx(LC_AST *n) { LC_IF(!LC_IsAggType(type), n->efield.left, "invalid operation, expected aggregate type, '%s' is not an aggregate", LC_GenLCType(type)); LC_PROP_ERROR(op, n, LC_ResolveNameInScope(n, type->decl)); LC_ASSERT(n, op.decl->kind == LC_DeclKind_Var); - result.flags |= LC_OPF_Const; + result.flags |= LC_OPF_LValue | LC_OPF_Const; + result.val = op.decl->val; + } else { + result.val = op.decl->val; + // @copy_paste from ExprIdent + if (op.decl->kind == LC_DeclKind_Const) { + result.flags |= LC_OPF_UTConst | LC_OPF_Const; + SetConstVal(n, result.val); + } else { + result.flags |= LC_OPF_LValue | LC_OPF_Const; + } } - result.flags |= LC_OPF_LValue; - result.val = op.decl->val; } break; case LC_ASTKind_ExprCall: { diff --git a/tests/const_not_folding/main/test_unused.lc b/tests/const_not_folding/main/test_unused.lc new file mode 100644 index 0000000..119fc9b --- /dev/null +++ b/tests/const_not_folding/main/test_unused.lc @@ -0,0 +1,5 @@ +import b "second"; + +main :: proc(): int { + return b.A; +} \ No newline at end of file diff --git a/tests/const_not_folding/second/second.lc b/tests/const_not_folding/second/second.lc new file mode 100644 index 0000000..4d53462 --- /dev/null +++ b/tests/const_not_folding/second/second.lc @@ -0,0 +1 @@ +A :: 0; \ No newline at end of file diff --git a/tests/defer.txt b/tests/defer.txt new file mode 100644 index 0000000..be7150a --- /dev/null +++ b/tests/defer.txt @@ -0,0 +1,11 @@ +a: char; + +main :: proc(): int { + for i := 0; i < 4; i += 1 { + if i == 2 { break; } + + string := &a; + defer *string = 1; + } + return 0; +} \ No newline at end of file