2023-01-20 18:52:57 +01:00
|
|
|
package io.gitlab.jfronny.muscript.ast.math;
|
2022-06-07 13:36:48 +02:00
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
|
|
|
|
import io.gitlab.jfronny.muscript.compiler.Order;
|
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.ast.Expr;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.NumberExpr;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.literal.NumberLiteral;
|
2022-06-07 13:36:48 +02:00
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2022-06-07 13:36:48 +02:00
|
|
|
public class Plus extends NumberExpr {
|
2022-06-29 17:38:05 +02:00
|
|
|
public final NumberExpr augend;
|
|
|
|
public final NumberExpr addend;
|
2022-06-07 13:36:48 +02:00
|
|
|
|
2023-01-20 17:47:41 +01:00
|
|
|
public Plus(int chStart, int chEnd, NumberExpr augend, NumberExpr addend) {
|
2023-03-12 15:28:44 +01:00
|
|
|
super(Order.Term, chStart, chEnd);
|
2022-06-07 13:36:48 +02:00
|
|
|
this.augend = augend;
|
|
|
|
this.addend = addend;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-01-20 21:05:04 +01:00
|
|
|
public Double get(Scope dataRoot) {
|
2022-06-13 13:31:54 +02:00
|
|
|
return augend.get(dataRoot) + addend.get(dataRoot);
|
2022-06-07 13:36:48 +02:00
|
|
|
}
|
2022-06-29 17:38:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberExpr optimize() {
|
|
|
|
NumberExpr augend = this.augend.optimize();
|
|
|
|
NumberExpr addend = this.addend.optimize();
|
2022-11-24 19:05:51 +01:00
|
|
|
if (augend instanceof NumberLiteral litU && addend instanceof NumberLiteral litD)
|
2023-01-20 17:47:41 +01:00
|
|
|
return Expr.literal(chStart, chEnd, litU.value + litD.value);
|
|
|
|
return new Plus(chStart, chEnd, augend, addend);
|
2022-06-29 17:38:05 +02:00
|
|
|
}
|
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
@Override
|
|
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
|
|
parenthesize(augend, writer, false);
|
|
|
|
writer.append(" + ");
|
|
|
|
parenthesize(addend, writer, true);
|
|
|
|
}
|
|
|
|
|
2022-06-29 17:38:05 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof Plus plus && augend.equals(plus.augend) && addend.equals(plus.addend);
|
|
|
|
}
|
2022-06-07 13:36:48 +02:00
|
|
|
}
|