2023-01-20 18:52:57 +01:00
|
|
|
package io.gitlab.jfronny.muscript.ast.dynamic;
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2023-01-20 18:52:57 +01:00
|
|
|
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
|
|
|
|
import io.gitlab.jfronny.muscript.annotations.CanThrow;
|
|
|
|
import io.gitlab.jfronny.muscript.annotations.UncheckedDynamic;
|
2022-06-29 15:41:44 +02:00
|
|
|
import io.gitlab.jfronny.muscript.error.LocationalException;
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2022-06-29 15:41:44 +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;
|
|
|
|
|
2023-01-20 17:47:41 +01:00
|
|
|
public Variable(int chStart, int chEnd, String name) {
|
|
|
|
super(chStart, chEnd);
|
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))
|
2023-01-20 17:47:41 +01:00
|
|
|
throw new LocationalException(chStart, chEnd, "This object doesn't contain '" + name + "'");
|
2022-06-03 19:54:31 +02:00
|
|
|
res = res.asObject().get(s);
|
|
|
|
}
|
|
|
|
return res;
|
2023-01-20 17:47:41 +01:00
|
|
|
} else throw new LocationalException(chStart, chEnd, "This object doesn't contain '" + name + "'");
|
2022-06-03 19:54:31 +02:00
|
|
|
}
|
2022-06-29 17:38:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public DynamicExpr optimize() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof Variable variable && name.equals(variable.name);
|
|
|
|
}
|
2022-06-03 19:54:31 +02:00
|
|
|
}
|