Added ability for dynamic functions.

This commit is contained in:
Lewis Van Winkle
2016-02-23 17:13:11 -06:00
parent 52a4995d6c
commit d9138d4bff
3 changed files with 263 additions and 197 deletions

117
test.c
View File

@@ -33,6 +33,14 @@ typedef struct {
} test_case; } test_case;
void test_results() {
test_case cases[] = { test_case cases[] = {
{"1", 1}, {"1", 1},
{"(1)", 1}, {"(1)", 1},
@@ -127,32 +135,6 @@ test_case cases[] = {
}; };
test_case errors[] = {
{"", 1},
{"1+", 2},
{"1)", 2},
{"(1", 2},
{"1**1", 3},
{"1*2(+4", 4},
{"1*2(1+4", 4},
{"a+5", 1},
{"A+5", 1},
{"Aa+5", 1},
{"1^^5", 3},
{"1**5", 3},
{"sin(cos5", 8},
};
const char *nans[] = {
"0/0",
"1%0",
"1%(1%0)",
"(1%0)%1",
};
void test_results() {
int i; int i;
for (i = 0; i < sizeof(cases) / sizeof(test_case); ++i) { for (i = 0; i < sizeof(cases) / sizeof(test_case); ++i) {
const char *expr = cases[i].expr; const char *expr = cases[i].expr;
@@ -171,6 +153,25 @@ void test_results() {
void test_syntax() { void test_syntax() {
test_case errors[] = {
{"", 1},
{"1+", 2},
{"1)", 2},
{"(1", 2},
{"1**1", 3},
{"1*2(+4", 4},
{"1*2(1+4", 4},
{"a+5", 1},
{"A+5", 1},
{"Aa+5", 1},
{"1^^5", 3},
{"1**5", 3},
{"sin(cos5", 8},
};
int i; int i;
for (i = 0; i < sizeof(errors) / sizeof(test_case); ++i) { for (i = 0; i < sizeof(errors) / sizeof(test_case); ++i) {
const char *expr = errors[i].expr; const char *expr = errors[i].expr;
@@ -196,6 +197,14 @@ void test_syntax() {
void test_nans() { void test_nans() {
const char *nans[] = {
"0/0",
"1%0",
"1%(1%0)",
"(1%0)%1",
};
int i; int i;
for (i = 0; i < sizeof(nans) / sizeof(const char *); ++i) { for (i = 0; i < sizeof(nans) / sizeof(const char *); ++i) {
const char *expr = nans[i]; const char *expr = nans[i];
@@ -317,6 +326,61 @@ void test_functions() {
} }
double test0() {
return 6;
}
double test1(double a) {
return a * 2;
}
double test2(double a, double b) {
return a + b;
}
void test_dynamic() {
double x, f;
te_variable lookup[] = {
{"x", &x},
{"f", &f},
{"test0", test0, TE_FUNCTION0},
{"test1", test1, TE_FUNCTION1},
{"test2", test2, TE_FUNCTION2},
};
test_case cases[] = {
{"x", 2},
{"f+x", 7},
{"x+x", 4},
{"x+f", 7},
{"f+f", 10},
{"f+test0", 11},
{"test0+test0", 12},
{"test1 test0", 12},
{"test1 f", 10},
{"test1 x", 4},
{"test2 (test0, x)", 8},
};
x = 2;
f = 5;
int i;
for (i = 0; i < sizeof(cases) / sizeof(test_case); ++i) {
const char *expr = cases[i].expr;
const double answer = cases[i].answer;
int err;
te_expr *ex = te_compile(expr, lookup, sizeof(lookup), &err);
lok(ex);
lfequal(te_eval(ex), answer);
te_free(ex);
}
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
lrun("Results", test_results); lrun("Results", test_results);
@@ -324,6 +388,7 @@ int main(int argc, char *argv[])
lrun("NaNs", test_nans); lrun("NaNs", test_nans);
lrun("Variables", test_variables); lrun("Variables", test_variables);
lrun("Functions", test_functions); lrun("Functions", test_functions);
lrun("Dynamic", test_dynamic);
lresults(); lresults();
return lfails != 0; return lfails != 0;

View File

@@ -29,15 +29,16 @@
#include <stdio.h> #include <stdio.h>
enum {TOK_NULL, TOK_END, TOK_OPEN, TOK_CLOSE, TOK_NUMBER, TOK_INFIX, TOK_FUNCTION1, TOK_FUNCTION2, TOK_VARIABLE, TOK_SEP, TOK_ERROR}; enum {TOK_NULL, TOK_END, TOK_OPEN, TOK_CLOSE, TOK_NUMBER, TOK_INFIX, TOK_FUNCTION0, TOK_FUNCTION1, TOK_FUNCTION2, TOK_VARIABLE, TOK_SEP, TOK_ERROR};
enum {TE_CONSTANT = -2};
typedef struct { typedef struct state {
const char *start; const char *start;
const char *next; const char *next;
int type; int type;
union {double value; te_fun1 f1; te_fun2 f2; const double *var;}; union {double value; te_fun0 f0; te_fun1 f1; te_fun2 f2; const double *bound;};
const te_variable *lookup; const te_variable *lookup;
int lookup_len; int lookup_len;
@@ -46,8 +47,9 @@ typedef struct {
static te_expr *new_expr(te_expr *l, te_expr *r) { static te_expr *new_expr(int type, te_expr *l, te_expr *r) {
te_expr *ret = malloc(sizeof(te_expr)); te_expr *ret = malloc(sizeof(te_expr));
ret->type = type;
ret->left = l; ret->left = l;
ret->right = r; ret->right = r;
ret->bound = 0; ret->bound = 0;
@@ -63,45 +65,38 @@ void te_free(te_expr *n) {
} }
typedef struct {
const char *name;
union {const void *v; double *value; te_fun1 f1; te_fun2 f2;};
int arity;
} builtin;
static const double pi = 3.14159265358979323846; static const double pi = 3.14159265358979323846;
static const double e = 2.71828182845904523536; static const double e = 2.71828182845904523536;
static const builtin functions[] = { static const te_variable functions[] = {
/* must be in alphabetical order */ /* must be in alphabetical order */
{"abs", {fabs}, 1}, {"abs", fabs, TE_FUNCTION1},
{"acos", {acos}, 1}, {"acos", acos, TE_FUNCTION1},
{"asin", {asin}, 1}, {"asin", asin, TE_FUNCTION1},
{"atan", {atan}, 1}, {"atan", atan, TE_FUNCTION1},
{"atan2", {atan2}, 2}, {"atan2", atan2, TE_FUNCTION2},
{"ceil", {ceil}, 1}, {"ceil", ceil, TE_FUNCTION1},
{"cos", {cos}, 1}, {"cos", cos, TE_FUNCTION1},
{"cosh", {cosh}, 1}, {"cosh", cosh, TE_FUNCTION1},
{"e", {&e}, 0}, {"e", &e, TE_VARIABLE},
{"exp", {exp}, 1}, {"exp", exp, TE_FUNCTION1},
{"floor", {floor}, 1}, {"floor", floor, TE_FUNCTION1},
{"ln", {log}, 1}, {"ln", log, TE_FUNCTION1},
{"log", {log10}, 1}, {"log", log10, TE_FUNCTION1},
{"pi", {&pi}, 0}, {"pi", &pi, TE_VARIABLE},
{"pow", {pow}, 2}, {"pow", pow, TE_FUNCTION2},
{"sin", {sin}, 1}, {"sin", sin, TE_FUNCTION1},
{"sinh", {sinh}, 1}, {"sinh", sinh, TE_FUNCTION1},
{"sqrt", {sqrt}, 1}, {"sqrt", sqrt, TE_FUNCTION1},
{"tan", {tan}, 1}, {"tan", tan, TE_FUNCTION1},
{"tanh", {tanh}, 1}, {"tanh", tanh, TE_FUNCTION1},
{0} {0}
}; };
static const builtin *find_function(const char *name, int len) { static const te_variable *find_function(const char *name, int len) {
int imin = 0; int imin = 0;
int imax = sizeof(functions) / sizeof(builtin) - 2; int imax = sizeof(functions) / sizeof(te_variable) - 2;
/*Binary search.*/ /*Binary search.*/
while (imax >= imin) { while (imax >= imin) {
@@ -121,12 +116,12 @@ static const builtin *find_function(const char *name, int len) {
} }
static const double *find_var(const state *s, const char *name, int len) { static const te_variable *find_var(const state *s, const char *name, int len) {
int i; int i;
if (!s->lookup) return 0; if (!s->lookup) return 0;
for (i = 0; i < s->lookup_len; ++i) { for (i = 0; i < s->lookup_len; ++i) {
if (strncmp(name, s->lookup[i].name, len) == 0 && s->lookup[i].name[len] == '\0') { if (strncmp(name, s->lookup[i].name, len) == 0 && s->lookup[i].name[len] == '\0') {
return s->lookup[i].value; return s->lookup + i;
} }
} }
return 0; return 0;
@@ -163,29 +158,24 @@ void next_token(state *s) {
start = s->next; start = s->next;
while ((s->next[0] >= 'a' && s->next[0] <= 'z') || (s->next[0] >= '0' && s->next[0] <= '9')) s->next++; while ((s->next[0] >= 'a' && s->next[0] <= 'z') || (s->next[0] >= '0' && s->next[0] <= '9')) s->next++;
const double *var = find_var(s, start, s->next - start); const te_variable *var = find_var(s, start, s->next - start);
if (var) { if (!var) var = find_function(start, s->next - start);
if (!var) {
s->type = TOK_ERROR;
} else {
if (var->type == TE_VARIABLE) {
s->type = TOK_VARIABLE; s->type = TOK_VARIABLE;
s->var = var; s->bound = var->value;
} else { } else if (var->type == TE_FUNCTION0) {
if (s->next - start > 15) { s->type = TOK_FUNCTION0;
s->type = TOK_ERROR; s->f0 = var->value;
} else { } else if (var->type == TE_FUNCTION1) {
const builtin *f = find_function(start, s->next - start);
if (!f) {
s->type = TOK_ERROR;
} else {
if (f->arity == 0) {
s->type = TOK_NUMBER;
s->value = *f->value;
} else if (f->arity == 1) {
s->type = TOK_FUNCTION1; s->type = TOK_FUNCTION1;
s->f1 = f->f1; s->f1 = var->value;
} else if (f->arity == 2) { } else if (var->type == TE_FUNCTION2) {
s->type = TOK_FUNCTION2; s->type = TOK_FUNCTION2;
s->f2 = f->f2; s->f2 = var->value;
}
}
} }
} }
@@ -220,26 +210,32 @@ static te_expr *base(state *s) {
switch (s->type) { switch (s->type) {
case TOK_NUMBER: case TOK_NUMBER:
ret = new_expr(0, 0); ret = new_expr(TE_CONSTANT, 0, 0);
ret->value = s->value; ret->value = s->value;
next_token(s); next_token(s);
break; break;
case TOK_VARIABLE: case TOK_VARIABLE:
ret = new_expr(0, 0); ret = new_expr(TE_VARIABLE, 0, 0);
ret->bound = s->var; ret->bound = s->bound;
next_token(s);
break;
case TOK_FUNCTION0:
ret = new_expr(TE_FUNCTION0, 0, 0);
ret->f0 = s->f0;
next_token(s); next_token(s);
break; break;
case TOK_FUNCTION1: case TOK_FUNCTION1:
ret = new_expr(0, 0); ret = new_expr(TE_FUNCTION1, 0, 0);
ret->f1 = s->f1; ret->f1 = s->f1;
next_token(s); next_token(s);
ret->left = power(s); ret->left = power(s);
break; break;
case TOK_FUNCTION2: case TOK_FUNCTION2:
ret = new_expr(0, 0); ret = new_expr(TE_FUNCTION2, 0, 0);
ret->f2 = s->f2; ret->f2 = s->f2;
next_token(s); next_token(s);
@@ -275,7 +271,7 @@ static te_expr *base(state *s) {
break; break;
default: default:
ret = new_expr(0, 0); ret = new_expr(0, 0, 0);
s->type = TOK_ERROR; s->type = TOK_ERROR;
ret->value = 0.0/0.0; ret->value = 0.0/0.0;
break; break;
@@ -298,7 +294,7 @@ static te_expr *power(state *s) {
if (sign == 1) { if (sign == 1) {
ret = base(s); ret = base(s);
} else { } else {
ret = new_expr(base(s), 0); ret = new_expr(TE_FUNCTION1, base(s), 0);
ret->f1 = negate; ret->f1 = negate;
} }
@@ -313,7 +309,7 @@ static te_expr *factor(state *s) {
while (s->type == TOK_INFIX && (s->f2 == pow)) { while (s->type == TOK_INFIX && (s->f2 == pow)) {
te_fun2 t = s->f2; te_fun2 t = s->f2;
next_token(s); next_token(s);
ret = new_expr(ret, power(s)); ret = new_expr(TE_FUNCTION2, ret, power(s));
ret->f2 = t; ret->f2 = t;
} }
@@ -328,7 +324,7 @@ static te_expr *term(state *s) {
while (s->type == TOK_INFIX && (s->f2 == mul || s->f2 == divide || s->f2 == fmod)) { while (s->type == TOK_INFIX && (s->f2 == mul || s->f2 == divide || s->f2 == fmod)) {
te_fun2 t = s->f2; te_fun2 t = s->f2;
next_token(s); next_token(s);
ret = new_expr(ret, factor(s)); ret = new_expr(TE_FUNCTION2, ret, factor(s));
ret->f2 = t; ret->f2 = t;
} }
@@ -343,7 +339,7 @@ static te_expr *expr(state *s) {
while (s->type == TOK_INFIX && (s->f2 == add || s->f2 == sub)) { while (s->type == TOK_INFIX && (s->f2 == add || s->f2 == sub)) {
te_fun2 t = s->f2; te_fun2 t = s->f2;
next_token(s); next_token(s);
ret = new_expr(ret, term(s)); ret = new_expr(TE_FUNCTION2, ret, term(s));
ret->f2 = t; ret->f2 = t;
} }
@@ -357,7 +353,7 @@ static te_expr *list(state *s) {
while (s->type == TOK_SEP) { while (s->type == TOK_SEP) {
next_token(s); next_token(s);
ret = new_expr(ret, term(s)); ret = new_expr(TE_FUNCTION2, ret, term(s));
ret->f2 = comma; ret->f2 = comma;
} }
@@ -366,23 +362,21 @@ static te_expr *list(state *s) {
double te_eval(const te_expr *n) { double te_eval(const te_expr *n) {
double ret; switch (n->type) {
case TE_CONSTANT: return n->value;
if (n->bound) { case TE_VARIABLE: return *n->bound;
ret = *n->bound; case TE_FUNCTION0: return n->f0();
} else if (n->left == 0 && n->right == 0) { case TE_FUNCTION1: return n->f1(te_eval(n->left));
ret = n->value; case TE_FUNCTION2: return n->f2(te_eval(n->left), te_eval(n->right));
} else if (n->left && n->right == 0) { default: return 0.0/0.0;
ret = n->f1(te_eval(n->left));
} else {
ret = n->f2(te_eval(n->left), te_eval(n->right));
} }
return ret;
} }
static void optimize(te_expr *n) { static void optimize(te_expr *n) {
/* Evaluates as much as possible. */ /* Evaluates as much as possible. */
/*
if (n->bound) return; if (n->bound) return;
if (n->left) optimize(n->left); if (n->left) optimize(n->left);
@@ -405,6 +399,7 @@ static void optimize(te_expr *n) {
n->value = r; n->value = r;
} }
} }
*/
} }

View File

@@ -31,20 +31,26 @@ extern "C" {
#endif #endif
typedef double (*te_fun0)(void);
typedef double (*te_fun1)(double); typedef double (*te_fun1)(double);
typedef double (*te_fun2)(double, double); typedef double (*te_fun2)(double, double);
typedef struct te_expr { typedef struct te_expr {
struct te_expr *left, *right; struct te_expr *left, *right;
union {double value; te_fun1 f1; te_fun2 f2;}; int type;
const double *bound; union {double value; const double *bound; te_fun0 f0; te_fun1 f1; te_fun2 f2;};
} te_expr; } te_expr;
typedef struct {
enum {TE_FUNCTION0 = -1, TE_VARIABLE = 0, TE_FUNCTION1 = 1, TE_FUNCTION2 = 2};
typedef struct te_variable {
const char *name; const char *name;
const double *value; const void *value;
int type;
} te_variable; } te_variable;