Skip to content

Commit f87b118

Browse files
committed
wip
1 parent 3b5e17c commit f87b118

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ RUN(NAME test_str_02 LABELS cpython llvm)
149149
RUN(NAME test_str_03 LABELS cpython llvm c)
150150
RUN(NAME test_list_01 LABELS cpython llvm c)
151151
RUN(NAME test_list_02 LABELS cpython llvm c)
152-
RUN(NAME test_list_03 LABELS cpython llvm)
152+
RUN(NAME test_list_03 LABELS cpython llvm c)
153153
RUN(NAME test_list_04 LABELS cpython llvm)
154154
RUN(NAME test_list_05 LABELS cpython llvm)
155155
RUN(NAME test_list_06 LABELS cpython llvm)

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class CList {
126126
list_deepcopy(list_struct_type, list_type_code, list_element_type);
127127
resize_if_needed(list_struct_type, list_type_code, list_element_type);
128128
list_append(list_struct_type, list_type_code, list_element_type, list_type->m_type);
129+
list_insert(list_struct_type, list_type_code, list_element_type, list_type->m_type);
129130
return list_struct_type;
130131
}
131132

@@ -144,6 +145,11 @@ class CList {
144145
return typecode2listfuncs[list_type_code]["list_append"];
145146
}
146147

148+
std::string get_list_insert_func(ASR::List_t* list_type) {
149+
std::string list_type_code = ASRUtils::get_type_code(list_type->m_type, true);
150+
return typecode2listfuncs[list_type_code]["list_insert"];
151+
}
152+
147153
std::string get_list_resize_func(std::string list_type_code) {
148154
return typecode2listfuncs[list_type_code]["list_resize"];
149155
}
@@ -234,6 +240,40 @@ class CList {
234240
generated_code += indent + "}\n\n";
235241
}
236242

243+
void list_insert(std::string list_struct_type,
244+
std::string list_type_code, std::string list_element_type,
245+
ASR::ttype_t* m_type) {
246+
std::string indent(indentation_level * indentation_spaces, ' ');
247+
std::string tab(indentation_spaces, ' ');
248+
std::string list_insert_func = global_scope->get_unique_name("list_insert_" + list_type_code);
249+
typecode2listfuncs[list_type_code]["list_insert"] = list_insert_func;
250+
std::string signature = "void " + list_insert_func + "("
251+
+ list_struct_type + "* x, "
252+
+ "int pos, "
253+
+ list_element_type + " element)";
254+
list_func_decls += signature + ";\n";
255+
generated_code += indent + signature + " {\n";
256+
std::string list_resize_func = get_list_resize_func(list_type_code);
257+
generated_code += indent + tab + list_resize_func + "(x);\n";
258+
generated_code += indent + tab + "int pos_ptr = pos;\n";
259+
generated_code += indent + tab + list_element_type + " tmp_ptr = x->data[pos];\n";
260+
generated_code += indent + tab + list_element_type + " tmp = 0;\n";
261+
262+
generated_code += indent + tab + "while(x->current_end_point > pos_ptr) {\n";
263+
generated_code += indent + tab + tab + "tmp = x->data[pos_ptr + 1];\n";
264+
generated_code += indent + tab + tab + "x->data[pos_ptr + 1] = tmp_ptr;\n";
265+
generated_code += indent + tab + tab + "tmp_ptr = tmp;\n";
266+
generated_code += indent + tab + tab + "pos_ptr++;\n";
267+
generated_code += indent + tab + "}\n\n";
268+
269+
if( ASR::is_a<ASR::Character_t>(*m_type) ) {
270+
generated_code += indent + tab + "x->data[pos] = malloc(40 * sizeof(char));\n";
271+
}
272+
generated_code += indent + tab + CUtils::deepcopy("x->data[pos]", "element", m_type) + "\n";
273+
generated_code += indent + tab + "x->current_end_point += 1;\n";
274+
generated_code += indent + "}\n\n";
275+
}
276+
237277
~CList() {
238278
typecode2listtype.clear();
239279
generated_code.clear();
@@ -829,6 +869,20 @@ R"(#include <stdio.h>
829869
src = indent + list_append_func + "(&" + list_var + ", " + element + ");\n";
830870
}
831871

872+
void visit_ListInsert(const ASR::ListInsert_t& x) {
873+
ASR::ttype_t* t_ttype = ASRUtils::expr_type(x.m_a);
874+
ASR::List_t* t = ASR::down_cast<ASR::List_t>(t_ttype);
875+
std::string list_insert_func = list_api->get_list_insert_func(t);
876+
self().visit_expr(*x.m_a);
877+
std::string list_var = std::move(src);
878+
self().visit_expr(*x.m_ele);
879+
std::string element = std::move(src);
880+
self().visit_expr(*x.m_pos);
881+
std::string pos = std::move(src);
882+
std::string indent(indentation_level * indentation_spaces, ' ');
883+
src = indent + list_insert_func + "(&" + list_var + ", " + pos + ", " + element + ");\n";
884+
}
885+
832886
void visit_ListLen(const ASR::ListLen_t& x) {
833887
if( x.m_value ) {
834888
self().visit_expr(*x.m_value);
@@ -956,14 +1010,9 @@ R"(#include <stdio.h>
9561010
break;
9571011
}
9581012
case (ASR::cast_kindType::IntegerToCharacter) : {
959-
current_body.clear();
960-
const_name += std::to_string(const_list_count);
961-
const_list_count += 1;
962-
const_name = current_scope->get_unique_name(const_name);
963-
std::string indent(indentation_level*indentation_spaces, ' ');
964-
current_body += indent + "char* " + const_name + " = (char*) malloc(40 * sizeof(char));\n";
965-
current_body += indent + "sprintf(" + const_name + ", \"%d\", " + src + ");\n";
966-
src = const_name;
1013+
ASR::ttype_t* src_type = ASRUtils::expr_type(x.m_arg);
1014+
int src_kind = ASRUtils::extract_kind_from_ttype_t(src_type);
1015+
src = "_lfortran_int_to_str" + std::to_string(src_kind) + "(" + src + ")";
9671016
break ;
9681017
}
9691018
default : throw CodeGenError("Cast kind " + std::to_string(x.m_kind) + " not implemented",

0 commit comments

Comments
 (0)