| 347 | |
| 348 | |
| 349 | struct value_array_t : public value_t { |
| 350 | value_array_t() = default; |
| 351 | value_array_t(value & v) { |
| 352 | val_arr = v->val_arr; |
| 353 | } |
| 354 | value_array_t(std::vector<value> && arr) { |
| 355 | val_arr = arr; |
| 356 | } |
| 357 | value_array_t(const std::vector<value> & arr) { |
| 358 | val_arr = arr; |
| 359 | } |
| 360 | void reverse() { |
| 361 | if (is_immutable()) { |
| 362 | throw std::runtime_error("Attempting to modify immutable type"); |
| 363 | } |
| 364 | std::reverse(val_arr.begin(), val_arr.end()); |
| 365 | } |
| 366 | void push_back(const value & val) { |
| 367 | if (is_immutable()) { |
| 368 | throw std::runtime_error("Attempting to modify immutable type"); |
| 369 | } |
| 370 | val_arr.push_back(val); |
| 371 | } |
| 372 | void push_back(value && val) { |
| 373 | if (is_immutable()) { |
| 374 | throw std::runtime_error("Attempting to modify immutable type"); |
| 375 | } |
| 376 | val_arr.push_back(std::move(val)); |
| 377 | } |
| 378 | value pop_at(int64_t index) { |
| 379 | if (is_immutable()) { |
| 380 | throw std::runtime_error("Attempting to modify immutable type"); |
| 381 | } |
| 382 | if (index < 0) { |
| 383 | index = static_cast<int64_t>(val_arr.size()) + index; |
| 384 | } |
| 385 | if (index < 0 || index >= static_cast<int64_t>(val_arr.size())) { |
| 386 | throw std::runtime_error("Index " + std::to_string(index) + " out of bounds for array of size " + std::to_string(val_arr.size())); |
| 387 | } |
| 388 | value val = val_arr.at(static_cast<size_t>(index)); |
| 389 | val_arr.erase(val_arr.begin() + index); |
| 390 | return val; |
| 391 | } |
| 392 | virtual std::string type() const override { return "Array"; } |
| 393 | virtual bool is_immutable() const override { return false; } |
| 394 | virtual const std::vector<value> & as_array() const override { return val_arr; } |
| 395 | virtual string as_string() const override { |
| 396 | const bool immutable = is_immutable(); |
| 397 | std::ostringstream ss; |
| 398 | ss << (immutable ? "(" : "["); |
| 399 | for (size_t i = 0; i < val_arr.size(); i++) { |
| 400 | if (i > 0) ss << ", "; |
| 401 | value val = val_arr.at(i); |
| 402 | ss << value_to_string_repr(val); |
| 403 | } |
| 404 | if (immutable && val_arr.size() == 1) { |
| 405 | ss << ","; |
| 406 | } |