Fix field resolution not resolving constant value properly

This commit is contained in:
Krzosa Karol
2024-06-10 08:45:58 +02:00
parent f8cf91ed2a
commit ff612d00ef
9 changed files with 66 additions and 39 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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);

View File

@@ -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

View File

@@ -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: {

View File

@@ -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: {

View File

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

View File

@@ -0,0 +1 @@
A :: 0;

11
tests/defer.txt Normal file
View File

@@ -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;
}