46 lines
1.4 KiB
Java
46 lines
1.4 KiB
Java
package io.gitlab.jfronny.muscript.ast.dynamic;
|
|
|
|
import io.gitlab.jfronny.muscript.compiler.*;
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
|
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;
|
|
import io.gitlab.jfronny.muscript.error.LocationalException;
|
|
|
|
import java.io.IOException;
|
|
|
|
@CanThrow
|
|
@UncheckedDynamic
|
|
public class Variable extends DynamicExpr {
|
|
public final String name;
|
|
|
|
public Variable(int chStart, int chEnd, String name) {
|
|
super(Order.Primary, chStart, chEnd);
|
|
this.name = name;
|
|
}
|
|
|
|
@Override
|
|
public Dynamic<?> get(Scope dataRoot) {
|
|
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 + "'");
|
|
}
|
|
|
|
@Override
|
|
public DynamicExpr optimize() {
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
if (!Lexer.isValidId(name)) throw new IllegalArgumentException("Not a valid variable name: " + name);
|
|
writer.append(name);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof Variable variable && name.equals(variable.name);
|
|
}
|
|
}
|