Skip to content

Commit 6d010bd

Browse files
committed
wip
1 parent f87b118 commit 6d010bd

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ 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)
152152
RUN(NAME test_list_03 LABELS cpython llvm c)
153-
RUN(NAME test_list_04 LABELS cpython llvm)
153+
RUN(NAME test_list_04 LABELS cpython llvm c)
154154
RUN(NAME test_list_05 LABELS cpython llvm)
155155
RUN(NAME test_list_06 LABELS cpython llvm)
156156
RUN(NAME test_list_07 LABELS cpython llvm)

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class CList {
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);
129129
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);
130132
return list_struct_type;
131133
}
132134

@@ -154,6 +156,15 @@ class CList {
154156
return typecode2listfuncs[list_type_code]["list_resize"];
155157
}
156158

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+
157168
std::string get_generated_code() {
158169
return generated_code;
159170
}
@@ -274,6 +285,51 @@ class CList {
274285
generated_code += indent + "}\n\n";
275286
}
276287

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+
277333
~CList() {
278334
typecode2listtype.clear();
279335
generated_code.clear();
@@ -883,6 +939,18 @@ R"(#include <stdio.h>
883939
src = indent + list_insert_func + "(&" + list_var + ", " + pos + ", " + element + ");\n";
884940
}
885941

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+
886954
void visit_ListLen(const ASR::ListLen_t& x) {
887955
if( x.m_value ) {
888956
self().visit_expr(*x.m_value);

0 commit comments

Comments
 (0)