1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
using Utils;
using GLib;
namespace VQDR.Expression {
public abstract class Token : GLib.Object {
//public static int32 VALUES_OUTPUT_PRECISION_FACTOR = (int)Math.pow(10, VALUES_OUTPUT_PRECISION_DIGITS);
/** Max size of a single token */
public const int32 MAX_TOKEN_STRING_LENGTH = 64;
/** Max size of an expression */
public const int32 MAX_TOTAL_STRING_LENGTH = 200;
/** Max iteration number for a token (function) */
public const int32 MAX_TOKEN_ITERATIONS = 500;
/** Max iteration number for the expression */
public const int32 MAX_TOTAL_ITERATIONS = 5000;
/**
* Operator precedence and associativity
*
* higher number is higher priority, and is to be done befroe those
* with lower number.
*/
public enum Prio {
/** invalid case. Prevents null-trap. */
INVALID = 0,
/** Priority for assignment "=" operator */
ASSIGNMENT = 1,
/** Priority for conditional OR "||" operator */
CONDITIONAL_OR,
/** Priority for conditional AND "&&" operator */
CONDITIONAL_AND,
/** Priority for equality "==" and "!=" operators */
EQUALITY,
/** Priority for comparison ">", "<", ">=", etc operators */
COMPARISON,
/** Priority for addictive "+" and "-" operators */
ADDICTIVE,
/** Priority for multiplicative "*" and "/" operators */
MULTIPLICATIVE,
/** Priority for unary "+" and "-" and "!" operators */
UNARY,
/** Priority for label assignment ":" operator */
LABEL,
/** Priority for dice "d" operator */
DICE,
/** Priority for functions */
FUNCTION,
/** Priority for values */
VALUE,
/** The number of values. */
_NUM_VAL;
/** get the name of the priority */
public string to_string () {
// We have to add 1 as we have an invalid case at zero.
static_assert (_NUM_VAL == 12 + 1);
switch (this) {
case ASSIGNMENT:
return "ASSIGNMENT";
case CONDITIONAL_OR:
return "CONDITIONAL_OR";
case CONDITIONAL_AND:
return "CONDITIONAL_AND";
case EQUALITY:
return "EQUALITY";
case COMPARISON:
return "COMPARISON";
case ADDICTIVE:
return "ADDICTIVE";
case MULTIPLICATIVE:
return "MULTIPLICATIVE";
case UNARY:
return "UNARY";
case LABEL:
return "LABEL";
case DICE:
return "DICE";
case FUNCTION:
return "FUNCTION";
case VALUE:
return "VALUE";
default:
assert_not_reached ();
}
}
public Prio from_string (string name) {
// We have to add 1 as we have an invalid case at zero.
static_assert (_NUM_VAL == 12 + 1);
var collected = name.up ();
switch (collected) {
case "ASSIGMENT":
return ASSIGNMENT;
case "CONDITIONAL_OR":
return CONDITIONAL_OR;
case "CONDITIONAL_AND":
return CONDITIONAL_AND;
case "EQUALITY":
return EQUALITY;
case "COMPARISON":
return COMPARISON;
case "ADDICTIVE":
return ADDICTIVE;
case "MULTIPLICATIVE":
return MULTIPLICATIVE;
case "UNARY":
return UNARY;
case "LABEL":
return LABEL;
case "DICE":
return DICE;
case "FUNCTON":
return FUNCTION;
case "VALUE":
return VALUE;
default:
assert_not_reached ();
}
}
}
/** Generation to use to get the root with @link parent */
protected const int32 ROOT_GENERATION = int.MAX;
/* ********************************************************************** */
/**
* tells whether the token is right associative or not.
*/
public bool right_assosiative {get;set; default = false;}
/** The parent token of this token*/
protected unowned Token? parent {protected get; protected set;}
public virtual Prio priority {public get; protected construct set;}
/** Starting position of the token in the expression */
protected int32 position;
private int internal_next_child_num;
/** The index of the next child */
public int next_child_num {get {return internal_next_child_num;}
protected set{internal_next_child_num = value;}}
/**
* The optional that this child represents.
*
*
*/
public int32 optional_num_child {public get; protected construct set;}
/** The mandatory number of child this token can have */
public int32 mandatory_num_child {public get; protected construct set;}
/**
* The maximum number of chidren
*
* This is calculated at runtime as
* ''optional_num_child + mandatory_num_child''.
*
* Can not be set.
*/
public int32 max_num_child {get {
return optional_num_child + mandatory_num_child;
}}
/** all children of this token */
private (unowned Token?)[] children;
/*
* These values should have a protected setter, but I could not get it to
* work. So we will have to live with this.
*/
public FastNumber result_value;
public FastNumber result_max_value;
public FastNumber result_min_value;
public string result_string {public get; protected set; default = "";}
construct {
children = new Token[max_num_child];
next_child_num = 0;
result_value = FastNumber ();
result_max_value = FastNumber ();
result_min_value = FastNumber ();
}
protected Token (int32 position) {
this.position = position;
}
/**
* Reorders result_min_value and result_max_value, so that
* result_max_value is the bigger of the two, and
* result_min_value is the smaller.
*/
protected void reorder_max_min_values () {
if (result_max_value.compare (result_min_value) <= 0) {
FastNumber tmp = result_max_value;
result_max_value = result_min_value;
result_min_value = tmp;
}
}
/**
* Get a child token to this token.
*
* Child positions are between 1 and max_num_child.
*
* and index of 0 is illegal.
*/
public unowned Token? get_child (int32 index) requires (index > 0 && index <= max_num_child) {
return children[index -1 ];
}
public void set_next_child (Token child) throws ArgError {
next_child_num++;
set_child (next_child_num, child);
}
/**
* Set a child token to this this token.
*
* Child positions are between 1 and max_num_child.
*
* and index of 0 is illegal.
*/
public void set_child (int32 index, Token? child) requires (index > 0 && index <= max_num_child) {
children[index - 1] = child;
if (children[index - 1] != null) {
children[index -1].parent = this;
}
}
/**
* is functionally equal to get_child (1);
*/
public Token get_left_child () {
return get_child (1);
}
/**
* is functionally equal to get_child (2);
*/
public Token get_right_child () {
return get_child (2);
}
/**
* is functionally equal to set_child (1, token);
*/
public void set_left_child (Token token) {
set_child (1, token);
}
/**
* is functionally equal to set_child (2, token);
*/
public void set_right_child (Token token) {
set_child (2, token);
}
public void evaluate (Context instance) throws GLib.Error {
evaluate_self (instance);
}
/**
* Evalutates current token tree.
*
* This Method must evaluate the token subtree, and assign proper values to
* the following properties:
*
* * result_value
* * result_max_value
* * result_min_value
* * result_string
*
* @param instance The context to be used valfor this tree.
* @throws GLib.Error an error if an error has orrured in the evaluation of the tree.
*/
protected abstract void evaluate_self (Context instance) throws GLib.Error;
}
}
|