2023-03-13 14:15:36 +01:00
|
|
|
package io.gitlab.jfronny.muscript.ast.dynamic.assign;
|
2023-01-20 21:05:04 +01:00
|
|
|
|
2023-09-21 14:10:41 +02:00
|
|
|
import io.gitlab.jfronny.muscript.ast.Expr;
|
2023-03-13 14:15:36 +01:00
|
|
|
import io.gitlab.jfronny.muscript.ast.StringExpr;
|
2023-04-18 13:40:34 +02:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.*;
|
2023-01-20 21:05:04 +01:00
|
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
2023-03-11 13:39:17 +01:00
|
|
|
import io.gitlab.jfronny.muscript.error.LocationalException;
|
2023-01-20 21:05:04 +01:00
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
import java.io.IOException;
|
2023-09-21 14:10:41 +02:00
|
|
|
import java.util.stream.Stream;
|
2023-03-12 15:28:44 +01:00
|
|
|
|
2023-03-13 14:15:36 +01:00
|
|
|
public class StringAssign extends StringExpr {
|
2023-01-20 21:05:04 +01:00
|
|
|
private final String name;
|
2023-03-13 14:15:36 +01:00
|
|
|
private final StringExpr value;
|
2023-01-20 21:05:04 +01:00
|
|
|
|
2023-04-18 13:40:34 +02:00
|
|
|
protected StringAssign(CodeLocation location, String name, StringExpr value) {
|
|
|
|
super(Order.Primary, location);
|
|
|
|
if (name.equals("this")) throw new LocationalException(location, "Cannot reassign 'this'");
|
2023-01-20 21:05:04 +01:00
|
|
|
this.name = name;
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-03-13 14:15:36 +01:00
|
|
|
public String get(Scope dataRoot) {
|
|
|
|
String data = value.get(dataRoot);
|
2023-01-20 21:05:04 +01:00
|
|
|
dataRoot.set(name, data);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-03-13 14:15:36 +01:00
|
|
|
public StringExpr optimize() {
|
2023-04-18 13:40:34 +02:00
|
|
|
return new StringAssign(location, name, value.optimize());
|
2023-01-20 21:05:04 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 14:10:41 +02:00
|
|
|
@Override
|
|
|
|
public Stream<Expr<?>> extractSideEffects() {
|
|
|
|
return Stream.of(this);
|
|
|
|
}
|
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
@Override
|
|
|
|
public void decompile(ExprWriter writer) throws IOException {
|
2023-03-12 16:16:27 +01:00
|
|
|
writer.appendLiteral(name).append(" = ");
|
2023-03-12 15:28:44 +01:00
|
|
|
value.decompile(writer);
|
|
|
|
}
|
|
|
|
|
2023-01-20 21:05:04 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
2023-03-13 14:15:36 +01:00
|
|
|
return obj instanceof StringAssign assign
|
2023-01-20 21:05:04 +01:00
|
|
|
&& name.equals(assign.name)
|
|
|
|
&& value.equals(assign.value);
|
|
|
|
}
|
|
|
|
}
|