From 51cebbf4d03e84b02dd409c9b0abb3b071d39602 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 21 Apr 2023 09:45:57 +0200 Subject: [PATCH] Core: Fix assigning to arrays --- build.bat | 2 +- build/rtsgame/main.core | 10 ----- core_codegen_c_language.cpp | 89 ++++++++++++++++++++++--------------- 3 files changed, 53 insertions(+), 48 deletions(-) diff --git a/build.bat b/build.bat index 7d8ea68..8e18089 100644 --- a/build.bat +++ b/build.bat @@ -3,7 +3,7 @@ call "..\misc\compile_setup.bat" bld --dont_compile_core 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 rem build\generated_main.exe cd .. diff --git a/build/rtsgame/main.core b/build/rtsgame/main.core index 80f8282..756f075 100644 --- a/build/rtsgame/main.core +++ b/build/rtsgame/main.core @@ -45,14 +45,6 @@ guys: Array(Guy) 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" #load "array.core" MAP :: #load "map.core" @@ -87,8 +79,6 @@ MouseSelectedActors: Array(*MAP.Actor) // @todo: ids main :: (): int MAP.Init() - // HERE_IT_IS: [16*16]int = {0, 1} - // InitAudioDevice() // sound := LoadSound("catune - Pass the town, and to the C.mp3") // SetMasterVolume(0.01) diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index 13f42ec..17ef26b 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -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 gen_try_any_or_slice(Ast_Expr *expr, Ast_Type *decl_type) { // 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 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 "); gen_simple_decl(decl->type, decl->name); if (is_flag_set(decl->flags, AST_FOREIGN)) return; @@ -337,8 +381,13 @@ gen_var(Ast_Decl *decl, B32 emit_value, B32 scope_names) { } if (decl->expr) { - gen(" = "); - gen_try_any_or_slice(decl->expr, decl->type); + if (is_array(decl->type)) { + gen_compound((Ast_Call *)decl->expr, false); + } + else { + gen(" = "); + gen_try_any_or_slice(decl->expr, decl->type); + } } else { // Default zero if (is_numeric(decl->type)) { @@ -505,41 +554,7 @@ gen_expr(Ast_Expr *ast) { } CASE(COMPOUND, Call) { - bool generated_anything = false; - 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("}"); + gen_compound(node); BREAK(); }