50 lines
1.5 KiB
Java
50 lines
1.5 KiB
Java
package io.gitlab.jfronny.muscript.ast.math;
|
|
|
|
import io.gitlab.jfronny.muscript.compiler.*;
|
|
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;
|
|
import java.util.stream.Stream;
|
|
|
|
public class Invert extends NumberExpr {
|
|
public final NumberExpr inner;
|
|
|
|
public Invert(CodeLocation location, NumberExpr inner) {
|
|
super(Order.Unary, location);
|
|
this.inner = inner;
|
|
}
|
|
|
|
@Override
|
|
public Double get(Scope dataRoot) {
|
|
return -inner.get(dataRoot);
|
|
}
|
|
|
|
@Override
|
|
public NumberExpr optimize() {
|
|
NumberExpr inner = this.inner.optimize();
|
|
if (inner instanceof Invert invert) return invert.inner;
|
|
if (inner instanceof NumberLiteral literal) return Expr.literal(location, -literal.value);
|
|
if (inner instanceof Minus minus) return new Minus(location, minus.subtrahend, minus.minuend).optimize();
|
|
return new Invert(location, inner);
|
|
}
|
|
|
|
@Override
|
|
public Stream<Expr<?>> extractSideEffects() {
|
|
return inner.extractSideEffects();
|
|
}
|
|
|
|
@Override
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
writer.append('-');
|
|
parenthesize(inner, writer, false);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof Invert invert && inner.equals(invert.inner);
|
|
}
|
|
}
|