Skip to content

Commit 8a572b0

Browse files
committed
Refactor to pass ElementArrays around
1 parent dd4cb0f commit 8a572b0

24 files changed

+2054
-541
lines changed

ecr/command.h

Lines changed: 168 additions & 92 deletions
Large diffs are not rendered by default.

ecr/core-conditions.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class CoreConditions {
44
private:
55

66
enum coreIndices {
7+
IS,
78
LESS,
89
GREATER
910
};
@@ -57,6 +58,16 @@ class CoreConditions {
5758
RuntimeValue* value1;
5859
RuntimeValue* value2;
5960
const char* type = condition->getType();
61+
if (strcmp(type, "is") == 0) {
62+
value1 = condition->getValue(0);
63+
value2 = condition->getValue(1);
64+
return (value1->getIntValue() == value2->getIntValue());
65+
}
66+
if (strcmp(type, "greater") == 0) {
67+
value1 = condition->getValue(0);
68+
value2 = condition->getValue(1);
69+
return (value1->getIntValue() > value2->getIntValue());
70+
}
6071
if (strcmp(type, "less") == 0) {
6172
value1 = condition->getValue(0);
6273
value2 = condition->getValue(1);
@@ -71,8 +82,6 @@ class CoreConditions {
7182
keyArray = array;
7283
map = new int[array->getSize()];
7384
keywords = new KeywordArray();
74-
add("symbol");
75-
add("cat");
7685
keywords->flatten();
7786
}
7887

ecr/core-keywords.h

Lines changed: 90 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CoreKeywords {
1010
BEGIN,
1111
CLEAR,
1212
CLOSE,
13+
DEBUG,
1314
DECREMENT,
1415
DELETE,
1516
DIVIDE,
@@ -96,40 +97,49 @@ class CoreKeywords {
9697

9798
///////////////////////////////////////////////////////////////////////
9899
// Run a command. All the information needed is in 'runtime'
99-
int run(Runtime* runtime, int code) {
100+
int run(ElementArray* elements, Runtime* runtime, int code) {
100101
RuntimeValue* runtimeValue = nullptr;
101102
RuntimeValue* runtimeValue2 = nullptr;
102103
int next = runtime->getPC() + 1;
103104
int index = map[code];
104105
switch (index)
105106
{
106107
case ADD: {
107-
runtimeValue = runtime->getRuntimeValue("value1");
108-
runtimeValue2 = runtime->getRuntimeValue("value2");
109-
Symbol* target = runtime->getSymbol("target");
108+
Symbol* target = runtime->getSymbol(elements, "target");
109+
runtimeValue = runtime->getRuntimeValue(elements, "value1");
110+
runtimeValue2 = runtime->getRuntimeValue(elements, "value2");
110111
if (runtimeValue2 == nullptr) {
111112
runtimeValue2 = target->getValue();
112113
}
113-
runtimeValue->setIntValue(runtimeValue->getIntValue() + runtimeValue2->getIntValue());
114-
runtime->setSymbolValue("target", runtimeValue->copy());
114+
target->setIntValue(runtimeValue->getIntValue() + runtimeValue2->getIntValue());
115115
return next;
116116
}
117117
// case APPEND:
118118
// case ARRAY:
119119
// case BEGIN:
120120
// case CLEAR:
121121
// case CLOSE:
122-
// case DECREMENT:
122+
case DEBUG: {
123+
Text* type = runtime->getParameter(elements, "type");
124+
singleStep = (type->is("step"));
125+
return next;
126+
}
127+
case DECREMENT: {
128+
Symbol* target = runtime->getSymbol(elements, "target");
129+
runtimeValue = target->getValue();
130+
runtimeValue->setIntValue(runtimeValue->getIntValue() - 1);
131+
runtime->setSymbolValue(elements, "target", runtimeValue->copy());
132+
return next;
133+
}
123134
// case DELETE:
124135
case DIVIDE: {
125-
runtimeValue = runtime->getRuntimeValue("value1");
126-
runtimeValue2 = runtime->getRuntimeValue("value2");
127-
Symbol* target = runtime->getSymbol("target");
136+
Symbol* target = runtime->getSymbol(elements, "target");
137+
runtimeValue = runtime->getRuntimeValue(elements, "value1");
138+
runtimeValue2 = runtime->getRuntimeValue(elements, "value2");
128139
if (runtimeValue2 == nullptr) {
129140
runtimeValue2 = target->getValue();
130141
}
131-
runtimeValue->setIntValue(runtimeValue->getIntValue() / runtimeValue2->getIntValue());
132-
runtime->setSymbolValue("target", runtimeValue->copy());
142+
target->setIntValue(runtimeValue->getIntValue() / runtimeValue2->getIntValue());
133143
return next;
134144
}
135145
// case DUMMY:
@@ -143,83 +153,120 @@ class CoreKeywords {
143153
// case GO:
144154
// case GOTO:
145155
case GOTOPC:
146-
return atoi(runtime->getValueProperty(runtime->getCommand()->getElements(), "goto")->getText());
147-
// case IF:
148-
// case INCREMENT:
149-
// case INDEX:
156+
return atoi(runtime->getValueProperty(elements, "goto")->getText());
157+
case IF: {
158+
return runtime->getCondition(elements) ? next + 1 : next;
159+
}
160+
case INCREMENT: {
161+
Symbol* target = runtime->getSymbol(elements, "target");
162+
target->setIntValue(target->getIntValue() + 1);
163+
return next;
164+
}
165+
case INDEX:{
166+
Symbol* target = runtime->getSymbol(elements, "target");
167+
runtimeValue = runtime->getRuntimeValue(elements, "value");
168+
target->setIndex(runtimeValue->getIntValue());
169+
return next;
170+
}
150171
// case INIT:
151172
case MULTIPLY: {
152-
runtimeValue = runtime->getRuntimeValue("value1");
153-
runtimeValue2 = runtime->getRuntimeValue("value2");
154-
Symbol* target = runtime->getSymbol("target");
173+
Symbol* target = runtime->getSymbol(elements, "target");
174+
runtimeValue = runtime->getRuntimeValue(elements, "value1");
175+
runtimeValue2 = runtime->getRuntimeValue(elements, "value2");
155176
if (runtimeValue2 == nullptr) {
156177
runtimeValue2 = target->getValue();
157178
}
158-
runtimeValue->setIntValue(runtimeValue->getIntValue() * runtimeValue2->getIntValue());
159-
runtime->setSymbolValue("target", runtimeValue->copy());
179+
target->setIntValue(runtimeValue->getIntValue() * runtimeValue2->getIntValue());
180+
return next;
181+
}
182+
case OBJECT:{
183+
Symbol* symbol = new Symbol(runtime->getCommand()->getCommandProperty(elements, "name"));
184+
runtime->getSymbols()->add(symbol);
160185
return next;
161186
}
162-
// case OBJECT:
163187
// case OPEN:
164188
// case POP:
165189
// case POST:
166190
case PRINT:{
167-
const char* buf = runtime->getTextValue("value");
191+
const char* buf = runtime->getTextValue(elements, "value");
168192
if (buf == nullptr) {
169-
print("No 'value' at line %d\n", atoi(runtime->getLineNumber()) + 1);
170-
exit(1);
193+
sprintf(exceptionBuffer, "No 'value' at line %d\n", runtime->getLineNumber(elements) + 1);
194+
throw exceptionBuffer;
171195
}
172196
printf("->%s\n", buf);
173197
return next;
174198
}
175199
// case PUSH:
176200
case PUT:{
177-
runtimeValue = runtime->getRuntimeValue("value");
178-
runtime->setSymbolValue("target", runtimeValue->copy());
179-
return runtime->getPC() + 1;
201+
runtimeValue = runtime->getRuntimeValue(elements, "value");
202+
runtime->setSymbolValue(elements, "target", runtimeValue->copy());
203+
return next;
180204
}
181205
// case READ:
182206
// case REPLACE:
183207
// case RETURN:
184208
// case SCRIPT:
185-
// case SET:
209+
case SET: {
210+
Text* type = runtime->getParameter(elements, "type");
211+
if (type->is("elements")) {
212+
Symbol* symbol = runtime->getCommand()->getSymbol(elements, "name");
213+
runtimeValue = runtime->getRuntimeValue(elements, "value");
214+
symbol->setElements(runtimeValue->getIntValue());
215+
} else if (type->is("property")) {
216+
Symbol* target = runtime->getSymbol(elements, "target");
217+
Symbol* object = runtime->getSymbol(elements, "object");
218+
runtimeValue = runtime->getRuntimeValue(elements, "value1");
219+
if (object == nullptr) {
220+
runtimeValue2 = runtime->getRuntimeValue(elements, "value2");
221+
target->setProperty(runtimeValue, runtimeValue2);
222+
} else {
223+
target->setProperty(runtimeValue, object->getProperties());
224+
}
225+
} else if (type->is("setprop")) {
226+
Symbol* source = runtime->getSymbol(elements, "source");
227+
Symbol* target = runtime->getSymbol(elements, "target");
228+
runtimeValue = runtime->getRuntimeValue(elements, "key");
229+
PropertyArray* properties = source->getProperties();
230+
properties->flatten();
231+
Property* property = properties->getProperty(runtimeValue->getTextValue());
232+
target->setProperties(property->getProperties());
233+
}
234+
return next;
235+
}
186236
// case SPLIT:
187237
// case STACK:
188238
case STOP:
189239
return STOPPED;
190240
// case SYSTEM:
191241
case TAKE: {
192-
runtimeValue = runtime->getRuntimeValue("value1");
193-
runtimeValue2 = runtime->getRuntimeValue("value2");
194-
Symbol* target = runtime->getSymbol("target");
242+
Symbol* target = runtime->getSymbol(elements, "target");
243+
runtimeValue = runtime->getRuntimeValue(elements, "value1");
244+
runtimeValue2 = runtime->getRuntimeValue(elements, "value2");
195245
if (runtimeValue2 == nullptr) {
196246
runtimeValue2 = target->getValue();
197247
}
198-
runtimeValue->setIntValue(runtimeValue2->getIntValue() - runtimeValue->getIntValue());
199-
runtime->setSymbolValue("target", runtimeValue->copy());
248+
target->setIntValue(runtimeValue2->getIntValue() - runtimeValue->getIntValue());
200249
return next;
201250
}
202251
case VARIABLE:{
203-
Symbol* symbol = new Symbol(runtime->getCommand()->getCommandProperty("name"));
252+
Symbol* symbol = new Symbol(runtime->getCommand()->getCommandProperty(elements, "name"));
204253
runtime->getSymbols()->add(symbol);
205254
return next;
206255
}
207256
case WAIT: {
208-
runtimeValue = runtime->getRuntimeValue("value");
209-
Text* multiplier = runtime->getParameter("multiplier");
257+
runtimeValue = runtime->getRuntimeValue(elements, "value");
258+
Text* multiplier = runtime->getParameter(elements, "multiplier");
210259
long delay = runtimeValue->getIntValue() * atoi(multiplier->getText());
211260
runtime->getThreads()->add(new Thread(delay, next));
212261
return STOPPED;
213262
}
214263
case WHILE: {
215-
bool condition = runtime->getCondition();
216-
return condition ? next + 1 : next;
217-
// return runtime->getCondition() ? next + 1 : next;
264+
return runtime->getCondition(elements) ? next + 1 : next;
218265
}
219266
// case WRITE:
220267
default:
221-
print("Unknown keyword code %d in core-keywords\n", index);
222-
return FINISHED;
268+
sprintf(exceptionBuffer, "Unknown keyword code '%d'\n", code);
269+
throw exceptionBuffer;
223270
}
224271
}
225272

@@ -235,6 +282,7 @@ class CoreKeywords {
235282
add("begin");
236283
add("clear");
237284
add("close");
285+
add("debug");
238286
add("decrement");
239287
add("delete");
240288
add("divide");

ecr/core-values.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ class CoreValues {
66
enum coreIndices {
77
SYMBOL,
88
CAT,
9-
TIMESTAMP
9+
TIMESTAMP,
10+
NOW,
11+
EMPTY
1012
};
1113

1214
int index = 0;
@@ -71,7 +73,8 @@ class CoreValues {
7173
value->append(runtimeValues->get(n)->getTextValue());
7274
}
7375
runtimeValue->setType(TEXT_VALUE);
74-
runtimeValue->setTextValue(value->getText());
76+
runtimeValue->setTextValue(value->copy()->getText());
77+
delete value;
7578
return runtimeValue;
7679
}
7780
case TIMESTAMP: {
@@ -82,6 +85,18 @@ class CoreValues {
8285
runtimeValue->setIntValue(millis);
8386
return runtimeValue;
8487
}
88+
case NOW: {
89+
runtimeValue->setType(INT_VALUE);
90+
struct timeval now;
91+
gettimeofday(&now, 0);
92+
runtimeValue->setIntValue(now.tv_sec);
93+
return runtimeValue;
94+
}
95+
case EMPTY: {
96+
runtimeValue->setType(TEXT_VALUE);
97+
runtimeValue->setTextValue("");
98+
return runtimeValue;
99+
}
85100
default:
86101
print("Unknown keyword in CoreValues\n");
87102
exit(1);
@@ -102,6 +117,8 @@ class CoreValues {
102117
add("symbol");
103118
add("cat");
104119
add("timestamp");
120+
add("now");
121+
add("empty");
105122
keywords->flatten();
106123
}
107124

ecr/debug.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#if DEBUG
3+
#if DEBUGGING
44
#define print(...) printf(__VA_ARGS__)
55
#else
66
#define print(...)

ecr/definitions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ enum domains {
77
enum valueTypes {
88
TEXT_VALUE,
99
INT_VALUE,
10-
BOOL_VALUE
10+
BOOL_VALUE,
11+
PROPERTY_VALUE
1112
};
1213

1314
#define STOPPED -1
1415
#define FINISHED -2
16+

ecr/ecr.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#define DEBUG 1 // set to 1 to show debug messages
2-
#define DESTROY 0 // set to 1 to show destructors
3-
#define LINENUMBERS 0 // set to 1 to show each line number
1+
#define DEBUGGING 1 // set to 1 to show debug messages
2+
#define DESTROY 0 // set to 1 to show destructors
43
#define _LINUX
54
//#define _WINDOWS
65
//#define _ARDUINO
@@ -20,10 +19,15 @@
2019
#ifdef _ARDUINO
2120
#define Sleep(x) delay((x))
2221
#endif
22+
23+
bool singleStep = false;
24+
char exceptionBuffer[80];
25+
2326
#include "debug.h"
2427
#include "definitions.h"
2528
#include "linkedlist.h"
2629
#include "text.h"
30+
#include "property.h"
2731
#include "element.h"
2832
#include "keyword.h"
2933
#include "runtimevalue.h"
@@ -102,7 +106,6 @@ int main(int argc, char* argv[])
102106
// print("codes: %s", codes->getText());
103107
// print("keys: %s", keys->getText());
104108

105-
Run runner = Run(codes, keys);
106-
109+
Run(codes, keys);
107110
return 0;
108111
};

0 commit comments

Comments
 (0)