Mercurial > hg > dmlib
annotate dmeval.h @ 765:c68258fd81a1
Moar work.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 09 May 2013 08:38:52 +0300 |
parents | 181d659bba39 |
children | 11fba703014f |
rev | line source |
---|---|
0 | 1 #ifndef DMEVAL_H |
2 #define DMEVAL_H | |
3 | |
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 | 6 |
7 | |
8 typedef double DMValue; | |
670 | 9 #define DMCONVTYPE (int) |
10 #define DM_MAX_ARGS 8 | |
0 | 11 |
747 | 12 |
0 | 13 enum |
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 | 17 |
18 OP_ADD, | |
19 OP_SUB, | |
20 OP_MUL, | |
21 OP_DIV, | |
22 OP_MOD, | |
23 | |
747 | 24 OP_BIT_LSHIFT, |
25 OP_BIT_RSHIFT, | |
0 | 26 |
747 | 27 OP_BIT_AND, |
28 OP_BIT_OR, | |
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 | 36 // Special ops |
0 | 37 OP_FUNC, |
38 OP_VAR, | |
764 | 39 OP_VALUE, |
0 | 40 OP_SUBEXPR, |
41 | |
674 | 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 | 64 |
685 | 65 |
0 | 66 enum |
67 { | |
764 | 68 SYM_FUNC, |
69 SYM_VAR, | |
765 | 70 SYM_CONST, |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
71 } DMEvalSymbolType; |
0 | 72 |
73 | |
74 typedef struct | |
75 { | |
764 | 76 char *name; // Name of the symbol |
77 int type; // Type (SYM_*) | |
78 int nargs; // Number of arguments, if SYM_FUNC | |
79 | |
670 | 80 DMValue (*func)(DMValue arg[DM_MAX_ARGS]); |
764 | 81 |
82 DMValue *var; // Pointer to variable value if SYM_VAR | |
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 | 85 |
86 | |
87 typedef struct DMEvalNode | |
88 { | |
764 | 89 int op; // Operator/token type |
90 DMValue val; // Value, if immediate constant | |
91 DMEvalSymbol *symbol; // Symbol pointer, if function/variable/constvar | |
0 | 92 |
764 | 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 | 95 struct DMEvalNode *subexpr, *left, *right, *next, *prev; |
0 | 96 } DMEvalNode; |
97 | |
98 | |
99 typedef struct | |
100 { | |
101 BOOL err; | |
102 char *errStr; | |
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 | 106 |
107 int mode, prev, expect; | |
108 } DMEvalContext; | |
109 | |
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 | 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 | 122 |
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 | 135 #endif // DMEVAL_H |