2022-06-07 13:36:48 +02:00
|
|
|
package io.gitlab.jfronny.muscript.compiler.expr.dynamic;
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2022-06-07 13:36:48 +02:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.expr.*;
|
2022-06-13 13:31:54 +02:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
|
2022-06-07 12:19:58 +02:00
|
|
|
import io.gitlab.jfronny.muscript.dynamic.*;
|
2022-06-13 13:31:54 +02:00
|
|
|
import io.gitlab.jfronny.muscript.error.*;
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2022-06-13 13:31:54 +02:00
|
|
|
@CanThrow @UncheckedDynamic
|
2022-06-07 13:36:48 +02:00
|
|
|
public class Variable extends DynamicExpr {
|
2022-06-03 19:54:31 +02:00
|
|
|
private final String name;
|
|
|
|
|
2022-06-13 13:31:54 +02:00
|
|
|
public Variable(int character, String name) {
|
|
|
|
super(character);
|
2022-06-03 19:54:31 +02:00
|
|
|
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);
|
2022-06-03 19:54:31 +02:00
|
|
|
else if (name.contains("::")) {
|
2022-06-13 13:31:54 +02:00
|
|
|
Dynamic<?> res = dataRoot;
|
2022-06-03 19:54:31 +02:00
|
|
|
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");
|
2022-06-03 19:54:31 +02:00
|
|
|
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");
|
2022-06-03 19:54:31 +02:00
|
|
|
}
|
|
|
|
}
|