Skip to content
/ eval Public

C++ : C-like Expression Evaluator to be used as module in applications.

License

Notifications You must be signed in to change notification settings

c-pointer/eval

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Generic C Expression Evaluation Module for C++

C-like Expression Evaluator to be used as module in programs. It can be used to add mathematical - and not only - expressions to be executed at runtime. It can be used to create scripts, simple save/load files, to let the user to do calculations between your application data.

Dependencies

Nothing.

The C/C++ standard libraries

  • libm ; ISO C standard mathematics library
  • libstdc++ ; ISO C++ Standard Template Library

How to use it

See the c-eval.cc which is an example in C++.

The most simple use is this

#include "cexp.cc"
...
CExp	cexp;	// the engine
CVar	result;	// a variable to store the result

if ( cexp.evaluate("sqrt(2)", &result) != E_SUCCESS )
	cout << cexp.error_message << endl;
else
	result.print();

Add custom C++ functions

see CExp::add_func(...), cexp-lib.cc

Set (add) or get variables by C++

see CExp::getvar(...), CExp::setvar(...).

Get multiple results

Get multiple results by expressions separated by ;.

vector<CVar> rstk;
if ( cexp.evaluate("1;2;3", &rstk) == E_SUCCESS ) {
	for ( CVar ans : rstk )
		ans.print();
	}

How link it

Create the static library

make lib

Link it...

g++ myapp.cc -L. -lcexp

Mathematics

  • Integer and floating point numbers; although that converted inside when it is needed.
  • Support exponent format and powers. Example: 1.2*10^16 == 1.2e+16
  • Supports base conversion (0x, 0)
  • Supports matrices and operators between them.
  • All standard C mathematics library functions (sqrt, pow, log10, cos, sin, atan2, ... see the manual)
  • Additional mathematic functions
    • SGN(x) sign of number,
    • RAD/DEG(x) convert to rad / degrees,
    • RND(x) random number 0 to x,
    • MIN/MAX(...) with any number of arguments,
    • T(m) matrix transpose,
    • I(m) matrix identity,
    • DET(m) matrix determinant,
    • PF(fmt, ...) printf's output (result of evaluator).

Syntax

number: [0x | 0] 0-9 [ . 0..9] [E{+|-}0-9..]
string: "..." or '...'
	inside double quotes `${}` and `$()` works same as shell, also backslash allowed.
matrix: [ ... [ , ...] [ ; [ , ... ] ... ] ]
variable: name
property: variable.name
array element: variable[index or string], matrix[row, col]

function call: name(p1, .. , pN)
expressions separators: ';' [and/or ',']

Support

  • String values supported; string values are anything inside the double or single quotes.
  • Only operator + allowed between strings; otherwise values converted to numbers.
  • All C string codes (\\), are supported inside and outside the strings.
  • Environment variables are supported inside and outside the strings. ($name or ${name})
  • Execute shell commands and return the result. ($(...)) ... Example: $(cat scripts/expr-test.txt)
  • String quotes are same as shell's; double quotes allow ${} and $(), single quotes are not.
  • All C operators implemented; except (++ and --).
  • ^ operator used for power. C has no operator for power, so XOR used by #.
  • Decimal, Hexadecimal 0x and Octal 0, notation supported.
  • Custom Variables, created by code or run-time in expressions.
  • No declaration of variables needed, no datatype needed to specified.
  • Assign variables. =, :=, +=, -=, *=, /=, %= works. Example: x=1;y=2;x+y
  • Associative arrays (hash-tables). Example: x[0]=1,x[1]=1,x['sum']=2
  • Custom Functions, with a fixed number of parameters, or with a variable number of parameters.
  • rnd(n), min(...), max(...), if(expr, true-expr, false-expr), (sprintf) pf(...) are functions defined by default in the module.
  • Matrices. Example: x=[1;2;3];y=[4,5,6];x*y
	| 1 |               |  4   5   6 |
	| 2 | x [ 4 5 6 ] = |  8  10  12 |
	| 3 |               | 12  15  18 |

Marix operators: -M, M+n, M+M, M-n, M-M, M*n, M*M, M^n, inv(M), t(M), i(M), det(M)

  • ans variable; the result of the previous expression.

Links

Matrix calculator