Mercurial > hg > dmlib
annotate src/dmeval.h @ 1315:7687412f9aef
Fix jssmod sample conversion flags storing .. urgh.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 20 Aug 2017 01:54:54 +0300 |
parents | e06abfde6c39 |
children |
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) |
852 | 10 #define DM_EVAL_MAX_ARGS 4 |
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 |
766
11fba703014f
Some more work. Still broken.
Matti Hamalainen <ccr@tnsp.org>
parents:
765
diff
changeset
|
31 OP_GT_EQ, |
11fba703014f
Some more work. Still broken.
Matti Hamalainen <ccr@tnsp.org>
parents:
765
diff
changeset
|
32 OP_LT_EQ, |
740
48c48e94d87f
More work on the evaluator. Still broken, of course.
Matti Hamalainen <ccr@tnsp.org>
parents:
725
diff
changeset
|
33 OP_GT, |
48c48e94d87f
More work on the evaluator. Still broken, of course.
Matti Hamalainen <ccr@tnsp.org>
parents:
725
diff
changeset
|
34 OP_LT, |
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, |
766
11fba703014f
Some more work. Still broken.
Matti Hamalainen <ccr@tnsp.org>
parents:
765
diff
changeset
|
56 } DMEvalOperAssoc; |
761
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 { |
766
11fba703014f
Some more work. Still broken.
Matti Hamalainen <ccr@tnsp.org>
parents:
765
diff
changeset
|
61 char *name; // Token |
11fba703014f
Some more work. Still broken.
Matti Hamalainen <ccr@tnsp.org>
parents:
765
diff
changeset
|
62 int assoc; // Associativity type (DMEvalOperAssoc) |
11fba703014f
Some more work. Still broken.
Matti Hamalainen <ccr@tnsp.org>
parents:
765
diff
changeset
|
63 BOOL tokenize; // Automatically tokenize? |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
64 } DMEvalOper; |
0 | 65 |
685 | 66 |
0 | 67 enum |
68 { | |
764 | 69 SYM_FUNC, |
70 SYM_VAR, | |
765 | 71 SYM_CONST, |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
72 } DMEvalSymbolType; |
0 | 73 |
74 | |
75 typedef struct | |
76 { | |
764 | 77 char *name; // Name of the symbol |
78 int type; // Type (SYM_*) | |
79 int nargs; // Number of arguments, if SYM_FUNC | |
80 | |
852 | 81 DMValue (*func)(DMValue arg[DM_EVAL_MAX_ARGS]); |
764 | 82 |
83 DMValue *var; // Pointer to variable value if SYM_VAR | |
84 DMValue cvalue; // Const value, if SYM_CVAR | |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
85 } DMEvalSymbol; |
0 | 86 |
87 | |
88 typedef struct DMEvalNode | |
89 { | |
764 | 90 int op; // Operator/token type |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
852
diff
changeset
|
91 DMValue val; // Value, if immediate constant |
764 | 92 DMEvalSymbol *symbol; // Symbol pointer, if function/variable/constvar |
0 | 93 |
852 | 94 struct DMEvalNode *args[DM_EVAL_MAX_ARGS]; // Arguments, if function |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
95 |
764 | 96 struct DMEvalNode *subexpr, *left, *right, *next, *prev; |
0 | 97 } DMEvalNode; |
98 | |
99 | |
100 typedef struct | |
101 { | |
102 BOOL err; | |
103 char *errStr; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
852
diff
changeset
|
104 |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
105 int nsymbols; |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
106 DMEvalSymbol *symbols; |
0 | 107 |
108 int mode, prev, expect; | |
109 } DMEvalContext; | |
110 | |
111 | |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
112 // Evaluation context management |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
113 DMEvalContext * dmEvalContextNew(void); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
114 void dmEvalContextClose(DMEvalContext *ev); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
115 void dmEvalContextClear(DMEvalContext *ev); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
116 |
0 | 117 |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
118 // Symbol management |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
119 DMEvalSymbol * dmEvalContextFindSymbol(DMEvalContext *ev, const char *name); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
120 DMEvalSymbol * dmEvalContextAddVar(DMEvalContext *ev, const char *name, DMValue *var); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
121 DMEvalSymbol * dmEvalContextAddConst(DMEvalContext *ev, const char *name, DMValue value); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
122 DMEvalSymbol * dmEvalContextAddFunc(DMEvalContext *ev, const char *name, DMValue (*func)(DMValue *), int nargs); |
0 | 123 |
124 | |
761
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
125 // Evaluation trees, tokenization, parsing |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
126 int dmEvalParseExpr(DMEvalContext *ev, char *expr, DMEvalNode **plist); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
127 int dmEvalTreeExecute(DMEvalContext *ev, DMEvalNode *tree, DMValue *presult); |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
128 void dmEvalTreeFree(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 void dmEvalPrintOpTree(FILE *out, DMEvalContext *ev, DMEvalNode *node); |
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 |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
134 extern const DMEvalOper dmEvalOpers[OP_NOPERS]; |
7be7c7c5deaa
Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents:
747
diff
changeset
|
135 |
0 | 136 #endif // DMEVAL_H |