2023-01-20 18:52:57 +01:00
|
|
|
package io.gitlab.jfronny.muscript.ast.bool;
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
import io.gitlab.jfronny.muscript.ast.compare.Equal;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.compare.Greater;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.literal.NumberLiteral;
|
|
|
|
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.BoolExpr;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.Expr;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.literal.BoolLiteral;
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2022-06-07 13:36:48 +02:00
|
|
|
public class Not extends BoolExpr {
|
2022-06-03 19:54:31 +02:00
|
|
|
private final BoolExpr inner;
|
|
|
|
|
2023-01-20 17:47:41 +01:00
|
|
|
public Not(int chStart, int chEnd, BoolExpr inner) {
|
2023-03-12 15:28:44 +01:00
|
|
|
super(Order.Unary, chStart, chEnd);
|
2022-06-03 19:54:31 +02:00
|
|
|
this.inner = inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-01-20 21:05:04 +01:00
|
|
|
public Boolean get(Scope dataRoot) {
|
2022-06-13 13:31:54 +02:00
|
|
|
return !inner.get(dataRoot);
|
2022-06-03 19:54:31 +02:00
|
|
|
}
|
2022-06-29 17:38:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public BoolExpr optimize() {
|
|
|
|
BoolExpr inner = this.inner.optimize();
|
|
|
|
if (inner instanceof Not not) return not.inner;
|
2023-01-20 17:47:41 +01:00
|
|
|
if (inner instanceof BoolLiteral literal) return Expr.literal(chStart, chEnd, !literal.value);
|
|
|
|
return new Not(chStart, chEnd, inner);
|
2022-06-29 17:38:05 +02:00
|
|
|
}
|
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
@Override
|
|
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
|
|
if (inner instanceof Equal eq) {
|
|
|
|
parenthesize(eq.left, writer, false);
|
|
|
|
writer.append(" != ");
|
|
|
|
parenthesize(eq.right, writer, true);
|
|
|
|
} else if (inner instanceof Greater gt) {
|
|
|
|
if (gt.left instanceof NumberLiteral && !(gt.right instanceof NumberLiteral)) {
|
|
|
|
parenthesize(gt.right, writer, false);
|
|
|
|
writer.append(" >= ");
|
|
|
|
parenthesize(gt.left, writer, true);
|
|
|
|
} else {
|
|
|
|
parenthesize(gt.left, writer, false);
|
|
|
|
writer.append(" <= ");
|
|
|
|
parenthesize(gt.right, writer, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writer.append("!");
|
|
|
|
parenthesize(inner, writer, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 17:38:05 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof Not not && inner.equals(not.inner);
|
|
|
|
}
|
2022-06-03 19:54:31 +02:00
|
|
|
}
|