Skip to content

Commit dd4cb0f

Browse files
committed
Implementation of keywords, values and conditions
1 parent 4a4fd3a commit dd4cb0f

18 files changed

+858
-273
lines changed

ecr/command.h

Lines changed: 140 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,28 @@
22
class Command {
33

44
private:
5-
int line; // the number of items
5+
int line; // the number of items
66
Functions* functions;
77
ElementArray* elements = nullptr; // an array of Element objects
88
CoreValues* coreValues;
9+
CoreConditions* coreConditions;
910
int* noDomainValueMap;
1011
KeywordArray* noDomainValueTypes; // an array of no-domain value types
1112
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+
}
1227

1328
///////////////////////////////////////////////////////////////////////
1429
// Get item codes from an {a}:{b} pair
@@ -26,7 +41,7 @@ class Command {
2641
Text* right = element->from(colon + 1);
2742
Text* retval = new Text(select ? right : left);
2843
if (text) {
29-
retval = getKeyArray()->get(atoi(retval->getText()));
44+
retval = getKey(retval->getText());
3045
}
3146
delete left;
3247
delete right;
@@ -103,6 +118,12 @@ class Command {
103118
coreValues = v;
104119
}
105120

121+
///////////////////////////////////////////////////////////////////////
122+
// Set the core conditions
123+
void setCoreConditions(CoreConditions* c) {
124+
coreConditions = c;
125+
}
126+
106127
///////////////////////////////////////////////////////////////////////
107128
// Add an element. This goes into the linked list.
108129
void add(Element* element) {
@@ -122,13 +143,19 @@ class Command {
122143
Symbol* symbol = getSymbol(key);
123144
return symbol->getValue();
124145
}
146+
147+
///////////////////////////////////////////////////////////////////////
148+
// Get the line number
149+
const char* getLineNumber() {
150+
return elements->get(0)->getElement()->getText();
151+
}
125152

126153
///////////////////////////////////////////////////////////////////////
127154
// Add a no-domain value type
128155
void addNoDomainType(const char* name) {
129156
int size = getKeyArray()->getSize();
130157
for (int n = 0; n < size; n++) {
131-
if (getKeyArray()->get(n)->is(name)) {
158+
if (getKey(n)->is(name)) {
132159
// printf("Adding %s at position %d pointing to %d\n", name, n, valueIndex);
133160
Keyword* keyword = new Keyword();
134161
keyword->setName(new Text(name));
@@ -153,6 +180,12 @@ class Command {
153180
noDomainValueTypes->flatten();
154181
}
155182

183+
///////////////////////////////////////////////////////////////////////
184+
// Get the named parameter
185+
Text* getParameter(const char* key) {
186+
return functions->getValueProperty(elements, key);
187+
}
188+
156189
///////////////////////////////////////////////////////////////////////
157190
// Get the runtime value of a value element
158191
RuntimeValue* getRuntimeValue(ElementArray* value) {
@@ -170,7 +203,7 @@ class Command {
170203
int colon = el->positionOf(':');
171204
if (colon > 0) {
172205
Text* left = el->left(colon);
173-
if (getKeyArray()->get(atoi(left->getText()))->is("value")) {
206+
if (getKey(left->getText())->is("value")) {
174207
delete left;
175208
ElementArray* values = el->getValue();
176209
int size = values->getSize();
@@ -225,12 +258,11 @@ class Command {
225258
int colon = element->positionOf(':');
226259
if (colon > 0) {
227260
Text* left = element->left(colon);
228-
if (getKeyArray()->get(atoi(left->getText()))->is(name)) {
261+
if (getKey(left->getText())->is(name)) {
229262
// Verify that the right-hand element is an open brace
230263
Text* right = element->from(colon + 1);
231264
if (right->is("{")) {
232265
delete right;
233-
// print("Process an inner value\n");
234266
return getRuntimeValue(element->getValue());
235267
} else {
236268
printf("Item %d of command; expecting '{' but got %s:\n", n, right->getText());
@@ -244,6 +276,88 @@ class Command {
244276
return nullptr;
245277
}
246278

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+
247361
///////////////////////////////////////////////////////////////////////
248362
// Find the code for a named value property
249363
const char* getCommandPropertyCode(const char* key) {
@@ -264,7 +378,7 @@ class Command {
264378
Text* getCommandProperty(const char* key) {
265379
int val = atoi(getCommandPropertyCode(key));
266380
if (val >= 0) {
267-
return getKeyArray()->get(val);
381+
return getKey(val);
268382
}
269383
return nullptr;
270384
}
@@ -286,25 +400,25 @@ class Command {
286400
if (value == nullptr) {
287401
return nullptr;
288402
}
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;
308422
}
309423

310424
///////////////////////////////////////////////////////////////////////

ecr/condition.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Condition {
2+
3+
private:
4+
5+
const char* type;
6+
bool negate = false;
7+
RuntimeValueArray* values;
8+
9+
public:
10+
11+
void setType(const char* type) {
12+
this->type = type;
13+
}
14+
15+
const char* getType() {
16+
return type;
17+
}
18+
19+
void setNegate(bool negate) {
20+
this->negate = negate;
21+
}
22+
23+
void addValue(RuntimeValue* value) {
24+
values->add(value);
25+
}
26+
27+
RuntimeValue* getValue(int index) {
28+
return values->get(index);
29+
}
30+
31+
void flatten() {
32+
values->flatten();
33+
}
34+
35+
void dump() {
36+
values->flatten();
37+
print("%s: ", type);
38+
for (int n = 0; n < values->getSize(); n++) {
39+
print("%s ", values->get(n)->getTextValue());
40+
}
41+
print("\n");
42+
}
43+
44+
Condition() {
45+
values = new RuntimeValueArray();
46+
}
47+
48+
~Condition() {
49+
delete values;
50+
}
51+
};
52+

ecr/core-conditions.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Conditions for the 'core' domain
2+
class CoreConditions {
3+
4+
private:
5+
6+
enum coreIndices {
7+
LESS,
8+
GREATER
9+
};
10+
11+
int index = 0;
12+
int* map;
13+
TextArray* keyArray;
14+
KeywordArray* keywords;
15+
Text* domain = new Text("core");
16+
17+
///////////////////////////////////////////////////////////////////////
18+
// Add a keyword. Only add keywords that are found in the key array.
19+
// The map contains the index of the variable (and thus its handler)
20+
// against the index of its name in the key array.
21+
void add(const char* name) {
22+
int size = keyArray->getSize();
23+
for (int n = 0; n < size; n++) {
24+
if (keyArray->get(n)->is(name)) {
25+
// printf("Adding %s at position %d pointing to %d\n", name, n, index);
26+
Keyword* keyword = new Keyword();
27+
keyword->setName(new Text(name));
28+
keyword->setDomain(domain);
29+
keyword->setIndex(n);
30+
map[n] = index;
31+
keywords->add(keyword);
32+
}
33+
}
34+
index++;
35+
}
36+
37+
public:
38+
39+
void info() {
40+
print("This is the list of keywords defined for the 'core' package\n");
41+
for (int n = 0; n < keywords->getSize(); n++) {
42+
Keyword* k = (Keyword*)keywords->get(n);
43+
print("%s\n", k->getName()->getText());
44+
}
45+
print("-----------------\n");
46+
};
47+
48+
///////////////////////////////////////////////////////////////////////
49+
// Get the keyword array
50+
KeywordArray* getKeywords() {
51+
return keywords;
52+
}
53+
54+
///////////////////////////////////////////////////////////////////////
55+
// Run a condition. All necessary information is passed in
56+
bool run(Condition* condition, Functions* functions) {
57+
RuntimeValue* value1;
58+
RuntimeValue* value2;
59+
const char* type = condition->getType();
60+
if (strcmp(type, "less") == 0) {
61+
value1 = condition->getValue(0);
62+
value2 = condition->getValue(1);
63+
return (value1->getIntValue() < value2->getIntValue());
64+
}
65+
return false;
66+
}
67+
68+
///////////////////////////////////////////////////////////////////////
69+
// Constructor
70+
CoreConditions(TextArray* array) {
71+
keyArray = array;
72+
map = new int[array->getSize()];
73+
keywords = new KeywordArray();
74+
add("symbol");
75+
add("cat");
76+
keywords->flatten();
77+
}
78+
79+
///////////////////////////////////////////////////////////////////////
80+
// Destructor
81+
~CoreConditions() {
82+
delete domain;
83+
print("CoreConditions: Destructor executed\n");
84+
}
85+
};

0 commit comments

Comments
 (0)