diff --git a/docs/list.md b/docs/list.md index f4e202a..7df2db6 100644 --- a/docs/list.md +++ b/docs/list.md @@ -95,3 +95,26 @@ The `DELETE LAST ELEMENT OF` deletes the last element pushed to a LIST. If the L ```coffeescript DELETE LAST ELEMENT OF ``` + +## `REMOVE ELEMENT AT _ FROM _` + +The `REMOVE ELEMENT AT - FROM` statement deletes the element at the specified index from a LIST. If the index is out of bounds, this statement does nothing. + +**Syntax:** + +```coffeescript +REMOVE ELEMENT AT FROM +``` + +**Example:** + +```coffeescript +DATA: + foo IS TEXT LIST +PROCEDURE: + PUSH "Hello there!" TO foo + PUSH "How are you?" TO foo + REMOVE ELEMENT AT 0 FROM foo + DISPLAY foo:0 CRLF + # Will display "How are you?" +``` diff --git a/src/aux/aux_compile_line.cpp b/src/aux/aux_compile_line.cpp index 72f276e..1c1d2d4 100644 --- a/src/aux/aux_compile_line.cpp +++ b/src/aux/aux_compile_line.cpp @@ -967,6 +967,15 @@ void compile_line(vector &tokens, compiler_state &state) state.add_code(get_c_variable(state, tokens[4]) + ".inner_collection.pop_back();", state.where); return; } + if (line_like("REMOVE ELEMENT AT $num-expr FROM $list", tokens, state)) + { + if (!in_procedure_section(state)) + badcode("REMOVE ELEMENT AT statement outside PROCEDURE section", state.where); + // C++ Code + state.add_code("if(" + get_c_variable(state, tokens[5]) + ".inner_collection.size() > " + get_c_expression(state, tokens[3]) + ")", state.where); + state.add_code(get_c_variable(state, tokens[5]) + ".inner_collection.erase(" + get_c_variable(state, tokens[5]) + ".inner_collection.begin() + " + get_c_expression(state, tokens[3]) + ");", state.where); + return; + } if (line_like("SPLIT $str-expr BY $str-expr IN $str-list", tokens, state)) { if (!in_procedure_section(state))