From 2674adb6fc40c283843526f4bcbc43a684eabd32 Mon Sep 17 00:00:00 2001 From: FourteenBrush Date: Thu, 20 Jun 2024 11:50:25 +0200 Subject: [PATCH] Add missing ExpressionParser::insertX overloads --- .../ExpressionParser.java | 70 +++++++++++++++++-- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/me/fourteendoggo/mathexpressionparser/ExpressionParser.java b/core/src/main/java/me/fourteendoggo/mathexpressionparser/ExpressionParser.java index 54598d9..10077fa 100644 --- a/core/src/main/java/me/fourteendoggo/mathexpressionparser/ExpressionParser.java +++ b/core/src/main/java/me/fourteendoggo/mathexpressionparser/ExpressionParser.java @@ -22,7 +22,8 @@ @SuppressWarnings("unused") public class ExpressionParser { - private ExpressionParser() {} + private ExpressionParser() { + } /** * @see ExpressionParser#parse(String, ExecutionEnv) @@ -35,10 +36,10 @@ public static double parse(String input) { * Parses the given expression and returns the result * * @param input the expression to parse - * @param env the execution environment to obtain symbols from + * @param env the execution environment to obtain symbols from * @return the result of the expression * @throws NullPointerException if the expression or env is null - * @throws SyntaxException if the given expression is invalid or empty + * @throws SyntaxException if the given expression is invalid or empty */ public static double parse(String input, ExecutionEnv env) { Objects.requireNonNull(input, "input was null"); @@ -48,6 +49,16 @@ public static double parse(String input, ExecutionEnv env) { return tokenizer.readTokens().solve(); } + public static void insertVariable(String name, double value) { + DEFAULT_ENV.insertVariable(name, value); + } + + public static Symbol insertVariableIfAbsent(String name, double value) { + return DEFAULT_ENV.insertVariableIfAbsent(name, value); + } + + // region functions + /** * @see #insertFunction(String, int, int, ToDoubleFunction) */ @@ -71,22 +82,71 @@ public static void insertFunction(String functionName, DoubleBinaryOperator fn) /** * Inserts a function into the default execution environment. + * * @see ExecutionEnv#insertFunction(String, int, ToDoubleFunction) */ public static void insertFunction(String functionName, int numArgs, ToDoubleFunction fn) { - insertFunction(functionName, numArgs, numArgs, fn); + DEFAULT_ENV.insertFunction(functionName, numArgs, fn); } /** * Inserts a function into the default execution environment. + * * @see ExecutionEnv#insertFunction(String, int, int, ToDoubleFunction) */ public static void insertFunction(String functionName, int minArgs, int maxArgs, ToDoubleFunction fn) { DEFAULT_ENV.insertFunction(functionName, minArgs, maxArgs, fn); } + /** + * @see #insertSymbolIfAbsent(Symbol) + */ + public static Symbol insertFunctionIfAbsent(String name, DoubleSupplier fn) { + return DEFAULT_ENV.insertFunctionIfAbsent(name, fn); + } + + /** + * @see #insertSymbolIfAbsent(Symbol) + */ + public static Symbol insertFunctionIfAbsent(String name, DoubleUnaryOperator fn) { + return DEFAULT_ENV.insertFunctionIfAbsent(name, fn); + } + + /** + * @see #insertSymbolIfAbsent(Symbol) + */ + public static Symbol insertFunctionIfAbsent(String name, DoubleBinaryOperator fn) { + return DEFAULT_ENV.insertFunctionIfAbsent(name, fn); + } + + /** + * @see #insertSymbolIfAbsent(Symbol) + */ + public static Symbol insertFunctionIfAbsent(String name, int numArgs, ToDoubleFunction fn) { + return DEFAULT_ENV.insertFunctionIfAbsent(name, numArgs, fn); + } + + /** + * @see #insertSymbolIfAbsent(Symbol) + */ + public static Symbol insertFunctionIfAbsent(String name, int minArgs, int maxArgs, ToDoubleFunction fn) { + return DEFAULT_ENV.insertFunctionIfAbsent(name, minArgs, maxArgs, fn); + } + + // endregion + + /** + * Inserts a symbol into the global environment, if it is not already present. + * + * @return the previously inserted symbol, or null. + */ + public static Symbol insertSymbolIfAbsent(Symbol symbol) { + return DEFAULT_ENV.insertSymbolIfAbsent(symbol); + } + /** * Inserts a symbol into the default execution environment. + * * @param symbol the symbol to insert. * @see FunctionCallSite * @see Variable @@ -95,6 +155,8 @@ public static void insertSymbol(Symbol symbol) { DEFAULT_ENV.insertSymbol(symbol); } + // TODO(urgent): create overrides similar to ExecutionEnv::insertX + static class DefaultEnvHolder { static final ExecutionEnv DEFAULT_ENV = ExecutionEnv.defaulted(); }