From 44ae60654516488afe537e812511af770898d40a Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Tue, 17 Dec 2024 22:09:14 +0200 Subject: [PATCH] Removed Unit library and tests --- .../latvian/mods/unit/EmptyVariableSet.java | 37 --- .../latvian/mods/unit/FixedBooleanUnit.java | 42 --- .../dev/latvian/mods/unit/FixedColorUnit.java | 64 ---- .../latvian/mods/unit/FixedNumberUnit.java | 216 ------------ .../latvian/mods/unit/MutableNumberUnit.java | 29 -- .../dev/latvian/mods/unit/TernaryUnit.java | 37 --- src/main/java/dev/latvian/mods/unit/Unit.java | 310 ------------------ .../dev/latvian/mods/unit/UnitContext.java | 148 --------- .../dev/latvian/mods/unit/UnitVariables.java | 6 - .../dev/latvian/mods/unit/VariableSet.java | 40 --- .../dev/latvian/mods/unit/VariableSubset.java | 16 - .../dev/latvian/mods/unit/VariableUnit.java | 37 --- .../mods/unit/function/AbsFuncUnit.java | 17 - .../mods/unit/function/Atan2FuncUnit.java | 17 - .../mods/unit/function/AtanFuncUnit.java | 17 - .../mods/unit/function/BoolFuncUnit.java | 32 -- .../mods/unit/function/CeilFuncUnit.java | 17 - .../mods/unit/function/ClampFuncUnit.java | 25 -- .../mods/unit/function/CosFuncUnit.java | 17 - .../mods/unit/function/DegFuncUnit.java | 17 - .../mods/unit/function/FloorFuncUnit.java | 17 - .../latvian/mods/unit/function/Func1Unit.java | 17 - .../latvian/mods/unit/function/Func2Unit.java | 18 - .../latvian/mods/unit/function/Func3Unit.java | 19 -- .../latvian/mods/unit/function/FuncUnit.java | 35 -- .../mods/unit/function/FunctionFactory.java | 93 ------ .../mods/unit/function/HsvFuncUnit.java | 107 ------ .../mods/unit/function/LerpFuncUnit.java | 21 -- .../mods/unit/function/Log10FuncUnit.java | 17 - .../mods/unit/function/Log1pFuncUnit.java | 17 - .../mods/unit/function/LogFuncUnit.java | 17 - .../mods/unit/function/MapFuncUnit.java | 33 -- .../mods/unit/function/MaxFuncUnit.java | 17 - .../mods/unit/function/MinFuncUnit.java | 17 - .../mods/unit/function/RadFuncUnit.java | 17 - .../mods/unit/function/RandomUnit.java | 22 -- .../mods/unit/function/RgbFuncUnit.java | 84 ----- .../mods/unit/function/RoundedTimeUnit.java | 22 -- .../mods/unit/function/SinFuncUnit.java | 17 - .../unit/function/SmoothstepFuncUnit.java | 18 - .../mods/unit/function/SqFuncUnit.java | 18 - .../mods/unit/function/SqrtFuncUnit.java | 17 - .../mods/unit/function/TanFuncUnit.java | 17 - .../latvian/mods/unit/function/TimeUnit.java | 22 -- .../mods/unit/function/WithAlphaFuncUnit.java | 17 - .../mods/unit/operator/BitNotOpUnit.java | 26 -- .../mods/unit/operator/BoolNotOpUnit.java | 26 -- .../latvian/mods/unit/operator/GroupUnit.java | 57 ---- .../mods/unit/operator/NegateOpUnit.java | 21 -- .../latvian/mods/unit/operator/OpUnit.java | 37 --- .../mods/unit/operator/OperatorFactory.java | 8 - .../latvian/mods/unit/operator/SetUnit.java | 39 --- .../mods/unit/operator/UnaryOpUnit.java | 22 -- .../unit/operator/UnaryOperatorFactory.java | 8 - .../mods/unit/operator/cond/AndOpUnit.java | 16 - .../mods/unit/operator/cond/CondOpUnit.java | 30 -- .../mods/unit/operator/cond/EqOpUnit.java | 16 - .../mods/unit/operator/cond/GtOpUnit.java | 16 - .../mods/unit/operator/cond/GteOpUnit.java | 16 - .../mods/unit/operator/cond/LtOpUnit.java | 16 - .../mods/unit/operator/cond/LteOpUnit.java | 16 - .../mods/unit/operator/cond/NeqOpUnit.java | 16 - .../mods/unit/operator/cond/OrOpUnit.java | 16 - .../mods/unit/operator/op/AddOpUnit.java | 17 - .../mods/unit/operator/op/BitAndOpUnit.java | 27 -- .../mods/unit/operator/op/BitOrOpUnit.java | 27 -- .../mods/unit/operator/op/DivOpUnit.java | 17 - .../mods/unit/operator/op/LshOpUnit.java | 22 -- .../mods/unit/operator/op/ModOpUnit.java | 17 - .../mods/unit/operator/op/MulOpUnit.java | 17 - .../mods/unit/operator/op/PowOpUnit.java | 17 - .../mods/unit/operator/op/RshOpUnit.java | 22 -- .../mods/unit/operator/op/SubOpUnit.java | 17 - .../mods/unit/operator/op/XorOpUnit.java | 27 -- .../latvian/mods/unit/token/CharStream.java | 58 ---- .../mods/unit/token/FunctionUnitToken.java | 27 -- .../mods/unit/token/OpResultUnitToken.java | 17 - .../mods/unit/token/PostfixUnitToken.java | 117 ------- .../mods/unit/token/StringUnitToken.java | 27 -- .../mods/unit/token/TernaryUnitToken.java | 16 - .../mods/unit/token/UnaryOpUnitToken.java | 16 - .../unit/token/UnitInterpretException.java | 7 - .../mods/unit/token/UnitParseException.java | 7 - .../latvian/mods/unit/token/UnitSymbol.java | 149 --------- .../latvian/mods/unit/token/UnitToken.java | 19 -- .../mods/unit/token/UnitTokenStream.java | 212 ------------ .../latvian/mods/rhino/test/TestConsole.java | 5 - .../latvian/mods/rhino/test/UnitTests.java | 187 ----------- 88 files changed, 3350 deletions(-) delete mode 100644 src/main/java/dev/latvian/mods/unit/EmptyVariableSet.java delete mode 100644 src/main/java/dev/latvian/mods/unit/FixedBooleanUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/FixedColorUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/FixedNumberUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/MutableNumberUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/TernaryUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/Unit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/UnitContext.java delete mode 100644 src/main/java/dev/latvian/mods/unit/UnitVariables.java delete mode 100644 src/main/java/dev/latvian/mods/unit/VariableSet.java delete mode 100644 src/main/java/dev/latvian/mods/unit/VariableSubset.java delete mode 100644 src/main/java/dev/latvian/mods/unit/VariableUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/AbsFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/Atan2FuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/AtanFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/BoolFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/CeilFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/ClampFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/CosFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/DegFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/FloorFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/Func1Unit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/Func2Unit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/Func3Unit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/FuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/FunctionFactory.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/HsvFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/LerpFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/Log10FuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/Log1pFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/LogFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/MapFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/MaxFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/MinFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/RadFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/RandomUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/RgbFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/RoundedTimeUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/SinFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/SmoothstepFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/SqFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/SqrtFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/TanFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/TimeUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/function/WithAlphaFuncUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/BitNotOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/BoolNotOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/GroupUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/NegateOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/OpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/OperatorFactory.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/SetUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/UnaryOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/UnaryOperatorFactory.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/cond/AndOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/cond/CondOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/cond/EqOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/cond/GtOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/cond/GteOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/cond/LtOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/cond/LteOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/cond/NeqOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/cond/OrOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/AddOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/BitAndOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/BitOrOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/DivOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/LshOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/ModOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/MulOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/PowOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/RshOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/SubOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/operator/op/XorOpUnit.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/CharStream.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/FunctionUnitToken.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/OpResultUnitToken.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/PostfixUnitToken.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/StringUnitToken.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/TernaryUnitToken.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/UnaryOpUnitToken.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/UnitInterpretException.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/UnitParseException.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/UnitSymbol.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/UnitToken.java delete mode 100644 src/main/java/dev/latvian/mods/unit/token/UnitTokenStream.java delete mode 100644 src/test/java/dev/latvian/mods/rhino/test/UnitTests.java diff --git a/src/main/java/dev/latvian/mods/unit/EmptyVariableSet.java b/src/main/java/dev/latvian/mods/unit/EmptyVariableSet.java deleted file mode 100644 index 7cb66f02..00000000 --- a/src/main/java/dev/latvian/mods/unit/EmptyVariableSet.java +++ /dev/null @@ -1,37 +0,0 @@ -package dev.latvian.mods.unit; - -import org.jetbrains.annotations.Nullable; - -public class EmptyVariableSet extends VariableSet { - - public static final EmptyVariableSet INSTANCE = new EmptyVariableSet(); - - private EmptyVariableSet() { - } - - @Override - public VariableSet set(String name, Unit value) { - return this; - } - - @Override - public VariableSet set(String name, double value) { - return this; - } - - @Override - public MutableNumberUnit setMutable(String name, double initialValue) { - return new MutableNumberUnit(initialValue); - } - - @Override - @Nullable - public Unit get(String entry) { - return null; - } - - @Override - public VariableSet createSubset() { - return new VariableSet(); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/FixedBooleanUnit.java b/src/main/java/dev/latvian/mods/unit/FixedBooleanUnit.java deleted file mode 100644 index 08edf8b8..00000000 --- a/src/main/java/dev/latvian/mods/unit/FixedBooleanUnit.java +++ /dev/null @@ -1,42 +0,0 @@ -package dev.latvian.mods.unit; - -public class FixedBooleanUnit extends Unit { - public static final FixedBooleanUnit TRUE = new FixedBooleanUnit(true); - public static final FixedBooleanUnit FALSE = new FixedBooleanUnit(false); - - public final boolean value; - - private FixedBooleanUnit(boolean value) { - this.value = value; - } - - @Override - public double get(UnitVariables variables) { - return value ? 1D : 0D; - } - - @Override - public int getInt(UnitVariables variables) { - return value ? 1 : 0; - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return value; - } - - @Override - public String toString() { - return value ? "true" : "false"; - } - - @Override - public Unit boolNot() { - return value ? FALSE : TRUE; - } - - @Override - public Unit bool() { - return this; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/FixedColorUnit.java b/src/main/java/dev/latvian/mods/unit/FixedColorUnit.java deleted file mode 100644 index 8cc8f7cf..00000000 --- a/src/main/java/dev/latvian/mods/unit/FixedColorUnit.java +++ /dev/null @@ -1,64 +0,0 @@ -package dev.latvian.mods.unit; - -import dev.latvian.mods.unit.token.UnitToken; - -public class FixedColorUnit extends Unit implements UnitToken { - public static final FixedColorUnit WHITE = new FixedColorUnit(0xFFFFFFFF, true); - public static final FixedColorUnit BLACK = new FixedColorUnit(0xFF000000, true); - public static final FixedColorUnit TRANSPARENT = new FixedColorUnit(0x00000000, true); - - public static FixedColorUnit of(int color, boolean alpha) { - if (color == 0xFFFFFFFF) { - return WHITE; - } else if (color == 0xFF000000) { - return BLACK; - } else if (color == 0x00000000) { - return TRANSPARENT; - } - - return new FixedColorUnit(color, alpha); - } - - public final int color; - public final boolean alpha; - - private FixedColorUnit(int c, boolean a) { - color = c; - alpha = a; - } - - @Override - public double get(UnitVariables variables) { - return getInt(variables); - } - - @Override - public int getInt(UnitVariables variables) { - return alpha ? color : (color | 0xFF000000); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return !alpha || ((color >> 24) & 0xFF) != 0; - } - - @Override - public void toString(StringBuilder builder) { - builder.append(String.format(alpha ? "#%08X" : "#%06X", color)); - } - - @Override - public Unit withAlpha(Unit a) { - if (a instanceof FixedNumberUnit u) { - if (u.value >= 1D) { - return of(color, false); - } else if (u.value <= 0D) { - return of(color & 0xFFFFFF, true); - } else { - return of((color & 0xFFFFFF) | ((int) (u.value * 255D) << 24), true); - } - } - - return super.withAlpha(a); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/FixedNumberUnit.java b/src/main/java/dev/latvian/mods/unit/FixedNumberUnit.java deleted file mode 100644 index 1ec97d59..00000000 --- a/src/main/java/dev/latvian/mods/unit/FixedNumberUnit.java +++ /dev/null @@ -1,216 +0,0 @@ -package dev.latvian.mods.unit; - -public final class FixedNumberUnit extends Unit { - public static final FixedNumberUnit ZERO = new FixedNumberUnit(0); - public static final FixedNumberUnit ONE = new FixedNumberUnit(1); - public static final FixedNumberUnit MINUS_ONE = new FixedNumberUnit(-1); - public static final FixedNumberUnit TEN = new FixedNumberUnit(10); - public static final FixedNumberUnit SIXTEEN = new FixedNumberUnit(16); - public static final FixedNumberUnit PI = new FixedNumberUnit(Math.PI); - public static final FixedNumberUnit TWO_PI = new FixedNumberUnit(Math.PI * 2D); - public static final FixedNumberUnit HALF_PI = new FixedNumberUnit(Math.PI / 2D); - public static final FixedNumberUnit E = new FixedNumberUnit(Math.E); - public static final FixedNumberUnit NaN = new FixedNumberUnit(Double.NaN); - - public static FixedNumberUnit of(double value) { - if (value == 0D) { - return ZERO; - } else if (value == 1D) { - return ONE; - } else if (value == -1D) { - return MINUS_ONE; - } else if (value == 10D) { - return TEN; - } else if (value == 16D) { - return SIXTEEN; - } else { - return new FixedNumberUnit(value); - } - } - - public final double value; - - private FixedNumberUnit(double value) { - this.value = value; - } - - @Override - public boolean isFixed() { - return true; - } - - @Override - public double get(UnitVariables variables) { - return value; - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return this != ZERO; - } - - @Override - public boolean equals(Object obj) { - return obj == this || obj instanceof FixedNumberUnit u && value == u.value; - } - - @Override - public int hashCode() { - return Double.hashCode(value); - } - - @Override - public void toString(StringBuilder builder) { - long r = Math.round(value); - - if (Math.abs(r - value) < 0.00001D) { - builder.append(r); - } else { - builder.append(value); - } - } - - // Operators - - @Override - public Unit negate() { - return of(-value); - } - - @Override - public Unit add(Unit other) { - return other instanceof FixedNumberUnit u ? of(value + u.value) : super.add(other); - } - - @Override - public Unit add(double value) { - return of(this.value + value); - } - - @Override - public Unit sub(Unit other) { - return other instanceof FixedNumberUnit u ? of(value - u.value) : super.sub(other); - } - - @Override - public Unit sub(double value) { - return of(this.value - value); - } - - @Override - public Unit mul(Unit other) { - return other instanceof FixedNumberUnit u ? of(value * u.value) : super.mul(other); - } - - @Override - public Unit mul(double value) { - return of(this.value * value); - } - - @Override - public Unit div(Unit other) { - return other instanceof FixedNumberUnit u ? of(value / u.value) : super.div(other); - } - - @Override - public Unit div(double value) { - return of(this.value / value); - } - - @Override - public Unit mod(Unit other) { - return other instanceof FixedNumberUnit u ? of(value % u.value) : super.mod(other); - } - - @Override - public Unit mod(double value) { - return of(this.value % value); - } - - @Override - public Unit pow(Unit other) { - return other instanceof FixedNumberUnit u ? of(Math.pow(value, u.value)) : super.add(other); - } - - // Functions - - @Override - public Unit abs() { - return of(Math.abs(value)); - } - - @Override - public Unit sin() { - return of(Math.sin(value)); - } - - @Override - public Unit cos() { - return of(Math.cos(value)); - } - - @Override - public Unit tan() { - return of(Math.tan(value)); - } - - @Override - public Unit deg() { - return of(Math.toDegrees(value)); - } - - @Override - public Unit rad() { - return of(Math.toRadians(value)); - } - - @Override - public Unit atan() { - return of(Math.atan(value)); - } - - @Override - public Unit log() { - return of(Math.log(value)); - } - - @Override - public Unit log10() { - return of(Math.log10(value)); - } - - @Override - public Unit log1p() { - return of(Math.log1p(value)); - } - - @Override - public Unit sqrt() { - return of(Math.sqrt(value)); - } - - @Override - public Unit sq() { - return of(value * value); - } - - @Override - public Unit floor() { - return of(Math.floor(value)); - } - - @Override - public Unit ceil() { - return of(Math.ceil(value)); - } - - @Override - public Unit bool() { - return this == ZERO ? FixedBooleanUnit.FALSE : FixedBooleanUnit.TRUE; - } - - @Override - public Unit smoothstep() { - return of(value * value * value * (value * (value * 6D - 15D) + 10D)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/MutableNumberUnit.java b/src/main/java/dev/latvian/mods/unit/MutableNumberUnit.java deleted file mode 100644 index 2d183fc1..00000000 --- a/src/main/java/dev/latvian/mods/unit/MutableNumberUnit.java +++ /dev/null @@ -1,29 +0,0 @@ -package dev.latvian.mods.unit; - -public final class MutableNumberUnit extends Unit { - public double value; - - public MutableNumberUnit(double value) { - this.value = value; - } - - public void set(double value) { - this.value = value; - } - - @Override - public double get(UnitVariables variables) { - return value; - } - - @Override - public void toString(StringBuilder builder) { - long r = Math.round(value); - - if (Math.abs(r - value) < 0.00001D) { - builder.append(r); - } else { - builder.append(value); - } - } -} diff --git a/src/main/java/dev/latvian/mods/unit/TernaryUnit.java b/src/main/java/dev/latvian/mods/unit/TernaryUnit.java deleted file mode 100644 index e03d7777..00000000 --- a/src/main/java/dev/latvian/mods/unit/TernaryUnit.java +++ /dev/null @@ -1,37 +0,0 @@ -package dev.latvian.mods.unit; - -public class TernaryUnit extends Unit { - public final Unit cond, left, right; - - public TernaryUnit(Unit cond, Unit left, Unit right) { - this.cond = cond; - this.left = left; - this.right = right; - } - - @Override - public double get(UnitVariables variables) { - return cond.getBoolean(variables) ? left.get(variables) : right.get(variables); - } - - @Override - public int getInt(UnitVariables variables) { - return cond.getBoolean(variables) ? left.getInt(variables) : right.getInt(variables); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return cond.getBoolean(variables) ? left.getBoolean(variables) : right.getBoolean(variables); - } - - @Override - public void toString(StringBuilder builder) { - builder.append('('); - cond.toString(builder); - builder.append(" ? "); - left.toString(builder); - builder.append(" : "); - right.toString(builder); - builder.append(')'); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/Unit.java b/src/main/java/dev/latvian/mods/unit/Unit.java deleted file mode 100644 index cced1e19..00000000 --- a/src/main/java/dev/latvian/mods/unit/Unit.java +++ /dev/null @@ -1,310 +0,0 @@ -package dev.latvian.mods.unit; - -import dev.latvian.mods.unit.function.AbsFuncUnit; -import dev.latvian.mods.unit.function.Atan2FuncUnit; -import dev.latvian.mods.unit.function.AtanFuncUnit; -import dev.latvian.mods.unit.function.BoolFuncUnit; -import dev.latvian.mods.unit.function.CeilFuncUnit; -import dev.latvian.mods.unit.function.ClampFuncUnit; -import dev.latvian.mods.unit.function.CosFuncUnit; -import dev.latvian.mods.unit.function.DegFuncUnit; -import dev.latvian.mods.unit.function.FloorFuncUnit; -import dev.latvian.mods.unit.function.LerpFuncUnit; -import dev.latvian.mods.unit.function.Log10FuncUnit; -import dev.latvian.mods.unit.function.Log1pFuncUnit; -import dev.latvian.mods.unit.function.LogFuncUnit; -import dev.latvian.mods.unit.function.MaxFuncUnit; -import dev.latvian.mods.unit.function.MinFuncUnit; -import dev.latvian.mods.unit.function.RadFuncUnit; -import dev.latvian.mods.unit.function.SinFuncUnit; -import dev.latvian.mods.unit.function.SmoothstepFuncUnit; -import dev.latvian.mods.unit.function.SqFuncUnit; -import dev.latvian.mods.unit.function.SqrtFuncUnit; -import dev.latvian.mods.unit.function.TanFuncUnit; -import dev.latvian.mods.unit.function.WithAlphaFuncUnit; -import dev.latvian.mods.unit.operator.BitNotOpUnit; -import dev.latvian.mods.unit.operator.BoolNotOpUnit; -import dev.latvian.mods.unit.operator.NegateOpUnit; -import dev.latvian.mods.unit.operator.SetUnit; -import dev.latvian.mods.unit.operator.cond.AndOpUnit; -import dev.latvian.mods.unit.operator.cond.EqOpUnit; -import dev.latvian.mods.unit.operator.cond.GtOpUnit; -import dev.latvian.mods.unit.operator.cond.GteOpUnit; -import dev.latvian.mods.unit.operator.cond.LtOpUnit; -import dev.latvian.mods.unit.operator.cond.LteOpUnit; -import dev.latvian.mods.unit.operator.cond.NeqOpUnit; -import dev.latvian.mods.unit.operator.cond.OrOpUnit; -import dev.latvian.mods.unit.operator.op.AddOpUnit; -import dev.latvian.mods.unit.operator.op.BitAndOpUnit; -import dev.latvian.mods.unit.operator.op.BitOrOpUnit; -import dev.latvian.mods.unit.operator.op.DivOpUnit; -import dev.latvian.mods.unit.operator.op.LshOpUnit; -import dev.latvian.mods.unit.operator.op.ModOpUnit; -import dev.latvian.mods.unit.operator.op.MulOpUnit; -import dev.latvian.mods.unit.operator.op.PowOpUnit; -import dev.latvian.mods.unit.operator.op.RshOpUnit; -import dev.latvian.mods.unit.operator.op.SubOpUnit; -import dev.latvian.mods.unit.operator.op.XorOpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public abstract class Unit { - public static Unit[] EMPTY_ARRAY = new Unit[0]; - - public boolean isFixed() { - return false; - } - - public abstract double get(UnitVariables variables); - - public float getFloat(UnitVariables variables) { - return (float) get(variables); - } - - public int getInt(UnitVariables variables) { - double d = get(variables); - int i = (int) d; - return d < (double) i ? i - 1 : i; - } - - public boolean getBoolean(UnitVariables variables) { - return get(variables) != 0D; - } - - public void toString(StringBuilder builder) { - builder.append(this); - } - - public String toString() { - StringBuilder builder = new StringBuilder(); - toString(builder); - return builder.toString(); - } - - // Operators - - public Unit positive() { - return this; - } - - public Unit negate() { - return new NegateOpUnit(this); - } - - public Unit add(Unit other) { - return new AddOpUnit(this, other); - } - - public Unit add(double value) { - return add(FixedNumberUnit.of(value)); - } - - public Unit sub(Unit other) { - return new SubOpUnit(this, other); - } - - public Unit sub(double value) { - return sub(FixedNumberUnit.of(value)); - } - - public Unit mul(Unit other) { - return new MulOpUnit(this, other); - } - - public Unit mul(double value) { - return add(FixedNumberUnit.of(value)); - } - - public Unit div(Unit other) { - return new DivOpUnit(this, other); - } - - public Unit div(double value) { - return add(FixedNumberUnit.of(value)); - } - - public Unit mod(Unit other) { - return new ModOpUnit(this, other); - } - - public Unit mod(double value) { - return mod(FixedNumberUnit.of(value)); - } - - public Unit pow(Unit other) { - return new PowOpUnit(this, other); - } - - public Unit lsh(Unit other) { - return new LshOpUnit(this, other); - } - - public Unit rsh(Unit other) { - return new RshOpUnit(this, other); - } - - public Unit bitAnd(Unit other) { - return new BitAndOpUnit(this, other); - } - - public Unit bitOr(Unit other) { - return new BitOrOpUnit(this, other); - } - - public Unit xor(Unit other) { - return new XorOpUnit(this, other); - } - - public Unit bitNot() { - return new BitNotOpUnit(this); - } - - public Unit eq(Unit other) { - return new EqOpUnit(this, other); - } - - public Unit neq(Unit other) { - return new NeqOpUnit(this, other); - } - - public Unit lt(Unit other) { - return new LtOpUnit(this, other); - } - - public Unit gt(Unit other) { - return new GtOpUnit(this, other); - } - - public Unit lte(Unit other) { - return new LteOpUnit(this, other); - } - - public Unit gte(Unit other) { - return new GteOpUnit(this, other); - } - - public Unit and(Unit other) { - return new AndOpUnit(this, other); - } - - public Unit or(Unit other) { - return new OrOpUnit(this, other); - } - - public Unit boolNot() { - return new BoolNotOpUnit(this); - } - - // Functions - - public Unit min(Unit other) { - return new MinFuncUnit(this, other); - } - - public Unit max(Unit other) { - return new MaxFuncUnit(this, other); - } - - public Unit abs() { - return new AbsFuncUnit(this); - } - - public Unit sin() { - return new SinFuncUnit(this); - } - - public Unit cos() { - return new CosFuncUnit(this); - } - - public Unit tan() { - return new TanFuncUnit(this); - } - - public Unit deg() { - return new DegFuncUnit(this); - } - - public Unit rad() { - return new RadFuncUnit(this); - } - - public Unit atan() { - return new AtanFuncUnit(this); - } - - public Unit atan2(Unit other) { - return new Atan2FuncUnit(this, other); - } - - public Unit log() { - return new LogFuncUnit(this); - } - - public Unit log10() { - return new Log10FuncUnit(this); - } - - public Unit log1p() { - return new Log1pFuncUnit(this); - } - - public Unit sqrt() { - return new SqrtFuncUnit(this); - } - - public Unit sq() { - return new SqFuncUnit(this); - } - - public Unit floor() { - return new FloorFuncUnit(this); - } - - public Unit ceil() { - return new CeilFuncUnit(this); - } - - public Unit bool() { - return new BoolFuncUnit(this); - } - - public Unit clamp(Unit a, Unit b) { - return new ClampFuncUnit(this, a, b); - } - - public Unit lerp(Unit a, Unit b) { - return new LerpFuncUnit(this, a, b); - } - - public Unit smoothstep() { - return new SmoothstepFuncUnit(this); - } - - public Unit withAlpha(Unit a) { - return new WithAlphaFuncUnit(this, a); - } - - public Unit set(Unit unit) { - return new SetUnit(UnitSymbol.SET, this, unit); - } - - public Unit addSet(Unit unit) { - return new SetUnit(UnitSymbol.ADD_SET, this, add(unit)); - } - - public Unit subSet(Unit unit) { - return new SetUnit(UnitSymbol.SUB_SET, this, sub(unit)); - } - - public Unit mulSet(Unit unit) { - return new SetUnit(UnitSymbol.MUL_SET, this, mul(unit)); - } - - public Unit divSet(Unit unit) { - return new SetUnit(UnitSymbol.DIV_SET, this, div(unit)); - } - - public Unit modSet(Unit unit) { - return new SetUnit(UnitSymbol.MOD_SET, this, mod(unit)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/UnitContext.java b/src/main/java/dev/latvian/mods/unit/UnitContext.java deleted file mode 100644 index 515950ff..00000000 --- a/src/main/java/dev/latvian/mods/unit/UnitContext.java +++ /dev/null @@ -1,148 +0,0 @@ -package dev.latvian.mods.unit; - -import dev.latvian.mods.unit.function.AbsFuncUnit; -import dev.latvian.mods.unit.function.Atan2FuncUnit; -import dev.latvian.mods.unit.function.AtanFuncUnit; -import dev.latvian.mods.unit.function.BoolFuncUnit; -import dev.latvian.mods.unit.function.CeilFuncUnit; -import dev.latvian.mods.unit.function.ClampFuncUnit; -import dev.latvian.mods.unit.function.CosFuncUnit; -import dev.latvian.mods.unit.function.DegFuncUnit; -import dev.latvian.mods.unit.function.FloorFuncUnit; -import dev.latvian.mods.unit.function.FunctionFactory; -import dev.latvian.mods.unit.function.HsvFuncUnit; -import dev.latvian.mods.unit.function.LerpFuncUnit; -import dev.latvian.mods.unit.function.Log10FuncUnit; -import dev.latvian.mods.unit.function.Log1pFuncUnit; -import dev.latvian.mods.unit.function.LogFuncUnit; -import dev.latvian.mods.unit.function.MapFuncUnit; -import dev.latvian.mods.unit.function.MaxFuncUnit; -import dev.latvian.mods.unit.function.MinFuncUnit; -import dev.latvian.mods.unit.function.RadFuncUnit; -import dev.latvian.mods.unit.function.RandomUnit; -import dev.latvian.mods.unit.function.RgbFuncUnit; -import dev.latvian.mods.unit.function.RoundedTimeUnit; -import dev.latvian.mods.unit.function.SinFuncUnit; -import dev.latvian.mods.unit.function.SmoothstepFuncUnit; -import dev.latvian.mods.unit.function.SqFuncUnit; -import dev.latvian.mods.unit.function.SqrtFuncUnit; -import dev.latvian.mods.unit.function.TanFuncUnit; -import dev.latvian.mods.unit.function.TimeUnit; -import dev.latvian.mods.unit.function.WithAlphaFuncUnit; -import dev.latvian.mods.unit.token.UnitTokenStream; -import org.jetbrains.annotations.Nullable; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.stream.Collectors; - -public class UnitContext { - public static final UnitContext DEFAULT = new UnitContext(); - - static { - DEFAULT.addFunction(TimeUnit.FACTORY); - DEFAULT.addFunction(RoundedTimeUnit.FACTORY); - DEFAULT.addFunction(RandomUnit.FACTORY); - DEFAULT.addFunction(FunctionFactory.of3("if", TernaryUnit::new)); - DEFAULT.addFunction(RgbFuncUnit.FACTORY); - - DEFAULT.addFunction(MinFuncUnit.FACTORY); - DEFAULT.addFunction(MaxFuncUnit.FACTORY); - DEFAULT.addFunction(AbsFuncUnit.FACTORY); - DEFAULT.addFunction(SinFuncUnit.FACTORY); - DEFAULT.addFunction(CosFuncUnit.FACTORY); - DEFAULT.addFunction(TanFuncUnit.FACTORY); - DEFAULT.addFunction(DegFuncUnit.FACTORY); - DEFAULT.addFunction(RadFuncUnit.FACTORY); - DEFAULT.addFunction(AtanFuncUnit.FACTORY); - DEFAULT.addFunction(Atan2FuncUnit.FACTORY); - DEFAULT.addFunction(LogFuncUnit.FACTORY); - DEFAULT.addFunction(Log10FuncUnit.FACTORY); - DEFAULT.addFunction(Log1pFuncUnit.FACTORY); - DEFAULT.addFunction(SqrtFuncUnit.FACTORY); - DEFAULT.addFunction(SqFuncUnit.FACTORY); - DEFAULT.addFunction(FloorFuncUnit.FACTORY); - DEFAULT.addFunction(CeilFuncUnit.FACTORY); - DEFAULT.addFunction(BoolFuncUnit.FACTORY); - DEFAULT.addFunction(ClampFuncUnit.FACTORY); - DEFAULT.addFunction(LerpFuncUnit.FACTORY); - DEFAULT.addFunction(SmoothstepFuncUnit.FACTORY); - DEFAULT.addFunction(HsvFuncUnit.FACTORY); - DEFAULT.addFunction(WithAlphaFuncUnit.FACTORY); - DEFAULT.addFunction(MapFuncUnit.FACTORY); - - DEFAULT.addConstant("true", FixedBooleanUnit.TRUE); - DEFAULT.addConstant("false", FixedBooleanUnit.FALSE); - DEFAULT.addConstant("PI", FixedNumberUnit.PI); - DEFAULT.addConstant("TWO_PI", FixedNumberUnit.TWO_PI); - DEFAULT.addConstant("HALF_PI", FixedNumberUnit.HALF_PI); - DEFAULT.addConstant("E", FixedNumberUnit.E); - } - - public final Map constants = new HashMap<>(); - private final Map functions = new HashMap<>(); - private final Map cache = new HashMap<>(); - private int debug = -1; - - public void addFunction(FunctionFactory factory) { - functions.put(factory.name(), factory); - } - - @Nullable - public FunctionFactory getFunctionFactory(String name) { - return functions.get(name); - } - - public void addConstant(String s, Unit u) { - constants.put(s, u); - } - - public UnitContext sub() { - UnitContext ctx = new UnitContext(); - ctx.functions.putAll(functions); - ctx.debug = debug; - return ctx; - } - - public UnitTokenStream createStream(String input) { - return new UnitTokenStream(this, input); - } - - public Unit parse(String input) { - Unit u = cache.get(input); - - if (u == null) { - u = createStream(input).getUnit(); - cache.put(input, u); - } - - return u; - } - - public boolean isDebug() { - return debug >= 0; - } - - public void pushDebug() { - debug++; - } - - public void popDebug() { - debug--; - } - - public void debugInfo(String s) { - if (debug >= 0) { - if (debug >= 2) { - System.out.println(" ".repeat(debug - 1) + s); - } else { - System.out.println(s); - } - } - } - - public void debugInfo(String s, Collection values) { - debugInfo(s + ": " + values.stream().map(Object::toString).collect(Collectors.joining(" "))); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/UnitVariables.java b/src/main/java/dev/latvian/mods/unit/UnitVariables.java deleted file mode 100644 index 04720c26..00000000 --- a/src/main/java/dev/latvian/mods/unit/UnitVariables.java +++ /dev/null @@ -1,6 +0,0 @@ -package dev.latvian.mods.unit; - -@FunctionalInterface -public interface UnitVariables { - VariableSet getVariables(); -} diff --git a/src/main/java/dev/latvian/mods/unit/VariableSet.java b/src/main/java/dev/latvian/mods/unit/VariableSet.java deleted file mode 100644 index a8fb11b8..00000000 --- a/src/main/java/dev/latvian/mods/unit/VariableSet.java +++ /dev/null @@ -1,40 +0,0 @@ -package dev.latvian.mods.unit; - -import org.jetbrains.annotations.Nullable; - -import java.util.HashMap; -import java.util.Map; - -public class VariableSet implements UnitVariables { - - private final Map variables = new HashMap<>(); - - public VariableSet set(String name, Unit value) { - variables.put(name, value); - return this; - } - - public VariableSet set(String name, double value) { - return set(name, FixedNumberUnit.of(value)); - } - - public MutableNumberUnit setMutable(String name, double initialValue) { - MutableNumberUnit unit = new MutableNumberUnit(initialValue); - set(name, unit); - return unit; - } - - @Nullable - public Unit get(String entry) { - return variables.get(entry); - } - - public VariableSet createSubset() { - return new VariableSubset(this); - } - - @Override - public final VariableSet getVariables() { - return this; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/VariableSubset.java b/src/main/java/dev/latvian/mods/unit/VariableSubset.java deleted file mode 100644 index 86928a62..00000000 --- a/src/main/java/dev/latvian/mods/unit/VariableSubset.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit; - -public class VariableSubset extends VariableSet { - - private final VariableSet parent; - - VariableSubset(VariableSet parent) { - this.parent = parent; - } - - @Override - public Unit get(String entry) { - Unit v = super.get(entry); - return v == null ? parent.get(entry) : v; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/VariableUnit.java b/src/main/java/dev/latvian/mods/unit/VariableUnit.java deleted file mode 100644 index 32e0e0a5..00000000 --- a/src/main/java/dev/latvian/mods/unit/VariableUnit.java +++ /dev/null @@ -1,37 +0,0 @@ -package dev.latvian.mods.unit; - -import java.util.HashMap; -import java.util.Map; - -public class VariableUnit extends Unit { - private static final Object CACHE_LOCK = new Object(); - private static final Map CACHE = new HashMap<>(); - - public static VariableUnit of(String name) { - synchronized (CACHE_LOCK) { - return CACHE.computeIfAbsent(name, VariableUnit::new); - } - } - - public final String name; - - private VariableUnit(String n) { - name = n; - } - - @Override - public double get(UnitVariables variables) { - Unit var = variables.getVariables().get(name); - - if (var == null) { - throw new IllegalStateException("Variable " + name + " is not defined!"); - } - - return var.get(variables); - } - - @Override - public void toString(StringBuilder builder) { - builder.append(name); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/AbsFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/AbsFuncUnit.java deleted file mode 100644 index 7ff8f0b7..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/AbsFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class AbsFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("abs", Unit::abs); - - public AbsFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.abs(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/Atan2FuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/Atan2FuncUnit.java deleted file mode 100644 index 02e08cd2..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/Atan2FuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class Atan2FuncUnit extends Func2Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of2("atan2", Unit::atan2); - - public Atan2FuncUnit(Unit a, Unit b) { - super(FACTORY, a, b); - } - - @Override - public double get(UnitVariables variables) { - return Math.atan2(a.get(variables), b.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/AtanFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/AtanFuncUnit.java deleted file mode 100644 index 3678f111..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/AtanFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class AtanFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("atan", Unit::atan); - - public AtanFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.atan(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/BoolFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/BoolFuncUnit.java deleted file mode 100644 index 87cfbfbe..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/BoolFuncUnit.java +++ /dev/null @@ -1,32 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class BoolFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("bool", Unit::bool); - - public BoolFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return getBoolean(variables) ? 1D : 0D; - } - - @Override - public float getFloat(UnitVariables variables) { - return getBoolean(variables) ? 1F : 0F; - } - - @Override - public int getInt(UnitVariables variables) { - return getBoolean(variables) ? 1 : 0; - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return a.getBoolean(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/CeilFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/CeilFuncUnit.java deleted file mode 100644 index a00a1dbb..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/CeilFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class CeilFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("ceil", Unit::ceil); - - public CeilFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.ceil(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/ClampFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/ClampFuncUnit.java deleted file mode 100644 index 45934bb8..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/ClampFuncUnit.java +++ /dev/null @@ -1,25 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class ClampFuncUnit extends Func3Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of3("clamp", Unit::clamp); - - public static double clamp(double a, double b, double c) { - if (a < b) { - return b; - } else { - return a > c ? c : a; - } - } - - public ClampFuncUnit(Unit a, Unit b, Unit c) { - super(FACTORY, a, b, c); - } - - @Override - public double get(UnitVariables variables) { - return clamp(a.get(variables), b.get(variables), c.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/CosFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/CosFuncUnit.java deleted file mode 100644 index b47cf957..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/CosFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class CosFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("cos", Unit::cos); - - public CosFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.cos(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/DegFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/DegFuncUnit.java deleted file mode 100644 index c49747b5..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/DegFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class DegFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("deg", Unit::deg); - - public DegFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.toDegrees(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/FloorFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/FloorFuncUnit.java deleted file mode 100644 index d9cf86a9..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/FloorFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class FloorFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("floor", Unit::floor); - - public FloorFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.floor(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/Func1Unit.java b/src/main/java/dev/latvian/mods/unit/function/Func1Unit.java deleted file mode 100644 index 4ea7f5f6..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/Func1Unit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; - -public abstract class Func1Unit extends FuncUnit { - public final Unit a; - - public Func1Unit(FunctionFactory factory, Unit a) { - super(factory); - this.a = a; - } - - @Override - protected final Unit[] getArguments() { - return new Unit[]{a}; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/Func2Unit.java b/src/main/java/dev/latvian/mods/unit/function/Func2Unit.java deleted file mode 100644 index 7877b667..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/Func2Unit.java +++ /dev/null @@ -1,18 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; - -public abstract class Func2Unit extends FuncUnit { - public final Unit a, b; - - public Func2Unit(FunctionFactory factory, Unit a, Unit b) { - super(factory); - this.a = a; - this.b = b; - } - - @Override - protected Unit[] getArguments() { - return new Unit[]{a, b}; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/Func3Unit.java b/src/main/java/dev/latvian/mods/unit/function/Func3Unit.java deleted file mode 100644 index a09f1bb7..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/Func3Unit.java +++ /dev/null @@ -1,19 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; - -public abstract class Func3Unit extends FuncUnit { - public final Unit a, b, c; - - public Func3Unit(FunctionFactory factory, Unit a, Unit b, Unit c) { - super(factory); - this.a = a; - this.b = b; - this.c = c; - } - - @Override - protected Unit[] getArguments() { - return new Unit[]{a, b, c}; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/FuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/FuncUnit.java deleted file mode 100644 index 6cdd9353..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/FuncUnit.java +++ /dev/null @@ -1,35 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; - -public abstract class FuncUnit extends Unit { - private static final Unit[] NO_ARGS = new Unit[0]; - - public final FunctionFactory factory; - - public FuncUnit(FunctionFactory factory) { - this.factory = factory; - } - - protected Unit[] getArguments() { - return NO_ARGS; - } - - @Override - public void toString(StringBuilder builder) { - builder.append(factory.name()); - builder.append('('); - - var args = getArguments(); - - for (int i = 0; i < args.length; i++) { - if (i > 0) { - builder.append(','); - } - - args[i].toString(builder); - } - - builder.append(')'); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/FunctionFactory.java b/src/main/java/dev/latvian/mods/unit/function/FunctionFactory.java deleted file mode 100644 index 22299a5d..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/FunctionFactory.java +++ /dev/null @@ -1,93 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.token.UnitInterpretException; - -import java.util.function.Supplier; - -public record FunctionFactory(String name, int minArgs, int maxArgs, FuncSupplier supplier) { - public static final class Arg0 implements FuncSupplier { - private final Supplier unit; - private Unit cachedUnit; - - public Arg0(Supplier unit) { - this.unit = unit; - } - - @Override - public Unit create(Unit[] args) { - if (cachedUnit == null) { - cachedUnit = unit.get(); - } - - return cachedUnit; - } - } - - public static FunctionFactory of(String name, int minArgs, int maxArgs, FuncSupplier supplier) { - return new FunctionFactory(name, minArgs, maxArgs, supplier); - } - - public static FunctionFactory of(String name, int args, FuncSupplier supplier) { - return of(name, args, args, supplier); - } - - public static FunctionFactory of0(String name, Supplier supplier) { - return of(name, 0, new Arg0(supplier)); - } - - public static FunctionFactory of1(String name, Arg1 supplier) { - return of(name, 1, supplier); - } - - public static FunctionFactory of2(String name, Arg2 supplier) { - return of(name, 2, supplier); - } - - public static FunctionFactory of3(String name, Arg3 supplier) { - return of(name, 3, supplier); - } - - public Unit create(Unit[] args) { - if (args.length < minArgs || args.length > maxArgs) { - throw new UnitInterpretException("Invalid number of arguments for function '" + name + "'. Expected " + (minArgs == maxArgs ? String.valueOf(minArgs) : (minArgs + "-" + maxArgs)) + " but got " + args.length); - } - - return supplier.create(args); - } - - @FunctionalInterface - public interface FuncSupplier { - Unit create(Unit[] args); - } - - @FunctionalInterface - public interface Arg1 extends FuncSupplier { - Unit createArg(Unit a); - - @Override - default Unit create(Unit[] args) { - return createArg(args[0]); - } - } - - @FunctionalInterface - public interface Arg2 extends FuncSupplier { - Unit createArg(Unit a, Unit b); - - @Override - default Unit create(Unit[] args) { - return createArg(args[0], args[1]); - } - } - - @FunctionalInterface - public interface Arg3 extends FuncSupplier { - Unit createArg(Unit a, Unit b, Unit c); - - @Override - default Unit create(Unit[] args) { - return createArg(args[0], args[1], args[2]); - } - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/HsvFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/HsvFuncUnit.java deleted file mode 100644 index 13b22cd3..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/HsvFuncUnit.java +++ /dev/null @@ -1,107 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.FixedNumberUnit; -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class HsvFuncUnit extends FuncUnit { - public final Unit h, s, v, a; - public static final FunctionFactory FACTORY = FunctionFactory.of("hsv", 3, 4, HsvFuncUnit::new); - - public HsvFuncUnit(Unit[] args) { - super(FACTORY); - h = args[0]; - s = args[1]; - v = args[2]; - a = args.length == 4 ? args[3] : FixedNumberUnit.ONE; - } - - @Override - protected Unit[] getArguments() { - if (a == FixedNumberUnit.ONE) { - return new Unit[]{h, s, v}; - } - - return new Unit[]{h, s, v, a}; - } - - @Override - public double get(UnitVariables variables) { - return getInt(variables); - } - - @Override - public int getInt(UnitVariables variables) { - double h = this.h.get(variables); - double s = this.s.get(variables); - double v = this.v.get(variables); - - int i = (int) (h * 6D) % 6; - double j = h * 6D - (float) i; - double k = v * (1D - s); - double l = v * (1D - j * s); - double m = v * (1D - (1D - j) * s); - double dr; - double dg; - double db; - switch (i) { - case 0 -> { - dr = v; - dg = m; - db = k; - } - case 1 -> { - dr = l; - dg = v; - db = k; - } - case 2 -> { - dr = k; - dg = v; - db = m; - } - case 3 -> { - dr = k; - dg = l; - db = v; - } - case 4 -> { - dr = m; - dg = k; - db = v; - } - case 5 -> { - dr = v; - dg = k; - db = l; - } - default -> { - dr = 0D; - dg = 0D; - db = 0D; - } - } - - int cr = (int) (ClampFuncUnit.clamp(dr * 255D, 0D, 255D)); - int cg = (int) (ClampFuncUnit.clamp(dg * 255D, 0D, 255D)); - int cb = (int) (ClampFuncUnit.clamp(db * 255D, 0D, 255D)); - int ca = (int) (ClampFuncUnit.clamp(this.a.get(variables) * 255D, 0D, 255D)); - return ca << 24 | cr << 16 | cg << 8 | cb; - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return a.getBoolean(variables); - } - - @Override - public Unit withAlpha(Unit a) { - if (a == this.a) { - return this; - } - - return new HsvFuncUnit(new Unit[]{h, s, v, a}); - } - - -} diff --git a/src/main/java/dev/latvian/mods/unit/function/LerpFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/LerpFuncUnit.java deleted file mode 100644 index 41ee29c0..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/LerpFuncUnit.java +++ /dev/null @@ -1,21 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class LerpFuncUnit extends Func3Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of3("lerp", Unit::lerp); - - public static double lerp(double a, double b, double c) { - return b + a * (c - b); - } - - public LerpFuncUnit(Unit a, Unit b, Unit c) { - super(FACTORY, a, b, c); - } - - @Override - public double get(UnitVariables variables) { - return lerp(a.get(variables), b.get(variables), c.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/Log10FuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/Log10FuncUnit.java deleted file mode 100644 index 9f8a2b10..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/Log10FuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class Log10FuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("log10", Unit::log10); - - public Log10FuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.log10(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/Log1pFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/Log1pFuncUnit.java deleted file mode 100644 index 2478be0a..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/Log1pFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class Log1pFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("log1p", Unit::log1p); - - public Log1pFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.log1p(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/LogFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/LogFuncUnit.java deleted file mode 100644 index 3cf06be8..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/LogFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class LogFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("log", Unit::log); - - public LogFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.log(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/MapFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/MapFuncUnit.java deleted file mode 100644 index b75f4267..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/MapFuncUnit.java +++ /dev/null @@ -1,33 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class MapFuncUnit extends FuncUnit { - public static double map(double value, double min1, double max1, double min2, double max2) { - return LerpFuncUnit.lerp(min2, max2, (value - min1) / (max1 - min1)); - } - - public static final FunctionFactory FACTORY = FunctionFactory.of("map", 5, MapFuncUnit::new); - public final Unit value; - public final Unit min1; - public final Unit max1; - public final Unit min2; - public final Unit max2; - - public MapFuncUnit(Unit[] args) { - super(FACTORY); - value = args[0]; - min1 = args[1]; - max1 = args[2]; - min2 = args[3]; - max2 = args[4]; - } - - @Override - public double get(UnitVariables variables) { - return map(value.get(variables), min1.get(variables), max1.get(variables), min2.get(variables), max2.get(variables)); - } - - -} diff --git a/src/main/java/dev/latvian/mods/unit/function/MaxFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/MaxFuncUnit.java deleted file mode 100644 index c3823531..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/MaxFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class MaxFuncUnit extends Func2Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of2("max", Unit::max); - - public MaxFuncUnit(Unit a, Unit b) { - super(FACTORY, a, b); - } - - @Override - public double get(UnitVariables variables) { - return Math.max(a.get(variables), b.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/MinFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/MinFuncUnit.java deleted file mode 100644 index 76eb213d..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/MinFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class MinFuncUnit extends Func2Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of2("min", Unit::min); - - public MinFuncUnit(Unit a, Unit b) { - super(FACTORY, a, b); - } - - @Override - public double get(UnitVariables variables) { - return Math.min(a.get(variables), b.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/RadFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/RadFuncUnit.java deleted file mode 100644 index 85a07168..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/RadFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class RadFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("rad", Unit::rad); - - public RadFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.toRadians(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/RandomUnit.java b/src/main/java/dev/latvian/mods/unit/function/RandomUnit.java deleted file mode 100644 index c20fefc9..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/RandomUnit.java +++ /dev/null @@ -1,22 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.UnitVariables; - -import java.util.Random; - -public class RandomUnit extends FuncUnit { - public static final Random RANDOM = new Random(); - - private RandomUnit() { - super(FACTORY); - } - - public static final FunctionFactory FACTORY = FunctionFactory.of0("random", RandomUnit::new); - - @Override - public double get(UnitVariables variables) { - return RANDOM.nextDouble(); - } - - -} \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/unit/function/RgbFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/RgbFuncUnit.java deleted file mode 100644 index 8d2bb338..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/RgbFuncUnit.java +++ /dev/null @@ -1,84 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.EmptyVariableSet; -import dev.latvian.mods.unit.FixedColorUnit; -import dev.latvian.mods.unit.FixedNumberUnit; -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class RgbFuncUnit extends FuncUnit { - public static final FunctionFactory FACTORY = FunctionFactory.of("rgb", 1, 4, args -> { - if (args.length == 1 && args[0] instanceof FixedColorUnit) { - return args[0]; - } - - RgbFuncUnit c = new RgbFuncUnit(); - c.a = FixedNumberUnit.ONE; - - if (args.length == 3 || args.length == 4) { - c.r = args[0]; - c.g = args[1]; - c.b = args[2]; - - if (args.length == 4) { - c.a = args[3]; - } - } else if (args.length == 2) { - if (args[0] instanceof FixedColorUnit u) { - if (args[1].isFixed()) { - return u.withAlpha(args[1]); - } else { - c.r = FixedNumberUnit.of(((u.color >> 16) & 0xFF) / 255D); - c.g = FixedNumberUnit.of(((u.color >> 8) & 0xFF) / 255D); - c.b = FixedNumberUnit.of(((u.color >> 0) & 0xFF) / 255D); - c.a = args[1]; - } - } else { - c.r = c.g = c.b = args[0]; - c.a = args[1]; - } - } else if (args.length == 1) { - c.r = c.g = c.b = args[0]; - } - - if (c.r.isFixed() && c.g.isFixed() && c.b.isFixed() && c.a.isFixed()) { - return FixedColorUnit.of(c.getInt(EmptyVariableSet.INSTANCE), true); - } - - return c; - }); - - private static int c(UnitVariables variables, Unit u) { - return (int) (Math.min(Math.max(0D, u.get(variables) * 255D), 255D)); - } - - public Unit r, g, b, a; - - private RgbFuncUnit() { - super(FACTORY); - } - - @Override - protected Unit[] getArguments() { - if (a == FixedNumberUnit.ONE) { - return new Unit[]{r, g, b}; - } - - return new Unit[]{r, g, b, a}; - } - - @Override - public double get(UnitVariables variables) { - return getInt(variables); - } - - @Override - public int getInt(UnitVariables variables) { - return (c(variables, r) << 16) | (c(variables, g) << 8) | c(variables, b) | (c(variables, a) << 24); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return a.getBoolean(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/RoundedTimeUnit.java b/src/main/java/dev/latvian/mods/unit/function/RoundedTimeUnit.java deleted file mode 100644 index 6c4df63e..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/RoundedTimeUnit.java +++ /dev/null @@ -1,22 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.UnitVariables; - -public class RoundedTimeUnit extends FuncUnit { - public static long time() { - return Math.round(System.currentTimeMillis() / 1000D); - } - - public static final FunctionFactory FACTORY = FunctionFactory.of0("roundedTime", RoundedTimeUnit::new); - - private RoundedTimeUnit() { - super(FACTORY); - } - - @Override - public double get(UnitVariables variables) { - return time(); - } - - -} \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/unit/function/SinFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/SinFuncUnit.java deleted file mode 100644 index e7ba2eb1..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/SinFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class SinFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("sin", Unit::sin); - - public SinFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.sin(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/SmoothstepFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/SmoothstepFuncUnit.java deleted file mode 100644 index e18b9636..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/SmoothstepFuncUnit.java +++ /dev/null @@ -1,18 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class SmoothstepFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("smoothstep", Unit::smoothstep); - - public SmoothstepFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - double d = a.get(variables); - return d * d * d * (d * (d * 6D - 15D) + 10D); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/SqFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/SqFuncUnit.java deleted file mode 100644 index 18fb14ea..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/SqFuncUnit.java +++ /dev/null @@ -1,18 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class SqFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("sq", Unit::sq); - - public SqFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - double x = a.get(variables); - return x * x; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/SqrtFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/SqrtFuncUnit.java deleted file mode 100644 index 1e075e7d..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/SqrtFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class SqrtFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("sqrt", Unit::sqrt); - - public SqrtFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.sqrt(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/TanFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/TanFuncUnit.java deleted file mode 100644 index e23f7f1e..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/TanFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class TanFuncUnit extends Func1Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of1("tan", Unit::tan); - - public TanFuncUnit(Unit a) { - super(FACTORY, a); - } - - @Override - public double get(UnitVariables variables) { - return Math.tan(a.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/function/TimeUnit.java b/src/main/java/dev/latvian/mods/unit/function/TimeUnit.java deleted file mode 100644 index f1d55104..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/TimeUnit.java +++ /dev/null @@ -1,22 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.UnitVariables; - -public class TimeUnit extends FuncUnit { - public static double time() { - return System.currentTimeMillis() / 1000D; - } - - public static final FunctionFactory FACTORY = FunctionFactory.of0("time", TimeUnit::new); - - private TimeUnit() { - super(FACTORY); - } - - @Override - public double get(UnitVariables variables) { - return time(); - } - - -} \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/unit/function/WithAlphaFuncUnit.java b/src/main/java/dev/latvian/mods/unit/function/WithAlphaFuncUnit.java deleted file mode 100644 index 180382dc..00000000 --- a/src/main/java/dev/latvian/mods/unit/function/WithAlphaFuncUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.function; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class WithAlphaFuncUnit extends Func2Unit { - public static final FunctionFactory FACTORY = FunctionFactory.of2("withAlpha", Unit::withAlpha); - - public WithAlphaFuncUnit(Unit a, Unit b) { - super(FACTORY, a, b); - } - - @Override - public double get(UnitVariables variables) { - return Math.atan2(a.get(variables), b.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/BitNotOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/BitNotOpUnit.java deleted file mode 100644 index 7e509e65..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/BitNotOpUnit.java +++ /dev/null @@ -1,26 +0,0 @@ -package dev.latvian.mods.unit.operator; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class BitNotOpUnit extends UnaryOpUnit { - public BitNotOpUnit(Unit unit) { - super(UnitSymbol.BIT_NOT, unit); - } - - @Override - public double get(UnitVariables variables) { - return getInt(variables); - } - - @Override - public int getInt(UnitVariables variables) { - return ~unit.getInt(variables); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return !unit.getBoolean(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/BoolNotOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/BoolNotOpUnit.java deleted file mode 100644 index 9cc97d23..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/BoolNotOpUnit.java +++ /dev/null @@ -1,26 +0,0 @@ -package dev.latvian.mods.unit.operator; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class BoolNotOpUnit extends UnaryOpUnit { - public BoolNotOpUnit(Unit unit) { - super(UnitSymbol.BOOL_NOT, unit); - } - - @Override - public double get(UnitVariables variables) { - return getBoolean(variables) ? 1.0D : 0.0D; - } - - @Override - public int getInt(UnitVariables variables) { - return getBoolean(variables) ? 1 : 0; - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return !unit.getBoolean(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/GroupUnit.java b/src/main/java/dev/latvian/mods/unit/operator/GroupUnit.java deleted file mode 100644 index e30ebc21..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/GroupUnit.java +++ /dev/null @@ -1,57 +0,0 @@ -package dev.latvian.mods.unit.operator; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; - -public class GroupUnit extends Unit { - public final Unit[] units; - - public GroupUnit(Unit[] units) { - this.units = units; - } - - @Override - public double get(UnitVariables variables) { - double value = 0D; - - for (Unit unit : units) { - value = unit.get(variables); - } - - return value; - } - - @Override - public int getInt(UnitVariables variables) { - int value = 0; - - for (Unit unit : units) { - value = unit.getInt(variables); - } - - return value; - } - - @Override - public boolean getBoolean(UnitVariables variables) { - boolean value = false; - - for (Unit unit : units) { - value = unit.getBoolean(variables); - } - - return value; - } - - @Override - public void toString(StringBuilder builder) { - for (int i = 0; i < units.length; i++) { - if (i > 0) { - builder.append(';'); - builder.append(' '); - } - - units[i].toString(builder); - } - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/NegateOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/NegateOpUnit.java deleted file mode 100644 index 64c3da66..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/NegateOpUnit.java +++ /dev/null @@ -1,21 +0,0 @@ -package dev.latvian.mods.unit.operator; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class NegateOpUnit extends UnaryOpUnit { - public NegateOpUnit(Unit unit) { - super(UnitSymbol.BIT_NOT, unit); - } - - @Override - public double get(UnitVariables variables) { - return -unit.get(variables); - } - - @Override - public int getInt(UnitVariables variables) { - return -unit.getInt(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/OpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/OpUnit.java deleted file mode 100644 index bc0b853c..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/OpUnit.java +++ /dev/null @@ -1,37 +0,0 @@ -package dev.latvian.mods.unit.operator; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public abstract class OpUnit extends Unit { - public final UnitSymbol symbol; - public Unit left; - public Unit right; - - public OpUnit(UnitSymbol symbol, Unit left, Unit right) { - this.symbol = symbol; - this.left = left; - this.right = right; - } - - @Override - public void toString(StringBuilder builder) { - builder.append('('); - - if (left == null) { - builder.append("null"); - } else { - left.toString(builder); - } - - builder.append(symbol); - - if (right == null) { - builder.append("null"); - } else { - right.toString(builder); - } - - builder.append(')'); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/OperatorFactory.java b/src/main/java/dev/latvian/mods/unit/operator/OperatorFactory.java deleted file mode 100644 index 4aa5e721..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/OperatorFactory.java +++ /dev/null @@ -1,8 +0,0 @@ -package dev.latvian.mods.unit.operator; - -import dev.latvian.mods.unit.Unit; - -@FunctionalInterface -public interface OperatorFactory { - Unit create(Unit left, Unit right); -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/SetUnit.java b/src/main/java/dev/latvian/mods/unit/operator/SetUnit.java deleted file mode 100644 index d6429958..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/SetUnit.java +++ /dev/null @@ -1,39 +0,0 @@ -package dev.latvian.mods.unit.operator; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.VariableUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class SetUnit extends OpUnit { - public SetUnit(UnitSymbol symbol, Unit left, Unit right) { - super(symbol, left, right); - } - - @Override - public double get(UnitVariables variables) { - if (left instanceof VariableUnit var) { - variables.getVariables().set(var.name, right.get(variables)); - } - - return right.get(variables); - } - - @Override - public int getInt(UnitVariables variables) { - if (left instanceof VariableUnit var) { - variables.getVariables().set(var.name, right.get(variables)); - } - - return right.getInt(variables); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - if (left instanceof VariableUnit var) { - variables.getVariables().set(var.name, right.get(variables)); - } - - return right.getBoolean(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/UnaryOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/UnaryOpUnit.java deleted file mode 100644 index d67cd800..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/UnaryOpUnit.java +++ /dev/null @@ -1,22 +0,0 @@ -package dev.latvian.mods.unit.operator; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public abstract class UnaryOpUnit extends Unit { - public final UnitSymbol symbol; - public Unit unit; - - public UnaryOpUnit(UnitSymbol symbol, Unit unit) { - this.symbol = symbol; - this.unit = unit; - } - - @Override - public void toString(StringBuilder builder) { - builder.append('('); - builder.append(symbol); - unit.toString(builder); - builder.append(')'); - } -} \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/unit/operator/UnaryOperatorFactory.java b/src/main/java/dev/latvian/mods/unit/operator/UnaryOperatorFactory.java deleted file mode 100644 index a7f043b9..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/UnaryOperatorFactory.java +++ /dev/null @@ -1,8 +0,0 @@ -package dev.latvian.mods.unit.operator; - -import dev.latvian.mods.unit.Unit; - -@FunctionalInterface -public interface UnaryOperatorFactory { - Unit create(Unit unit); -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/cond/AndOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/cond/AndOpUnit.java deleted file mode 100644 index 032a58fa..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/cond/AndOpUnit.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit.operator.cond; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class AndOpUnit extends CondOpUnit { - public AndOpUnit(Unit left, Unit right) { - super(UnitSymbol.AND, left, right); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left.getBoolean(variables) && right.getBoolean(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/cond/CondOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/cond/CondOpUnit.java deleted file mode 100644 index c455d6fb..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/cond/CondOpUnit.java +++ /dev/null @@ -1,30 +0,0 @@ -package dev.latvian.mods.unit.operator.cond; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public abstract class CondOpUnit extends OpUnit { - public CondOpUnit(UnitSymbol symbol, Unit left, Unit right) { - super(symbol, left, right); - } - - @Override - public final double get(UnitVariables variables) { - return getBoolean(variables) ? 1D : 0D; - } - - @Override - public final float getFloat(UnitVariables variables) { - return getBoolean(variables) ? 1F : 0F; - } - - @Override - public final int getInt(UnitVariables variables) { - return getBoolean(variables) ? 1 : 0; - } - - @Override - public abstract boolean getBoolean(UnitVariables variables); -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/cond/EqOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/cond/EqOpUnit.java deleted file mode 100644 index 7406a670..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/cond/EqOpUnit.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit.operator.cond; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class EqOpUnit extends CondOpUnit { - public EqOpUnit(Unit left, Unit right) { - super(UnitSymbol.EQ, left, right); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left == right || Math.abs(left.get(variables) - right.get(variables)) < 0.00001D; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/cond/GtOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/cond/GtOpUnit.java deleted file mode 100644 index 35ffafa3..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/cond/GtOpUnit.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit.operator.cond; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class GtOpUnit extends CondOpUnit { - public GtOpUnit(Unit left, Unit right) { - super(UnitSymbol.GT, left, right); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left.get(variables) > right.get(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/cond/GteOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/cond/GteOpUnit.java deleted file mode 100644 index 9ac6874c..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/cond/GteOpUnit.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit.operator.cond; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class GteOpUnit extends CondOpUnit { - public GteOpUnit(Unit left, Unit right) { - super(UnitSymbol.GTE, left, right); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left.get(variables) >= right.get(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/cond/LtOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/cond/LtOpUnit.java deleted file mode 100644 index 89ab26d4..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/cond/LtOpUnit.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit.operator.cond; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class LtOpUnit extends CondOpUnit { - public LtOpUnit(Unit left, Unit right) { - super(UnitSymbol.LT, left, right); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left.get(variables) < right.get(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/cond/LteOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/cond/LteOpUnit.java deleted file mode 100644 index 256e54cd..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/cond/LteOpUnit.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit.operator.cond; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class LteOpUnit extends CondOpUnit { - public LteOpUnit(Unit left, Unit right) { - super(UnitSymbol.LTE, left, right); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left.get(variables) <= right.get(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/cond/NeqOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/cond/NeqOpUnit.java deleted file mode 100644 index eef8038c..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/cond/NeqOpUnit.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit.operator.cond; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class NeqOpUnit extends CondOpUnit { - public NeqOpUnit(Unit left, Unit right) { - super(UnitSymbol.NEQ, left, right); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left != right && Math.abs(left.get(variables) - right.get(variables)) >= 0.00001D; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/cond/OrOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/cond/OrOpUnit.java deleted file mode 100644 index e4ea4090..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/cond/OrOpUnit.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit.operator.cond; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class OrOpUnit extends CondOpUnit { - public OrOpUnit(Unit left, Unit right) { - super(UnitSymbol.OR, left, right); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left.getBoolean(variables) || right.getBoolean(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/AddOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/AddOpUnit.java deleted file mode 100644 index 95b70590..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/AddOpUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class AddOpUnit extends OpUnit { - public AddOpUnit(Unit left, Unit right) { - super(UnitSymbol.ADD, left, right); - } - - @Override - public double get(UnitVariables variables) { - return left.get(variables) + right.get(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/BitAndOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/BitAndOpUnit.java deleted file mode 100644 index 4f5532f0..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/BitAndOpUnit.java +++ /dev/null @@ -1,27 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class BitAndOpUnit extends OpUnit { - public BitAndOpUnit(Unit left, Unit right) { - super(UnitSymbol.BIT_AND, left, right); - } - - @Override - public double get(UnitVariables variables) { - return getInt(variables); - } - - @Override - public int getInt(UnitVariables variables) { - return left.getInt(variables) & right.getInt(variables); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left.getBoolean(variables) && right.getBoolean(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/BitOrOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/BitOrOpUnit.java deleted file mode 100644 index 6961a10d..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/BitOrOpUnit.java +++ /dev/null @@ -1,27 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class BitOrOpUnit extends OpUnit { - public BitOrOpUnit(Unit left, Unit right) { - super(UnitSymbol.BIT_OR, left, right); - } - - @Override - public double get(UnitVariables variables) { - return getInt(variables); - } - - @Override - public int getInt(UnitVariables variables) { - return left.getInt(variables) | right.getInt(variables); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left.getBoolean(variables) || right.getBoolean(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/DivOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/DivOpUnit.java deleted file mode 100644 index a8d35e6f..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/DivOpUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class DivOpUnit extends OpUnit { - public DivOpUnit(Unit left, Unit right) { - super(UnitSymbol.DIV, left, right); - } - - @Override - public double get(UnitVariables variables) { - return left.get(variables) / right.get(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/LshOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/LshOpUnit.java deleted file mode 100644 index d16234bb..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/LshOpUnit.java +++ /dev/null @@ -1,22 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class LshOpUnit extends OpUnit { - public LshOpUnit(Unit left, Unit right) { - super(UnitSymbol.LSH, left, right); - } - - @Override - public double get(UnitVariables variables) { - return getInt(variables); - } - - @Override - public int getInt(UnitVariables variables) { - return left.getInt(variables) << right.getInt(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/ModOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/ModOpUnit.java deleted file mode 100644 index 502de546..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/ModOpUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class ModOpUnit extends OpUnit { - public ModOpUnit(Unit left, Unit right) { - super(UnitSymbol.MOD, left, right); - } - - @Override - public double get(UnitVariables variables) { - return left.get(variables) % right.get(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/MulOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/MulOpUnit.java deleted file mode 100644 index 52bf1994..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/MulOpUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class MulOpUnit extends OpUnit { - public MulOpUnit(Unit left, Unit right) { - super(UnitSymbol.MUL, left, right); - } - - @Override - public double get(UnitVariables variables) { - return left.get(variables) * right.get(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/PowOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/PowOpUnit.java deleted file mode 100644 index dfc8da4a..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/PowOpUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class PowOpUnit extends OpUnit { - public PowOpUnit(Unit left, Unit right) { - super(UnitSymbol.POW, left, right); - } - - @Override - public double get(UnitVariables variables) { - return Math.pow(left.get(variables), right.get(variables)); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/RshOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/RshOpUnit.java deleted file mode 100644 index 9b468d5d..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/RshOpUnit.java +++ /dev/null @@ -1,22 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class RshOpUnit extends OpUnit { - public RshOpUnit(Unit left, Unit right) { - super(UnitSymbol.RSH, left, right); - } - - @Override - public double get(UnitVariables variables) { - return getInt(variables); - } - - @Override - public int getInt(UnitVariables variables) { - return left.getInt(variables) >> right.getInt(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/SubOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/SubOpUnit.java deleted file mode 100644 index 183abaee..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/SubOpUnit.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class SubOpUnit extends OpUnit { - public SubOpUnit(Unit left, Unit right) { - super(UnitSymbol.SUB, left, right); - } - - @Override - public double get(UnitVariables variables) { - return left.get(variables) - right.get(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/operator/op/XorOpUnit.java b/src/main/java/dev/latvian/mods/unit/operator/op/XorOpUnit.java deleted file mode 100644 index f12bce98..00000000 --- a/src/main/java/dev/latvian/mods/unit/operator/op/XorOpUnit.java +++ /dev/null @@ -1,27 +0,0 @@ -package dev.latvian.mods.unit.operator.op; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitVariables; -import dev.latvian.mods.unit.operator.OpUnit; -import dev.latvian.mods.unit.token.UnitSymbol; - -public class XorOpUnit extends OpUnit { - public XorOpUnit(Unit left, Unit right) { - super(UnitSymbol.XOR, left, right); - } - - @Override - public double get(UnitVariables variables) { - return getInt(variables); - } - - @Override - public int getInt(UnitVariables variables) { - return left.getInt(variables) ^ right.getInt(variables); - } - - @Override - public boolean getBoolean(UnitVariables variables) { - return left.getBoolean(variables) ^ right.getBoolean(variables); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/CharStream.java b/src/main/java/dev/latvian/mods/unit/token/CharStream.java deleted file mode 100644 index b1cc04fe..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/CharStream.java +++ /dev/null @@ -1,58 +0,0 @@ -package dev.latvian.mods.unit.token; - -public class CharStream { - public final char[] chars; - public int position; - public boolean skipWhitespace; - - public CharStream(char[] c) { - chars = c; - position = -1; - skipWhitespace = true; - } - - public char next() { - if (++position >= chars.length) { - return 0; - } - - while (skipWhitespace && chars[position] <= ' ') { - position++; - - if (position >= chars.length) { - return 0; - } - } - - return chars[position]; - } - - public boolean nextIf(char match) { - if (peek() == match) { - next(); - return true; - } - - return false; - } - - public char peek(int ahead) { - if (position + ahead >= chars.length) { - return 0; - } - - while (skipWhitespace && chars[position + ahead] <= ' ') { - ahead++; - - if (position + ahead >= chars.length) { - return 0; - } - } - - return chars[position + ahead]; - } - - public char peek() { - return peek(1); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/FunctionUnitToken.java b/src/main/java/dev/latvian/mods/unit/token/FunctionUnitToken.java deleted file mode 100644 index 12a6a6f3..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/FunctionUnitToken.java +++ /dev/null @@ -1,27 +0,0 @@ -package dev.latvian.mods.unit.token; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.function.FunctionFactory; - -import java.util.List; - -public record FunctionUnitToken(String name, List args) implements UnitToken { - @Override - public Unit interpret(UnitTokenStream stream) { - FunctionFactory factory = stream.context.getFunctionFactory(name); - - if (factory == null) { - throw new IllegalStateException("Unknown function '" + name + "'!"); - } else if (args.isEmpty()) { - return factory.create(Unit.EMPTY_ARRAY); - } - - Unit[] newArgs = new Unit[args.size()]; - - for (int i = 0; i < args.size(); i++) { - newArgs[i] = args.get(i).interpret(stream); - } - - return factory.create(newArgs); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/OpResultUnitToken.java b/src/main/java/dev/latvian/mods/unit/token/OpResultUnitToken.java deleted file mode 100644 index 7c3ed47e..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/OpResultUnitToken.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.mods.unit.token; - -import dev.latvian.mods.unit.Unit; - -public record OpResultUnitToken(UnitSymbol operator, UnitToken left, UnitToken right) implements UnitToken { - @Override - public Unit interpret(UnitTokenStream stream) { - var uleft = left.interpret(stream); - var uright = right.interpret(stream); - return operator.op.create(uleft, uright); - } - - @Override - public String toString() { - return "(" + left + " " + operator + " " + right + ")"; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/PostfixUnitToken.java b/src/main/java/dev/latvian/mods/unit/token/PostfixUnitToken.java deleted file mode 100644 index 375cadb9..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/PostfixUnitToken.java +++ /dev/null @@ -1,117 +0,0 @@ -package dev.latvian.mods.unit.token; - -import dev.latvian.mods.unit.Unit; - -import java.util.LinkedList; -import java.util.List; -import java.util.Stack; - -public record PostfixUnitToken(List infix) implements UnitToken { - @Override - public Unit interpret(UnitTokenStream stream) { - Stack operatorsStack = new Stack<>(); - LinkedList postfix = new LinkedList<>(); - - for (UnitToken next : infix) { - if (stream.context.isDebug()) { - stream.context.debugInfo("> " + next); - } - - if (next instanceof UnitSymbol nextOperator) { - boolean pushedCurrent = false; - - while (!operatorsStack.isEmpty()) { - var o = operatorsStack.peek(); - - if (o != null) { - if (o.hasHigherPrecedenceThan(nextOperator)) { - postfix.add(operatorsStack.pop()); - - if (stream.context.isDebug()) { - stream.context.debugInfo("Operator Stack", operatorsStack); - stream.context.debugInfo("Operand Stack", postfix); - } - } else { - pushedCurrent = true; - operatorsStack.push(nextOperator); - - if (stream.context.isDebug()) { - stream.context.debugInfo("Operator Stack", operatorsStack); - stream.context.debugInfo("Operand Stack", postfix); - } - - break; - } - } else { - break; - } - } - - if (!pushedCurrent) { - operatorsStack.push(nextOperator); - - if (stream.context.isDebug()) { - stream.context.debugInfo("Operator Stack", operatorsStack); - stream.context.debugInfo("Operand Stack", postfix); - } - } - } else { - postfix.add(next); - - if (stream.context.isDebug()) { - stream.context.debugInfo("Operator Stack", operatorsStack); - stream.context.debugInfo("Operand Stack", postfix); - } - } - } - - while (!operatorsStack.isEmpty()) { - var last = operatorsStack.pop(); - postfix.add(last); - } - - if (stream.context.isDebug()) { - stream.context.debugInfo("Postfix", postfix); - } - - var resultStack = new Stack(); - - for (var token : postfix) { - token.unstack(resultStack); - - if (stream.context.isDebug()) { - stream.context.debugInfo("Result Stack", resultStack); - } - } - - var lastUnit = resultStack.pop(); - return lastUnit.interpret(stream); - } - - public UnitToken normalize() { - if (infix.size() == 1) { - return infix.get(0); - } else if (infix.size() == 3 && infix.get(1) instanceof UnitSymbol symbol && symbol.op != null) { - return new OpResultUnitToken(symbol, infix.get(0), infix.get(2)); - } - - return this; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append('('); - - for (int i = 0; i < infix.size(); i++) { - if (i > 0) { - sb.append(' '); - } - - sb.append(infix.get(i)); - } - - sb.append(')'); - return sb.toString(); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/StringUnitToken.java b/src/main/java/dev/latvian/mods/unit/token/StringUnitToken.java deleted file mode 100644 index 08577b82..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/StringUnitToken.java +++ /dev/null @@ -1,27 +0,0 @@ -package dev.latvian.mods.unit.token; - -import dev.latvian.mods.unit.FixedNumberUnit; -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.VariableUnit; - -public record StringUnitToken(String name) implements UnitToken { - @Override - public Unit interpret(UnitTokenStream stream) { - var constant = stream.context.constants.get(name); - - if (constant != null) { - return constant; - } - - try { - return FixedNumberUnit.of(Double.parseDouble(name)); - } catch (Exception ex) { - return VariableUnit.of(name); - } - } - - @Override - public String toString() { - return name; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/TernaryUnitToken.java b/src/main/java/dev/latvian/mods/unit/token/TernaryUnitToken.java deleted file mode 100644 index 54335c79..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/TernaryUnitToken.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit.token; - -import dev.latvian.mods.unit.TernaryUnit; -import dev.latvian.mods.unit.Unit; - -public record TernaryUnitToken(UnitToken cond, UnitToken ifTrue, UnitToken ifFalse) implements UnitToken { - @Override - public Unit interpret(UnitTokenStream stream) { - return new TernaryUnit(cond.interpret(stream), ifTrue.interpret(stream), ifFalse.interpret(stream)); - } - - @Override - public String toString() { - return "(" + cond + " ? " + ifTrue + " : " + ifFalse + ")"; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/UnaryOpUnitToken.java b/src/main/java/dev/latvian/mods/unit/token/UnaryOpUnitToken.java deleted file mode 100644 index e42a4251..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/UnaryOpUnitToken.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.mods.unit.token; - -import dev.latvian.mods.unit.Unit; - -public record UnaryOpUnitToken(UnitSymbol operator, UnitToken token) implements UnitToken { - @Override - public Unit interpret(UnitTokenStream stream) { - var unit = token.interpret(stream); - return operator.unaryOp.create(unit); - } - - @Override - public String toString() { - return "(" + operator + token + ")"; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/UnitInterpretException.java b/src/main/java/dev/latvian/mods/unit/token/UnitInterpretException.java deleted file mode 100644 index 2e0c040d..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/UnitInterpretException.java +++ /dev/null @@ -1,7 +0,0 @@ -package dev.latvian.mods.unit.token; - -public class UnitInterpretException extends IllegalStateException { - public UnitInterpretException(String s) { - super(s); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/UnitParseException.java b/src/main/java/dev/latvian/mods/unit/token/UnitParseException.java deleted file mode 100644 index 2271ef11..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/UnitParseException.java +++ /dev/null @@ -1,7 +0,0 @@ -package dev.latvian.mods.unit.token; - -public class UnitParseException extends IllegalStateException { - public UnitParseException(String s) { - super(s); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/UnitSymbol.java b/src/main/java/dev/latvian/mods/unit/token/UnitSymbol.java deleted file mode 100644 index efca65c4..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/UnitSymbol.java +++ /dev/null @@ -1,149 +0,0 @@ -package dev.latvian.mods.unit.token; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.operator.OperatorFactory; -import dev.latvian.mods.unit.operator.UnaryOperatorFactory; -import org.jetbrains.annotations.Nullable; - -import java.util.Stack; - -public enum UnitSymbol implements UnitToken { - // Misc - COMMA(","), - LP("("), - RP(")"), - HASH("#"), - HOOK("?"), - COLON(":"), - SEMICOLON(";"), - POSITIVE("+", Unit::positive), - NEGATE("-", Unit::negate), - // Operators - ADD("+", 2, Unit::add), - SUB("-", 2, Unit::sub), - MUL("*", 3, Unit::mul), - DIV("/", 3, Unit::div), - MOD("%", 3, Unit::mod), - POW("**", 4, Unit::pow), - // Int Operators - LSH("<<", 2, Unit::lsh), - RSH(">>", 2, Unit::rsh), - BIT_AND("&", 2, Unit::bitAnd), - BIT_OR("|", 2, Unit::bitOr), - XOR("^", 2, Unit::xor), - BIT_NOT("~", Unit::bitNot), - // Conditions - EQ("==", 1, Unit::eq), - NEQ("!=", 1, Unit::neq), - LT("<", 1, Unit::lt), - GT(">", 1, Unit::gt), - LTE("<=", 1, Unit::lte), - GTE(">=", 1, Unit::gte), - AND("&&", 1, Unit::and), - OR("||", 1, Unit::or), - BOOL_NOT("!", Unit::boolNot), - // Mutators - SET("=", 0, Unit::set), - ADD_SET("+=", 0, Unit::addSet), - SUB_SET("-=", 0, Unit::subSet), - MUL_SET("*=", 0, Unit::mulSet), - DIV_SET("/=", 0, Unit::divSet), - MOD_SET("%=", 0, Unit::modSet), - - ; - - @Nullable - public static UnitSymbol read(char first, CharStream stream) { - return switch (first) { - case ',' -> COMMA; - case '(' -> LP; - case ')' -> RP; - case '#' -> HASH; - case '?' -> HOOK; - case ':' -> COLON; - case ';' -> SEMICOLON; - case '+' -> stream.nextIf('=') ? ADD_SET : ADD; - case '-' -> stream.nextIf('=') ? SUB_SET : SUB; - case '*' -> stream.nextIf('*') ? POW : stream.nextIf('=') ? MUL_SET : MUL; - case '/' -> stream.nextIf('=') ? DIV_SET : DIV; - case '%' -> stream.nextIf('=') ? MOD_SET : MOD; - case '^' -> XOR; - case '~' -> BIT_NOT; - case '&' -> stream.nextIf('&') ? AND : BIT_AND; - case '|' -> stream.nextIf('|') ? OR : BIT_OR; - case '!' -> stream.nextIf('=') ? NEQ : BOOL_NOT; - case '<' -> stream.nextIf('=') ? LTE : stream.nextIf('<') ? LSH : LT; - case '>' -> stream.nextIf('=') ? GTE : stream.nextIf('>') ? RSH : GT; - case '=' -> stream.nextIf('=') ? EQ : SET; - default -> null; - }; - } - - public final String symbol; - public final int precedence; - public final OperatorFactory op; - public final UnaryOperatorFactory unaryOp; - - UnitSymbol(String s) { - symbol = s; - precedence = 0; - op = null; - unaryOp = null; - } - - UnitSymbol(String s, int p, OperatorFactory opUnit) { - symbol = s; - precedence = p; - op = opUnit; - unaryOp = null; - } - - UnitSymbol(String s, UnaryOperatorFactory unaryOpUnit) { - symbol = s; - precedence = 0; - op = null; - unaryOp = unaryOpUnit; - } - - @Override - public String toString() { - return symbol; - } - - @Override - public boolean nextUnaryOperator() { - return this != RP; - } - - @Nullable - public UnitSymbol getUnarySymbol() { - return switch (this) { - case ADD -> POSITIVE; - case SUB -> NEGATE; - default -> null; - }; - } - - public boolean is(UnitToken next) { - return next == this; - } - - @Override - public void unstack(Stack stack) { - if (op != null) { - if (stack.size() < 2) { - throw new UnitInterpretException("Not enough elements in stack!"); - } - - var right = stack.pop(); - var left = stack.pop(); - stack.push(new OpResultUnitToken(this, left, right)); - } else { - throw new UnitInterpretException("Unexpected symbol '" + this + "'!"); - } - } - - public final boolean hasHigherPrecedenceThan(UnitSymbol operator) { - return operator.precedence <= precedence; - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/UnitToken.java b/src/main/java/dev/latvian/mods/unit/token/UnitToken.java deleted file mode 100644 index a841caf8..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/UnitToken.java +++ /dev/null @@ -1,19 +0,0 @@ -package dev.latvian.mods.unit.token; - -import dev.latvian.mods.unit.Unit; - -import java.util.Stack; - -public interface UnitToken { - default Unit interpret(UnitTokenStream stream) { - return (Unit) this; - } - - default boolean nextUnaryOperator() { - return false; - } - - default void unstack(Stack resultStack) { - resultStack.push(this); - } -} diff --git a/src/main/java/dev/latvian/mods/unit/token/UnitTokenStream.java b/src/main/java/dev/latvian/mods/unit/token/UnitTokenStream.java deleted file mode 100644 index c1266a41..00000000 --- a/src/main/java/dev/latvian/mods/unit/token/UnitTokenStream.java +++ /dev/null @@ -1,212 +0,0 @@ -package dev.latvian.mods.unit.token; - -import dev.latvian.mods.unit.FixedColorUnit; -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitContext; -import dev.latvian.mods.unit.operator.GroupUnit; - -import java.util.ArrayList; - -public final class UnitTokenStream { - private static boolean isHex(char c) { - return c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'; - } - - public final UnitContext context; - - public final String input; - public final CharStream charStream; - public final ArrayList infix; - public final ArrayList inputStringPos; - public final Unit unit; - private int infixPos; - - public UnitTokenStream(UnitContext context, String input) { - this.context = context; - this.input = input; - this.charStream = new CharStream(input.toCharArray()); - this.infix = new ArrayList<>(); - this.inputStringPos = new ArrayList<>(); - this.infixPos = -1; - - var current = new StringBuilder(); - - while (true) { - var c = charStream.next(); - - if (c == 0) { - break; - } - - int cpos = charStream.position; - var symbol = UnitSymbol.read(c, charStream); - - if (symbol == UnitSymbol.HASH) { - if (isHex(charStream.peek(1)) && isHex(charStream.peek(2)) && isHex(charStream.peek(3)) && isHex(charStream.peek(4)) && isHex(charStream.peek(5)) && isHex(charStream.peek(6))) { - var alpha = isHex(charStream.peek(7)) && isHex(charStream.peek(8)); - - current.append('#'); - - for (var i = 0; i < (alpha ? 8 : 6); i++) { - current.append(charStream.next()); - } - - var color = Long.decode(current.toString()).intValue(); - current.setLength(0); - - inputStringPos.add(cpos); - infix.add(FixedColorUnit.of(color, alpha)); - } else { - throw new UnitParseException("Invalid color code @ " + charStream.position); - } - } else { - if (symbol != null && current.length() > 0) { - inputStringPos.add(cpos); - infix.add(new StringUnitToken(current.toString())); - current.setLength(0); - } - - UnitSymbol unary = symbol == null ? null : symbol.getUnarySymbol(); - - if (unary != null && (infix.isEmpty() || infix.get(infix.size() - 1).nextUnaryOperator())) { - inputStringPos.add(cpos); - infix.add(unary); - } else if (symbol != null) { - inputStringPos.add(cpos); - infix.add(symbol); - } else { - current.append(c); - } - } - } - - if (current.length() > 0) { - inputStringPos.add(charStream.position - current.length()); - infix.add(new StringUnitToken(current.toString())); - current.setLength(0); - } - - var interpretedUnits = new ArrayList(); - - if (context.isDebug()) { - context.debugInfo("Infix", infix); - } - - try { - do { - var unitToken = readFully(); - interpretedUnits.add(unitToken.interpret(this)); - } - while (ifNextToken(UnitSymbol.SEMICOLON)); - } catch (UnitInterpretException ex) { - throw new RuntimeException("Error parsing '" + input + "' @ " + (infixPos < 0 || infixPos >= inputStringPos.size() ? -1 : inputStringPos.get(infixPos)), ex); - } - - this.unit = interpretedUnits.size() == 1 ? interpretedUnits.get(0) : new GroupUnit(interpretedUnits.toArray(Unit.EMPTY_ARRAY)); - } - - public Unit getUnit() { - return unit; - } - - @Override - public String toString() { - return infix.toString(); - } - - public UnitToken nextToken() { - if (++infixPos >= infix.size()) { - throw new UnitInterpretException("EOL!"); - } - - return infix.get(infixPos); - } - - public UnitToken peekToken() { - if (infixPos + 1 >= infix.size()) { - return null; - } - - return infix.get(infixPos + 1); - } - - public boolean ifNextToken(UnitToken token) { - if (token.equals(peekToken())) { - nextToken(); - return true; - } - - return false; - } - - public UnitToken readFully() { - PostfixUnitToken postfix = new PostfixUnitToken(new ArrayList<>()); - - if (ifNextToken(UnitSymbol.LP)) { - postfix.infix().add(readFully()); - - if (!ifNextToken(UnitSymbol.RP)) { - throw new UnitInterpretException("Expected ')', got '" + peekToken() + "'!"); - } - } else { - postfix.infix().add(readSingleToken()); - } - - while (peekToken() instanceof UnitSymbol symbol && symbol.op != null) { - postfix.infix().add(nextToken()); - - if (peekToken() == UnitSymbol.LP) { - postfix.infix().add(readFully()); - } else { - postfix.infix().add(readSingleToken()); - } - } - - if (ifNextToken(UnitSymbol.HOOK)) { - var left = readFully(); - - if (!ifNextToken(UnitSymbol.COLON)) { - throw new UnitInterpretException("Expected ':', got '" + peekToken() + "'!"); - } - - var right = readFully(); - return new TernaryUnitToken(postfix.normalize(), left, right); - } - - return postfix.normalize(); - } - - public UnitToken readSingleToken() { - UnitToken token = nextToken(); - - if (token instanceof UnitSymbol symbol && symbol.unaryOp != null) { - if (peekToken() == UnitSymbol.LP) { - return new UnaryOpUnitToken(symbol, readFully()); - } else { - return new UnaryOpUnitToken(symbol, readSingleToken()); - } - } - - if (token instanceof StringUnitToken str) { - if (ifNextToken(UnitSymbol.LP)) { - FunctionUnitToken func = new FunctionUnitToken(str.name(), new ArrayList<>()); - - while (true) { - if (ifNextToken(UnitSymbol.RP)) { - break; - } else if (!ifNextToken(UnitSymbol.COMMA)) { - func.args().add(readFully()); - } - } - - return func; - } - - return str; - } else if (token instanceof FixedColorUnit) { - return token; - } - - throw new UnitInterpretException("Unexpected token: " + token); - } -} diff --git a/src/test/java/dev/latvian/mods/rhino/test/TestConsole.java b/src/test/java/dev/latvian/mods/rhino/test/TestConsole.java index 672091cd..9537f530 100644 --- a/src/test/java/dev/latvian/mods/rhino/test/TestConsole.java +++ b/src/test/java/dev/latvian/mods/rhino/test/TestConsole.java @@ -4,7 +4,6 @@ import dev.latvian.mods.rhino.ContextFactory; import dev.latvian.mods.rhino.ScriptRuntime; import dev.latvian.mods.rhino.util.RemapPrefixForJS; -import dev.latvian.mods.unit.UnitContext; import java.util.ArrayList; import java.util.Arrays; @@ -83,10 +82,6 @@ public Map getTestMap() { return theme; } - public void printUnit(String input) { - info(input + " -> " + UnitContext.DEFAULT.parse(input)); - } - public void printMaterial(TestMaterial material) { info("%s#%08x".formatted(material.name(), material.hashCode())); } diff --git a/src/test/java/dev/latvian/mods/rhino/test/UnitTests.java b/src/test/java/dev/latvian/mods/rhino/test/UnitTests.java deleted file mode 100644 index 6dc23394..00000000 --- a/src/test/java/dev/latvian/mods/rhino/test/UnitTests.java +++ /dev/null @@ -1,187 +0,0 @@ -package dev.latvian.mods.rhino.test; - -import dev.latvian.mods.unit.Unit; -import dev.latvian.mods.unit.UnitContext; -import dev.latvian.mods.unit.VariableSet; -import dev.latvian.mods.unit.function.RoundedTimeUnit; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -@SuppressWarnings("unused") -public class UnitTests { - public static UnitContext CONTEXT = UnitContext.DEFAULT.sub(); - public static VariableSet VARIABLE_SET = new VariableSet(); - public static final double $test = 3.5D * Math.random(); - public static final double $test2 = 2D * Math.random(); - public static final double $test3 = 100D * Math.random(); - - static { - CONTEXT.pushDebug(); - VARIABLE_SET.set("$test", $test); - VARIABLE_SET.set("$test2", $test2); - VARIABLE_SET.set("$test3", $test3); - } - - public static double eval(String input) { - System.out.println("Input: " + input); - Unit unit = CONTEXT.parse(input); - System.out.println("Result: " + unit); - double eval = unit.get(VARIABLE_SET); - System.out.println("Eval: " + eval); - return eval; - } - - public static void assertEval(String input, double expected) { - Assertions.assertEquals(expected, eval(input), 0.00001D); - } - - @Test - public void number() { - assertEval("2.8", 2.8); - } - - @Test - public void negativeNumber() { - assertEval("-2", -2.0); - } - - @Test - public void variable() { - assertEval("$test", $test); - } - - @Test - public void negativeVariable() { - assertEval("-$test", -$test); - } - - @Test - public void subtraction() { - assertEval("4 -2.5", 1.5); - } - - @Test - public void negSubtraction() { - assertEval("-2.3 -3", -5.3); - } - - @Test - public void doubleNegSubtraction() { - assertEval("-7 - --2", -9.0); - } - - @Test - public void integerEqFraction() { - assertEval("5==5.0", 1.0); - } - - @Test - public void integerEqFractionSpaces() { - assertEval("5 == 5.0", 1.0); - } - - @Test - public void colorRGB() { - assertEval("#FF0044", 0xFFFF0044); - } - - @Test - public void colorARGB() { - assertEval("#FFFF0044", 0xFFFF0044); - } - - @Test - public void colorFunc3() { - assertEval("rgb(1, 0, 0.5)", 0xFFFF007F); - } - - @Test - public void colorFunc4() { - assertEval("rgb(1, 0, 0.5, 0.5)", 0x7FFF007F); - } - - @Test - public void colorFunc2() { - assertEval("rgb(#99FF007F, 0.5)", 0x7FFF007F); - } - - @Test - public void simpleOrderOfOperations1() { - assertEval("2 + 3 * 4", 14); - } - - @Test - public void simpleOrderOfOperations2() { - assertEval("2 + -(3 * 4)", -10); - } - - @Test - public void simpleOrderOfOperations3() { - assertEval("(2 + 3) * 4", 20); - } - - @Test - public void orderOfOperations() { - assertEval("4 * -(2 + 8 * 2 - 1) / 5", -13.6); - } - - @Test - public void ternary1() { - assertEval("3 >= 2 ? 6 : 4", 6); - } - - @Test - public void ternary2() { - assertEval("2.4 + 3.5 * 4 == 16.4 ? 4.0 : 2.0", 4.0); - } - - @Test - public void ternary3() { - assertEval("$test < 0.5 ? -30 : 40", $test < 0.5 ? -30 : 40); - } - - @Test - public void ternary4() { - assertEval("$test < 0.5 ? $test2 < 1.5 ? 1.5 : -4 : $test3 < 50 * -3 ? 1.5 : -4", $test < 0.5 ? $test2 < 1.5 ? 1.5 : -4 : $test3 < 50 * -3 ? 1.5 : -4); - } - - @Test - public void functions1() { - assertEval("roundedTime()", RoundedTimeUnit.time()); - } - - @Test - public void functions2() { - assertEval("sin(3.0)", Math.sin(3D)); - } - - @Test - public void functions3() { - assertEval("-abs(-4.0)", -4.0); - } - - @Test - public void functions4() { - assertEval("sin($test*10)*5", Math.sin($test * 10D) * 5D); - } - - @Test - public void functions5() { - assertEval("sin(roundedTime() * 1.1) * (($test - 32) / 2)", Math.sin(RoundedTimeUnit.time() * 1.1D) * (($test - 32D) / 2D)); - } - - @Test - public void semicolon1() { - assertEval("5; 6", 6); - } - - @Test - public void semicolon2() { - assertEval("$setTest = 4 * 5; $setTest * 2", 40); - } - - @Test - public void semicolon3() { - assertEval("$setTest = 8; $setTest *= 0.5; $setTest * 3", 12); - } -}