48 lines
1.5 KiB
Java
48 lines
1.5 KiB
Java
package io.gitlab.jfronny.muscript.ast.math;
|
|
|
|
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.ast.Expr;
|
|
import io.gitlab.jfronny.muscript.ast.NumberExpr;
|
|
import io.gitlab.jfronny.muscript.ast.literal.NumberLiteral;
|
|
|
|
import java.io.IOException;
|
|
|
|
public class Plus extends NumberExpr {
|
|
public final NumberExpr augend;
|
|
public final NumberExpr addend;
|
|
|
|
public Plus(int chStart, int chEnd, NumberExpr augend, NumberExpr addend) {
|
|
super(Order.Term, chStart, chEnd);
|
|
this.augend = augend;
|
|
this.addend = addend;
|
|
}
|
|
|
|
@Override
|
|
public Double get(Scope dataRoot) {
|
|
return augend.get(dataRoot) + addend.get(dataRoot);
|
|
}
|
|
|
|
@Override
|
|
public NumberExpr optimize() {
|
|
NumberExpr augend = this.augend.optimize();
|
|
NumberExpr addend = this.addend.optimize();
|
|
if (augend instanceof NumberLiteral litU && addend instanceof NumberLiteral litD)
|
|
return Expr.literal(chStart, chEnd, litU.value + litD.value);
|
|
return new Plus(chStart, chEnd, augend, addend);
|
|
}
|
|
|
|
@Override
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
parenthesize(augend, writer, false);
|
|
writer.append(" + ");
|
|
parenthesize(addend, writer, true);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof Plus plus && augend.equals(plus.augend) && addend.equals(plus.addend);
|
|
}
|
|
}
|