'&' 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,8 +180,14 @@ Result<Value> builtin_range(Interpreter&, std::vector<Value> args)
} }
Array array; Array array;
for (; start < stop; start += step) { if constexpr (dir == Range_Direction::Up) {
array.elements.push_back(Value::from(start)); 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)); return Value::from(std::move(array));
} }

View File

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