Core: Fix assigning to arrays

This commit is contained in:
Krzosa Karol
2023-04-21 09:45:57 +02:00
parent ef243724dc
commit 51cebbf4d0
3 changed files with 53 additions and 48 deletions

View File

@@ -3,7 +3,7 @@ call "..\misc\compile_setup.bat"
bld --dont_compile_core bld --dont_compile_core
cd build cd build
rem 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
rem build\generated_main.exe rem build\generated_main.exe
cd .. cd ..

View File

@@ -45,14 +45,6 @@ guys: Array(Guy)
Add(&guys, {100, 100}) Add(&guys, {100, 100})
*/ */
// @reproduction
//
//
// This generates: int MAP[256] = (int [256]){[0] = 0, [0] = 1}; it doesn't work because of the (int[256]) is this MSVC being retarded here? not supporting C properly?
// Also this ^ ^
#import "raylib.core" #import "raylib.core"
#load "array.core" #load "array.core"
MAP :: #load "map.core" MAP :: #load "map.core"
@@ -87,8 +79,6 @@ MouseSelectedActors: Array(*MAP.Actor) // @todo: ids
main :: (): int main :: (): int
MAP.Init() MAP.Init()
// HERE_IT_IS: [16*16]int = {0, 1}
// InitAudioDevice() // InitAudioDevice()
// sound := LoadSound("catune - Pass the town, and to the C.mp3") // sound := LoadSound("catune - Pass the town, and to the C.mp3")
// SetMasterVolume(0.01) // SetMasterVolume(0.01)

View File

@@ -305,6 +305,49 @@ gen_pointer(Ast_Expr *expr) {
} }
} }
CORE_Static void gen_try_any_or_slice(Ast_Expr *expr, Ast_Type *decl_type);
CORE_Static void
gen_compound(Ast *ast, bool generate_compound_decl = true) {
assert(ast->kind == AST_COMPOUND);
auto node = (Ast_Call *)ast;
bool generated_anything = false;
bool is_global_variable = node->parent_scope->kind == AST_FILE;
if (!is_global_variable && generate_compound_decl) {
gen("(");
gen_simple_decl(node->resolved_type);
gen(")");
}
gen("{");
// We need to double wrap it because Any is a pointer
// We need to pass it a compound array
if (is_slice(node->resolved_type)) {
generated_anything = true;
gen(".len = %d, ", node->exprs.len);
gen(".data = (");
gen_simple_decl(node->resolved_type->base);
gen("[]");
gen(")");
gen("{");
}
For(node->exprs) {
generated_anything = true;
if (is_struct(node->resolved_type) || is_union(node->resolved_type))
gen(".%Q = ", it->resolved_name);
else if (is_array(node->resolved_type))
gen("[%d] = ", (int)it->resolved_index);
gen_try_any_or_slice(it->item, it->resolved_type);
if (!node->exprs.is_last(it)) gen(", ");
}
if (is_slice(node->resolved_type)) gen("}");
if (!generated_anything) gen("0");
gen("}");
}
CORE_Static void CORE_Static void
gen_try_any_or_slice(Ast_Expr *expr, Ast_Type *decl_type) { gen_try_any_or_slice(Ast_Expr *expr, Ast_Type *decl_type) {
// We want normal values to get boxed as Any pointers // We want normal values to get boxed as Any pointers
@@ -328,6 +371,7 @@ gen_try_any_or_slice(Ast_Expr *expr, Ast_Type *decl_type) {
CORE_Static void CORE_Static void
gen_var(Ast_Decl *decl, B32 emit_value, B32 scope_names) { gen_var(Ast_Decl *decl, B32 emit_value, B32 scope_names) {
if (decl->name == pctx->internf("HERE_IT_IS")) __debugbreak();
if (is_flag_set(decl->flags, AST_FOREIGN)) gen("extern "); if (is_flag_set(decl->flags, AST_FOREIGN)) gen("extern ");
gen_simple_decl(decl->type, decl->name); gen_simple_decl(decl->type, decl->name);
if (is_flag_set(decl->flags, AST_FOREIGN)) return; if (is_flag_set(decl->flags, AST_FOREIGN)) return;
@@ -337,9 +381,14 @@ gen_var(Ast_Decl *decl, B32 emit_value, B32 scope_names) {
} }
if (decl->expr) { if (decl->expr) {
if (is_array(decl->type)) {
gen_compound((Ast_Call *)decl->expr, false);
}
else {
gen(" = "); gen(" = ");
gen_try_any_or_slice(decl->expr, decl->type); gen_try_any_or_slice(decl->expr, decl->type);
} }
}
else { // Default zero else { // Default zero
if (is_numeric(decl->type)) { if (is_numeric(decl->type)) {
gen(" = 0"); gen(" = 0");
@@ -505,41 +554,7 @@ gen_expr(Ast_Expr *ast) {
} }
CASE(COMPOUND, Call) { CASE(COMPOUND, Call) {
bool generated_anything = false; gen_compound(node);
bool is_global_variable = node->parent_scope->kind == AST_FILE;
if (!is_global_variable) {
gen("(");
gen_simple_decl(node->resolved_type);
gen(")");
}
gen("{");
// We need to double wrap it because Any is a pointer
// We need to pass it a compound array
if (is_slice(node->resolved_type)) {
generated_anything = true;
gen(".len = %d, ", node->exprs.len);
gen(".data = (");
gen_simple_decl(node->resolved_type->base);
gen("[]");
gen(")");
gen("{");
}
For(node->exprs) {
generated_anything = true;
if (is_struct(node->resolved_type) || is_union(node->resolved_type))
gen(".%Q = ", it->resolved_name);
else if (is_array(node->resolved_type))
gen("[%d] = ", (int)it->resolved_index);
gen_try_any_or_slice(it->item, it->resolved_type);
if (!node->exprs.is_last(it)) gen(", ");
}
if (is_slice(node->resolved_type)) gen("}");
if (!generated_anything) gen("0");
gen("}");
BREAK(); BREAK();
} }