java-commons/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/dynamic/Variable.java

31 lines
1.1 KiB
Java
Raw Normal View History

package io.gitlab.jfronny.muscript.compiler.expr.dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.*;
2022-06-13 13:31:54 +02:00
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
import io.gitlab.jfronny.muscript.dynamic.*;
2022-06-13 13:31:54 +02:00
import io.gitlab.jfronny.muscript.error.*;
2022-06-13 13:31:54 +02:00
@CanThrow @UncheckedDynamic
public class Variable extends DynamicExpr {
private final String name;
2022-06-13 13:31:54 +02:00
public Variable(int character, String name) {
super(character);
this.name = name;
}
@Override
2022-06-13 13:31:54 +02:00
public Dynamic<?> get(Dynamic<?> dataRoot) {
if (dataRoot.asObject().has(name)) return dataRoot.asObject().get(name);
else if (name.contains("::")) {
2022-06-13 13:31:54 +02:00
Dynamic<?> res = dataRoot;
for (String s : name.split("::")) {
if (!res.asObject().has(s))
2022-06-13 13:31:54 +02:00
throw new LocationalException(character, "This object doesn't contain that name");
res = res.asObject().get(s);
}
return res;
2022-06-13 13:31:54 +02:00
} else throw new LocationalException(character, "This object doesn't contain that name");
}
}