-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename binary operator to post fix operator, add some non-binary oper…
…ators Just standard stuff like duplicate, swap, negate, abs, and subtract/divide/power with reversed operands
- Loading branch information
1 parent
d25fce0
commit d17923f
Showing
6 changed files
with
168 additions
and
115 deletions.
There are no files selected for viewing
102 changes: 0 additions & 102 deletions
102
src/main/java/slimeknights/tconstruct/library/json/math/BinaryOperator.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/slimeknights/tconstruct/library/json/math/PostFixOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package slimeknights.tconstruct.library.json.math; | ||
|
||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSyntaxException; | ||
import it.unimi.dsi.fastutil.floats.FloatStack; | ||
import lombok.RequiredArgsConstructor; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
|
||
/** Represents 2 argument stack operations */ | ||
@RequiredArgsConstructor | ||
public enum PostFixOperator implements StackOperation { | ||
ADD("+", Float::sum), | ||
SUBTRACT("-", (left, right) -> left - right), | ||
SUBTRACT_FLIPPED("--", (left, right) -> right - left), | ||
MULTIPLY("*", (left, right) -> left * right), | ||
NEGATE("negate") { | ||
@Override | ||
public void perform(FloatStack stack, float[] variables) { | ||
stack.push(-stack.popFloat()); | ||
} | ||
}, | ||
DIVIDE("/", (left, right) -> { | ||
if (left == 0) { | ||
return 0; | ||
} | ||
return right / left; | ||
}), | ||
DIVIDE_FLIPPED("//", (left, right) -> { | ||
if (right == 0) { | ||
return 0; | ||
} | ||
return left / right; | ||
}), | ||
POWER("^", (left, right) -> (float)Math.pow(left, right)), | ||
POWER_FLIPPED("^^", (left, right) -> (float)Math.pow(right, left)), | ||
MIN("min", Math::min), | ||
MAX("max", Math::max), | ||
ABS("abs") { | ||
@Override | ||
public void perform(FloatStack stack, float[] variables) { | ||
stack.push(Math.abs(stack.popFloat())); | ||
} | ||
}, | ||
/** Swaps the top two elements */ | ||
SWAP("swap") { | ||
@Override | ||
public void perform(FloatStack stack, float[] variables) { | ||
float first = stack.popFloat(); | ||
float second = stack.popFloat(); | ||
stack.push(first); | ||
stack.push(second); | ||
} | ||
}, | ||
/** Copies the top element on the stack */ | ||
DUPLICATE("duplicate") { | ||
@Override | ||
public void perform(FloatStack stack, float[] variables) { | ||
float value = stack.popFloat(); | ||
stack.push(value); | ||
stack.push(value); | ||
} | ||
}; | ||
|
||
/** Index used for serializing value to the network */ | ||
public static final int VALUE_INDEX = values().length; | ||
/** Index used for serializing a variable to the network */ | ||
public static final int VARIABLE_INDEX = VALUE_INDEX + 1; | ||
|
||
/** Name of this operator when serialized into JSON */ | ||
private final String serialized; | ||
/** Binary function to run, used for most operators */ | ||
private final BinaryOperator binary; | ||
|
||
PostFixOperator(String serialized) { | ||
this(serialized, BinaryOperator.ZERO); | ||
} | ||
|
||
@Override | ||
public void perform(FloatStack stack, float[] variables) { | ||
// this may throw, but that is okay as we will run this formula during parsing to make sure its valid | ||
// the way formulas are setup, if it does not throw during parsing, it cannot throw ever | ||
float right = stack.popFloat(); | ||
float left = stack.popFloat(); | ||
stack.push(binary.apply(left, right)); | ||
} | ||
|
||
|
||
/* JSON and network */ | ||
|
||
/** Deserializes the operator from a character */ | ||
public static PostFixOperator deserialize(String name) { | ||
for (PostFixOperator operator : PostFixOperator.values()) { | ||
if (operator.serialized.equals(name)) { | ||
return operator; | ||
} | ||
} | ||
throw new JsonSyntaxException("Unknown post fix operator '" + name + "'"); | ||
} | ||
|
||
@Override | ||
public JsonPrimitive serialize(String[] variableNames) { | ||
return new JsonPrimitive(serialized); | ||
} | ||
|
||
@Override | ||
public void toNetwork(FriendlyByteBuf buffer) { | ||
// comment on buffer internals: the indices of this enum and StackNetworkType match up until divide, | ||
// so writing our ordinal allows us to read an ordinal for the other enum | ||
buffer.writeEnum(this); | ||
} | ||
|
||
/** Interface for common operators */ | ||
private interface BinaryOperator { | ||
BinaryOperator ZERO = (left, right) -> 0; | ||
|
||
/** Applies this operator to the given values */ | ||
float apply(float left, float right); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters