Skip to content
This repository was archived by the owner on Oct 12, 2026. It is now read-only.

Commit 078d75f

Browse files
committed
docs: add comprehensive list example
1 parent 8968ea6 commit 078d75f

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎examples/list.lbd‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
a: List = (list)
2-
(print a)
2+
(print "list: " a "\n")
33

4-
(list_append a "World")
5-
(print a)
4+
(list_append a "Hello")
5+
(print "list: " a "\n")
6+
7+
(print "list size: " (list_size a) "\n")
68

7-
-- TODO: Add support for list_get or nth NativeFunction
9+
(list_append a "World")
10+
(print "list: " a "\n")
11+
(print "list[1]: " (list_get a 1) "\n")

‎src/intp/builtins.cpp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ namespace intp::interp {
120120
return list_v->elements[index];
121121
}
122122

123+
static Value list_remove(const std::shared_ptr<List> &list_v, size_t index) {
124+
if (index >= list_v->elements.size()) {
125+
options_v.logger.error({}, "runtime error: list index out of range, index is ", index);
126+
}
127+
Value &value = list_v->elements[index];
128+
list_v->elements.erase(list_v->elements.begin() + index);
129+
return value;
130+
}
131+
123132
static void list_append(const std::shared_ptr<List> &list_v, Value value) {
124133
list_v->elements.push_back(std::move(value));
125134
}
@@ -182,6 +191,33 @@ namespace intp::interp {
182191
};
183192
}
184193

194+
static NativeFunction make_list_remove() {
195+
const std::string name = "list_remove";
196+
return {
197+
// TODO: Add type checker to replace this manual approach
198+
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args,
199+
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
200+
const Value &arg0 = args[0]->force();
201+
if (!std::holds_alternative<std::shared_ptr<List> >(arg0)) {
202+
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
203+
"\n", name,
204+
" signature: List -> Float -> List""\n"
205+
"runtime error: expected <List> got ", arg0);
206+
}
207+
const Value &arg1 = args[1]->force();
208+
if (!std::holds_alternative<double>(arg1)) {
209+
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
210+
"\n", name,
211+
" signature: List -> Float -> List""\n"
212+
"runtime error: expected <Float> got ", arg1);
213+
}
214+
return std::make_pair(
215+
Value{list_remove(std::get<std::shared_ptr<List> >(arg0), std::get<double>(arg1))},
216+
ResultOptions{});
217+
}
218+
};
219+
}
220+
185221
static NativeFunction make_list_append() {
186222
const std::string name = "list_append";
187223
return {

0 commit comments

Comments
 (0)