annotate dmeval.h @ 764:181d659bba39

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 09 May 2013 08:28:39 +0300
parents 7be7c7c5deaa
children c68258fd81a1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 #ifndef DMEVAL_H
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 #define DMEVAL_H
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 #include "dmlib.h"
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
5 #include <stdio.h>
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 typedef double DMValue;
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
9 #define DMCONVTYPE (int)
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
10 #define DM_MAX_ARGS 8
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
747
faf43d8aa841 Rename some constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 742
diff changeset
12
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 enum
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 {
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
15 OP_SUB_UNARY,
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
16 OP_BIT_COMPLEMENT,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 OP_ADD,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 OP_SUB,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 OP_MUL,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 OP_DIV,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 OP_MOD,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
747
faf43d8aa841 Rename some constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 742
diff changeset
24 OP_BIT_LSHIFT,
faf43d8aa841 Rename some constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 742
diff changeset
25 OP_BIT_RSHIFT,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26
747
faf43d8aa841 Rename some constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 742
diff changeset
27 OP_BIT_AND,
faf43d8aa841 Rename some constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 742
diff changeset
28 OP_BIT_OR,
faf43d8aa841 Rename some constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 742
diff changeset
29 OP_BIT_XOR,
740
48c48e94d87f More work on the evaluator. Still broken, of course.
Matti Hamalainen <ccr@tnsp.org>
parents: 725
diff changeset
30
48c48e94d87f More work on the evaluator. Still broken, of course.
Matti Hamalainen <ccr@tnsp.org>
parents: 725
diff changeset
31 OP_GT,
48c48e94d87f More work on the evaluator. Still broken, of course.
Matti Hamalainen <ccr@tnsp.org>
parents: 725
diff changeset
32 OP_LT,
48c48e94d87f More work on the evaluator. Still broken, of course.
Matti Hamalainen <ccr@tnsp.org>
parents: 725
diff changeset
33 OP_GT_EQ,
48c48e94d87f More work on the evaluator. Still broken, of course.
Matti Hamalainen <ccr@tnsp.org>
parents: 725
diff changeset
34 OP_LT_EQ,
48c48e94d87f More work on the evaluator. Still broken, of course.
Matti Hamalainen <ccr@tnsp.org>
parents: 725
diff changeset
35
674
a25d30714cfe Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 670
diff changeset
36 // Special ops
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 OP_FUNC,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 OP_VAR,
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
39 OP_VALUE,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 OP_SUBEXPR,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41
674
a25d30714cfe Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 670
diff changeset
42 // Total number of operators
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
43 OP_NOPERS,
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
44
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
45 OP_INVALID
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
46 } DMEvalOperId;
742
aba7dde79a04 Add associativity types enum.
Matti Hamalainen <ccr@tnsp.org>
parents: 740
diff changeset
47
aba7dde79a04 Add associativity types enum.
Matti Hamalainen <ccr@tnsp.org>
parents: 740
diff changeset
48
aba7dde79a04 Add associativity types enum.
Matti Hamalainen <ccr@tnsp.org>
parents: 740
diff changeset
49 enum
aba7dde79a04 Add associativity types enum.
Matti Hamalainen <ccr@tnsp.org>
parents: 740
diff changeset
50 {
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
51 OT_NONE,
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
52
742
aba7dde79a04 Add associativity types enum.
Matti Hamalainen <ccr@tnsp.org>
parents: 740
diff changeset
53 OT_LEFT, // Left-associative
aba7dde79a04 Add associativity types enum.
Matti Hamalainen <ccr@tnsp.org>
parents: 740
diff changeset
54 OT_RIGHT, // Right-associative
aba7dde79a04 Add associativity types enum.
Matti Hamalainen <ccr@tnsp.org>
parents: 740
diff changeset
55 OT_UNARY,
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
56 } DMEvalOperType;
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
57
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
58
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
59 typedef struct
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
60 {
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
61 char *name;
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
62 int type;
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
63 } DMEvalOper;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64
685
f4005431de35 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
65
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 enum
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 {
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
68 SYM_FUNC,
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
69 SYM_VAR,
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
70 SYM_CVAR,
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
71 } DMEvalSymbolType;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 typedef struct
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 {
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
76 char *name; // Name of the symbol
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
77 int type; // Type (SYM_*)
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
78 int nargs; // Number of arguments, if SYM_FUNC
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
79
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
80 DMValue (*func)(DMValue arg[DM_MAX_ARGS]);
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
81
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
82 DMValue *var; // Pointer to variable value if SYM_VAR
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
83 DMValue cvalue; // Const value, if SYM_CVAR
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
84 } DMEvalSymbol;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 typedef struct DMEvalNode
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 {
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
89 int op; // Operator/token type
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
90 DMValue val; // Value, if immediate constant
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
91 DMEvalSymbol *symbol; // Symbol pointer, if function/variable/constvar
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
93 struct DMEvalNode *args[DM_MAX_ARGS]; // Arguments, if function
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
94
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
95 struct DMEvalNode *subexpr, *left, *right, *next, *prev;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 } DMEvalNode;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 typedef struct
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 BOOL err;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 char *errStr;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
104 int nsymbols;
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
105 DMEvalSymbol *symbols;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 int mode, prev, expect;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 } DMEvalContext;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
111 // Evaluation context management
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
112 DMEvalContext * dmEvalContextNew(void);
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
113 void dmEvalContextClose(DMEvalContext *ev);
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
114 void dmEvalContextClear(DMEvalContext *ev);
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
115
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
117 // Symbol management
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
118 DMEvalSymbol * dmEvalContextFindSymbol(DMEvalContext *ev, const char *name);
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
119 DMEvalSymbol * dmEvalContextAddVar(DMEvalContext *ev, const char *name, DMValue *var);
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
120 DMEvalSymbol * dmEvalContextAddConst(DMEvalContext *ev, const char *name, DMValue value);
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
121 DMEvalSymbol * dmEvalContextAddFunc(DMEvalContext *ev, const char *name, DMValue (*func)(DMValue *), int nargs);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
124 // Evaluation trees, tokenization, parsing
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
125 int dmEvalParseExpr(DMEvalContext *ev, char *expr, DMEvalNode **plist);
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
126 int dmEvalTreeExecute(DMEvalContext *ev, DMEvalNode *tree, DMValue *presult);
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
127 void dmEvalTreeFree(DMEvalNode *node);
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
128
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
129
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
130 void dmEvalPrintOpTree(FILE *out, DMEvalContext *ev, DMEvalNode *node);
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
131
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
132
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
133 extern const DMEvalOper dmEvalOpers[OP_NOPERS];
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
134
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 #endif // DMEVAL_H