Skip to content

Commit

Permalink
Added additional example and info.
Browse files Browse the repository at this point in the history
  • Loading branch information
codeplea committed May 5, 2016
1 parent cf03962 commit 4791163
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 $@

Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -219,7 +237,12 @@ TinyExpr parses the following grammar:
<term> = <factor> {("*" | "/" | "%") <factor>}
<factor> = <power> {"^" <power>}
<power> = {("-" | "+")} <base>
<base> = <constant> | <variable> | <function-0> {"(" ")"} | <function-1> <power> | <function-2> "(" <expr> "," <expr> ")" | "(" <list> ")"
<base> = <constant>
| <variable>
| <function-0> {"(" ")"}
| <function-1> <power>
| <function-X> "(" <expr> {"," <expr>} ")"
| "(" <list> ")"

In addition, whitespace between tokens is ignored.

Expand Down
35 changes: 35 additions & 0 deletions example3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "tinyexpr.h"
#include <stdio.h>


/* 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;
}
1 change: 1 addition & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ void test_optimize() {
{"5+5", 10},
{"pow(2,2)", 4},
{"sqrt 100", 10},
{"pi * 2", 6.2832},
};

int i;
Expand Down

0 comments on commit 4791163

Please sign in to comment.