feat(muscript-runtime): enhance GetOrAt heuristics

This commit is contained in:
Johannes Frohnmeyer 2024-04-12 16:57:59 +02:00
parent df78e10c6a
commit 1f51e9025d
Signed by: Johannes
GPG Key ID: E76429612C2929F4
1 changed files with 8 additions and 8 deletions

View File

@ -198,19 +198,19 @@ public class Runtime {
}
case GetOrAt(var location, var leftExpr, var nameOrIndex) -> {
var left = evaluate(leftExpr, scope);
var right = evaluate(nameOrIndex, scope);
if (Dynamic.isNull(left)) throw locationalException(expr, "Could not get \"" + evaluate(nameOrIndex, scope) + "\" because left is null");
if (left.isObject()) {
var o = left.asObject();
var n = evaluate(asString(nameOrIndex), scope);
if (!o.has(n)) throw locationalException(expr, "Object does not contain \"" + n + "\"");
yield o.get(n);
} else if (left.isList()) {
if (left.isList() && right.isNumber()) {
var l = left.asList();
int idx = (int) evaluate(asNumber(nameOrIndex), scope);
if (idx < 0 || idx >= l.size()) throw locationalException(expr, "Index " + idx + " is out of range for list with size " + l.size());
yield l.get(idx);
}
throw new DynamicTypeConversionException("object or list", left);
} else if (left.isObject()) {
var o = left.asObject();
var n = right.asString().getValue();
if (!o.has(n)) throw locationalException(expr, "Object does not contain \"" + n + "\"");
yield o.get(n);
} else throw new DynamicTypeConversionException("object or list", left);
}
case Bind(var location, var callable, var parameter) -> {
var param = evaluate(parameter, scope);