Core Compiler: Fix type incomplete polymorph, work on RTS

This commit is contained in:
Krzosa Karol
2023-04-18 16:23:36 +02:00
parent 4ecb329033
commit bea10a621e
6 changed files with 73 additions and 36 deletions

View File

@@ -119,6 +119,8 @@ typedef double F64;
#define F64MAX DBL_MAX #define F64MAX DBL_MAX
#define F64MIN DBL_MIN #define F64MIN DBL_MIN
#include <stdio.h>
#define api #define api
#define CORE_Static static #define CORE_Static static
#define global static #define global static
@@ -126,7 +128,14 @@ typedef double F64;
do { \ do { \
if (!(x)) Breakpoint; \ if (!(x)) Breakpoint; \
} while (0) } while (0)
#define assert_message(x, ...) assert(x) #define assert_message(x, ...) \
do { \
if (!(x)) { \
printf(__VA_ARGS__); \
printf("\n"); \
Breakpoint; \
} \
} while (0)
#define invalid_codepath assert_message(0, "Invalid codepath") #define invalid_codepath assert_message(0, "Invalid codepath")
#define invalid_return \ #define invalid_return \
do { \ do { \

View File

@@ -1,7 +1,7 @@
@echo off @echo off
call "..\misc\compile_setup.bat" call "..\misc\compile_setup.bat"
rem bld --dont_compile_core bld --dont_compile_core
cd build cd build
core_main.exe rtsgame/main.core core_main.exe rtsgame/main.core
bld --dont_compile_core --link=vendor/raylib/windows/raylibdll.lib bld --dont_compile_core --link=vendor/raylib/windows/raylibdll.lib

View File

@@ -26,12 +26,27 @@ Free :: (a: *Array($T))
Reset :: (a: *Array($T)) Reset :: (a: *Array($T))
a.len = 0 a.len = 0
Insert :: (a: *Array($T), item: T, index: int)
if index == a.len
Add(a, item)
return
Assert(index < a.len)
Assert(index >= 0)
TryGrowing(a)
right_len := (a.len - index)->size_t
memmove(a.data + index + 1, a.data + index, SizeOf(T) * right_len)
a.data[index] = item
a.len += 1
GetIndex :: (a: *Array($T), item: *T): int GetIndex :: (a: *Array($T), item: *T): int
Assert(a.len > 0) Assert(a.len > 0)
Assert(item >= a.data && item < a.data + a.len) Assert(item >= a.data && item < a.data + a.len)
// @reproduction: compiler hangs // @reproduction: compiler hangs
// index := (item - a.data)->*void->size_t // index := (item - a.data)->*void->size_t
index := (item - a.data)->int index := (item - a.data)->int
Assert(index >= 0 && index < a.len) Assert(index >= 0 && index < a.len)
return index return index
@@ -52,7 +67,6 @@ OrderedRemove :: (a: *Array($T), item: *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 a.len -= 1
TryGrowing :: (a: *Array($T)) TryGrowing :: (a: *Array($T))
if a.cap == 0 if a.cap == 0
a.cap = 16 a.cap = 16
@@ -61,11 +75,25 @@ TryGrowing :: (a: *Array($T))
a.cap *= 2 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) Add :: (a: *Array($T), item: T)
TryGrowing(a) TryGrowing(a)
a.data[a.len++] = item a.data[a.len++] = item
BoundedAdd :: (a: *Array($T), item: T)
Assert(a.len + 1 <= a.cap)
Add(a, item)
InsertSortedDecreasing :: (a: *Array($T), item: T)
insert_index := -1
for i := 0, i < a.len, i += 1
it := a.data + i
if it.value_to_sort_by <= item.value_to_sort_by
Insert(a, item, i)
break
if insert_index == -1
Add(a, item)
Reserve :: (a: *Array($T), size: int) Reserve :: (a: *Array($T), size: int)
Assert(size > a.cap) Assert(size > a.cap)
a.cap = size a.cap = size

View File

@@ -71,51 +71,24 @@ Add(&guys, {100, 100})
#import "raylib.core" #import "raylib.core"
MA :: #import "Arena.core"
#load "array.core" #load "array.core"
#load "map.core"
V2I :: struct V2I :: struct
x: int x: int
y: int y: int
Map_Tile :: int
Map :: struct
data: *Map_Tile
x: int
y: int
Actor :: struct
p: V2I
target_p: V2I
WinX := 1280 WinX := 1280
WinY := 720 WinY := 720
MouseX := 0 MouseX := 0
MouseY := 0 MouseY := 0
MouseP: Vector2 MouseP: Vector2
Mode := 0 Mode := 0
RectX :: 16 RectX :: 16
RectY :: 16 RectY :: 16
GuyP: V2I
main :: (): int main :: (): int
MAP_Init()
MAP_X :: 60
MAP_Y :: 40
map_data: [MAP_X*MAP_Y]int
map: Map = {data = &map_data[0], x = MAP_X, y = MAP_Y}
actors: Array(Actor)
Reserve(&actors, 4)
Add(&actors, {{4, 4}, {8, 8}})
Assert(Contains(&actors, actors.data) == true)
OrderedRemove(&actors, actors.data)
Assert(Contains(&actors, actors.data) == false)
// InitAudioDevice() // InitAudioDevice()
// sound := LoadSound("catune - Pass the town, and to the C.mp3") // sound := LoadSound("catune - Pass the town, and to the C.mp3")
@@ -138,6 +111,8 @@ main :: (): int
BeginDrawing() BeginDrawing()
ClearBackground(RAYWHITE) ClearBackground(RAYWHITE)
map := &MAP_CurrentMap
for x := 0, x < map.x, x += 1 for x := 0, x < map.x, x += 1
for y := 0, y < map.y, y += 1 for y := 0, y < map.y, y += 1
it := map.data + (x + y*map.x) it := map.data + (x + y*map.x)
@@ -147,7 +122,7 @@ main :: (): int
colliding := CheckCollisionPointRec(MouseP, r) colliding := CheckCollisionPointRec(MouseP, r)
if Mode == 0 && colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT) ;; *it = 1 if Mode == 0 && colliding && IsMouseButtonDown(MOUSE_BUTTON_LEFT) ;; *it = 1
if Mode == 0 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) ;; GuyP = V2I{x,y} // if Mode == 0 && colliding && IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) ;; GuyP = V2I{x,y}
color := GREEN color := GREEN
if *it == 1 ;; color = RED if *it == 1 ;; color = RED
@@ -155,8 +130,8 @@ main :: (): int
DrawRectangleRec(r2, color) DrawRectangleRec(r2, color)
for i := 0, i < actors.len, i += 1 for i := 0, i < map.actors.len, i += 1
it := actors.data + i it := map.actors.data + i
r := Rectangle{it.p.x->F32 * RectX, it.p.y->F32 * RectY, RectX, RectY} r := Rectangle{it.p.x->F32 * RectX, it.p.y->F32 * RectY, RectX, RectY}
target_r := Rectangle{it.target_p.x->F32 * RectX, it.target_p.y->F32 * RectY, RectX, RectY} target_r := Rectangle{it.target_p.x->F32 * RectX, it.target_p.y->F32 * RectY, RectX, RectY}

