https://github.com/jcarrano/expr
C library for parsing and evaluating simple expressions.
https://github.com/jcarrano/expr
bytecode c expression-evaluator
Last synced: about 1 year ago
JSON representation
C library for parsing and evaluating simple expressions.
- Host: GitHub
- URL: https://github.com/jcarrano/expr
- Owner: jcarrano
- Created: 2013-04-14T23:33:02.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2013-04-14T23:40:07.000Z (about 13 years ago)
- Last Synced: 2025-02-01T20:11:35.868Z (over 1 year ago)
- Topics: bytecode, c, expression-evaluator
- Language: C
- Size: 160 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README
Awesome Lists containing this project
README
expr
C library for parsing and evaluating simple expressions.
Juan I Carrano.
## What is expr? ##
expr is a system for compiling and evaluating simple procedures, such as
formulas. It does not support conditionals or loops. This allows for a fast
and simple implementation, at the expense of Turing-completeness.
Expressions are transformed into bytecode and evaluated by a stack-machine.
Evaluation speed is ~45% versus native C functions.
## What makes expr? ##
Expr is fairly modular. Its components are
expr: Used for defining environments (variables and procedures) which will
be visible to expressions.
parse: A simple reverse polish notation parser.
compile: Handles the construction of a high-level representation of the expr.
The parser directly uses this module.
assemble: Takes the output of the compile module and translates it into a
compact representation (an 'expr' object). This includes resolving
references, allocating space for stack and variables, etc.
vm: Executes the expr object.
Any of these components can be modified. (For example, it would be highly
desirable to have an infix parser).
## What can it be used for? ##
Anything that requires fast evaluation of a (simple) procedure that is to be
determined at runtime. For example, a calculator which does numerical
integration (see under the 'examples' directory) or data processing programs.
Also, note that, aside from the extra_math library, the rest of the code does
not assume anything about the type of expressions. While the default definition
of 'data_t' is 'double', you can change it to whatever you like, recompile, and
make full use of the system. Of course you will have to define some builtin
functions.
## How is it used? ##
Expressions take a number of arguments and return a number of values. They
can store values in local variables, and access global variables. Besides
variable access and stack manipulation, everything else (including + - * /) is
a function call.
See the code and the examples to learn the API.
* RPN parser
The current parser gives a fairly low level access to the compiler primitives.
The latter will check, however, that the program is correctly defined and will
report any errors.
Expressions are writen in RPN. For example '1 1 + 3 * sin' is equivalent to
sin((1 + 1)*3).
To access arguments use '$ N' where N >= 0 is the argument number. Note the
space between $ and N.
To assign to a variable use '->'. For example '1 + 1 -> x1'. This does NOT pop
any values out of the stack, so for example '3 -> v 4 *' returns 12. It is an
error to reference a local variable before assignment.
To reference a variable just spell it's name. The same applies to a function.
Variable names can mask function names, so be careful.
The operator ';' completely clears the stack. 'clear n' clears n positions. n
must be constant. For example '6 4 7 clear 1 +' gives 4.
* Test it!
Under 'examples' lauch calc.test. If called without input arguments it is just
a RPN calculator. Call it with "calc.test " and it will
integrate each expression you input between a and b using steps (it
implements a simple rectangle rule).
## Is is complete? ##
No.
There is no optimization module. The goal in the near future is to be able
to eliminate constant subexpressions. There is already a way to specify which
functions depend only on their input (and have no side effects).
The parser can be greatly improved.
It would be nice to be able to write functions in expr and use then inside other
expressions.
There are some comments in spanish that need to be translated.