From 1e12f51451e8c7e8a57240dcaa04455fb0c6e0df Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 21 Apr 2023 09:06:48 +0200 Subject: [PATCH] Compiler: Fix empty return compound codegen --- build/rtsgame/array.core | 6 +----- build/rtsgame/main.core | 6 ++++++ build/rtsgame/map.core | 2 +- core_codegen_c_language.cpp | 8 +++++++- core_compiler_interface.hpp | 1 + 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/build/rtsgame/array.core b/build/rtsgame/array.core index 0308352..9000e1b 100644 --- a/build/rtsgame/array.core +++ b/build/rtsgame/array.core @@ -9,11 +9,7 @@ Pop :: (a: *Array($T)): T if a.len > 0 a.len -= 1 return a.data[a.len] - // @reproduction: c compile error, probably because of compound expression (Array){} - // return {} - - result: T - return result + return {} Contains :: (a: *Array($T), item: *T): bool result := item >= a.data && item < a.data + a.len diff --git a/build/rtsgame/main.core b/build/rtsgame/main.core index 9af3fd4..511940a 100644 --- a/build/rtsgame/main.core +++ b/build/rtsgame/main.core @@ -1,4 +1,10 @@ + + +/*@language_refactor: Remove tuples and simplify, Make var unpacking general*/ + +/*@language_todo: make polymorphism checking more robust*/ + /*@language_todo: labeled block It acts just like a scope in C. diff --git a/build/rtsgame/map.core b/build/rtsgame/map.core index 66bc7f6..c19f596 100644 --- a/build/rtsgame/map.core +++ b/build/rtsgame/map.core @@ -85,7 +85,7 @@ GetRandomUnblockedP :: (m: *Map): V2I return p Assert(false, "Invalid codepath") - r: V2I; return r + return {} Init :: () CurrentMap.x = WinX / RectX diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index 654a5f1..13f42ec 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -11,6 +11,9 @@ } while (0) global S32 global_indent; global S32 is_inside_struct; +/*@language_refactor: Add context to typechecking and codegen, use arrays to construct stacks + instead of passing arguments to functions you use the context stacks +*/ CORE_Static void gen_ast(Ast *ast); CORE_Static bool gen_expr(Ast_Expr *ast); @@ -304,7 +307,6 @@ gen_pointer(Ast_Expr *expr) { 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 // but other then that we shouldn't box other Any values if (expr->kind != AST_COMPOUND && is_any(decl_type) && !is_any(expr->resolved_type)) { @@ -503,6 +505,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("("); @@ -514,6 +517,7 @@ gen_expr(Ast_Expr *ast) { // 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); @@ -523,6 +527,7 @@ gen_expr(Ast_Expr *ast) { } 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)) @@ -533,6 +538,7 @@ gen_expr(Ast_Expr *ast) { } if (is_slice(node->resolved_type)) gen("}"); + if (!generated_anything) gen("0"); gen("}"); BREAK(); } diff --git a/core_compiler_interface.hpp b/core_compiler_interface.hpp index db1480a..b1d5104 100644 --- a/core_compiler_interface.hpp +++ b/core_compiler_interface.hpp @@ -595,6 +595,7 @@ struct Ast_Scope : Ast { uint32_t scope_id; Ast_Scope *file; // Self referential for file and module Ast_Module *module; + Ast_Decl *parent_decl; // @language_todo: add parent decl }; enum Ast_Module_State {