Skip to content

Commit

Permalink
C: Implement DictLen
Browse files Browse the repository at this point in the history
  • Loading branch information
Smit-create committed Mar 2, 2023
1 parent 27537a7 commit 3fc4b4e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,7 @@ R"(#include <stdio.h>
this->visit_expr(*x.m_key);
std::string k = std::move(src);

std::string indent(indentation_level * indentation_spaces, ' ');
src = indent + dict_get_fun + "(&" + d_var + ", " + k + ")";
src = dict_get_fun + "(&" + d_var + ", " + k + ")";
}

void visit_ListAppend(const ASR::ListAppend_t& x) {
Expand Down Expand Up @@ -1137,6 +1136,18 @@ R"(#include <stdio.h>
src = src + ".length";
}

void visit_DictLen(const ASR::DictLen_t& x) {
if ( x.m_value ) {
self().visit_expr(*x.m_value);
return ;
}
ASR::ttype_t* t_ttype = ASRUtils::expr_type(x.m_arg);
ASR::Dict_t* t = ASR::down_cast<ASR::Dict_t>(t_ttype);
std::string dict_len_fun = c_ds_api->get_dict_len_func(t);
self().visit_expr(*x.m_arg);
src = dict_len_fun + "(&" + src + ")";
}

void visit_ListItem(const ASR::ListItem_t& x) {
if( x.m_value ) {
self().visit_expr(*x.m_value);
Expand Down
24 changes: 24 additions & 0 deletions src/libasr/codegen/c_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,11 @@ class CCPPDSUtils {
return typecodeToDSfuncs[dict_type_code]["dict_get"];
}

std::string get_dict_len_func(ASR::Dict_t* d_type) {
std::string dict_type_code = ASRUtils::get_type_code((ASR::ttype_t*)d_type, true);
return typecodeToDSfuncs[dict_type_code]["dict_len"];
}

std::string get_dict_init_func(ASR::Dict_t* d_type) {
std::string dict_type_code = ASRUtils::get_type_code((ASR::ttype_t*)d_type, true);
return typecodeToDSfuncs[dict_type_code]["dict_init"];
Expand Down Expand Up @@ -1156,6 +1161,7 @@ class CCPPDSUtils {
dict_resize(dict_type, dict_struct_type, dict_type_code);
dict_insert(dict_type, dict_struct_type, dict_type_code);
dict_get_item(dict_type, dict_struct_type, dict_type_code);
dict_len(dict_type, dict_struct_type, dict_type_code);
return dict_struct_type;
}

Expand Down Expand Up @@ -1275,6 +1281,24 @@ class CCPPDSUtils {
generated_code += indent + "}\n\n";
}

void dict_len(ASR::Dict_t *dict_type, std::string dict_struct_type,
std::string dict_type_code) {
std::string indent(indentation_level * indentation_spaces, ' ');
std::string tab(indentation_spaces, ' ');
std::string dict_get_func = global_scope->get_unique_name("dict_len_" + dict_type_code);
typecodeToDSfuncs[dict_type_code]["dict_len"] = dict_get_func;
std::string key = CUtils::get_c_type_from_ttype_t(dict_type->m_key_type);
std::string val = CUtils::get_c_type_from_ttype_t(dict_type->m_value_type);
std::string signature = "int32_t " + dict_get_func + "(" + dict_struct_type + "* x)";
func_decls += indent + "inline " + signature + ";\n";
signature = indent + signature;
generated_code += indent + signature + " {\n";
generated_code += indent + tab + "int32_t len = 0;\n";
generated_code += indent + tab + "for(int i=0; i<x->capacity; i++) len += (int)x->present[i];\n";
generated_code += indent + tab + "return len;\n";
generated_code += indent + "}\n\n";
}

~CCPPDSUtils() {
typecodeToDStype.clear();
generated_code.clear();
Expand Down

0 comments on commit 3fc4b4e

Please sign in to comment.