From 87657a99a60c42e5e8f625ec0791d159e39ca198 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 19 Apr 2023 12:50:58 +0200 Subject: [PATCH] Core: match sizeof etc. syntax with C, Core RTS: Beautifying + animations --- README.md | 2 +- .../drawing_to_screen_using_windows_api.core | 2 +- build/examples/language_basics.core | 14 ++-- build/examples/polymorphism.core | 2 +- build/examples/push_struct.core | 6 +- build/examples/raymarcher.core | 2 +- build/examples/runtime_type_information.core | 6 +- build/examples/unions.core | 2 +- build/modules/Arena.core | 4 +- build/modules/Base.core | 4 +- build/modules/win32_multimedia.core | 4 +- build/rtsgame/array.core | 10 +-- build/rtsgame/main.core | 79 +++++++++++++++++-- build/rtsgame/map.core | 20 ++++- core_compiler.cpp | 6 +- core_lexing.cpp | 2 +- core_parsing.cpp | 4 +- core_printer.cpp | 6 +- core_typechecking.cpp | 4 +- meta.py | 8 +- 20 files changed, 131 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 810df5c..659e320 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ main :: (): int - [x] Runtime reflection - [x] Package of type and pointer which enables dynamic typing - - [x] Types are values holding type ids, this way we can do if TypeOf(value) == S64 ;; blah + - [x] Types are values holding type ids, this way we can do if typeof(value) == S64 ;; blah - [x] Typesafe variadic arguments using []Any slice (no more var args!) - [x] Any semantics that make it easy to use (automatic unpacking to pointer and type) - [x] Optional type information dump which allows an application to recursively serialize the data. Something you would need special tools for in C++. diff --git a/build/examples/drawing_to_screen_using_windows_api.core b/build/examples/drawing_to_screen_using_windows_api.core index 700cdb4..e256e44 100644 --- a/build/examples/drawing_to_screen_using_windows_api.core +++ b/build/examples/drawing_to_screen_using_windows_api.core @@ -29,7 +29,7 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS ShowWindow(window, nShowCmd) window_dc := GetDC(window) - header_size: U32 = SizeOf(BITMAPINFOHEADER) + header_size: U32 = sizeof(BITMAPINFOHEADER) Assert(header_size == 40) bminfo := BITMAPINFO{ BITMAPINFOHEADER{ diff --git a/build/examples/language_basics.core b/build/examples/language_basics.core index ad72646..08d1794 100644 --- a/build/examples/language_basics.core +++ b/build/examples/language_basics.core @@ -64,12 +64,12 @@ main :: (): int b = 2, } - size0 := SizeOf(Data) - size1 := SizeOf(data1) - align0 := AlignOf(Data) - align1 := AlignOf(data1) - type0 := TypeOf(Data) - type1 := TypeOf(data1) + size0 := sizeof(Data) + size1 := sizeof(data1) + align0 := alignof(Data) + align1 := alignof(data1) + type0 := typeof(Data) + type1 := typeof(data1) Assert(s64val == 0 && s32val == 0 && s16val == 0 && s8val == 0 && intval == 0 && u64val == 0 && u32val == 0 && u16val == 0 && u8val == 0 && f64val == 0 && f32val == 0) Assert(string_val[0] == 'S') @@ -92,7 +92,7 @@ main :: (): int Assert(size0 == size1) Assert(align0 == align1) Assert(type0 == type1) - Assert(TypeOf(data2) == Data) + Assert(typeof(data2) == Data) return 0 diff --git a/build/examples/polymorphism.core b/build/examples/polymorphism.core index 1711994..3ddd38e 100644 --- a/build/examples/polymorphism.core +++ b/build/examples/polymorphism.core @@ -61,7 +61,7 @@ C :: #import "LibC.core" Add :: (arr: *Array($T), val: T) if arr.cap == 0 arr.cap = 16 - arr.data = C.malloc(SizeOf(T)->U64 * arr.cap->U64) + arr.data = C.malloc(sizeof(T)->U64 * arr.cap->U64) arr.data[arr.len++] = val main :: (argc: int, argv: **char): int diff --git a/build/examples/push_struct.core b/build/examples/push_struct.core index 46ff98d..c2c8b71 100644 --- a/build/examples/push_struct.core +++ b/build/examples/push_struct.core @@ -1,7 +1,7 @@ MA :: #import "Arena.core" PushStruct :: (a: *MA.Arena, $K: Type, $T: Type): *T - size := SizeOf(T) + size := sizeof(T) result := MA.PushSize(a, size->U64) return result->*T @@ -9,7 +9,7 @@ main :: (argc: int, argv: **char): int arena: MA.Arena a: *int = PushStruct(&arena, int, int) b: *F32 = PushStruct(&arena, int, F32) - padding := SizeOf(int) - Assert(arena.len->S64 == (SizeOf(int) + SizeOf(F32) + padding)) + padding := sizeof(int) + Assert(arena.len->S64 == (sizeof(int) + sizeof(F32) + padding)) return 0 \ No newline at end of file diff --git a/build/examples/raymarcher.core b/build/examples/raymarcher.core index a0ded55..85c90c6 100644 --- a/build/examples/raymarcher.core +++ b/build/examples/raymarcher.core @@ -189,7 +189,7 @@ CreateBitmap :: (size: V2.Vec2I, bottom_up: bool = true): Windows_Bitmap if bottom_up == false result.size.y = -result.size.y - header_size: U32 = SizeOf(BITMAPINFOHEADER) + header_size: U32 = sizeof(BITMAPINFOHEADER) Assert(header_size == 40) bminfo := BITMAPINFO{ BITMAPINFOHEADER{ diff --git a/build/examples/runtime_type_information.core b/build/examples/runtime_type_information.core index 495fe32..fb3aa2e 100644 --- a/build/examples/runtime_type_information.core +++ b/build/examples/runtime_type_information.core @@ -14,10 +14,10 @@ main :: (): int return 1 if type_info.type == S64 - // We can use SizeOf and AlignOf operators + // We can use sizeof and alignof operators // to figure out the type alignment and it's size - Assert(type_info.size == SizeOf(S64)) - Assert(type_info.align == AlignOf(S64)) + Assert(type_info.size == sizeof(S64)) + Assert(type_info.align == alignof(S64)) else;; Assert(false, "We expected S64 here! What a boomer!") diff --git a/build/examples/unions.core b/build/examples/unions.core index 14d54b7..b35b9cc 100644 --- a/build/examples/unions.core +++ b/build/examples/unions.core @@ -18,7 +18,7 @@ main :: (argc: int, argv: **char): int t := U ti := GetTypeInfo(t) - Assert(ti.size == SizeOf(U)) + Assert(ti.size == sizeof(U)) for ti.struct_members Assert(it.offset == 0) diff --git a/build/modules/Arena.core b/build/modules/Arena.core index c32805b..27b7931 100644 --- a/build/modules/Arena.core +++ b/build/modules/Arena.core @@ -42,9 +42,9 @@ Release :: (a: *Arena) OS.Release(&a.memory) PushArray :: (a: *Arena, count: int, $T: Type): *T - result := PushSize(a, SizeOf(T) * count->U64) + result := PushSize(a, sizeof(T) * count->U64) return result->*T PushStruct :: (a: *Arena, $T: Type): *T - result := PushSize(a, SizeOf(T)) + result := PushSize(a, sizeof(T)) return result->*T \ No newline at end of file diff --git a/build/modules/Base.core b/build/modules/Base.core index a55b9f8..c4d5c6c 100644 --- a/build/modules/Base.core +++ b/build/modules/Base.core @@ -7,7 +7,7 @@ ClampTopSizeU :: (val: SizeU, max: SizeU): SizeU return max return val -GetAlignOffset :: (size: SizeU, align: SizeU): SizeU +Getalignoffset :: (size: SizeU, align: SizeU): SizeU mask := align - 1 val := size & mask if val != 0 @@ -15,7 +15,7 @@ GetAlignOffset :: (size: SizeU, align: SizeU): SizeU return val AlignUp :: (size: SizeU, align: SizeU): SizeU - result := size + GetAlignOffset(size, align) + result := size + Getalignoffset(size, align) return result ZeroMemory :: (p: *void, size: SizeU) diff --git a/build/modules/win32_multimedia.core b/build/modules/win32_multimedia.core index 7d7c442..219db3f 100644 --- a/build/modules/win32_multimedia.core +++ b/build/modules/win32_multimedia.core @@ -26,7 +26,7 @@ CreateBitmap :: (for_dc: HDC, size: Vec2I, bottom_up: bool = true): WIN32_Bitmap if bottom_up == false result.size.y = -result.size.y - header_size: U32 = SizeOf(BITMAPINFOHEADER) + header_size: U32 = sizeof(BITMAPINFOHEADER) Assert(header_size == 40) bminfo := BITMAPINFO{ BITMAPINFOHEADER{ @@ -50,7 +50,7 @@ DestroyBitmap :: (b: *WIN32_Bitmap) if IsValidBitmap(b) DeleteDC(b.hdc) DeleteObject(b.dib) - ZeroMemory(b, SizeOf(WIN32_Bitmap)) + ZeroMemory(b, sizeof(WIN32_Bitmap)) DrawBitmapInCompatibleDC :: (b: *WIN32_Bitmap) if IsValidBitmap(b) diff --git a/build/rtsgame/array.core b/build/rtsgame/array.core index 18e0ab8..0308352 100644 --- a/build/rtsgame/array.core +++ b/build/rtsgame/array.core @@ -41,7 +41,7 @@ Insert :: (a: *Array($T), item: T, index: int) TryGrowing(a) right_len := (a.len - index)->size_t - memmove(a.data + index + 1, a.data + index, SizeOf(T) * right_len) + memmove(a.data + index + 1, a.data + index, sizeof(T) * right_len) a.data[index] = item a.len += 1 @@ -69,16 +69,16 @@ OrderedRemove :: (a: *Array($T), item: *T) return length_right_of_item := (a.len - index - 1)->size_t - memmove(a.data + index, a.data + index + 1, length_right_of_item * SizeOf(T)) + memmove(a.data + index, a.data + index + 1, length_right_of_item * sizeof(T)) a.len -= 1 TryGrowing :: (a: *Array($T)) if a.cap == 0 a.cap = 16 - a.data = malloc(SizeOf(T) * a.cap->size_t) + a.data = malloc(sizeof(T) * a.cap->size_t) if a.len + 1 > a.cap a.cap *= 2 - a.data = realloc(a.data, SizeOf(T) * a.cap->size_t) + a.data = realloc(a.data, sizeof(T) * a.cap->size_t) Add :: (a: *Array($T), item: T) TryGrowing(a) @@ -103,6 +103,6 @@ InsertSortedDecreasing :: (a: *Array($T), item: T) Reserve :: (a: *Array($T), size: int) Assert(size > a.cap) a.cap = size - p := realloc(a.data, SizeOf(T) * a.cap->size_t) + p := realloc(a.data, sizeof(T) * a.cap->size_t) Assert(p != 0) a.data = p \ No newline at end of file diff --git a/build/rtsgame/main.core b/build/rtsgame/main.core index 84b29db..fdb201a 100644 --- a/build/rtsgame/main.core +++ b/build/rtsgame/main.core @@ -34,7 +34,7 @@ Guy :: struct Add :: (a: *Array($T), item: T) if a.cap == 0 a.cap = 16 - a.slice.data = malloc(SizeOf(T) * a.cap) + a.slice.data = malloc(sizeof(T) * a.cap) a.slice.data[a.slice.len++] = item guys: Array(Guy) @@ -89,6 +89,13 @@ MouseP: Vector2 Mode := 0 RectX :: 16 RectY :: 16 +Dt: F32 + +ANI_SetTile :: struct + set: bool + p: V2I + t: F32 +ANI_SetTiles: Array(ANI_SetTile) main :: (): int MAP_Init() @@ -107,6 +114,7 @@ main :: (): int MouseX = GetMouseX() MouseY = GetMouseY() MouseP = GetMousePosition() + Dt = GetFrameTime() map := &MAP_CurrentMap @@ -124,6 +132,9 @@ main :: (): int BeginDrawing() ClearBackground(RAYWHITE) + map_rectangle: Rectangle = {0, 0, map.x->F32 * RectX, map.y->F32 * RectY} + DrawRectangleRec(map_rectangle, LIGHTGRAY) + for x := 0, x < map.x, x += 1 for y := 0, y < map.y, y += 1 it := map.data + (x + y*map.x) @@ -132,16 +143,56 @@ main :: (): int colliding := CheckCollisionPointRec(MouseP, r) - if Mode == 0 && colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT) ;; *it = 1 - if Mode == 1 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_LEFT) ;; MAP_CurrentMap.actors.data[0].p = V2I{x,y} + if Mode == 0 && colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT) + Add(&ANI_SetTiles, {true, {x,y}}) + if Mode == 0 && colliding && IsMouseButtonDown(MOUSE_BUTTON_RIGHT) + Add(&ANI_SetTiles, {false, {x,y}}) + if Mode == 1 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_LEFT) ;; MAP_ActorSetP(MAP_CurrentMap.actors.data, {x,y}) if Mode == 1 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) ;; MAP_CurrentMap.actors.data[0].target_p = V2I{x,y} - color := GREEN - if *it == 1 ;; color = RED - if colliding == true ;; color = MAROON + color := RAYWHITE + if *it == 1 ;; color = GRAY + if colliding == true + color = MAROON + color.a = 255/2 DrawRectangleRec(r2, color) + for tile_i := 0, tile_i < ANI_SetTiles.len, tile_i += 1 + tile_it := ANI_SetTiles.data + tile_i + remove := false + + t := tile_it.t + if tile_it.set == false + t = 1 - t + + + x := tile_it.p.x->F32 * RectX + 1 + y := tile_it.p.y->F32 * RectY + 1 + w: F32 = (RectX - 2) + h: F32 = (RectY - 2) + wt := w * t + ht := h * t + wd := w - wt + hd := h - ht + + r: Rectangle = {x + wd/2, y + hd/2, wt, ht} + DrawRectangleRec(r, GRAY) + + + if tile_it.t > 1 + map_tile := map.data + (tile_it.p.x + tile_it.p.y*map.x) + if tile_it.set ;; *map_tile |= MAP_TILE_BLOCKER + else ;; *map_tile &= ~MAP_TILE_BLOCKER + remove = true + + tile_it.t += Dt*8 + if remove + UnorderedRemove(&ANI_SetTiles, tile_it) + tile_i -= 1 + + + for i := 0, i < map.actors.len, i += 1 actor_it := map.actors.data + i r := Rectangle{actor_it.p.x->F32 * RectX, actor_it.p.y->F32 * RectY, RectX, RectY} @@ -163,8 +214,20 @@ main :: (): int MAP_RecomputeHistory(actor_it) for path_i := 0, path_i < actor_it.history.len, path_i += 1 path_it := actor_it.history.data + path_i - path_r := Rectangle{path_it.p.x->F32 * RectX, path_it.p.y->F32 * RectY, RectX, RectY} - DrawRectangleRec(path_r, BLACK) + // path_r := Rectangle{path_it.p.x->F32 * RectX, path_it.p.y->F32 * RectY, RectX, RectY} + + x0 := path_it.came_from.x->F32 * RectX + RectX/2 + y0 := path_it.came_from.y->F32 * RectY + RectY/2 + + x1 := path_it.p.x->F32 * RectX + RectX/2 + y1 := path_it.p.y->F32 * RectY + RectY/2 + + p0 := Vector2{x0, y0} + p1 := Vector2{x1, y1} + + DrawLineEx(p0, p1, 5, LIGHTGRAY) + DrawCircleV(p0, 4, LIGHTGRAY) + DrawCircleV(p1, 4, LIGHTGRAY) if Mode == 0 ;; DrawText("F1", 0, 0, 32, BLACK) diff --git a/build/rtsgame/map.core b/build/rtsgame/map.core index 92783d6..846041c 100644 --- a/build/rtsgame/map.core +++ b/build/rtsgame/map.core @@ -36,10 +36,22 @@ MAP_AddActor :: (map: *MAP_Map, p: V2I): *MAP_Actor actor := GetLast(&MAP_CurrentMap.actors) return actor +MAP_ActorSetP :: (actor: *MAP_Actor, p: V2I) + map := actor.map + new_tile := map.data + p.x + p.y * map.x + if *new_tile != 0 ;; return + + tile := map.data + actor.p.x + actor.p.y * map.x + Assert((*tile & MAP_TILE_ACTOR_IS_STANDING) != 0) + *tile &= ~MAP_TILE_ACTOR_IS_STANDING + + *new_tile |= MAP_TILE_ACTOR_IS_STANDING + actor.p = p + MAP_Init :: () - MAP_CurrentMap.x = 60 - MAP_CurrentMap.y = 40 - bytes := SizeOf(MAP_Tile) * MAP_CurrentMap.x->U64 * MAP_CurrentMap.y->U64 + MAP_CurrentMap.x = WinX / RectX + MAP_CurrentMap.y = WinY / RectY + bytes := sizeof(MAP_Tile) * MAP_CurrentMap.x->U64 * MAP_CurrentMap.y->U64 MAP_CurrentMap.data = malloc(bytes) memset(MAP_CurrentMap.data, 0, bytes) actor := MAP_AddActor(&MAP_CurrentMap, {4,4}) @@ -109,7 +121,7 @@ MAP_MoveTowardsTarget :: (s: *MAP_Actor) MAP_PathFindStep :: (s: *MAP_Actor) if s.state == MAP_PATH_REACHED ;; return if s.open_paths.len == 0 - MAP_InsertOpenPath(s, s.p, s.target_p, ignore_blocks = true) + MAP_InsertOpenPath(s, s.p, s.p, ignore_blocks = true) it := Pop(&s.open_paths) Add(&s.close_paths, it) diff --git a/core_compiler.cpp b/core_compiler.cpp index 4587054..75a7973 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -67,10 +67,10 @@ for i in meta.token_simple_expr: pctx->keyword_enum = pctx->intern("enum"_s); pctx->interns.first_keyword = pctx->keyword_struct.str; pctx->interns.last_keyword = pctx->keyword_enum.str; - pctx->intern_typeof = pctx->intern("TypeOf"_s); - pctx->intern_sizeof = pctx->intern("SizeOf"_s); + pctx->intern_typeof = pctx->intern("typeof"_s); + pctx->intern_sizeof = pctx->intern("sizeof"_s); pctx->intern_len = pctx->intern("Len"_s); - pctx->intern_alignof = pctx->intern("AlignOf"_s); + pctx->intern_alignof = pctx->intern("alignof"_s); pctx->intern_foreign = pctx->intern("foreign"_s); pctx->intern_strict = pctx->intern("strict"_s); pctx->intern_void = pctx->intern("void"_s); diff --git a/core_lexing.cpp b/core_lexing.cpp index f72822a..5abd189 100644 --- a/core_lexing.cpp +++ b/core_lexing.cpp @@ -769,7 +769,7 @@ for i in meta.token_kinds: case TK_At: return "@"; case TK_Arrow: return "->"; case TK_Polymorph: return "$"; - case TK_ExprSizeof: return "[SizeOf]"; + case TK_ExprSizeof: return "[sizeof]"; case TK_DocComment: return "[///]"; case TK_Comment: return "//"; case TK_Identifier: return "[Ident]"; diff --git a/core_parsing.cpp b/core_parsing.cpp index 46bef6b..b3d2553 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -83,7 +83,7 @@ static void compiler_error(Token *token1, Token *token2, const char *str, ...) { Core_Message *msg = core_add_message(CORE_ERROR, string, token1, token2); if (pctx->debugger_break_on_compiler_error) { String str = core_stringify_message(pctx, pctx->perm, msg, GLOBAL_EnabledConsoleColors); - printf("%s", str.str); // @! How to get rid of printf ? + printf("%s\n", str.str); // @! How to get rid of printf ? fflush(stdout); Breakpoint; } @@ -95,7 +95,7 @@ compiler_error(Token *token, const char *str, ...) { Core_Message *msg = core_add_message(CORE_ERROR, string, token); if (pctx->debugger_break_on_compiler_error) { String str = core_stringify_message(pctx, pctx->perm, msg, GLOBAL_EnabledConsoleColors); - printf("%s", str.str); // @! How to get rid of printf ? + printf("%s\n", str.str); // @! How to get rid of printf ? fflush(stdout); Breakpoint; } diff --git a/core_printer.cpp b/core_printer.cpp index 30413ba..127d526 100644 --- a/core_printer.cpp +++ b/core_printer.cpp @@ -192,16 +192,16 @@ void core__stringify(Ast *ast) { } break; case AST_TYPE_OF: - gen("TypeOf"); + gen("typeof"); goto builtin; case AST_LENGTH_OF: gen("LengthOf"); goto builtin; case AST_ALIGN_OF: - gen("AlignOf"); + gen("alignof"); goto builtin; case AST_SIZE_OF: - gen("SizeOf"); + gen("sizeof"); goto builtin; case AST_RUNTIME_ASSERT: gen("Assert"); diff --git a/core_typechecking.cpp b/core_typechecking.cpp index 6d120fe..35143d5 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -1527,7 +1527,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str type_complete(type); if (type->size == 0) { - compiler_error(node->pos, "Internal compiler error: calling SizeOf but the resulting size of type is obviously invalid suggesting that type was not completed properly"); + compiler_error(node->pos, "Internal compiler error: calling sizeof but the resulting size of type is obviously invalid suggesting that type was not completed properly"); } Value v = value_int(type->size); @@ -1555,7 +1555,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str Ast_Type *type = name.type == pctx->type_type ? name.type_val : name.type; type_complete(type); if (type->size == 0) { - compiler_error(node->pos, "Internal compiler error: calling SizeOf but the resulting size of type is obviously invalid suggesting that type was not completed properly"); + compiler_error(node->pos, "Internal compiler error: calling sizeof but the resulting size of type is obviously invalid suggesting that type was not completed properly"); } Value v = value_int(type->align); diff --git a/meta.py b/meta.py index 8f6f4f3..9216816 100644 --- a/meta.py +++ b/meta.py @@ -84,7 +84,7 @@ token_rest = [ ["At", "@"], ["Arrow", "->"], ["Polymorph", "$"], - ["ExprSizeof", "[SizeOf]"], + ["ExprSizeof", "[sizeof]"], ["DocComment", "[///]"], ["Comment", "//"], ["Identifier", "[Ident]"], @@ -117,10 +117,10 @@ keywords = [ ] interns = [ - "TypeOf", - "SizeOf", + "typeof", + "sizeof", "Len", - "AlignOf", + "alignof", "foreign", "strict", "void",