ExprTk

Embedded ExprTk

Nift has the C++ Mathematical Expression Toolkit Library (ExprTk) embedded (official website, GitHub). ExprTk is a simple to use, easy to integrate and extremely efficient run-time mathematical expression parser and evaluation engine. ExprTk supports numerous forms of functional, logical and vector processing semantics and is very easily extendible.

[contents]

Contents

ExprTk interpreter

Nift has an ExprTk interpreter that you can start with either nsm interp -exprtk or nift interp -exprtk.

In Nift's interpreter mode the prompt will just display the language. If you would like the prompt to also display the present working directory (up to using half the width of the console) you can switch to the shell mode using nsm_mode("sh"). You can switch back again with nsm_mode("interp").

You can switch to one of the other languages available in Nift's interpreter using nsm_lang(langStr) where langStr is one of f++, n++, lua or exprtk.

Running ExprTk scripts

If you have an ExprTk script saved in a file path/script-name.exprtk you can run it with either of the following:

nsm run path/script-name.exprtk
nift run path/script-name.exprtk

If the script has a different extension, say .ext, you can run the script with either of the following:

nsm run -exprtk path/script-name.ext
nift run -exprtk path/script-name.ext

ExprTk from f++

You can run ExprTk code from f++ using any of the following (without needing the comment syntax):

exprtk(expression)

`expression`

exprtk
{
	// block of ExprTk code
}

ExprTk from n++

You can run ExprTk code from n++ using any of the following (without needing the comment syntax):

@exprtk(expression)

@`expression`

@exprtk
{
	// block of ExprTk code
}

ExprTk with n++/f++ conditions

You can use ExprTk with conditions for if, else-if and else statements, along with do-while, for, while loops. You can also use ExprTk for the post-loop increment code with for loops (ie. the code after the second ;). For example:

for(int i=0; i<10; i+=1)
	console("i: ", i)

ExprTk access to Nift variables

ExprTk has access to Nift variables of the following types: bool, int, double, char, string and std::vector<double>. You can simply use the variables as if they had been defined inside ExprTk, although a char is treated as a string, and a bool or an int is treated as a double.

Nift functions

The following functions specific to Nift are available inside ExprTk code.

syntax example about
cd(string) cd('~/') change directory
sys(string) sys('ls *.txt') execute system call/command
to_string(double) to_string(10) convert double to string
nsm_setnumber(string, number) nsm_setnumber('i', 0) set Nift variable from number
nsm_setstring(string, string) nsm_setstring('str', 'hello!') set Nift variable from string
nsm_tonumber(string) nsm_tonumber('i') get number from Nift variable
nsm_tostring(string) nsm_tostring('str') get string from Nift variable
nsm_write(ostream, params) nsm_write(console, 'x: ', x, endl) write to console, output file (ofile), or a stream
syntax example about

Benchmark results

Results from The Great C++ Mathematical Expression Parser Benchmark are below:

Scores:
  #     Parser                  Type            Points   Score   Failures
  -----------------------------------------------------------------------
  00    ExprTk                  double          901      100     0
  01    ExprTkFloat             float           740       82     9
  02    muparserSSE             float           726       93     9
  03    METL                    double          686       52     0
  04    FParser 4.5             double          584       43     0
  05    atmsp 1.0.4             double          530       38     2
  06    muparser 2.2.4          double          517       37     0
  07    muparser 2.2.4 (omp)    double          444       35     0
  08    MTParser                double          381       34     0
  09    MathExpr                double          360       29     2
  10    TinyExpr                double          354       31     2
  11    Lepton                  double          134        8     2
  12    muparserx               double           86        5     0

Sponsors