2022-06-03 19:54:31 +02:00
|
|
|
package io.gitlab.jfronny.muscript.compiler.expr;
|
|
|
|
|
|
|
|
import io.gitlab.jfronny.muscript.compiler.*;
|
2022-06-07 12:19:58 +02:00
|
|
|
import io.gitlab.jfronny.muscript.dynamic.*;
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2022-06-07 12:19:58 +02:00
|
|
|
public class Variable extends Expr.DynamicExpr {
|
2022-06-03 19:54:31 +02:00
|
|
|
private final String name;
|
|
|
|
|
|
|
|
public Variable(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-07 12:19:58 +02:00
|
|
|
public Dynamic<?> get(Dynamic<?> branch, Dynamic<?> dataRoot) {
|
2022-06-03 19:54:31 +02:00
|
|
|
if (branch.asObject().has(name)) return branch.asObject().get(name);
|
|
|
|
else if (name.contains("::")) {
|
2022-06-07 12:19:58 +02:00
|
|
|
Dynamic<?> res = branch;
|
2022-06-03 19:54:31 +02:00
|
|
|
for (String s : name.split("::")) {
|
|
|
|
if (!res.asObject().has(s))
|
|
|
|
throw new IllegalArgumentException("This object doesn't contain that name");
|
|
|
|
res = res.asObject().get(s);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
} else throw new IllegalArgumentException("This object doesn't contain that name");
|
|
|
|
}
|
|
|
|
}
|