Skip to content

Commit

Permalink
Added REMOVE ELEMENT AT _ FROM _ statement`
Browse files Browse the repository at this point in the history
  • Loading branch information
xvxx committed Dec 6, 2022
1 parent 77842d5 commit baa5378
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <LIST>
```

## `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 <NUMBER-VAR or NUMBER> FROM <LIST>
```

**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?"
```
9 changes: 9 additions & 0 deletions src/aux/aux_compile_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,15 @@ void compile_line(vector<string> &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))
Expand Down

0 comments on commit baa5378

Please sign in to comment.