@@ -126,6 +126,7 @@ class CList {
126
126
list_deepcopy (list_struct_type, list_type_code, list_element_type);
127
127
resize_if_needed (list_struct_type, list_type_code, list_element_type);
128
128
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 );
129
130
return list_struct_type;
130
131
}
131
132
@@ -144,6 +145,11 @@ class CList {
144
145
return typecode2listfuncs[list_type_code][" list_append" ];
145
146
}
146
147
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
+
147
153
std::string get_list_resize_func (std::string list_type_code) {
148
154
return typecode2listfuncs[list_type_code][" list_resize" ];
149
155
}
@@ -234,6 +240,40 @@ class CList {
234
240
generated_code += indent + " }\n\n " ;
235
241
}
236
242
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
+
237
277
~CList () {
238
278
typecode2listtype.clear ();
239
279
generated_code.clear ();
@@ -829,6 +869,20 @@ R"(#include <stdio.h>
829
869
src = indent + list_append_func + " (&" + list_var + " , " + element + " );\n " ;
830
870
}
831
871
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
+
832
886
void visit_ListLen (const ASR::ListLen_t& x) {
833
887
if ( x.m_value ) {
834
888
self ().visit_expr (*x.m_value );
@@ -956,14 +1010,9 @@ R"(#include <stdio.h>
956
1010
break ;
957
1011
}
958
1012
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 + " )" ;
967
1016
break ;
968
1017
}
969
1018
default : throw CodeGenError (" Cast kind " + std::to_string (x.m_kind ) + " not implemented" ,
0 commit comments