forked from erstan/ceval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceval.h
42 lines (35 loc) · 1.32 KB
/
ceval.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef CEVAL
#define CEVAL
#include "./core/parser.h"
#include "./core/evaluator.h"
// functions accessible from main()
// - double ceval_result(char * inp) returns the result of valid math expression stored as a char array `inp`
// - void ceval_tree(char * inp) prints the parse tree for the input expression `inp`
// - define CEVAL_EPSILON (default value : 1e-2), CEVAL_DELTA (default value : 1e-6) and CEVAL_MAX_DIGITS (default value : 15) manually before the include directive
// - define CEVAL_STOICAL before the #include directive to use the parser/evaluator in stoical (non-complaining) mode. It suppresses all the error messages from [ceval].
// prototypes
double ceval_result(char *);
void ceval_tree(char *);
double ceval_result(char * expr) {
void * tree = ceval_make_tree(expr);
double result = ceval_evaluate_tree(tree);
ceval_delete_tree(tree);
return result;
}
void ceval_tree(char * expr) {
void * tree = ceval_make_tree(expr);
ceval_print_tree(tree);
ceval_delete_tree(tree);
}
#ifdef CEVAL_CXX
// prototypes
double ceval_result(std::string);
void ceval_tree(std::string);
double ceval_result(std::string expr) {
return ceval_result((char * ) expr.c_str());
}
void ceval_tree(std::string expr) {
ceval_tree((char * ) expr.c_str());
}
#endif
#endif