47 lines
1.4 KiB
Java
47 lines
1.4 KiB
Java
package io.gitlab.jfronny.muscript.ast.dynamic.assign;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.StringExpr;
|
|
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
|
|
import io.gitlab.jfronny.muscript.compiler.Order;
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
|
import io.gitlab.jfronny.muscript.error.LocationalException;
|
|
|
|
import java.io.IOException;
|
|
|
|
public class StringAssign extends StringExpr {
|
|
private final String name;
|
|
private final StringExpr value;
|
|
|
|
protected StringAssign(int chStart, int chEnd, String name, StringExpr value) {
|
|
super(Order.Primary, chStart, chEnd);
|
|
if (name.equals("this")) throw new LocationalException(chStart, chEnd, "Cannot reassign 'this'");
|
|
this.name = name;
|
|
this.value = value;
|
|
}
|
|
|
|
@Override
|
|
public String get(Scope dataRoot) {
|
|
String data = value.get(dataRoot);
|
|
dataRoot.set(name, data);
|
|
return data;
|
|
}
|
|
|
|
@Override
|
|
public StringExpr optimize() {
|
|
return new StringAssign(chStart, chEnd, name, value.optimize());
|
|
}
|
|
|
|
@Override
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
writer.appendLiteral(name).append(" = ");
|
|
value.decompile(writer);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof StringAssign assign
|
|
&& name.equals(assign.name)
|
|
&& value.equals(assign.value);
|
|
}
|
|
}
|