|
1 | 1 | #include <lbd/intp/builtins.h> |
| 2 | +#include <lbd/intp/builtin-modules/builtin_module_list.h> |
2 | 3 |
|
3 | | -namespace intp::interp { |
4 | | - static options::Options options_v; |
| 4 | +namespace intp::interp::builtins { |
| 5 | + options::Options options_v; |
5 | 6 |
|
6 | 7 | // Prints Argument to stdout and returns 0 |
7 | 8 | static NativeFunction make_print() { |
@@ -109,139 +110,6 @@ namespace intp::interp { |
109 | 110 | }; |
110 | 111 | } |
111 | 112 |
|
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 | | - |
245 | 113 | std::vector<NativeFunction> get_builtins(const options::Options options_) { |
246 | 114 | options_v = options_; |
247 | 115 | return { |
|
0 commit comments