Skip to content

Commit

Permalink
'small changes'
Browse files Browse the repository at this point in the history
  • Loading branch information
yusshu committed Dec 26, 2023
1 parent b032858 commit 3d3dd79
Show file tree
Hide file tree
Showing 49 changed files with 2,032 additions and 1,530 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
import team.unnamed.mocha.lexer.Cursor;
import team.unnamed.mocha.parser.ParseException;
import team.unnamed.mocha.parser.ast.Expression;
import team.unnamed.mocha.runtime.Function;
import team.unnamed.mocha.runtime.binding.ObjectBinding;
import team.unnamed.mocha.runtime.binding.StandardBindings;
import team.unnamed.mocha.runtime.jvm.MolangNullaryFunction;
import team.unnamed.mocha.runtime.GlobalScope;
import team.unnamed.mocha.runtime.MochaFunction;
import team.unnamed.mocha.runtime.binding.JavaObjectBinding;
import team.unnamed.mocha.runtime.standard.MochaMath;
import team.unnamed.mocha.runtime.value.Function;

import java.io.IOException;
import java.io.Reader;
Expand All @@ -43,17 +44,17 @@
*
* @since 3.0.0
*/
public interface MolangEngine<T> {
static <T> MolangEngine<T> create(T entity) {
return new MolangEngineImpl<>(entity);
public interface MochaEngine<T> {
static <T> MochaEngine<T> create(T entity) {
return new MochaEngineImpl<>(entity);
}

static MolangEngine<?> create() {
return new MolangEngineImpl<>(null);
static MochaEngine<?> create() {
return new MochaEngineImpl<>(null);
}

static MolangEngine<?> createDefault() {
final MolangEngine<?> engine = create();
static MochaEngine<?> createDefault() {
final MochaEngine<?> engine = create();
engine.bindDefaults();
return engine;
}
Expand Down Expand Up @@ -106,7 +107,7 @@ default double eval(String script) {
* @return The compiled function.
* @since 3.0.0
*/
@NotNull MolangNullaryFunction compile(final @NotNull String script);
@NotNull MochaFunction compile(final @NotNull String script);

/**
* Returns the bindings for this Molang engine
Expand All @@ -115,7 +116,7 @@ default double eval(String script) {
* @return This engine's bindings
* @since 3.0.0
*/
@NotNull ObjectBinding bindings();
@NotNull GlobalScope scope();

/**
* Makes this engine use the default bindings.
Expand All @@ -125,11 +126,7 @@ default double eval(String script) {
* @since 3.0.0
*/
default void bindDefaults() {
final ObjectBinding bindings = bindings();
final ObjectBinding queryBinding = StandardBindings.createQueryBinding();
bindings.setProperty("query", queryBinding);
bindings.setProperty("q", queryBinding);
bindings.setProperty("math", StandardBindings.MATH_BINDING);
scope().forceSet("math", JavaObjectBinding.of(MochaMath.class, new MochaMath()));
}

void bindVariable(String key, Object binding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,54 +28,54 @@
import team.unnamed.mocha.parser.ParseException;
import team.unnamed.mocha.parser.ast.Expression;
import team.unnamed.mocha.runtime.ExpressionEvaluator;
import team.unnamed.mocha.runtime.binding.ObjectBinding;
import team.unnamed.mocha.runtime.binding.ValueConversions;
import team.unnamed.mocha.runtime.jvm.MolangCompiler;
import team.unnamed.mocha.runtime.jvm.MolangNullaryFunction;
import team.unnamed.mocha.runtime.GlobalScope;
import team.unnamed.mocha.runtime.MochaFunction;
import team.unnamed.mocha.runtime.MolangCompiler;
import team.unnamed.mocha.runtime.value.MutableObjectBinding;
import team.unnamed.mocha.runtime.value.NumberValue;
import team.unnamed.mocha.runtime.value.Value;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

final class MolangEngineImpl<T> implements MolangEngine<T> {
final class MochaEngineImpl<T> implements MochaEngine<T> {
private final GlobalScope scope = GlobalScope.create();
private final T entity;
private final MolangCompiler compiler = MolangCompiler.compiler();
private final ObjectBinding bindings;
private final MolangCompiler compiler;

public MolangEngineImpl(final T entity) {
public MochaEngineImpl(final T entity) {
this.entity = entity;
bindings = new ObjectBinding();
final ObjectBinding variableBindings = new ObjectBinding();
bindings.setProperty("variable", variableBindings);
bindings.setProperty("v", variableBindings);
this.compiler = new MolangCompiler(getClass().getClassLoader(), scope);
}


@Override
public double eval(List<Expression> expressions) {
// create bindings that just apply for this evaluation
final ObjectBinding localBindings = new ObjectBinding();
localBindings.setAllFrom(this.bindings);
final GlobalScope localBindings = scope.copy();
//localBindings.setAllFrom(this.bindings);
{
// create temp bindings
ObjectBinding temp = new ObjectBinding();
localBindings.setProperty("temp", temp);
localBindings.setProperty("t", temp);
MutableObjectBinding temp = new MutableObjectBinding();
localBindings.forceSet("temp", temp);
localBindings.forceSet("t", temp);
}
ExpressionEvaluator<T> evaluator = ExpressionEvaluator.evaluator(entity, localBindings);
Object lastResult = 0;
Value lastResult = NumberValue.zero();

for (Expression expression : expressions) {
System.out.println(expression);
lastResult = expression.visit(evaluator);
Object returnValue = evaluator.popReturnValue();
Value returnValue = evaluator.popReturnValue();
if (returnValue != null) {
lastResult = returnValue;
break;
}
}

// ensure returned value is a number
return ValueConversions.asDouble(lastResult);
return lastResult == null ? 0D : lastResult.getAsNumber();
}

@Override
Expand All @@ -92,25 +92,27 @@ public double eval(Reader reader) {
}

@Override
public @NotNull MolangNullaryFunction compile(@NotNull String script) {
return compiler.compile(script);
public @NotNull MochaFunction compile(final @NotNull String script) {
final List<Expression> parsed;
try {
parsed = parse(script);
} catch (ParseException e) {
return () -> 0;
}
return compiler.compile(parsed, MochaFunction.class);
}

@Override
public @NotNull ObjectBinding bindings() {
return bindings;
public @NotNull GlobalScope scope() {
return scope;
}

@Override
public void bindVariable(String key, Object binding) {
final ObjectBinding variableBinding = (ObjectBinding) bindings.getProperty("variable");
variableBinding.setProperty(key, binding);
}

@Override
public void bindQuery(String key, Object binding) {
final ObjectBinding queryBinding = (ObjectBinding) bindings.getProperty("query");
queryBinding.setProperty(key, binding);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.mocha.runtime.jvm;
package team.unnamed.mocha.runtime;

import javassist.CtClass;
import org.jetbrains.annotations.Nullable;
Expand Down
75 changes: 38 additions & 37 deletions src/main/java/team/unnamed/mocha/runtime/ExpressionEvaluator.java
Original file line number Diff line number Diff line change
@@ -1,58 +1,59 @@
/*
* 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;
/*
* 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;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import team.unnamed.mocha.MochaEngine;
import team.unnamed.mocha.parser.ast.Expression;
import team.unnamed.mocha.parser.ast.ExpressionVisitor;
import team.unnamed.mocha.runtime.binding.ObjectBinding;
import team.unnamed.mocha.runtime.value.Value;

/**
* An {@link ExpressionVisitor} implementation that evaluates
* (interprets) the expressions it visits and returns a single
* value, commonly, a double value.
*
* <p>It is recommended to use {@link team.unnamed.mocha.MolangEngine}
* <p>It is recommended to use {@link MochaEngine}
* evaluation instead of this. While this interface's evaluation may return
* non-numerical values, the {@link team.unnamed.mocha.MolangEngine} will
* non-numerical values, the {@link MochaEngine} will
* ensure them to be numerical, to comply with the Molang specification.</p>
*
* @since 3.0.0
*/
public /* sealed */ interface ExpressionEvaluator<T> /* permits ExpressionEvaluatorImpl */ extends ExecutionContext<T>, ExpressionVisitor<Object> {
public /* sealed */ interface ExpressionEvaluator<T> /* permits ExpressionEvaluatorImpl */ extends ExecutionContext<T>, ExpressionVisitor<Value> {
/**
* Creates a new {@link ExpressionEvaluator} instance with
* the given bindings.
*
* @param entity The entity object
* @param bindings The bindings to use.
* @param entity The entity object
* @param scope The global scope
* @return The created expression evaluator.
* @since 3.0.0
*/
static <T> @NotNull ExpressionEvaluator<T> evaluator(final @Nullable T entity, final @NotNull ObjectBinding bindings) {
return new ExpressionEvaluatorImpl<>(entity, bindings);
static <T> @NotNull ExpressionEvaluator<T> evaluator(final @Nullable T entity, final @NotNull GlobalScope scope) {
return new ExpressionEvaluatorImpl<>(entity, scope);
}

/**
Expand All @@ -63,7 +64,7 @@
* @return The created expression evaluator.
* @since 3.0.0
*/
static <T> @NotNull ExpressionEvaluator<T> evaluator(final @NotNull ObjectBinding bindings) {
static <T> @NotNull ExpressionEvaluator<T> evaluator(final @NotNull GlobalScope bindings) {
return evaluator(null, bindings);
}

Expand All @@ -75,11 +76,11 @@
* @since 3.0.0
*/
static <T> @NotNull ExpressionEvaluator<T> evaluator() {
return evaluator(ObjectBinding.EMPTY);
return evaluator(GlobalScope.create());
}

@Override
default @Nullable Object eval(final @NotNull Expression expression) {
default @Nullable Value eval(final @NotNull Expression expression) {
return expression.visit(this);
}

Expand All @@ -89,7 +90,7 @@
* @return The evaluator bindings.
* @since 3.0.0
*/
@NotNull ObjectBinding bindings();
@NotNull GlobalScope bindings();

/**
* Creates a new, child, expression evaluator.
Expand Down Expand Up @@ -126,6 +127,6 @@
* expression is found.
* @since 3.0.0
*/
@Nullable Object popReturnValue();
@Nullable Value popReturnValue();

}
Loading

0 comments on commit 3d3dd79

Please sign in to comment.