Skip to content

MihailRis/runexp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RunExp v1.1

A lightweight runtime math expressions solver/compiler for JVM.

  • does not use any scripting engine
  • uses java reflection
  • writes JVM bytecode
  • aimed to reuse compiled expressions

Future:

  • integer expressions, bitwise operations
  • double precision expressions

Download built jar library file from GitHub

Dependecies:

RunExp requires one dependency since v1.1

version used: asm-all-5.2

ASM is used for runtime java-bytecode generation.

Usage:

with x:

Expression expression = RunExp.compile("sin(x) * 0.5 + (x * 0.1)");
expression.eval(1.25f); // same as sin(1.25f) * 0.5f + (1.25f * 0.1f)

// or

RunExpSolver solver = new RunExpSolver();
Expression expression = solver.compile("sin(x) * 0.5 + (x * 0.1)");
expression.eval(1.25f);

Solve constant expression:

float value = RunExp.eval("pi * 0.5");

if Expression wrapper needed (ConstantExpression used):

ConstantExpression expression = RunExp.compileConstant("pi ^ 2");

Solvers:

RunExp class uses RunExpSolver instance available as RunExp.solver, but it's preferred to create new one.

RunExpSolver solver = new RunExpSolver();
solver.addConstant("g", 9.8f);
...
float value = solver.eval(expressionString);

solver.allowJVM - allow compiling expressions directly into JVM bytecode (true by default)

Custom constants:

Method:

RunExpSolver.addConstant(String name, float value);

Example:

solver.addConstant("g", 9.8f);

Custom functions:

Methods:

RunExpSolver.addFunction(String name, Class<?> class, String methodName);
RunExpSolver.addFunction(String name, Class<?> class, String methodName, Class<?>... args);

Example:

try {
  // adding Noise.noise2d(float, float) static method as function 'noise'
  solver.addFunction("noise", Noise.class, "noise2d");
  // if Noise.noise is overloaded
  solver.addFunction("noise", Noise.class, "noise2d", class.float, class.float);
} catch (NoSuchMethodException e){
  ...
}
// see RunExpSolver.addFunction docs for more info

Built-in functions may be overriden.

Example:

// override built-in 'rand' with some MathUtils.random method
solver.addFunction("rand", MathUtils.class, "random");

Features:

  • unary operations: '-'
  • binary operations: '+', '-', '*', '/' and '^' (exponentation)
  • functions:
    • abs
    • sin, cos, tan
    • sqrt, exp, pow (same as '^' operator)
    • min(a, b), max(a, b)
    • round, floor, ceil
    • sign / signum
    • rand - random number in range [0.0, 1.0]
    • smoother (smoother step)
  • constants:
    • pi (Math.PI)
    • pi2 (Math.PI * 2)
    • e (Math.E)
    • raddeg (180.0 / Math.PI) usage: degrees = radians * raddeg
    • degrad (Math.PI / 180.0) usage: radians = degreen * degrad
  • custom constants
  • custom functions (directly calling static methods as functions)