src/emath

emath is a math parser/evaluator library. it first converts raw expression [which is given as string] into AST and then evaluates it.

emath allows you to manipulate the generated AST, so it would not limit your POWER as a Nim programmer 👑.

Here's the flow:

string ──parsing──► AST ──evaluating──► number

Example:

import src/emath
# evaluating with default functions and variables
echo "1 + sin(PI)".parse.eval # 1.0

# using custom variables and functions
import std/tables
import emath/defaults

let vars = toTable {
  "myvar": 6.6
}

var fns = defaultFns
fns["pow2"] = proc(args: seq[float]): float =
  args[0] * args[0]

let ans = "myvar * pow2(3)".parse.eval(vars, fns)
echo ans # 59.4

Procs

func `$`(mn: EMathNode): string {....raises: [], tags: [].}
func eval(mn: EMathNode): float {....raises: [EMathNotDefined, Exception,
    EMathEvalError], tags: [RootEffect].}
calculates the final answer with default variables and default functions
func eval(mn: EMathNode; varLookup: EMathVarLookup; fnLookup: EMathFnLookup): float {.
    ...raises: [EMathNotDefined, EMathNotDefined, Exception, EMathEvalError],
    tags: [RootEffect].}
calculates the final answer
func isValid(mn: EMathNode): bool {....raises: [], tags: [].}
check for any AST errors in genereated AST
func parse(input: string): EMathNode {....raises: [ValueError, EMathParseError,
    EMathTokenError], tags: [].}
parses the math expression from raw string into its corresponding AST
func treeRepr(mn: EMathNode): string {....raises: [], tags: [].}

converts a EMathNode into its corresponding tree representation.

can be used for debugging purposes.