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

Commit d66b80d

Browse files
committed
chore: reorganized builtins
1 parent ed7a61a commit d66b80d

6 files changed

Lines changed: 159 additions & 137 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ add_library(intp STATIC
1919
${CMAKE_SOURCE_DIR}/src/intp/types.cpp
2020
${CMAKE_SOURCE_DIR}/src/intp/interpreter.cpp
2121
${CMAKE_SOURCE_DIR}/src/intp/builtins.cpp
22+
${CMAKE_SOURCE_DIR}/src/intp/builtin-modules/builtin_module_list.cpp
2223
)
2324

2425
add_executable(lbd
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <lbd/intp/builtins.h>
4+
5+
namespace intp::interp::builtins {
6+
NativeFunction make_list();
7+
8+
NativeFunction make_list_size();
9+
10+
NativeFunction make_list_get();
11+
12+
NativeFunction make_list_remove();
13+
14+
NativeFunction make_list_append();
15+
}

‎include/lbd/intp/builtins.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <lbd/intp/interpreter.h>
44
#include <lbd/options.h>
55

6-
namespace intp::interp {
6+
namespace intp::interp::builtins {
7+
extern options::Options options_v;
8+
79
std::vector<NativeFunction> get_builtins(options::Options options_ = {});
810
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <lbd/intp/builtin-modules/builtin_module_list.h>
2+
3+
namespace intp::interp::builtins {
4+
static Value list_get(const std::shared_ptr<List> &list_v, size_t index) {
5+
if (index >= list_v->elements.size()) {
6+
options_v.logger.error({}, "runtime error: list index out of range, index is ", index);
7+
}
8+
return list_v->elements[index];
9+
}
10+
11+
static Value list_remove(const std::shared_ptr<List> &list_v, size_t index) {
12+
if (index >= list_v->elements.size()) {
13+
options_v.logger.error({}, "runtime error: list index out of range, index is ", index);
14+
}
15+
Value value = list_v->elements[index];
16+
list_v->elements.erase(list_v->elements.begin() + static_cast<std::vector<Value>::difference_type>(index));
17+
return value;
18+
}
19+
20+
static void list_append(const std::shared_ptr<List> &list_v, Value value) {
21+
list_v->elements.push_back(std::move(value));
22+
}
23+
24+
std::shared_ptr<List> make_list_obj(const std::vector<Value> &elements) {
25+
return std::make_shared<List>(List{elements});
26+
}
27+
28+
NativeFunction make_list() {
29+
const std::string name = "list";
30+
return {
31+
-1, name, [](const std::vector<std::shared_ptr<Thunk> > &args,
32+
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
33+
std::vector<Value> values;
34+
for (auto &arg: args) {
35+
values.push_back(arg->force());
36+
}
37+
return std::make_pair(Value{make_list_obj(values)}, ResultOptions{});
38+
}
39+
};
40+
}
41+
42+
NativeFunction make_list_size() {
43+
const std::string name = "list_size";
44+
return {
45+
1, name, [name](const std::vector<std::shared_ptr<Thunk> > &args,
46+
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
47+
const Value &arg0 = args[0]->force();
48+
if (!std::holds_alternative<std::shared_ptr<List> >(arg0)) {
49+
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
50+
"\n", name,
51+
" signature: List -> Float""\n"
52+
"runtime error: expected <List> got ", arg0);
53+
}
54+
const auto list_v = std::get<std::shared_ptr<List> >(arg0);
55+
return std::make_pair(Value{static_cast<double>(list_v->elements.size())}, ResultOptions{});
56+
}
57+
};
58+
}
59+
60+
NativeFunction make_list_get() {
61+
const std::string name = "list_get";
62+
return {
63+
// TODO: Add type checker to replace this manual approach
64+
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args,
65+
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
66+
const Value &arg0 = args[0]->force();
67+
if (!std::holds_alternative<std::shared_ptr<List> >(arg0)) {
68+
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
69+
"\n", name,
70+
" signature: List -> Float -> List""\n"
71+
"runtime error: expected <List> got ", arg0);
72+
}
73+
const Value &arg1 = args[1]->force();
74+
if (!std::holds_alternative<double>(arg1)) {
75+
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
76+
"\n", name,
77+
" signature: List -> Float -> List""\n"
78+
"runtime error: expected <Float> got ", arg1);
79+
}
80+
return std::make_pair(Value{
81+
list_get(std::get<std::shared_ptr<List> >(arg0),
82+
static_cast<size_t>(std::get<double>(arg1)))
83+
},
84+
ResultOptions{});
85+
}
86+
};
87+
}
88+
89+
NativeFunction make_list_remove() {
90+
const std::string name = "list_remove";
91+
return {
92+
// TODO: Add type checker to replace this manual approach
93+
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args,
94+
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
95+
const Value &arg0 = args[0]->force();
96+
if (!std::holds_alternative<std::shared_ptr<List> >(arg0)) {
97+
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
98+
"\n", name,
99+
" signature: List -> Float -> List""\n"
100+
"runtime error: expected <List> got ", arg0);
101+
}
102+
const Value &arg1 = args[1]->force();
103+
if (!std::holds_alternative<double>(arg1)) {
104+
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
105+
"\n", name,
106+
" signature: List -> Float -> List""\n"
107+
"runtime error: expected <Float> got ", arg1);
108+
}
109+
return std::make_pair(
110+
Value{
111+
list_remove(std::get<std::shared_ptr<List> >(arg0), static_cast<size_t>(std::get<double>(arg1)))
112+
},
113+
ResultOptions{});
114+
}
115+
};
116+
}
117+
118+
NativeFunction make_list_append() {
119+
const std::string name = "list_append";
120+
return {
121+
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args,
122+
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
123+
const Value &arg0 = args[0]->force();
124+
if (!std::holds_alternative<std::shared_ptr<List> >(arg0)) {
125+
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
126+
"\n", name,
127+
" signature: List -> Any -> List""\n"
128+
"runtime error: expected <List> got ", arg0);
129+
}
130+
auto list_v = std::get<std::shared_ptr<List> >(arg0);
131+
list_append(list_v, args[1]->force());
132+
return std::make_pair(Value{list_v}, ResultOptions{});
133+
}
134+
};
135+
}
136+
}

