'&' operator can merge arrays; 'down' builtin works

This commit is contained in:
Robert Bendun 2022-09-05 20:47:15 +02:00
parent de92564cc1
commit eb822a4a6e
2 changed files with 26 additions and 14 deletions

View File

@ -180,9 +180,15 @@ Result<Value> builtin_range(Interpreter&, std::vector<Value> args)
}
Array array;
if constexpr (dir == Range_Direction::Up) {
for (; start < stop; start += step) {
array.elements.push_back(Value::from(start));
}
} else {
for (; start < stop; start += step) {
array.elements.push_back(Value::from(stop - start - Number(1)));
}
}
return Value::from(std::move(array));
}

View File

@ -254,9 +254,17 @@ static constexpr auto Operators = std::array {
},
Operator_Entry { "&",
+[](Interpreter&, std::vector<Value> args) -> Result<Value> {
using Chord_Chord = Shape<Value::Type::Music, Value::Type::Music>;
+[](Interpreter& i, std::vector<Value> args) -> Result<Value> {
constexpr auto guard = Guard<2> {
.name = "&",
.possibilities = {
"(array, array) -> array",
"(music, music) -> music",
},
.type = errors::Unsupported_Types_For::Operator
};
using Chord_Chord = Shape<Value::Type::Music, Value::Type::Music>;
if (Chord_Chord::typecheck(args)) {
auto [lhs, rhs] = Chord_Chord::move_from(args);
auto &l = lhs.notes;
@ -269,16 +277,14 @@ static constexpr auto Operators = std::array {
return Value::from(lhs);
}
return Error {
.details = errors::Unsupported_Types_For {
.type = errors::Unsupported_Types_For::Operator,
.name = "&",
.possibilities = {
"(music, music) -> music",
auto result = Array {};
for (auto&& array : args) {
Try(guard(is_indexable, array));
for (auto n = 0u; n < array.size(); ++n) {
result.elements.push_back(Try(array.index(i, n)));
}
},
.location = {}
};
}
return Value::from(std::move(result));
}
},
};