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
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)
852
ff13d79c75e0 Rename a const.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
10 #define DM_EVAL_MAX_ARGS 4
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
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
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,
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65
685
f4005431de35 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
66
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 enum
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 {
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
69 SYM_FUNC,
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
70 SYM_VAR,
765
c68258fd81a1 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 764
diff changeset
71 SYM_CONST,
761
7be7c7c5deaa Large cleanup, breaking everything. Yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 747
diff changeset
72 } DMEvalSymbolType;
0
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 typedef struct
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 {
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
77 char *name; // Name of the symbol
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
78 int type; // Type (SYM_*)
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
79 int nargs; // Number of arguments, if SYM_FUNC
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
80
852
ff13d79c75e0 Rename a const.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
81 DMValue (*func)(DMValue arg[DM_EVAL_MAX_ARGS]);
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
82
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
83 DMValue *var; // Pointer to variable value if SYM_VAR
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
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
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 typedef struct DMEvalNode
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 {
764
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
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
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
92 DMEvalSymbol *symbol; // Symbol pointer, if function/variable/constvar
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
852
ff13d79c75e0 Rename a const.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
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
181d659bba39 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 761
diff changeset
96 struct DMEvalNode *subexpr, *left, *right, *next, *prev;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 } DMEvalNode;
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 typedef struct
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 BOOL err;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 int mode, prev, expect;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 } DMEvalContext;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 #endif // DMEVAL_H