Index operator now reports when given unindexable lhs

This commit is contained in:
Robert Bendun 2022-09-04 13:25:55 +02:00
parent 1597b678e1
commit f2a78cd331

View File

@ -200,13 +200,16 @@ static constexpr auto Operators = std::array {
Operator_Entry { ".",
+[](Interpreter &i, std::vector<Value> args) -> Result<Value> {
assert(is_indexable(args[0].type), "One can only index was is indexable"); // TODO(assert)
//
if (args.size() == 2 && args[1].type == Value::Type::Number) {
if (args.size() == 2 && is_indexable(args[0].type)) {
if (args[1].type == Value::Type::Number) {
return std::move(args.front()).index(i, std::move(args.back()).n.as_int());
}
if (args.size() == 2 && is_indexable(args[1].type)) {
if (args[1].type == Value::Type::Bool) {
return std::move(args.front()).index(i, args.back().b ? 1 : 0);
}
if (is_indexable(args[1].type)) {
std::vector<Value> result;
for (size_t n = 0; n < args[1].size(); ++n) {
auto const v = Try(args[1].index(i, n));
@ -222,7 +225,19 @@ static constexpr auto Operators = std::array {
}
return Value::from(Array { result });
}
unimplemented();
}
return Error {
.details = errors::Unsupported_Types_For {
.type = errors::Unsupported_Types_For::Operator,
.name = ".",
.possibilities = {
"array . bool -> bool",
"array . number -> any",
"array . (array of numbers) -> array",
},
},
};
}
},