Skip to content

Commit

Permalink
feat: make bindings api more easy to use
Browse files Browse the repository at this point in the history
  • Loading branch information
yusshu committed Dec 26, 2023
1 parent 8683fb2 commit 4416a31
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 52 deletions.
11 changes: 11 additions & 0 deletions src/main/java/team/unnamed/mocha/runtime/GlobalScope.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/team/unnamed/mocha/runtime/GlobalScopeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Value> entries() {
return bindings;
}
}
79 changes: 27 additions & 52 deletions src/main/java/team/unnamed/mocha/runtime/standard/MochaMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,34 +61,34 @@ public final class MochaMath implements ObjectValue {
private final Map<String, Value> 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) {
Expand Down Expand Up @@ -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);
}
}
25 changes: 25 additions & 0 deletions src/main/java/team/unnamed/mocha/runtime/value/ObjectValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,29 @@ default boolean set(final @NotNull String name, final @Nullable Value value) {
default @NotNull Map<String, Value> 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);
}
}

0 comments on commit 4416a31

Please sign in to comment.