Skip to content

Commit

Permalink
feat: make compiler support more operations
Browse files Browse the repository at this point in the history
  • Loading branch information
yusshu committed Dec 22, 2023
1 parent 9e00d88 commit 90ae41b
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 99 deletions.
219 changes: 194 additions & 25 deletions src/main/java/team/unnamed/mocha/runtime/binding/MathBinding.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
/*
* This file is part of mocha, licensed under the MIT license
*
* Copyright (c) 2021-2023 Unnamed Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.mocha.runtime.binding;
/*
* This file is part of mocha, licensed under the MIT license
*
* Copyright (c) 2021-2023 Unnamed Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.mocha.runtime.binding;

import org.jetbrains.annotations.NotNull;
import team.unnamed.mocha.runtime.Function;
import team.unnamed.mocha.runtime.jvm.MolangNative;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -152,6 +153,175 @@ private static double toRadians(Object object) {
}
}

@MolangNative("math.abs")
public static double abs(final double value) {
return Math.abs(value);
}

@MolangNative("math.acos")
public static double acos(final double value) {
return preferZero(Math.acos(value) / RADIAN);
}

@MolangNative("math.asin")
public static double asin(final double value) {
return preferZero(Math.asin(value) / RADIAN);
}

@MolangNative("math.atan")
public static double atan(final double value) {
return Math.atan(value) / RADIAN;
}

@MolangNative("math.atan2")
public static double atan2(final double y, final double x) {
return Math.atan2(y, x) / RADIAN;
}

@MolangNative("math.ceil")
public static double ceil(final double value) {
return Math.ceil(value);
}

@MolangNative("math.clamp")
public static double clamp(final double value, final double min, final double max) {
return Math.max(Math.min(value, max), min);
}

@MolangNative("math.cos")
public static double cos(final double value) {
return Math.cos(value * RADIAN);
}

@MolangNative("math.die_roll")
public static double dieRoll(final double amount, final double low, final double high) {
double result = 0;
for (int i = 0; i < amount; i++) {
result += RANDOM.nextInt((int) high) + low;
}
return result / DECIMAL_PART;
}

@MolangNative("math.die_roll_integer")
public static int dieRollInteger(final double amount, final double low, final double high) {
int result = 0;
for (int i = 0; i < amount; i++) {
result += RANDOM.nextInt((int) low, (int) high);
}
return result;
}

@MolangNative("math.exp")
public static double exp(final double value) {
return Math.exp(value);
}

@MolangNative("math.floor")
public static double floor(final double value) {
return Math.floor(value);
}

@MolangNative("math.hermite_blend")
public static double hermiteBlend(final double t) {
final var t2 = t * t;
final var t3 = t2 * t;
return 3 * t2 - 2 * t3;
}

@MolangNative("math.lerp")
public static double lerp(final double start, final double end, final double lerp) {
return start + lerp * (end - start);
}

@MolangNative("math.lerprotate")
public static double lerpRotate(double start, double end, double lerp) {
start = radify(start);
end = radify(end);

if (start > end) {
// swap
double tmp = start;
start = end;
end = tmp;
}

double diff = end - start;
if (diff > 180F) {
return radify(end + lerp * (360F - diff));
} else {
return start + lerp * diff;
}
}

@MolangNative("math.ln")
public static double ln(final double value) {
return Math.log(value);
}

@MolangNative("math.max")
public static double max(final double a, final double b) {
return Math.max(a, b);
}

@MolangNative("math.min")
public static double min(final double a, final double b) {
return Math.min(a, b);
}

@MolangNative("math.min_angle")
public static double minAngle(double angle) {
while (angle > 180)
angle -= 360;
while (angle < -180)
angle += 360;
return angle;
}

@MolangNative("math.mod")
public static double mod(final double a, final double b) {
return a % b;
}

@MolangNative("math.pi")
public static double pi() {
return Math.PI;
}

@MolangNative("math.pow")
public static double pow(final double a, final double b) {
return Math.pow(a, b);
}

@MolangNative("math.random")
public static double random(final double min, final double max) {
return RANDOM.nextDouble(min, max);
}

@MolangNative("math.random_integer")
public static int randomInteger(final double min, final double max) {
return RANDOM.nextInt((int) min, (int) max);
}

@MolangNative("math.round")
public static double round(final double value) {
return Math.round(value);
}

@MolangNative("math.sin")
public static double sin(final double value) {
return Math.sin(value * RADIAN);
}

@MolangNative("math.sqrt")
public static double sqrt(final double value) {
return Math.sqrt(value);
}

@MolangNative("math.trunc")
public static double trunc(final double value) {
return value - value % 1;
}

private void bindCallable(final @NotNull String name, final @NotNull Function binding) {
bindings.put(name, binding);
}
Expand All @@ -164,5 +334,4 @@ public Object getProperty(String name) {
@Override
public void setProperty(String name, Object value) {
}

}
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
/*
* This file is part of mocha, licensed under the MIT license
*
* Copyright (c) 2021-2023 Unnamed Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.mocha.runtime.jvm;
/*
* This file is part of mocha, licensed under the MIT license
*
* Copyright (c) 2021-2023 Unnamed Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.mocha.runtime.jvm;

import javassist.CtClass;
import org.jetbrains.annotations.Nullable;

final class CompileVisitResult {
private final CtClass lastPushedType;
private final boolean returned;

public CompileVisitResult(final @Nullable CtClass lastPushedType) {
public CompileVisitResult(final @Nullable CtClass lastPushedType, final boolean returned) {
this.lastPushedType = lastPushedType;
this.returned = returned;
}

public CompileVisitResult(final @Nullable CtClass lastPushedType) {
this(lastPushedType, false);
}

public boolean returned() {
return returned;
}

public @Nullable CtClass lastPushedType() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* This file is part of mocha, licensed under the MIT license
*
* Copyright (c) 2021-2023 Unnamed Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.mocha.runtime.jvm;
/*
* This file is part of mocha, licensed under the MIT license
*
* Copyright (c) 2021-2023 Unnamed Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.mocha.runtime.jvm;

import javassist.ClassPool;
import javassist.CtClass;
Expand All @@ -45,6 +45,8 @@ final class FunctionCompileState {
private final Map<String, Object> requirements = new HashMap<>();
private final Map<String, RegisteredMolangNative> natives;
private final Map<String, Integer> argumentParameterIndexes;
private final Map<String, Integer> localsByName = new HashMap<>();
private int maxLocals = 0;

FunctionCompileState(
MolangCompilerImpl compiler,
Expand Down Expand Up @@ -90,4 +92,16 @@ final class FunctionCompileState {
public @NotNull Map<String, Integer> argumentParameterIndexes() {
return argumentParameterIndexes;
}

public int maxLocals() {
return maxLocals;
}

public void maxLocals(int maxLocals) {
this.maxLocals = maxLocals;
}

public Map<String, Integer> getLocalsByName() {
return localsByName;
}
}
Loading

0 comments on commit 90ae41b

Please sign in to comment.