-
Notifications
You must be signed in to change notification settings - Fork 0
jcarrano/expr
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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 <start> <stop> <steps>" and it will integrate each expression you input between a and b using <step> 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.
About
C library for parsing and evaluating simple expressions.
Topics
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published