24
build/rtsgame/map.core Normal file
View File

@@ -0,0 +1,24 @@
MAP_CurrentMap: MAP_Map
MAP_Tile :: int
MAP_Map :: struct
data: *MAP_Tile
x: int
y: int
actors: Array(Actor)
Actor :: struct
p: V2I
target_p: V2I
MAP_Init :: ()
MAP_X :: 60
MAP_Y :: 40
map_data: [MAP_X*MAP_Y]int
MAP_CurrentMap = MAP_Map{data = &map_data[0], x = MAP_X, y = MAP_Y}
Add(&MAP_CurrentMap.actors, {{4, 4}, {8, 8}})

View File

@@ -205,6 +205,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Poly_Replacement> *repl)
dst = (Ast_Atom *)ast_copy(it.ast, parent_scope, 0); dst = (Ast_Atom *)ast_copy(it.ast, parent_scope, 0);
} }
else if (it.kind == POLY_TYPE_REPLACEMENT) { else if (it.kind == POLY_TYPE_REPLACEMENT) {
type_complete(it.type);
dst = (Ast_Atom *)create_typespec_from_type(dst->pos, parent_scope, it.type); dst = (Ast_Atom *)create_typespec_from_type(dst->pos, parent_scope, it.type);
} }
else invalid_codepath; else invalid_codepath;