Skip to content
/ cev Public

Expression evaluator in C language.

License

Notifications You must be signed in to change notification settings

Silva97/cev

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8f37d6a · May 19, 2020

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub release (latest by date) Build Status

cev

This project is an expression evaluator developed in C language. This uses the shunting-yard algorithm to evaluate the expression respecting the precedence of the operators.

The functions cev() and cev_script() (inside cev.c file) is the main of the evaluator that be used to evaluate an string or a file. The expressions support definition of variables and use int64_t signed integers to numbers. All the following C operators can be used:

+ - * /
% ! && ||
> < ^ ~
& | >> <<
== != >= <=
= += -= /=
%= *= **

The ** is the same as in the JavaScript, calculating the power of the operands.

Simplest example usage:

int64_t result = cev(NULL, "234 + 0xffffffffff / (0732 ** 3)");

To use in your project, import cev.h and tree.h to your include's directory. Use the command below to compile a static library:

$ make lib

The file libcev.a will be created. Just include cev.h, link this static library and done!

Functions

int64_t cev(cev_t *ctx, char *input)

Evaluates input string like an expression. The ctx struct is an context that have the declared variables. If passed NULL like argument, this don't use a pre-defined context.
This function returns the result of the expression as int64_t integer.

int64_t cev_script(cev_t *ctx, char *filename)

Evaluates all lines of the file like expressions. The ctx is the same as cev() function.
Returns the latest expression result as int64_t integer.

Command-line tool

This project have an bc-like command-line calculator that can run scripts or evaluate expressions direct from input.

cev example

To install it, just run:

$ make
$ sudo make install

To see help about usage, run cev -h in your terminal.