mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-04 23:06:05 +00:00
lazily evaluate terrascript binary operations
This commit is contained in:
@@ -12,6 +12,8 @@ import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public abstract class BinaryOperation<I, O> implements Returnable<O> {
|
||||
private final Returnable<I> left;
|
||||
@@ -24,11 +26,11 @@ public abstract class BinaryOperation<I, O> implements Returnable<O> {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
public abstract O apply(I left, I right);
|
||||
public abstract O apply(Supplier<I> left, Supplier<I> right);
|
||||
|
||||
@Override
|
||||
public O apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return apply(left.apply(implementationArguments, scope), right.apply(implementationArguments, scope));
|
||||
return apply(() -> left.apply(implementationArguments, scope), () -> right.apply(implementationArguments, scope));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,8 @@ package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class BooleanAndOperation extends BinaryOperation<Boolean, Boolean> {
|
||||
public BooleanAndOperation(Returnable<Boolean> left, Returnable<Boolean> right, Position start) {
|
||||
@@ -17,8 +19,8 @@ public class BooleanAndOperation extends BinaryOperation<Boolean, Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(Boolean left, Boolean right) {
|
||||
return left && right;
|
||||
public Boolean apply(Supplier<Boolean> left, Supplier<Boolean> right) {
|
||||
return left.get() && right.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,8 @@ package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class BooleanOrOperation extends BinaryOperation<Boolean, Boolean> {
|
||||
public BooleanOrOperation(Returnable<Boolean> left, Returnable<Boolean> right, Position start) {
|
||||
@@ -17,8 +19,8 @@ public class BooleanOrOperation extends BinaryOperation<Boolean, Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(Boolean left, Boolean right) {
|
||||
return left || right;
|
||||
public Boolean apply(Supplier<Boolean> left, Supplier<Boolean> right) {
|
||||
return left.get() || right.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,8 @@ package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class ConcatenationOperation extends BinaryOperation<Object, Object> {
|
||||
public ConcatenationOperation(Returnable<Object> left, Returnable<Object> right, Position position) {
|
||||
@@ -17,8 +19,8 @@ public class ConcatenationOperation extends BinaryOperation<Object, Object> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(Object left, Object right) {
|
||||
return left.toString() + right.toString();
|
||||
public String apply(Supplier<Object> left, Supplier<Object> right) {
|
||||
return left.get().toString() + right.get().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,8 @@ package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class DivisionOperation extends BinaryOperation<Number, Number> {
|
||||
public DivisionOperation(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
@@ -17,8 +19,8 @@ public class DivisionOperation extends BinaryOperation<Number, Number> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Number left, Number right) {
|
||||
return left.doubleValue() / right.doubleValue();
|
||||
public Number apply(Supplier<Number> left, Supplier<Number> right) {
|
||||
return left.get().doubleValue() / right.get().doubleValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,8 @@ package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class ModuloOperation extends BinaryOperation<Number, Number> {
|
||||
public ModuloOperation(Returnable<Number> left, Returnable<Number> right, Position start) {
|
||||
@@ -17,8 +19,8 @@ public class ModuloOperation extends BinaryOperation<Number, Number> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Number left, Number right) {
|
||||
return left.doubleValue() % right.doubleValue();
|
||||
public Number apply(Supplier<Number> left, Supplier<Number> right) {
|
||||
return left.get().doubleValue() % right.get().doubleValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,8 @@ package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class MultiplicationOperation extends BinaryOperation<Number, Number> {
|
||||
public MultiplicationOperation(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
@@ -17,8 +19,8 @@ public class MultiplicationOperation extends BinaryOperation<Number, Number> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Number left, Number right) {
|
||||
return left.doubleValue() * right.doubleValue();
|
||||
public Number apply(Supplier<Number> left, Supplier<Number> right) {
|
||||
return left.get().doubleValue() * right.get().doubleValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,8 @@ package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class NumberAdditionOperation extends BinaryOperation<Number, Number> {
|
||||
public NumberAdditionOperation(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
@@ -17,8 +19,8 @@ public class NumberAdditionOperation extends BinaryOperation<Number, Number> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Number left, Number right) {
|
||||
return left.doubleValue() + right.doubleValue();
|
||||
public Number apply(Supplier<Number> left, Supplier<Number> right) {
|
||||
return left.get().doubleValue() + right.get().doubleValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,8 @@ package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class SubtractionOperation extends BinaryOperation<Number, Number> {
|
||||
public SubtractionOperation(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
@@ -17,8 +19,8 @@ public class SubtractionOperation extends BinaryOperation<Number, Number> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Number left, Number right) {
|
||||
return left.doubleValue() - right.doubleValue();
|
||||
public Number apply(Supplier<Number> left, Supplier<Number> right) {
|
||||
return left.get().doubleValue() - right.get().doubleValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,21 +13,26 @@ import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static com.dfsek.terra.api.util.MathUtil.EPSILON;
|
||||
|
||||
|
||||
public class EqualsStatement extends BinaryOperation<Object, Boolean> {
|
||||
private static final double EPSILON = 0.000000001D;
|
||||
|
||||
public EqualsStatement(Returnable<Object> left, Returnable<Object> right, Position position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(Object left, Object right) {
|
||||
if(left instanceof Number && right instanceof Number) {
|
||||
return FastMath.abs(((Number) left).doubleValue() - ((Number) right).doubleValue()) <= EPSILON;
|
||||
public Boolean apply(Supplier<Object> left, Supplier<Object> right) {
|
||||
Object leftUnwrapped = left.get();
|
||||
Object rightUnwrapped = right.get();
|
||||
if(leftUnwrapped instanceof Number l && rightUnwrapped instanceof Number r) {
|
||||
return FastMath.abs(l.doubleValue() - r.doubleValue()) <= EPSILON;
|
||||
}
|
||||
|
||||
return left.equals(right);
|
||||
return left.equals(rightUnwrapped);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class GreaterOrEqualsThanStatement extends BinaryOperation<Number, Boolean> {
|
||||
public GreaterOrEqualsThanStatement(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
@@ -18,8 +20,8 @@ public class GreaterOrEqualsThanStatement extends BinaryOperation<Number, Boolea
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(Number left, Number right) {
|
||||
return left.doubleValue() >= right.doubleValue();
|
||||
public Boolean apply(Supplier<Number> left, Supplier<Number> right) {
|
||||
return left.get().doubleValue() >= right.get().doubleValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class GreaterThanStatement extends BinaryOperation<Number, Boolean> {
|
||||
public GreaterThanStatement(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
@@ -18,8 +20,8 @@ public class GreaterThanStatement extends BinaryOperation<Number, Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(Number left, Number right) {
|
||||
return left.doubleValue() > right.doubleValue();
|
||||
public Boolean apply(Supplier<Number> left, Supplier<Number> right) {
|
||||
return left.get().doubleValue() > right.get().doubleValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class LessThanOrEqualsStatement extends BinaryOperation<Number, Boolean> {
|
||||
public LessThanOrEqualsStatement(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
@@ -18,8 +20,8 @@ public class LessThanOrEqualsStatement extends BinaryOperation<Number, Boolean>
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(Number left, Number right) {
|
||||
return left.doubleValue() <= right.doubleValue();
|
||||
public Boolean apply(Supplier<Number> left, Supplier<Number> right) {
|
||||
return left.get().doubleValue() <= right.get().doubleValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class LessThanStatement extends BinaryOperation<Number, Boolean> {
|
||||
public LessThanStatement(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
@@ -18,8 +20,8 @@ public class LessThanStatement extends BinaryOperation<Number, Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(Number left, Number right) {
|
||||
return left.doubleValue() < right.doubleValue();
|
||||
public Boolean apply(Supplier<Number> left, Supplier<Number> right) {
|
||||
return left.get().doubleValue() < right.get().doubleValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,6 +11,12 @@ import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static com.dfsek.terra.api.util.MathUtil.EPSILON;
|
||||
|
||||
|
||||
public class NotEqualsStatement extends BinaryOperation<Object, Boolean> {
|
||||
public NotEqualsStatement(Returnable<Object> left, Returnable<Object> right, Position position) {
|
||||
@@ -18,8 +24,14 @@ public class NotEqualsStatement extends BinaryOperation<Object, Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(Object left, Object right) {
|
||||
return !left.equals(right);
|
||||
public Boolean apply(Supplier<Object> left, Supplier<Object> right) {
|
||||
Object leftUnwrapped = left.get();
|
||||
Object rightUnwrapped = right.get();
|
||||
if(leftUnwrapped instanceof Number l && rightUnwrapped instanceof Number r) {
|
||||
return FastMath.abs(l.doubleValue() - r.doubleValue()) > EPSILON;
|
||||
}
|
||||
|
||||
return !left.equals(rightUnwrapped);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user