30 lines
1.2 KiB
Java
30 lines
1.2 KiB
Java
package io.gitlab.jfronny.muscript.compiler.expr.dynamic;
|
|
|
|
import io.gitlab.jfronny.muscript.compiler.*;
|
|
import io.gitlab.jfronny.muscript.compiler.expr.*;
|
|
import io.gitlab.jfronny.muscript.dynamic.*;
|
|
|
|
public class Get extends DynamicExpr {
|
|
private final DynamicExpr left;
|
|
private final Expr<?> name;
|
|
|
|
public Get(DynamicExpr left, Expr<?> name) {
|
|
this.left = left;
|
|
this.name = name;
|
|
if (name.getResultType() != Type.String && name.getResultType() != Type.Number && name.getResultType() != Type.Dynamic) {
|
|
throw new IllegalArgumentException("Name must be either a string or a number");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Dynamic<?> get(Dynamic<?> branch, Dynamic<?> dataRoot) {
|
|
Dynamic<?> left = this.left.get(branch, dataRoot);
|
|
if (left instanceof DObject o) {
|
|
return o.get(name.asStringExpr().get(dataRoot, dataRoot));
|
|
} else if (left instanceof DList l) {
|
|
return l.get(name.asNumberExpr().get(dataRoot, dataRoot).intValue());
|
|
}
|
|
throw new IllegalArgumentException("The element to get value \"" + name.asStringExpr().get(dataRoot, dataRoot) + "\" from is not of a valid type");
|
|
}
|
|
}
|