Skip to content

Commit

Permalink
Add missing ExpressionParser::insertX overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
FourteenBrush committed Jun 20, 2024
1 parent 7d1769d commit 2674adb
Showing 1 changed file with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
@SuppressWarnings("unused")
public class ExpressionParser {

private ExpressionParser() {}
private ExpressionParser() {
}

/**
* @see ExpressionParser#parse(String, ExecutionEnv)
Expand All @@ -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");
Expand All @@ -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)
*/
Expand All @@ -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<FunctionContext> 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<FunctionContext> 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<FunctionContext> fn) {
return DEFAULT_ENV.insertFunctionIfAbsent(name, numArgs, fn);
}

/**
* @see #insertSymbolIfAbsent(Symbol)
*/
public static Symbol insertFunctionIfAbsent(String name, int minArgs, int maxArgs, ToDoubleFunction<FunctionContext> 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
Expand All @@ -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();
}
Expand Down

0 comments on commit 2674adb

Please sign in to comment.