Mercurial > hg > dmlib
annotate dmeval.h @ 761:7be7c7c5deaa
Large cleanup, breaking everything. Yay.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 09 May 2013 06:21:36 +0300 |
parents | faf43d8aa841 |
children | 181d659bba39 |
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, | |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
39 OP_CONST, |
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 { | |
68 ID_FUNC, | |
670 | 69 ID_VAR, |
70 ID_CVAR, | |
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 { | |
76 char *name; | |
77 int type; | |
670 | 78 int nargs; |
79 DMValue (*func)(DMValue arg[DM_MAX_ARGS]); | |
80 DMValue *var, cvalue; | |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
81 } DMEvalSymbol; |
0 | 82 |
83 | |
84 typedef struct DMEvalNode | |
85 { | |
693
837ad9dcc348
More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents:
685
diff
changeset
|
86 BOOL ok; |
0 | 87 int op; |
88 DMValue val; | |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
89 DMEvalSymbol *id; |
0 | 90 |
670 | 91 struct DMEvalNode *args[DM_MAX_ARGS]; |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
92 |
0 | 93 struct DMEvalNode *subexpr, *next, *prev; |
94 } DMEvalNode; | |
95 | |
96 | |
97 typedef struct | |
98 { | |
99 BOOL err; | |
100 char *errStr; | |
101 | |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
102 int nsymbols; |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
103 DMEvalSymbol *symbols; |
0 | 104 |
105 int mode, prev, expect; | |
106 } DMEvalContext; | |
107 | |
108 | |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
109 // Evaluation context management |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
110 DMEvalContext * dmEvalContextNew(void); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
111 void dmEvalContextClose(DMEvalContext *ev); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
112 void dmEvalContextClear(DMEvalContext *ev); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
113 |
0 | 114 |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
115 // Symbol management |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
116 DMEvalSymbol * dmEvalContextFindSymbol(DMEvalContext *ev, const char *name); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
117 DMEvalSymbol * dmEvalContextAddVar(DMEvalContext *ev, const char *name, DMValue *var); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
118 DMEvalSymbol * dmEvalContextAddConst(DMEvalContext *ev, const char *name, DMValue value); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
119 DMEvalSymbol * dmEvalContextAddFunc(DMEvalContext *ev, const char *name, DMValue (*func)(DMValue *), int nargs); |
0 | 120 |
121 | |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
122 // Evaluation trees, tokenization, parsing |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
123 int dmEvalParseExpr(DMEvalContext *ev, char *expr, DMEvalNode **plist); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
124 int dmEvalTreeExecute(DMEvalContext *ev, DMEvalNode *tree, DMValue *presult); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
125 void dmEvalTreeFree(DMEvalNode *node); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
126 |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
127 |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
128 void dmEvalPrintOpTree(FILE *out, DMEvalContext *ev, DMEvalNode *node); |
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 |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
131 extern const DMEvalOper dmEvalOpers[OP_NOPERS]; |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
132 |
0 | 133 #endif // DMEVAL_H |