Skip to content

Commit

Permalink
fix(entry_struct_func_compiler): use simple representation of types t…
Browse files Browse the repository at this point in the history
…o create type names
  • Loading branch information
JaDogg committed Apr 29, 2024
1 parent 5e4e858 commit 83b835b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 61 deletions.
39 changes: 22 additions & 17 deletions compiler/src/compiler/entry_struct_func_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand All @@ -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; "
Expand All @@ -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) {
Expand All @@ -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 ||
Expand All @@ -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";
Expand Down Expand Up @@ -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_) {
Expand Down Expand Up @@ -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
Expand All @@ -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";
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions compiler/src/compiler/entry_struct_func_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> name_improvements_;
std::unordered_map<std::string, std::string> reverse_name_improvements_;
Expand Down
32 changes: 16 additions & 16 deletions compiler/src/compiler/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand All @@ -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());
}
}
Expand Down Expand Up @@ -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());
}
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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() &&
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/utilities/ykdatatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << "[";
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/utilities/ykdatatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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];
Expand All @@ -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);
Expand Down
32 changes: 16 additions & 16 deletions compiler/test_data/compiler_tests/arrays/fixed_arr_structure.yaka.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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)];
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit 83b835b

Please sign in to comment.