From 42ec3f7a186a4fa8ee08ca6c31e9626aadcd891f Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 14 Jun 2024 09:06:10 +0200 Subject: [PATCH] Text editor: Remove prototype. Update README --- README.md | 2 +- build.bat | 2 +- examples/text_editor/core/basic.lc | 5 ++ .../text_editor/entry_point/entry_point.lc | 2 +- examples/text_editor/entry_point/prototype.lc | 55 ------------------- examples/text_editor/text_editor/buffer.lc | 22 ++++++-- .../text_editor/text_editor/text_editor.lc | 14 ++--- src/standalone_libraries/arena.h | 2 +- 8 files changed, 32 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index 0e7e663..8cfe802 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Compiler front-end in a single-header-file C library -I have no illusions here, this is not the next big language. What I propose is very simple - a return to C. Not to the language as it was, but a return to the ideal of 'C'. A return to something in it - that is more then it. A small language supplemented with modern ideas - distributed as a dependency free, easy to use single-header-file library. +For a very simple statically typed language. I mostly designed it for personal use, it's small but complete - you can actually program in it and so on. It compiles to C with all the debug info so debuggers work. Currently I find that it's pretty good for metaprogramming and code generation, particularly when you have a workflow "metaprogram -> program". The library provides functions for traversing, printing the AST and other niceties: - **User has full control over compilation!** - **No dependencies, permissive license, single file that compile both in C and C++!** diff --git a/build.bat b/build.bat index d7b0ada..eb6f823 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 --tests text_editor +build\bld.exe diff --git a/examples/text_editor/core/basic.lc b/examples/text_editor/core/basic.lc index d36d89d..e644a50 100644 --- a/examples/text_editor/core/basic.lc +++ b/examples/text_editor/core/basic.lc @@ -36,3 +36,8 @@ ClampFloat :: proc(val: float, min: float, max: float): float { if (val < min) return min; return val; } + +IsFlagSet :: proc(flags: u64, f: u64): bool { + result := flags & f; + return :bool(result); +} \ No newline at end of file diff --git a/examples/text_editor/entry_point/entry_point.lc b/examples/text_editor/entry_point/entry_point.lc index e5fc801..67d88fd 100644 --- a/examples/text_editor/entry_point/entry_point.lc +++ b/examples/text_editor/entry_point/entry_point.lc @@ -34,7 +34,7 @@ main :: proc(): int { SANDBOX_TEXT_EDITOR :: 1; SANDBOX_PROTOTYPE :: 2; - sandbox_chosen := SANDBOX_PROTOTYPE; + sandbox_chosen := SANDBOX_TEXT_EDITOR; for !WindowShouldClose() { screen_size: Vector2 = {:f32(GetScreenWidth()), :f32(GetScreenHeight())}; diff --git a/examples/text_editor/entry_point/prototype.lc b/examples/text_editor/entry_point/prototype.lc index 0b2640b..c2db09a 100644 --- a/examples/text_editor/entry_point/prototype.lc +++ b/examples/text_editor/entry_point/prototype.lc @@ -1,58 +1,3 @@ -Inited: bool; -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) { - if !Inited { - 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) { - file_content := OS_ReadFile(&Perm, iter.absolute_path); - PushTranscript(&Transcripts, { - absolute_path = STR(iter.absolute_path), - file_content = STR(file_content)}); - } - } - - 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, flags = TE.WindowFlags_DrawScrollbar}); - TE.FocusedWindow = &TE.WindowStack[0]; - TE.FocusedWindow = nil; - } - - // Te.ComputeWindowRects(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); } -S8 :: proc(str: String): S8_String { return {str = str.str, len = :i64(str.len)}; } -STR :: proc(str: S8_String): String { return {str = str.str, len = :int(str.len)}; } - -Transcript :: struct { - file_content: String; - absolute_path: String; -} - -TranscriptStack :: struct { - data: [2048]Transcript; - len: int; -} - -PushTranscript :: proc(stack: *TranscriptStack, t: Transcript) { - assert(stack.len + 1 < lengthof(stack.data)); - stack.data[stack.len] = t; - stack.len += 1; -} diff --git a/examples/text_editor/text_editor/buffer.lc b/examples/text_editor/text_editor/buffer.lc index 78971cf..c097490 100644 --- a/examples/text_editor/text_editor/buffer.lc +++ b/examples/text_editor/text_editor/buffer.lc @@ -34,10 +34,25 @@ AdjustUTF8PosUnsafe :: proc(buffer: *Buffer, pos: int, direction: int = 1): int return pos; } +GetBufferEnd :: proc(buffer: *Buffer): int { + result := MaxInt(0, buffer.len - 1); + return result; +} + +GetBufferEndRange :: proc(buffer: *Buffer): Range { + result := GetBufferEnd(buffer); + return {result, result}; +} + +GetEntireBufferRange :: proc(buffer: *Buffer): Range { + result: Range = {0, GetBufferEnd(buffer)}; + return result; +} + AdjustUTF8Pos :: proc(buffer: *Buffer, pos: int, direction: int = 1): int { assert(direction == 1 || direction == -1); pos = AdjustUTF8PosUnsafe(buffer, pos, direction); - pos = ClampInt(pos, 0, MaxInt(0, buffer.len - 1)); + pos = ClampInt(pos, 0, GetBufferEnd(buffer)); if (buffer.data) assert(!IsUTF8ContinuationByte(buffer.data[pos])); return pos; } @@ -131,8 +146,7 @@ GetStringFromBuffer :: proc(buffer: *Buffer, range: Range): String { } AddText :: proc(buffer: *Buffer, text: String) { - end_of_buffer: Range = {buffer.len - 1, buffer.len - 1}; - ReplaceText(buffer, end_of_buffer, text); + ReplaceText(buffer, GetBufferEndRange(buffer), text); } FindLineOfPos :: proc(buffer: *Buffer, pos: int): LineInfo { @@ -204,7 +218,7 @@ SeekOnWordBoundary :: proc(buffer: *Buffer, pos: int, direction: int = GO_FORWAR seek_whitespace := standing_on_whitespace == false; seek_word := standing_on_whitespace; - end := buffer.len - 1; + end := GetBufferEnd(buffer); if (direction == GO_BACKWARD) end = 0; result := end; diff --git a/examples/text_editor/text_editor/text_editor.lc b/examples/text_editor/text_editor/text_editor.lc index 1cfd73e..cbeb501 100644 --- a/examples/text_editor/text_editor/text_editor.lc +++ b/examples/text_editor/text_editor/text_editor.lc @@ -26,11 +26,10 @@ 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; + flags: u64; buffer: *Buffer; cursor: Selection; @@ -76,10 +75,9 @@ UpdateAndDrawWindow :: proc(w: *Window, font: Font, font_size: float) { text_window_rect := w.rect; bar_rect := CutRight(&text_window_rect, 10); horizontal_bar_rect := CutBottom(&text_window_rect, 10); - horizontal_bar_rect; @unused // line_numbers_rect := CutLeft(&text_window_rect, Monosize.x * 4); - buffer_end_vpos := CalculateVisualPos(w.buffer, w.buffer.len - 1); + buffer_end_vpos := CalculateVisualPos(w.buffer, GetBufferEnd(w.buffer)); buffer_end_wpos := CalculateWorldPosUnscrolled(buffer_end_vpos); if CheckCollisionPointRec(GetMousePosition(), Rect2PToRectangle(w.rect)) { @@ -251,7 +249,7 @@ UpdateAndDrawWindow :: proc(w: *Window, font: Font, font_size: float) { // @todo: buffer.len abstract if IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_A) { w.cursor.a = 0; - w.cursor.b = w.buffer.len - 1; + w.cursor.b = GetBufferEnd(w.buffer); } if IsKeyPressed(KEY_PAGE_DOWN) || IsKeyPressedRepeat(KEY_PAGE_DOWN) { @@ -323,7 +321,6 @@ 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; - if (w.scroll.y > buffer_end_wpos.y) w.scroll.y = buffer_end_wpos.y; // @@ -345,7 +342,6 @@ UpdateAndDrawWindow :: proc(w: *Window, font: Font, font_size: float) { SetMouseCursor(MOUSE_CURSOR_IBEAM); } else { SetMouseCursor(MOUSE_CURSOR_DEFAULT); - if IsMouseButtonPressed(MOUSE_BUTTON_LEFT) { w.mouse_scrolling = true; } @@ -364,7 +360,7 @@ UpdateAndDrawWindow :: proc(w: *Window, font: Font, font_size: float) { w.mouse_selecting = false; } - if w.mouse_scrolling { + if IsFlagSet(w.flags, WindowFlags_DrawScrollbar) && w.mouse_scrolling { mouse_scroll_marker_normalized := (mouse_p.y - w.rect.min.y) / GetRectY(bar_rect); w.scroll.y = mouse_scroll_marker_normalized * buffer_end_wpos.y; @@ -459,7 +455,7 @@ UpdateAndDrawWindow :: proc(w: *Window, font: Font, font_size: float) { // Draw bar - { + if IsFlagSet(w.flags, WindowFlags_DrawScrollbar) { DrawRect(bar_rect, LIGHTGRAY); DrawRect(horizontal_bar_rect, LIGHTGRAY); diff --git a/src/standalone_libraries/arena.h b/src/standalone_libraries/arena.h index a78eac0..b7c9e87 100644 --- a/src/standalone_libraries/arena.h +++ b/src/standalone_libraries/arena.h @@ -118,7 +118,7 @@ MA_API void * MA__PushSizeNonZeroed(MA_Arena *a, size_t size); MA_API void * MA__PushSize(MA_Arena *arena, size_t size); MA_API char * MA__PushStringCopy(MA_Arena *arena, char *p, size_t size); MA_API void * MA__PushCopy(MA_Arena *arena, void *p, size_t size); -MA_API MA_Temp MA_BeginTemp(MA_Arena *arena); +MA_API MA_Temp MA_BeginTemp(MA_Arena *arena); MA_API void MA_EndTemp(MA_Temp checkpoint); MA_API void MA_PopToPos(MA_Arena *arena, size_t pos);