2023-01-20 18:52:57 +01:00
|
|
|
package io.gitlab.jfronny.muscript.ast.dynamic;
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.*;
|
2023-01-20 21:05:04 +01:00
|
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
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
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2022-06-29 15:41:44 +02:00
|
|
|
@CanThrow
|
|
|
|
@UncheckedDynamic
|
2022-06-07 13:36:48 +02:00
|
|
|
public class Variable extends DynamicExpr {
|
2023-03-12 15:28:44 +01:00
|
|
|
public final String name;
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2023-01-20 17:47:41 +01:00
|
|
|
public Variable(int chStart, int chEnd, String name) {
|
2023-03-12 15:28:44 +01:00
|
|
|
super(Order.Primary, chStart, chEnd);
|
2022-06-03 19:54:31 +02:00
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-01-20 21:05:04 +01:00
|
|
|
public Dynamic<?> get(Scope dataRoot) {
|
2023-03-11 13:39:17 +01:00
|
|
|
if (name.equals("this")) return dataRoot;
|
|
|
|
if (dataRoot.has(name)) return dataRoot.get(name);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
@Override
|
|
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
|
|
if (!Lexer.isValidId(name)) throw new IllegalArgumentException("Not a valid variable name: " + name);
|
|
|
|
writer.append(name);
|
|
|
|
}
|
|
|
|
|
2022-06-29 17:38:05 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof Variable variable && name.equals(variable.name);
|
|
|
|
}
|
2022-06-03 19:54:31 +02:00
|
|
|
}
|