From 83b835bce6731f9cf3ff73d736be79619a4bc2d2 Mon Sep 17 00:00:00 2001 From: JaDogg Date: Mon, 29 Apr 2024 23:57:10 +0100 Subject: [PATCH] fix(entry_struct_func_compiler): use simple representation of types to create type names --- .../compiler/entry_struct_func_compiler.cpp | 39 +++++++++++-------- .../src/compiler/entry_struct_func_compiler.h | 1 + compiler/src/compiler/type_checker.cpp | 32 +++++++-------- compiler/src/utilities/ykdatatype.cpp | 4 +- compiler/src/utilities/ykdatatype.h | 2 +- .../arrays/fixed_arr_loop.yaka.c | 6 +-- .../arrays/fixed_arr_structure.yaka.c | 32 +++++++-------- .../arrays/fixed_array_copying.yaka.c | 6 +-- .../arrays/simple_fixed_arr.yaka.c | 4 +- 9 files changed, 65 insertions(+), 61 deletions(-) diff --git a/compiler/src/compiler/entry_struct_func_compiler.cpp b/compiler/src/compiler/entry_struct_func_compiler.cpp index 239b220a..3d1e7d75 100644 --- a/compiler/src/compiler/entry_struct_func_compiler.cpp +++ b/compiler/src/compiler/entry_struct_func_compiler.cpp @@ -56,9 +56,10 @@ entry_struct_func_compiler::entry_struct_func_compiler(ykdt_pool *pool) std::string entry_struct_func_compiler::compile(ykdatatype *entry_dt, datatype_compiler *dtc) { std::string repr = entry_dt->as_string(); + std::string simple_repr = entry_dt->as_string_simplified(); if (autogen_structs_.find(repr) != autogen_structs_.end()) { return "struct " + - improve_name(repr, + improve_name(repr, simple_repr, "ykentry" + std::to_string(autogen_structs_[repr])); } entry_data d{}; @@ -74,8 +75,8 @@ std::string entry_struct_func_compiler::compile(ykdatatype *entry_dt, return "<><>"; } std::stringstream code{}; - std::string name = - improve_name(repr, "ykentry" + std::to_string(d.incremented_id_)); + std::string name = improve_name( + repr, simple_repr, "ykentry" + std::to_string(d.incremented_id_)); code << "struct " << name << " { " << dtc->convert_dt(d.key_dt_, datatype_location::STRUCT, "", "") << " key; " @@ -85,7 +86,7 @@ std::string entry_struct_func_compiler::compile(ykdatatype *entry_dt, autogen_structs_list_.emplace_back(d); autogen_structs_[repr] = d.incremented_id_; forward_decls_ << "struct " << name << ";\n"; - return "struct " + improve_name(repr, name); + return "struct " + improve_name(repr, simple_repr, name); } void entry_struct_func_compiler::compile_forward_declarations( std::stringstream &target) { @@ -102,9 +103,11 @@ std::string entry_struct_func_compiler::compile_function_dt(ykdatatype *function_dt, datatype_compiler *dtc) { std::string fdt_str = function_dt->as_string(); + std::string simple_fdt_str = function_dt->as_string_simplified(); if (autogen_func_typedefs_.find(fdt_str) != autogen_func_typedefs_.end()) { - return improve_name( - fdt_str, "ykfncptr" + std::to_string(autogen_func_typedefs_[fdt_str])); + return improve_name(fdt_str, simple_fdt_str, + "ykfncptr" + + std::to_string(autogen_func_typedefs_[fdt_str])); } // Check assumption that must not happen if (!function_dt->is_function() || function_dt->args_.size() != 2 || @@ -129,8 +132,8 @@ entry_struct_func_compiler::compile_function_dt(ykdatatype *function_dt, code << "<><>"; } unsigned int current_num = counter_functions_++; - std::string name = - improve_name(fdt_str, "ykfncptr" + std::to_string(current_num)); + std::string name = improve_name(fdt_str, simple_fdt_str, + "ykfncptr" + std::to_string(current_num)); code << "(*" << name << ")("; if (input->args_.empty()) { code << "void"; @@ -163,17 +166,18 @@ bool entry_struct_func_compiler::has_functions() { std::string entry_struct_func_compiler::compile_tuple(ykdatatype *tuple_dt, datatype_compiler *dtc) { std::string repr = tuple_dt->as_string(); + std::string simple_repr = tuple_dt->as_string_simplified(); if (autogen_tuples_.find(repr) != autogen_tuples_.end()) { - std::string name = - improve_name(repr, "yktuple" + std::to_string(autogen_tuples_[repr])); + std::string name = improve_name( + repr, simple_repr, "yktuple" + std::to_string(autogen_tuples_[repr])); return "struct " + name; } tuple_data d{}; d.incremented_id_ = counter_tuples_++; d.tuple_dt_ = tuple_dt; std::stringstream code{}; - std::string name = - improve_name(repr, "yktuple" + std::to_string(d.incremented_id_)); + std::string name = improve_name( + repr, simple_repr, "yktuple" + std::to_string(d.incremented_id_)); code << "struct " << name << " {"; size_t i = 1; for (ykdatatype *dt_arg : d.tuple_dt_->args_) { @@ -228,8 +232,9 @@ std::string entry_struct_func_compiler::compile_fixed_array(ykdatatype *fixed_array_dt, datatype_compiler *dtc) { std::string fxa_str = fixed_array_dt->as_string(); + std::string fxa_str_simple = fixed_array_dt->as_string_simplified(); if (autogen_fxa_.find(fxa_str) != autogen_fxa_.end()) { - return improve_name(fxa_str, + return improve_name(fxa_str, fxa_str_simple, "ykfxa" + std::to_string(autogen_fxa_[fxa_str])); } // Check assumption that must not happen @@ -249,8 +254,8 @@ entry_struct_func_compiler::compile_fixed_array(ykdatatype *fixed_array_dt, code << dtc->convert_dt(target_datatype, datatype_location::STRUCT, "", "") << " "; unsigned int current_num = counter_fxa_++; - std::string name = - improve_name(fxa_str, "ykfxa" + std::to_string(current_num)); + std::string name = improve_name(fxa_str, fxa_str_simple, + "ykfxa" + std::to_string(current_num)); code << name << "["; code << size_specifier->token_->token_; code << "];\n"; @@ -309,13 +314,13 @@ std::string sanitize_name(const std::string &input) { return result; } std::string entry_struct_func_compiler::improve_name( - const std::string &yaksha_datatype_string, + const std::string &yaksha_datatype_string, const std::string &simple_name, const std::string &numbered_name) { if (name_improvements_.find(yaksha_datatype_string) != name_improvements_.end()) { return name_improvements_[yaksha_datatype_string]; } - std::string improved = sanitize_name(yaksha_datatype_string); + std::string improved = sanitize_name(simple_name); // too large use numbered name if (improved.size() > 60 || improved.empty()) { improved = numbered_name; } // conflict, so use numbered name diff --git a/compiler/src/compiler/entry_struct_func_compiler.h b/compiler/src/compiler/entry_struct_func_compiler.h index e2deb1d8..7d17f26e 100644 --- a/compiler/src/compiler/entry_struct_func_compiler.h +++ b/compiler/src/compiler/entry_struct_func_compiler.h @@ -79,6 +79,7 @@ namespace yaksha { private: std::string improve_name(const std::string &yaksha_datatype_string, + const std::string &simple_name, const std::string &numbered_name); std::unordered_map name_improvements_; std::unordered_map reverse_name_improvements_; diff --git a/compiler/src/compiler/type_checker.cpp b/compiler/src/compiler/type_checker.cpp index aca83cd3..804a7415 100644 --- a/compiler/src/compiler/type_checker.cpp +++ b/compiler/src/compiler/type_checker.cpp @@ -293,7 +293,7 @@ void type_checker::visit_fncall_expr(fncall_expr *obj) { if (!slot_match(arg, param.data_type_)) { std::stringstream message{}; message << "Variable argument: " << (j + 1) << " mismatches. "; - message << "Expected: " << param.data_type_->as_simple_string(); + message << "Expected: " << param.data_type_->as_string_simplified(); error(obj->paren_token_, message.str()); } } @@ -302,7 +302,7 @@ void type_checker::visit_fncall_expr(fncall_expr *obj) { if (!slot_match(arg, param.data_type_)) { std::stringstream message{}; message << "Parameter & argument " << (i + 1) << " mismatches. "; - message << "Expected: " << param.data_type_->as_simple_string(); + message << "Expected: " << param.data_type_->as_string_simplified(); error(obj->paren_token_, message.str()); } } @@ -355,8 +355,8 @@ void type_checker::visit_fncall_expr(fncall_expr *obj) { std::stringstream message{}; message << "Function[] call parameter & argument " << (i + 1) << " mismatches. "; - message << "Expected: " << param->as_simple_string() - << ", Provided: " << arg.datatype_->as_simple_string(); + message << "Expected: " << param->as_string_simplified() + << ", Provided: " << arg.datatype_->as_string_simplified(); error(obj->paren_token_, message.str()); } } @@ -377,7 +377,7 @@ void type_checker::visit_fncall_expr(fncall_expr *obj) { } else { message << " is not allowed."; } - message << " datatype: " << name.datatype_->as_simple_string(); + message << " datatype: " << name.datatype_->as_string_simplified(); error(obj->paren_token_, message.str()); push(ykobject(dt_pool_));// Push None here } @@ -453,8 +453,8 @@ void type_checker::visit_logical_expr(logical_expr *obj) { std::stringstream message{}; message << "Both LHS and RHS of logical" " operator need to be boolean. "; - message << "LHS: " << lhs.datatype_->as_simple_string() - << ", RHS: " << rhs.datatype_->as_simple_string(); + message << "LHS: " << lhs.datatype_->as_string_simplified() + << ", RHS: " << rhs.datatype_->as_string_simplified(); error(obj->opr_, message.str()); } push(rhs); @@ -470,17 +470,17 @@ void type_checker::visit_unary_expr(unary_expr *obj) { if (obj->opr_->type_ == token_type::KEYWORD_NOT && !rhs.datatype_->const_unwrap()->is_bool()) { message << "Invalid unary operation. Not operator must follow a boolean."; - message << " Provided: " << rhs.datatype_->as_simple_string(); + message << " Provided: " << rhs.datatype_->as_string_simplified(); error(obj->opr_, message.str()); } else if (obj->opr_->type_ == token_type::TILDE && !rhs.datatype_->is_an_integer()) { message << "Bitwise not (~) is only supported for integers."; - message << " Provided: " << rhs.datatype_->as_simple_string(); + message << " Provided: " << rhs.datatype_->as_string_simplified(); error(obj->opr_, message.str()); } } else { message << "Unary operator is not supported for data type: "; - message << rhs.datatype_->as_simple_string(); + message << rhs.datatype_->as_string_simplified(); error(obj->opr_, message.str()); } push(rhs); @@ -621,7 +621,7 @@ void type_checker::visit_if_stmt(if_stmt *obj) { if (!bool_expression.is_primitive_or_obj() || !bool_expression.datatype_->const_unwrap()->is_bool()) { message << "If statement expression must be a boolean. "; - message << "Provided: " << bool_expression.datatype_->as_simple_string(); + message << "Provided: " << bool_expression.datatype_->as_string_simplified(); error(obj->if_keyword_, message.str()); } scope_.push(); @@ -672,9 +672,9 @@ void type_checker::visit_return_stmt(return_stmt *obj) { auto func = this->defs_classes_->get_function(function_name); if (!slot_match(return_data_type, func->return_type_)) { message << "Invalid return data type. "; - message << "Expected: " << func->return_type_->as_simple_string(); + message << "Expected: " << func->return_type_->as_string_simplified(); message << ", Provided: " - << return_data_type.datatype_->as_simple_string(); + << return_data_type.datatype_->as_string_simplified(); error(obj->return_keyword_, message.str()); } obj->result_type_ = func->return_type_; @@ -1164,7 +1164,7 @@ void type_checker::visit_foreach_stmt(foreach_stmt *obj) { if (!exp.datatype_->const_unwrap()->is_array() && !exp.datatype_->const_unwrap()->is_fixed_size_array()) { message << "foreach statement expression must be an array. "; - message << "Provided: " << exp.datatype_->as_simple_string(); + message << "Provided: " << exp.datatype_->as_string_simplified(); error(obj->for_keyword_, message.str()); } if ((exp.datatype_->const_unwrap()->is_array() && @@ -1187,8 +1187,8 @@ void type_checker::visit_foreach_stmt(foreach_stmt *obj) { auto rhs = obj->data_type_; if ((*lhs != *rhs)) { message << "foreach statement expression and element data type does not match."; - message << " LHS: " << lhs->as_simple_string(); - message << ", RHS: " << rhs->as_simple_string(); + message << " LHS: " << lhs->as_string_simplified(); + message << ", RHS: " << rhs->as_string_simplified(); error(obj->for_keyword_, message.str()); } push_scope_type(ast_type::STMT_WHILE); diff --git a/compiler/src/utilities/ykdatatype.cpp b/compiler/src/utilities/ykdatatype.cpp index b046d2f2..1af08acb 100644 --- a/compiler/src/utilities/ykdatatype.cpp +++ b/compiler/src/utilities/ykdatatype.cpp @@ -132,7 +132,7 @@ bool ykdatatype::is_none() const { return primitive_type_ == ykprimitive::NONE; } void ykdatatype::write_to_str(std::stringstream &s, bool write_mod) const { - if (!module_.empty() && write_mod) { s << module_ << ":::"; } + if (!module_.empty() && write_mod && !is_dimension()) { s << module_ << ":::"; } s << token_->token_; if (!args_.empty()) { s << "["; @@ -153,7 +153,7 @@ std::string ykdatatype::as_string() const { this->write_to_str(s); return s.str(); } -std::string ykdatatype::as_simple_string() const { +std::string ykdatatype::as_string_simplified() const { std::stringstream s{}; this->write_to_str(s, false); return s.str(); diff --git a/compiler/src/utilities/ykdatatype.h b/compiler/src/utilities/ykdatatype.h index a0d74cea..a5aa64f8 100644 --- a/compiler/src/utilities/ykdatatype.h +++ b/compiler/src/utilities/ykdatatype.h @@ -99,7 +99,7 @@ namespace yaksha { [[nodiscard]] ykdatatype *auto_cast(ykdatatype *rhs, ykdt_pool *pool, bool lhs_mutates, bool assignment); [[nodiscard]] std::string as_string() const; - [[nodiscard]] std::string as_simple_string() const; + [[nodiscard]] std::string as_string_simplified() const; // meta type [[nodiscard]] bool is_primitive() const; diff --git a/compiler/test_data/compiler_tests/arrays/fixed_arr_loop.yaka.c b/compiler/test_data/compiler_tests/arrays/fixed_arr_loop.yaka.c index 6d37fe32..9191f99b 100644 --- a/compiler/test_data/compiler_tests/arrays/fixed_arr_loop.yaka.c +++ b/compiler/test_data/compiler_tests/arrays/fixed_arr_loop.yaka.c @@ -1,8 +1,6 @@ // YK #include "yk__lib.h" // --forward declarations-- -typedef int32_t ykfxa0[3]; -typedef struct yk__bstr ykfxa1[3]; typedef int32_t yt_arr_int_3[3]; typedef struct yk__bstr yt_arr_sr_3[3]; typedef int32_t yt_arr_int_4[4]; @@ -12,11 +10,11 @@ int32_t yy__main(); int32_t yy__main() { yk__printlnstr("Fixed Array looping"); - ykfxa0 yy__a = {}; + yt_arr_int_3 yy__a = {}; yy__a[INT32_C(0)] = INT32_C(1); yy__a[INT32_C(1)] = INT32_C(2); yy__a[INT32_C(2)] = INT32_C(3); - ykfxa1 yy__b = {}; + yt_arr_sr_3 yy__b = {}; yy__b[INT32_C(0)] = yk__bstr_s("hello", 5); yy__b[INT32_C(1)] = yk__bstr_s("world", 5); yy__b[INT32_C(2)] = yk__bstr_s("!", 1); diff --git a/compiler/test_data/compiler_tests/arrays/fixed_arr_structure.yaka.c b/compiler/test_data/compiler_tests/arrays/fixed_arr_structure.yaka.c index 13121a0d..ad703678 100644 --- a/compiler/test_data/compiler_tests/arrays/fixed_arr_structure.yaka.c +++ b/compiler/test_data/compiler_tests/arrays/fixed_arr_structure.yaka.c @@ -4,28 +4,28 @@ struct yy__A; struct yy__B; typedef int32_t (*yt_fn_in_int_out_int)(int32_t); -typedef int32_t ykfxa0[3]; -typedef int32_t ykfxa1[2]; -typedef ykfxa1 ykfxa2[2]; -typedef int32_t* ykfxa3[2]; -typedef int32_t** ykfxa4[2]; -typedef yt_fn_in_int_out_int ykfxa5[2]; -typedef int32_t* const ykfxa6[2]; +typedef int32_t yt_arr_int_3[3]; +typedef int32_t yt_arr_int_2[2]; +typedef yt_arr_int_2 yt_arr_arr_int_2_2[2]; +typedef int32_t* yt_arr_ptr_int_2[2]; +typedef int32_t** yt_arr_ptr_ptr_int_2[2]; +typedef yt_fn_in_int_out_int yt_arr_fn_in_int_out_int_2[2]; +typedef int32_t* const yt_arr_const_ptr_int_2[2]; int32_t yy__main(); // --structs-- struct yy__A { int32_t yy__a; - ykfxa0 yy__b; - ykfxa0 yy__c; + yt_arr_int_3 yy__b; + yt_arr_int_3 yy__c; }; struct yy__B { - ykfxa2 yy__nested; - ykfxa1* yy__ptr_nested; - ykfxa3 yy__nested_ptr; - ykfxa3* yy__ptr_nested_ptr; - ykfxa4 yy__nested_ptr_ptr; - ykfxa5 yy__nested_func; - ykfxa6 yy__nested_const_ptr; + yt_arr_arr_int_2_2 yy__nested; + yt_arr_int_2* yy__ptr_nested; + yt_arr_ptr_int_2 yy__nested_ptr; + yt_arr_ptr_int_2* yy__ptr_nested_ptr; + yt_arr_ptr_ptr_int_2 yy__nested_ptr_ptr; + yt_arr_fn_in_int_out_int_2 yy__nested_func; + yt_arr_const_ptr_int_2 yy__nested_const_ptr; }; // --functions-- int32_t yy__main() diff --git a/compiler/test_data/compiler_tests/arrays/fixed_array_copying.yaka.c b/compiler/test_data/compiler_tests/arrays/fixed_array_copying.yaka.c index a5da8684..858bbd1b 100644 --- a/compiler/test_data/compiler_tests/arrays/fixed_array_copying.yaka.c +++ b/compiler/test_data/compiler_tests/arrays/fixed_array_copying.yaka.c @@ -1,17 +1,17 @@ // YK #include "yk__lib.h" // --forward declarations-- -typedef int32_t ykfxa0[3]; +typedef int32_t yt_arr_int_3[3]; int32_t yy__main(); // --structs-- // --functions-- int32_t yy__main() { - ykfxa0 yy__a = {}; + yt_arr_int_3 yy__a = {}; yy__a[INT32_C(0)] = INT32_C(1); yy__a[INT32_C(1)] = INT32_C(2); yy__a[INT32_C(2)] = INT32_C(3); - ykfxa0 yy__b = {}; + yt_arr_int_3 yy__b = {}; yy__b[INT32_C(0)] = yy__a[INT32_C(0)]; yy__b[INT32_C(1)] = yy__a[INT32_C(1)]; yy__b[INT32_C(2)] = yy__a[INT32_C(2)]; diff --git a/compiler/test_data/compiler_tests/arrays/simple_fixed_arr.yaka.c b/compiler/test_data/compiler_tests/arrays/simple_fixed_arr.yaka.c index 564fb7a1..e8ffef27 100644 --- a/compiler/test_data/compiler_tests/arrays/simple_fixed_arr.yaka.c +++ b/compiler/test_data/compiler_tests/arrays/simple_fixed_arr.yaka.c @@ -1,14 +1,14 @@ // YK #include "yk__lib.h" // --forward declarations-- -typedef int32_t ykfxa0[3]; +typedef int32_t yt_arr_int_3[3]; int32_t yy__main(); // --structs-- // --functions-- int32_t yy__main() { yk__printlnstr("Creating a fixed array"); - ykfxa0 yy__a = {}; + yt_arr_int_3 yy__a = {}; yy__a[INT32_C(0)] = INT32_C(1); yy__a[INT32_C(1)] = INT32_C(2); yy__a[INT32_C(2)] = INT32_C(3);