From 4791163e24864bb6c25d98358d4023ed911b6416 Mon Sep 17 00:00:00 2001 From: Lewis Van Winkle Date: Thu, 5 May 2016 17:12:02 -0500 Subject: [PATCH] Added additional example and info. --- Makefile | 5 ++++- README.md | 25 ++++++++++++++++++++++++- example3.c | 35 +++++++++++++++++++++++++++++++++++ test.c | 1 + 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 example3.c diff --git a/Makefile b/Makefile index 1d6e4aa..ae82611 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CCFLAGS = -ansi -Wall -Wshadow -O2 LFLAGS = -lm -all: test bench example example2 +all: test bench example example2 example3 test: test.o tinyexpr.o @@ -20,6 +20,9 @@ example: example.o tinyexpr.o example2: example2.o tinyexpr.o $(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS) +example3: example3.o tinyexpr.o + $(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS) + .c.o: $(CC) -c $(CCFLAGS) $< -o $@ diff --git a/README.md b/README.md index 2fd59db..5cfc164 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ the standard C math functions and runtime binding of variables. - Simple and fast. - Implements standard operators precedence. - Exposes standard C math functions (sin, sqrt, ln, etc.). +- Can add custom functions and variables easily. - Can bind variables at eval-time. - Released under the zlib license - free for nearly any use. - Easy to use and integrate with your code @@ -168,6 +169,23 @@ This produces the output: 5.000000 +##Binding to Custom Functions + +TinyExpr can also call to custom functions implemented in C. Here is a short example: + +```C +double my_sum(double a, double b) { + /* Example C function that adds two numbers together. */ + return a + b; +} + +te_variable vars[] = { + {"mysum", my_sum, TE_FUNCTION2} /* TE_FUNCTION2 used because my_sum takes two arguments. */ +}; + +te_expr *n = te_compile("mysum(5, 6)", vars, 1, 0); + +``` ##How it works @@ -219,7 +237,12 @@ TinyExpr parses the following grammar: = {("*" | "/" | "%") } = {"^" } = {("-" | "+")} - = | | {"(" ")"} | | "(" "," ")" | "(" ")" + = + | + | {"(" ")"} + | + | "(" {"," } ")" + | "(" ")" In addition, whitespace between tokens is ignored. diff --git a/example3.c b/example3.c new file mode 100644 index 0000000..80fe9da --- /dev/null +++ b/example3.c @@ -0,0 +1,35 @@ +#include "tinyexpr.h" +#include + + +/* An example of calling a C function. */ +double my_sum(double a, double b) { + printf("Called C function with %f and %f.\n", a, b); + return a + b; +} + + +int main(int argc, char *argv[]) +{ + te_variable vars[] = { + {"mysum", my_sum, TE_FUNCTION2} + }; + + const char *expression = "mysum(5, 6)"; + printf("Evaluating:\n\t%s\n", expression); + + int err; + te_expr *n = te_compile(expression, vars, 1, &err); + + if (n) { + const double r = te_eval(n); + printf("Result:\n\t%f\n", r); + te_free(n); + } else { + /* Show the user where the error is at. */ + printf("\t%*s^\nError near here", err-1, ""); + } + + + return 0; +} diff --git a/test.c b/test.c index 7607c5b..e840525 100644 --- a/test.c +++ b/test.c @@ -495,6 +495,7 @@ void test_optimize() { {"5+5", 10}, {"pow(2,2)", 4}, {"sqrt 100", 10}, + {"pi * 2", 6.2832}, }; int i;