@@ -127,6 +127,8 @@ class CList {
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
129
list_insert (list_struct_type, list_type_code, list_element_type, list_type->m_type );
130
+ list_find_item_position (list_struct_type, list_type_code, list_element_type, list_type->m_type );
131
+ list_remove (list_struct_type, list_type_code, list_element_type, list_type->m_type );
130
132
return list_struct_type;
131
133
}
132
134
@@ -154,6 +156,15 @@ class CList {
154
156
return typecode2listfuncs[list_type_code][" list_resize" ];
155
157
}
156
158
159
+ std::string get_list_remove_func (ASR::List_t* list_type) {
160
+ std::string list_type_code = ASRUtils::get_type_code (list_type->m_type , true );
161
+ return typecode2listfuncs[list_type_code][" list_remove" ];
162
+ }
163
+
164
+ std::string get_list_find_item_position_function (std::string list_type_code) {
165
+ return typecode2listfuncs[list_type_code][" list_find_item" ];
166
+ }
167
+
157
168
std::string get_generated_code () {
158
169
return generated_code;
159
170
}
@@ -274,6 +285,51 @@ class CList {
274
285
generated_code += indent + " }\n\n " ;
275
286
}
276
287
288
+ void list_find_item_position (std::string list_struct_type,
289
+ std::string list_type_code, std::string list_element_type,
290
+ ASR::ttype_t * /* m_type*/ ) {
291
+ std::string indent (indentation_level * indentation_spaces, ' ' );
292
+ std::string tab (indentation_spaces, ' ' );
293
+ std::string list_find_item_pos_func = global_scope->get_unique_name (" list_find_item_" + list_type_code);
294
+ typecode2listfuncs[list_type_code][" list_find_item" ] = list_find_item_pos_func;
295
+ std::string signature = " int " + list_find_item_pos_func + " ("
296
+ + list_struct_type + " * x, "
297
+ + list_element_type + " element)" ;
298
+ list_func_decls += signature + " ;\n " ;
299
+ generated_code += indent + signature + " {\n " ;
300
+ generated_code += indent + tab + " int el_pos = 0;\n " ;
301
+ generated_code += indent + tab + " while(x->current_end_point > el_pos) {;\n " ;
302
+ generated_code += indent + tab + tab + " if (x->data[el_pos] == element) return el_pos;\n " ;
303
+ generated_code += indent + tab + tab + " el_pos++;\n " ;
304
+ generated_code += indent + tab + " }\n " ;
305
+ generated_code += indent + tab + " return -1;\n " ;
306
+ generated_code += indent + " }\n\n " ;
307
+ }
308
+
309
+ void list_remove (std::string list_struct_type,
310
+ std::string list_type_code, std::string list_element_type,
311
+ ASR::ttype_t * /* m_type*/ ) {
312
+ std::string indent (indentation_level * indentation_spaces, ' ' );
313
+ std::string tab (indentation_spaces, ' ' );
314
+ std::string list_remove_func = global_scope->get_unique_name (" list_remove_" + list_type_code);
315
+ typecode2listfuncs[list_type_code][" list_remove" ] = list_remove_func;
316
+ std::string signature = " void " + list_remove_func + " ("
317
+ + list_struct_type + " * x, "
318
+ + list_element_type + " element)" ;
319
+ list_func_decls += signature + " ;\n " ;
320
+ generated_code += indent + signature + " {\n " ;
321
+ std::string find_item_pos_func = get_list_find_item_position_function (list_type_code);
322
+ generated_code += indent + tab + " int el_pos = " + find_item_pos_func + " (x, element);\n " ;
323
+ generated_code += indent + tab + " while(x->current_end_point > el_pos) {;\n " ;
324
+ generated_code += indent + tab + tab + " int tmp = el_pos + 1;\n " ;
325
+ generated_code += indent + tab + tab + " x->data[el_pos] = x->data[tmp];\n " ;
326
+ generated_code += indent + tab + tab + " el_pos = tmp;\n " ;
327
+ generated_code += indent + tab + " }\n " ;
328
+
329
+ generated_code += indent + tab + " x->current_end_point -= 1;\n " ;
330
+ generated_code += indent + " }\n\n " ;
331
+ }
332
+
277
333
~CList () {
278
334
typecode2listtype.clear ();
279
335
generated_code.clear ();
@@ -883,6 +939,18 @@ R"(#include <stdio.h>
883
939
src = indent + list_insert_func + " (&" + list_var + " , " + pos + " , " + element + " );\n " ;
884
940
}
885
941
942
+ void visit_ListRemove (const ASR::ListRemove_t& x) {
943
+ ASR::ttype_t * t_ttype = ASRUtils::expr_type (x.m_a );
944
+ ASR::List_t* t = ASR::down_cast<ASR::List_t>(t_ttype);
945
+ std::string list_remove_func = list_api->get_list_remove_func (t);
946
+ self ().visit_expr (*x.m_a );
947
+ std::string list_var = std::move (src);
948
+ self ().visit_expr (*x.m_ele );
949
+ std::string element = std::move (src);
950
+ std::string indent (indentation_level * indentation_spaces, ' ' );
951
+ src = indent + list_remove_func + " (&" + list_var + " , " + element + " );\n " ;
952
+ }
953
+
886
954
void visit_ListLen (const ASR::ListLen_t& x) {
887
955
if ( x.m_value ) {
888
956
self ().visit_expr (*x.m_value );
0 commit comments