2
2
class Command {
3
3
4
4
private:
5
- int line; // the number of items
5
+ int line; // the number of items
6
6
Functions* functions;
7
7
ElementArray* elements = nullptr ; // an array of Element objects
8
8
CoreValues* coreValues;
9
+ CoreConditions* coreConditions;
9
10
int * noDomainValueMap;
10
11
KeywordArray* noDomainValueTypes; // an array of no-domain value types
11
12
int valueIndex = 0 ; // used while building the nodomain array
13
+
14
+ // /////////////////////////////////////////////////////////////////////
15
+ // Get a key
16
+ Text* getKey (int index) {
17
+ return getKeyArray ()->get (index);
18
+ }
19
+
20
+ Text* getKey (const char * index) {
21
+ return getKey (atoi (index));
22
+ }
23
+
24
+ Text* getKey (Text* index) {
25
+ return getKey (atoi (index->getText ()));
26
+ }
12
27
13
28
// /////////////////////////////////////////////////////////////////////
14
29
// Get item codes from an {a}:{b} pair
@@ -26,7 +41,7 @@ class Command {
26
41
Text* right = element->from (colon + 1 );
27
42
Text* retval = new Text (select ? right : left);
28
43
if (text) {
29
- retval = getKeyArray ()-> get ( atoi ( retval->getText () ));
44
+ retval = getKey ( retval->getText ());
30
45
}
31
46
delete left;
32
47
delete right;
@@ -103,6 +118,12 @@ class Command {
103
118
coreValues = v;
104
119
}
105
120
121
+ // /////////////////////////////////////////////////////////////////////
122
+ // Set the core conditions
123
+ void setCoreConditions (CoreConditions* c) {
124
+ coreConditions = c;
125
+ }
126
+
106
127
// /////////////////////////////////////////////////////////////////////
107
128
// Add an element. This goes into the linked list.
108
129
void add (Element* element) {
@@ -122,13 +143,19 @@ class Command {
122
143
Symbol* symbol = getSymbol (key);
123
144
return symbol->getValue ();
124
145
}
146
+
147
+ // /////////////////////////////////////////////////////////////////////
148
+ // Get the line number
149
+ const char * getLineNumber () {
150
+ return elements->get (0 )->getElement ()->getText ();
151
+ }
125
152
126
153
// /////////////////////////////////////////////////////////////////////
127
154
// Add a no-domain value type
128
155
void addNoDomainType (const char * name) {
129
156
int size = getKeyArray ()->getSize ();
130
157
for (int n = 0 ; n < size; n++) {
131
- if (getKeyArray ()-> get (n)->is (name)) {
158
+ if (getKey (n)->is (name)) {
132
159
// printf("Adding %s at position %d pointing to %d\n", name, n, valueIndex);
133
160
Keyword* keyword = new Keyword ();
134
161
keyword->setName (new Text (name));
@@ -153,6 +180,12 @@ class Command {
153
180
noDomainValueTypes->flatten ();
154
181
}
155
182
183
+ // /////////////////////////////////////////////////////////////////////
184
+ // Get the named parameter
185
+ Text* getParameter (const char * key) {
186
+ return functions->getValueProperty (elements, key);
187
+ }
188
+
156
189
// /////////////////////////////////////////////////////////////////////
157
190
// Get the runtime value of a value element
158
191
RuntimeValue* getRuntimeValue (ElementArray* value) {
@@ -170,7 +203,7 @@ class Command {
170
203
int colon = el->positionOf (' :' );
171
204
if (colon > 0 ) {
172
205
Text* left = el->left (colon);
173
- if (getKeyArray ()-> get ( atoi ( left->getText () ))->is (" value" )) {
206
+ if (getKey ( left->getText ())->is (" value" )) {
174
207
delete left;
175
208
ElementArray* values = el->getValue ();
176
209
int size = values->getSize ();
@@ -225,12 +258,11 @@ class Command {
225
258
int colon = element->positionOf (' :' );
226
259
if (colon > 0 ) {
227
260
Text* left = element->left (colon);
228
- if (getKeyArray ()-> get ( atoi ( left->getText () ))->is (name)) {
261
+ if (getKey ( left->getText ())->is (name)) {
229
262
// Verify that the right-hand element is an open brace
230
263
Text* right = element->from (colon + 1 );
231
264
if (right->is (" {" )) {
232
265
delete right;
233
- // print("Process an inner value\n");
234
266
return getRuntimeValue (element->getValue ());
235
267
} else {
236
268
printf (" Item %d of command; expecting '{' but got %s:\n " , n, right->getText ());
@@ -244,6 +276,88 @@ class Command {
244
276
return nullptr ;
245
277
}
246
278
279
+ // /////////////////////////////////////////////////////////////////////
280
+ // Get a condition
281
+ bool getCondition (ElementArray* value) {
282
+ Text* domain = nullptr ;
283
+ Text* type = nullptr ;
284
+ functions->setElements (value);
285
+ Condition* condition = new Condition ();
286
+ int size = value->getSize ();
287
+ for (int n = 0 ; n < size; n++) {
288
+ Element* element = value->get (n);
289
+ int colon = element->positionOf (' :' );
290
+ Text* left = element->left (colon);
291
+ Text* right = element->from (colon + 1 );
292
+ Text* name = getKey (left->getText ());
293
+ if (right->is (" {" )) {
294
+ condition->addValue (getRuntimeValue (element->getValue ()));
295
+ } else if (name->is (" negate" )) {
296
+ const char * tf = getKey (right->getText ())->getText ();
297
+ if (strcmp (tf, " True" ) == 0 ) {
298
+ condition->setNegate (true );
299
+ }
300
+ else {
301
+ condition->setNegate (false );
302
+ }
303
+ } else if (name->is (" domain" )) {
304
+ domain = getKey (right->copy ());
305
+ } else if (name->is (" type" )) {
306
+ type = getKey (right->copy ());
307
+ } else {
308
+ printf (" Unknown property '%s' at line %s\n " , getKey (left->getText ())->getText (), getLineNumber ());
309
+ exit (1 );
310
+ }
311
+ delete left;
312
+ delete right;
313
+ }
314
+
315
+ if (domain == nullptr ) {
316
+ printf (" No domain specified at line %s\n " , getLineNumber ());
317
+ exit (1 );
318
+ }
319
+ if (type == nullptr ) {
320
+ printf (" No condition type specified at line %s\n " , getLineNumber ());
321
+ exit (1 );
322
+ }
323
+ condition->flatten ();
324
+ condition->setType (type->getText ());
325
+ functions->setElements (value);
326
+ if (domain->is (" core" )) {
327
+ return coreConditions->run (condition, functions);
328
+ };
329
+ return false ;
330
+ }
331
+
332
+ // /////////////////////////////////////////////////////////////////////
333
+ // Get the named runtime condition
334
+ bool getCondition () {
335
+ // Look for this name then process it
336
+ for (int n = 0 ; n < elements->getSize (); n++) {
337
+ Element* element = elements->get (n);
338
+ // Look for a value part
339
+ int colon = element->positionOf (' :' );
340
+ if (colon > 0 ) {
341
+ Text* left = element->left (colon);
342
+ if (getKey (left->getText ())->is (" condition" )) {
343
+ // Verify that the right-hand element is an open brace
344
+ Text* right = element->from (colon + 1 );
345
+ if (right->is (" {" )) {
346
+ delete right;
347
+ // print("Process a condition\n");
348
+ return getCondition (element->getValue ());
349
+ } else {
350
+ printf (" Item %d of command; expecting '{' but got %s:\n " , n, right->getText ());
351
+ dump ();
352
+ exit (1 );
353
+ }
354
+ }
355
+ delete left;
356
+ }
357
+ }
358
+ return false ;
359
+ }
360
+
247
361
// /////////////////////////////////////////////////////////////////////
248
362
// Find the code for a named value property
249
363
const char * getCommandPropertyCode (const char * key) {
@@ -264,7 +378,7 @@ class Command {
264
378
Text* getCommandProperty (const char * key) {
265
379
int val = atoi (getCommandPropertyCode (key));
266
380
if (val >= 0 ) {
267
- return getKeyArray ()-> get (val);
381
+ return getKey (val);
268
382
}
269
383
return nullptr ;
270
384
}
@@ -286,25 +400,25 @@ class Command {
286
400
if (value == nullptr ) {
287
401
return nullptr ;
288
402
}
289
- char * buf ;
290
- switch (value-> getType ()) {
291
- case TEXT_VALUE: {
292
- const char * v = value-> getTextValue ();
293
- int len = strlen (v );
294
- buf = new char [len + 1 ];
295
- strcpy (buf, v);
296
- }
297
- break ;
298
- case INT_VALUE:
299
- buf = new char [12 ];
300
- sprintf (buf, " %d" , value->getIntValue ());
301
- break ;
302
- case BOOL_VALUE:
303
- buf = new char [6 ];
304
- sprintf (buf, " %s" , value->getBoolValue () ? " true" : " false" );
305
- break ;
306
- };
307
- return buf;
403
+ return value-> getTextValue () ;
404
+ // char* buf;
405
+ // switch (value->getType()) {
406
+ // case TEXT_VALUE: {
407
+ // const char* v = value->getTextValue( );
408
+ // buf = new char[strlen(v) + 1];
409
+ // strcpy(buf, v);
410
+ // }
411
+ // break;
412
+ // case INT_VALUE:
413
+ // buf = new char[12];
414
+ // sprintf(buf, "%d", value->getIntValue());
415
+ // break;
416
+ // case BOOL_VALUE:
417
+ // buf = new char[6];
418
+ // sprintf(buf, "%s", value->getBoolValue() ? "true" : "false");
419
+ // break;
420
+ // };
421
+ // return buf;
308
422
}
309
423
310
424
// /////////////////////////////////////////////////////////////////////
0 commit comments