Skip to content

Commit 4791163

Browse files
committed
Added additional example and info.
1 parent cf03962 commit 4791163

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CCFLAGS = -ansi -Wall -Wshadow -O2
33
LFLAGS = -lm
44

55

6-
all: test bench example example2
6+
all: test bench example example2 example3
77

88

99
test: test.o tinyexpr.o
@@ -20,6 +20,9 @@ example: example.o tinyexpr.o
2020
example2: example2.o tinyexpr.o
2121
$(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS)
2222

23+
example3: example3.o tinyexpr.o
24+
$(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS)
25+
2326
.c.o:
2427
$(CC) -c $(CCFLAGS) $< -o $@
2528

README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ the standard C math functions and runtime binding of variables.
1515
- Simple and fast.
1616
- Implements standard operators precedence.
1717
- Exposes standard C math functions (sin, sqrt, ln, etc.).
18+
- Can add custom functions and variables easily.
1819
- Can bind variables at eval-time.
1920
- Released under the zlib license - free for nearly any use.
2021
- Easy to use and integrate with your code
@@ -168,6 +169,23 @@ This produces the output:
168169
5.000000
169170
170171
172+
##Binding to Custom Functions
173+
174+
TinyExpr can also call to custom functions implemented in C. Here is a short example:
175+
176+
```C
177+
double my_sum(double a, double b) {
178+
/* Example C function that adds two numbers together. */
179+
return a + b;
180+
}
181+
182+
te_variable vars[] = {
183+
{"mysum", my_sum, TE_FUNCTION2} /* TE_FUNCTION2 used because my_sum takes two arguments. */
184+
};
185+
186+
te_expr *n = te_compile("mysum(5, 6)", vars, 1, 0);
187+
188+
```
171189

172190

173191
##How it works
@@ -219,7 +237,12 @@ TinyExpr parses the following grammar:
219237
<term> = <factor> {("*" | "/" | "%") <factor>}
220238
<factor> = <power> {"^" <power>}
221239
<power> = {("-" | "+")} <base>
222-
<base> = <constant> | <variable> | <function-0> {"(" ")"} | <function-1> <power> | <function-2> "(" <expr> "," <expr> ")" | "(" <list> ")"
240+
<base> = <constant>
241+
| <variable>
242+
| <function-0> {"(" ")"}
243+
| <function-1> <power>
244+
| <function-X> "(" <expr> {"," <expr>} ")"
245+
| "(" <list> ")"
223246

224247
In addition, whitespace between tokens is ignored.
225248

example3.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "tinyexpr.h"
2+
#include <stdio.h>
3+
4+
5+
/* An example of calling a C function. */
6+
double my_sum(double a, double b) {
7+
printf("Called C function with %f and %f.\n", a, b);
8+
return a + b;
9+
}
10+
11+
12+
int main(int argc, char *argv[])
13+
{
14+
te_variable vars[] = {
15+
{"mysum", my_sum, TE_FUNCTION2}
16+
};
17+
18+
const char *expression = "mysum(5, 6)";
19+
printf("Evaluating:\n\t%s\n", expression);
20+
21+
int err;
22+
te_expr *n = te_compile(expression, vars, 1, &err);
23+
24+
if (n) {
25+
const double r = te_eval(n);
26+
printf("Result:\n\t%f\n", r);
27+
te_free(n);
28+
} else {
29+
/* Show the user where the error is at. */
30+
printf("\t%*s^\nError near here", err-1, "");
31+
}
32+
33+
34+
return 0;
35+
}

test.c

+1
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ void test_optimize() {
495495
{"5+5", 10},
496496
{"pow(2,2)", 4},
497497
{"sqrt 100", 10},
498+
{"pi * 2", 6.2832},
498499
};
499500

500501
int i;

0 commit comments

Comments
 (0)