Core: match sizeof etc. syntax with C, Core RTS: Beautifying + animations

This commit is contained in:
Krzosa Karol
2023-04-19 12:50:58 +02:00
parent 891221441e
commit 87657a99a6
20 changed files with 131 additions and 56 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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!")

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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