‎src/intp/builtins.cpp‎

Lines changed: 3 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include <lbd/intp/builtins.h>
2+
#include <lbd/intp/builtin-modules/builtin_module_list.h>
23

3-
namespace intp::interp {
4-
static options::Options options_v;
4+
namespace intp::interp::builtins {
5+
options::Options options_v;
56

67
// Prints Argument to stdout and returns 0
78
static NativeFunction make_print() {
@@ -109,139 +110,6 @@ namespace intp::interp {
109110
};
110111
}
111112

112-
static std::shared_ptr<List> make_list_obj(const std::vector<Value> &elements) {
113-
return std::make_shared<List>(List{elements});
114-
}
115-
116-
static Value list_get(const std::shared_ptr<List> &list_v, size_t index) {
117-
if (index >= list_v->elements.size()) {
118-
options_v.logger.error({}, "runtime error: list index out of range, index is ", index);
119-
}
120-
return list_v->elements[index];
121-
}
122-
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() + static_cast<std::vector<Value>::difference_type>(index));
129-
return value;
130-
}
131-
132-
static void list_append(const std::shared_ptr<List> &list_v, Value value) {
133-
list_v->elements.push_back(std::move(value));
134-
}
135-
136-
static NativeFunction make_list() {
137-
const std::string name = "list";
138-
return {
139-
-1, name, [](const std::vector<std::shared_ptr<Thunk> > &args,
140-
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
141-
std::vector<Value> values;
142-
for (auto &arg: args) {
143-
values.push_back(arg->force());
144-
}
145-
return std::make_pair(Value{make_list_obj(values)}, ResultOptions{});
146-
}
147-
};
148-
}
149-
150-
static NativeFunction make_list_size() {
151-
const std::string name = "list_size";
152-
return {
153-
1, name, [name](const std::vector<std::shared_ptr<Thunk> > &args,
154-
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
155-
const Value &arg0 = args[0]->force();
156-
if (!std::holds_alternative<std::shared_ptr<List> >(arg0)) {
157-
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
158-
"\n", name,
159-
" signature: List -> Float""\n"
160-
"runtime error: expected <List> got ", arg0);
161-
}
162-
const auto list_v = std::get<std::shared_ptr<List> >(arg0);
163-
return std::make_pair(Value{static_cast<double>(list_v->elements.size())}, ResultOptions{});
164-
}
165-
};
166-
}
167-
168-
static NativeFunction make_list_get() {
169-
const std::string name = "list_get";
170-
return {
171-
// TODO: Add type checker to replace this manual approach
172-
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args,
173-
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
174-
const Value &arg0 = args[0]->force();
175-
if (!std::holds_alternative<std::shared_ptr<List> >(arg0)) {
176-
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
177-
"\n", name,
178-
" signature: List -> Float -> List""\n"
179-
"runtime error: expected <List> got ", arg0);
180-
}
181-
const Value &arg1 = args[1]->force();
182-
if (!std::holds_alternative<double>(arg1)) {
183-
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
184-
"\n", name,
185-
" signature: List -> Float -> List""\n"
186-
"runtime error: expected <Float> got ", arg1);
187-
}
188-
return std::make_pair(Value{
189-
list_get(std::get<std::shared_ptr<List> >(arg0),
190-
static_cast<size_t>(std::get<double>(arg1)))
191-
},
192-
ResultOptions{});
193-
}
194-
};
195-
}
196-
197-
static NativeFunction make_list_remove() {
198-
const std::string name = "list_remove";
199-
return {
200-
// TODO: Add type checker to replace this manual approach
201-
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args,
202-
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
203-
const Value &arg0 = args[0]->force();
204-
if (!std::holds_alternative<std::shared_ptr<List> >(arg0)) {
205-
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
206-
"\n", name,
207-
" signature: List -> Float -> List""\n"
208-
"runtime error: expected <List> got ", arg0);
209-
}
210-
const Value &arg1 = args[1]->force();
211-
if (!std::holds_alternative<double>(arg1)) {
212-
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
213-
"\n", name,
214-
" signature: List -> Float -> List""\n"
215-
"runtime error: expected <Float> got ", arg1);
216-
}
217-
return std::make_pair(
218-
Value{
219-
list_remove(std::get<std::shared_ptr<List> >(arg0), static_cast<size_t>(std::get<double>(arg1)))
220-
},
221-
ResultOptions{});
222-
}
223-
};
224-
}
225-
226-
static NativeFunction make_list_append() {
227-
const std::string name = "list_append";
228-
return {
229-
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args,
230-
const std::shared_ptr<Env> &) -> std::pair<Value, ResultOptions> {
231-
const Value &arg0 = args[0]->force();
232-
if (!std::holds_alternative<std::shared_ptr<List> >(arg0)) {
233-
options_v.logger.error({}, "runtime error: wrong arguments provided to native function ", name,
234-
"\n", name,
235-
" signature: List -> Any -> List""\n"
236-
"runtime error: expected <List> got ", arg0);
237-
}
238-
auto list_v = std::get<std::shared_ptr<List> >(arg0);
239-
list_append(list_v, args[1]->force());
240-
return std::make_pair(Value{list_v}, ResultOptions{});
241-
}
242-
};
243-
}
244-
245113
std::vector<NativeFunction> get_builtins(const options::Options options_) {
246114
options_v = options_;
247115
return {

‎src/intp/interpreter.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ namespace intp::interp {
354354
}
355355

356356
void install_builtins(const std::shared_ptr<Env> &env) {
357-
for (auto &native_fn: get_builtins(options_v)) {
357+
for (auto &native_fn: builtins::get_builtins(options_v)) {
358358
const auto thunk = std::make_shared<Thunk>();
359359
thunk->cached = Value{std::make_shared<NativeFunction>(native_fn)};
360360
env->bind(native_fn.name, thunk);

0 commit comments

Comments
 (0)