Skip to content

Latest commit

 

History

History
58 lines (49 loc) · 2.1 KB

README.md

File metadata and controls

58 lines (49 loc) · 2.1 KB

#️⃣ StrEvaluate

Dead simple, small, fast expression evaluation in Java.

StrEvaluate has no external dependencies. Releases are avalible via Github Packages or .jar file found under "releases". To add and use StrEvaluate as a dependency use:

<dependency>
  <groupId>com.h_ansen</groupId>
  <artifactId>strevaluate</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

After you have StrEvaluate as a dependency or jar evaluating expressions is as easy as:

String expression = "(1 + 10) ^ 2";
double result = new StrEvaluate.eval(expression);

String expression = "1 + 2 == 3";
boolean result = new StrEvaluate.evalAsBool(expression)

Supported Operators

Operator Name
+ Plus
- Infix Minus
-n Prefix Minus/Negitive
* Multiplication
/ Division
^ Exponential
( and ) Grouping
== Equality
!= Inequality

Define your own functions

Functions can take in a variable number of arguments and return a result. StrEvaluate includes no built-in functions so your free to create only the functions you need!

Function MAX = new Function("max", 2) { // A function named max which takes in two arguments
    public double run(double... args) { // Override the 'run' function (called when this function is parsed)
        if (args[0] > args[1]) {
            return args[0];
        } else { 
            return args[1];
        }
    }
};

// Create an evaluator instance and add the function we created
StrEvaluate evaluator = new StrEvaluate();
evaluator.addFunction(MAX);

// The evaluator will now parse the function and use the function call back 'run' to evaluate the expression
double result = evaluator.eval("max(1, 1 + 1)"); // evaluates to 2.0

// We can even combine our function with other expressions
boolean result = evaluator.evalAsBool("max(1, 2) == 2") // evaluates to true