diff --git a/src/main/java/team/unnamed/mocha/runtime/GlobalScope.java b/src/main/java/team/unnamed/mocha/runtime/GlobalScope.java index bc72690..de2fedb 100644 --- a/src/main/java/team/unnamed/mocha/runtime/GlobalScope.java +++ b/src/main/java/team/unnamed/mocha/runtime/GlobalScope.java @@ -1,6 +1,7 @@ package team.unnamed.mocha.runtime; import org.jetbrains.annotations.NotNull; +import team.unnamed.mocha.runtime.value.MutableObjectBinding; import team.unnamed.mocha.runtime.value.ObjectValue; import team.unnamed.mocha.runtime.value.Value; @@ -12,4 +13,14 @@ public interface GlobalScope extends ObjectValue { @NotNull GlobalScope copy(); void forceSet(final @NotNull String name, final @NotNull Value value); + + default @NotNull MutableObjectBinding query() { + if (entries().containsKey("query")) { + return (MutableObjectBinding) get("query"); + } else { + final MutableObjectBinding query = new MutableObjectBinding(); + forceSet("query", query); + return query; + } + } } diff --git a/src/main/java/team/unnamed/mocha/runtime/GlobalScopeImpl.java b/src/main/java/team/unnamed/mocha/runtime/GlobalScopeImpl.java index ba2ad6c..c46f21e 100644 --- a/src/main/java/team/unnamed/mocha/runtime/GlobalScopeImpl.java +++ b/src/main/java/team/unnamed/mocha/runtime/GlobalScopeImpl.java @@ -26,4 +26,9 @@ final class GlobalScopeImpl implements GlobalScope { public void forceSet(final @NotNull String name, final @NotNull Value value) { bindings.put(name, value); } + + @Override + public @NotNull Map entries() { + return bindings; + } } diff --git a/src/main/java/team/unnamed/mocha/runtime/standard/MochaMath.java b/src/main/java/team/unnamed/mocha/runtime/standard/MochaMath.java index 16d38be..48abb70 100644 --- a/src/main/java/team/unnamed/mocha/runtime/standard/MochaMath.java +++ b/src/main/java/team/unnamed/mocha/runtime/standard/MochaMath.java @@ -26,7 +26,6 @@ import org.jetbrains.annotations.NotNull; import team.unnamed.mocha.runtime.binding.BindExternalFunction; import team.unnamed.mocha.runtime.binding.Binding; -import team.unnamed.mocha.runtime.value.Function; import team.unnamed.mocha.runtime.value.NumberValue; import team.unnamed.mocha.runtime.value.ObjectValue; import team.unnamed.mocha.runtime.value.Value; @@ -62,34 +61,34 @@ public final class MochaMath implements ObjectValue { private final Map entries = new HashMap<>(); public MochaMath() { - bindFunction("abs", Math::abs); - bindFunction("acos", MochaMath::acos); - bindFunction("asin", MochaMath::asin); - bindFunction("atan", MochaMath::atan); - bindFunction("atan2", MochaMath::atan2); - bindFunction("ceil", Math::ceil); - bindFunction("clamp", MochaMath::clamp); - bindFunction("cos", MochaMath::cos); - bindFunction("die_roll", MochaMath::dieRoll); - bindFunction("die_roll_integer", MochaMath::dieRollInteger); - bindFunction("exp", Math::exp); - bindFunction("floor", Math::floor); - bindFunction("hermite_blend", MochaMath::hermiteBlend); - bindFunction("lerp", MochaMath::lerp); - bindFunction("lerprotate", MochaMath::lerpRotate); - bindFunction("ln", Math::log); - bindFunction("max", Math::max); - bindFunction("min", Math::min); - bindFunction("min_angle", MochaMath::minAngle); - bindFunction("mod", MochaMath::mod); + setFunction("abs", Math::abs); + setFunction("acos", MochaMath::acos); + setFunction("asin", MochaMath::asin); + setFunction("atan", MochaMath::atan); + setFunction("atan2", MochaMath::atan2); + setFunction("ceil", Math::ceil); + setFunction("clamp", MochaMath::clamp); + setFunction("cos", MochaMath::cos); + setFunction("die_roll", MochaMath::dieRoll); + setFunction("die_roll_integer", MochaMath::dieRollInteger); + setFunction("exp", Math::exp); + setFunction("floor", Math::floor); + setFunction("hermite_blend", MochaMath::hermiteBlend); + setFunction("lerp", MochaMath::lerp); + setFunction("lerprotate", MochaMath::lerpRotate); + setFunction("ln", Math::log); + setFunction("max", Math::max); + setFunction("min", Math::min); + setFunction("min_angle", MochaMath::minAngle); + setFunction("mod", MochaMath::mod); entries.put("pi", NumberValue.of(Math.PI)); - bindFunction("pow", Math::pow); - bindFunction("random", MochaMath::random); - bindFunction("random_integer", MochaMath::randomInteger); - bindFunction("round", Math::round); - bindFunction("sin", MochaMath::sin); - bindFunction("sqrt", Math::sqrt); - bindFunction("trunc", MochaMath::trunc); + setFunction("pow", Math::pow); + setFunction("random", MochaMath::random); + setFunction("random_integer", MochaMath::randomInteger); + setFunction("round", Math::round); + setFunction("sin", MochaMath::sin); + setFunction("sqrt", Math::sqrt); + setFunction("trunc", MochaMath::trunc); } private static double radify(double n) { @@ -214,28 +213,4 @@ public static double trunc(final double value) { public @NotNull Value get(final @NotNull String name) { return entries.getOrDefault(name, NumberValue.zero()); } - - private void bindFunction(final @NotNull String name, final @NotNull DoubleFunction1 function) { - entries.put(name, (Function) (ctx, args) -> NumberValue.of(function.apply(args.next().eval().getAsNumber()))); - } - - private void bindFunction(final @NotNull String name, final @NotNull DoubleFunction2 function) { - entries.put(name, (Function) (ctx, args) -> NumberValue.of(function.apply(args.next().eval().getAsNumber(), args.next().eval().getAsNumber()))); - } - - private void bindFunction(final @NotNull String name, final @NotNull DoubleFunction3 function) { - entries.put(name, (Function) (ctx, args) -> NumberValue.of(function.apply(args.next().eval().getAsNumber(), args.next().eval().getAsNumber(), args.next().eval().getAsNumber()))); - } - - interface DoubleFunction1 { - double apply(double n); - } - - interface DoubleFunction2 { - double apply(double n1, double n2); - } - - interface DoubleFunction3 { - double apply(double n1, double n2, double n3); - } } diff --git a/src/main/java/team/unnamed/mocha/runtime/value/ObjectValue.java b/src/main/java/team/unnamed/mocha/runtime/value/ObjectValue.java index 46b6b2f..cc78320 100644 --- a/src/main/java/team/unnamed/mocha/runtime/value/ObjectValue.java +++ b/src/main/java/team/unnamed/mocha/runtime/value/ObjectValue.java @@ -16,4 +16,29 @@ default boolean set(final @NotNull String name, final @Nullable Value value) { default @NotNull Map entries() { return Collections.emptyMap(); } + + // :) overloads + default void setFunction(final @NotNull String name, final @NotNull DoubleFunction1 function) { + set(name, (Function) (ctx, args) -> NumberValue.of(function.apply(args.next().eval().getAsNumber()))); + } + + default void setFunction(final @NotNull String name, final @NotNull DoubleFunction2 function) { + set(name, (Function) (ctx, args) -> NumberValue.of(function.apply(args.next().eval().getAsNumber(), args.next().eval().getAsNumber()))); + } + + default void setFunction(final @NotNull String name, final @NotNull DoubleFunction3 function) { + set(name, (Function) (ctx, args) -> NumberValue.of(function.apply(args.next().eval().getAsNumber(), args.next().eval().getAsNumber(), args.next().eval().getAsNumber()))); + } + + interface DoubleFunction1 { + double apply(double n); + } + + interface DoubleFunction2 { + double apply(double n1, double n2); + } + + interface DoubleFunction3 { + double apply(double n1, double n2, double n3); + } }