Skip to content

Commit 4a4fd3a

Browse files
committed
Merge keyword handlers into domain classes
1 parent 4ecf0ac commit 4a4fd3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+538
-455
lines changed

ecr/command.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,16 @@ class Command {
112112
///////////////////////////////////////////////////////////////////////
113113
// Set the value of a variable
114114
void setSymbolValue(const char* key, RuntimeValue* runtimeValue) {
115-
// First we find the variable. Start by flattening the symbol array.
116115
Symbol* symbol = getSymbol(key);
117-
// Put the given value into the symbol
118116
symbol->setValue(runtimeValue);
119117
}
118+
119+
///////////////////////////////////////////////////////////////////////
120+
// Get the value of a variable
121+
RuntimeValue* getSymbolValue(const char* key) {
122+
Symbol* symbol = getSymbol(key);
123+
return symbol->getValue();
124+
}
120125

121126
///////////////////////////////////////////////////////////////////////
122127
// Add a no-domain value type
@@ -154,10 +159,31 @@ class Command {
154159
RuntimeValue* runtimeValue = new RuntimeValue();
155160
const char* domain = functions->getValuePropertyCode(value, "domain");
156161
const char* type = functions->getValuePropertyCode(value, "type");
162+
functions->setElements(value);
157163
// Test for the special case 'cat'
158164
int t = atoi(type);
159165
if (strcmp(getKeyArray()->getText(t), "cat") == 0) {
160-
return coreValues->run(t, value, getSymbols(), functions);
166+
// Find the cat array
167+
for (int n = 0; n < value->getSize(); n++) {
168+
Element* el = value->get(n);
169+
// Look for a value part
170+
int colon = el->positionOf(':');
171+
if (colon > 0) {
172+
Text* left = el->left(colon);
173+
if (getKeyArray()->get(atoi(left->getText()))->is("value")) {
174+
delete left;
175+
ElementArray* values = el->getValue();
176+
int size = values->getSize();
177+
RuntimeValueArray* rva = new RuntimeValueArray();
178+
for (int m = 0; m < size; m++) {
179+
RuntimeValue* rv = getRuntimeValue(values->get(m)->getValue());
180+
rva->add(rv);
181+
}
182+
rva->flatten();
183+
return coreValues->run(t, functions, rva);
184+
}
185+
}
186+
}
161187
}
162188
if (domain == nullptr) {
163189
// Here if no domain specified
@@ -183,7 +209,7 @@ class Command {
183209
switch (atoi(domain)) {
184210
case DOMAIN_CORE:
185211
functions->setElements(value);
186-
return coreValues->run(t, value, getSymbols(), functions);
212+
return coreValues->run(t, functions, nullptr);
187213
};
188214
}
189215
return runtimeValue;
@@ -277,8 +303,6 @@ class Command {
277303
buf = new char[6];
278304
sprintf(buf, "%s", value->getBoolValue() ? "true" : "false");
279305
break;
280-
case CAT_VALUE:
281-
break;
282306
};
283307
return buf;
284308
}

ecr/domain/core/core-keywords.h renamed to ecr/core-keywords.h

Lines changed: 76 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,3 @@
1-
#include "add.h"
2-
#include "append.h"
3-
#include "array.h"
4-
#include "begin.h"
5-
#include "clear.h"
6-
#include "close.h"
7-
#include "decrement.h"
8-
#include "delete.h"
9-
#include "divide.h"
10-
#include "dummy.h"
11-
#include "end.h"
12-
#include "exit.h"
13-
#include "file.h"
14-
#include "fork.h"
15-
#include "get.h"
16-
#include "gosub.h"
17-
#include "goto.h"
18-
#include "gotopc.h"
19-
#include "if.h"
20-
#include "increment.h"
21-
#include "index.h"
22-
#include "init.h"
23-
#include "multiply.h"
24-
#include "object.h"
25-
#include "open.h"
26-
#include "pop.h"
27-
#include "post.h"
28-
#include "print.h"
29-
#include "push.h"
30-
#include "put.h"
31-
#include "read.h"
32-
#include "replace.h"
33-
#include "return.h"
34-
#include "script.h"
35-
#include "set.h"
36-
#include "split.h"
37-
#include "stack.h"
38-
#include "stop.h"
39-
#include "system.h"
40-
#include "take.h"
41-
#include "variable.h"
42-
#include "wait.h"
43-
#include "while.h"
44-
#include "write.h"
45-
461
// Keywords for the 'core' domain
472
class CoreKeywords {
483

@@ -141,96 +96,124 @@ class CoreKeywords {
14196
///////////////////////////////////////////////////////////////////////
14297
// Run a command. All the information needed is in 'runtime'
14398
int run(Runtime* runtime, int code) {
99+
RuntimeValue* runtimeValue;
100+
RuntimeValue* runtimeValue2;
101+
int next = runtime->getPC() + 1;
144102
int index = map[code];
145103
switch (index)
146104
{
147-
case ADD:
148-
return core_add(runtime);
105+
case ADD: {
106+
runtimeValue = runtime->getRuntimeValue("value1");
107+
runtimeValue2 = runtime->getRuntimeValue("value2");
108+
Symbol* target = runtime->getSymbol("target");
109+
if (runtimeValue2 == nullptr) {
110+
runtimeValue2 = target->getValue();
111+
}
112+
runtimeValue->setIntValue(runtimeValue->getIntValue() + runtimeValue2->getIntValue());
113+
runtime->setSymbolValue("target", runtimeValue->copy());
114+
return next;
115+
}
149116
case APPEND:
150-
return core_append(runtime);
117+
return -1;
151118
case ARRAY:
152-
return core_array(runtime);
119+
return -1;
153120
case BEGIN:
154-
return core_begin(runtime);
121+
return -1;
155122
case CLEAR:
156-
return core_clear(runtime);
123+
return -1;
157124
case CLOSE:
158-
return core_close(runtime);
125+
return -1;
159126
case DECREMENT:
160-
return core_decrement(runtime);
127+
return -1;
161128
case DELETE:
162-
return core_delete(runtime);
129+
return -1;
163130
case DUMMY:
164-
return core_dummy(runtime);
131+
return -1;
165132
case END:
166-
return core_end(runtime);
167-
case EXIT:
168-
return core_exit(runtime);
133+
return -1;
134+
case EXIT:{
135+
return -1;
136+
}
169137
case FILE:
170-
return core_file(runtime);
138+
return -1;
171139
case FORK:
172-
return core_fork(runtime);
140+
return -1;
173141
case GET:
174-
return core_get(runtime);
142+
return -1;
175143
case GOSUB:
176-
return core_gosub(runtime);
144+
return -1;
177145
case GO:
178146
case GOTO:
179-
return core_goto(runtime);
147+
return -1;
180148
case GOTOPC:
181-
return core_gotoPC(runtime);
149+
return -1;
182150
case IF:
183-
return core_if(runtime);
151+
return -1;
184152
case INCREMENT:
185-
return core_increment(runtime);
153+
return -1;
186154
case INDEX:
187-
return core_index(runtime);
155+
return -1;
188156
case INIT:
189-
return core_init(runtime);
157+
return -1;
190158
case MULTIPLY:
191-
return core_multiply(runtime);
159+
return -1;
192160
case OBJECT:
193-
return core_object(runtime);
161+
return -1;
194162
case OPEN:
195-
return core_open(runtime);
163+
return -1;
196164
case POP:
197-
return core_pop(runtime);
165+
return -1;
198166
case POST:
199-
return core_post(runtime);
200-
case PRINT:
201-
return core_print(runtime);
167+
return -1;
168+
case PRINT:{
169+
const char* buf = runtime->getTextValue("value");
170+
if (buf == nullptr) {
171+
print("No 'value' at line %d\n", atoi(runtime->getLineNumber()) + 1);
172+
exit(1);
173+
}
174+
printf("->%s\n", buf);
175+
delete[] buf;
176+
return next;
177+
}
202178
case PUSH:
203-
return core_push(runtime);
204-
case PUT:
205-
return core_put(runtime);
179+
return -1;
180+
case PUT:{
181+
runtimeValue = runtime->getRuntimeValue("value");
182+
runtime->setSymbolValue("target", runtimeValue->copy());
183+
return runtime->getPC() + 1;
184+
}
206185
case READ:
207-
return core_read(runtime);
186+
return -1;
208187
case REPLACE:
209-
return core_replace(runtime);
188+
return -1;
210189
case RETURN:
211-
return core_return(runtime);
190+
return -1;
212191
case SCRIPT:
213-
return core_script(runtime);
192+
return -1;
214193
case SET:
215-
return core_set(runtime);
194+
return -1;
216195
case SPLIT:
217-
return core_split(runtime);
196+
return -1;
218197
case STACK:
219-
return core_stack(runtime);
198+
return -1;
220199
case STOP:
221-
return core_stop(runtime);
200+
return -1;
222201
case SYSTEM:
223-
return core_system(runtime);
202+
return -1;
224203
case TAKE:
225-
return core_take(runtime);
226-
case VARIABLE:
227-
return core_variable(runtime);
204+
return -1;
205+
case VARIABLE:{
206+
int pc = runtime->getPC();
207+
Symbol* symbol = new Symbol(runtime->getCommand()->getCommandProperty("name"));
208+
runtime->getSymbols()->add(symbol);
209+
return next;
210+
}
228211
case WAIT:
229-
return core_wait(runtime);
212+
return -1;
230213
case WHILE:
231-
return core_while(runtime);
214+
return -1;
232215
case WRITE:
233-
return core_write(runtime);
216+
return -1;
234217
default:
235218
print("Unknown keyword code %d in core-keywords\n", index);
236219
return -1;

ecr/domain/core/core-values.h renamed to ecr/core-values.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#include "symbol.h"
2-
#include "cat.h"
3-
41
// Values for the 'core' domain
52
class CoreValues {
63

@@ -25,7 +22,7 @@ class CoreValues {
2522
int size = keyArray->getSize();
2623
for (int n = 0; n < size; n++) {
2724
if (keyArray->get(n)->is(name)) {
28-
printf("Adding %s at position %d pointing to %d\n", name, n, index);
25+
// printf("Adding %s at position %d pointing to %d\n", name, n, index);
2926
Keyword* keyword = new Keyword();
3027
keyword->setName(new Text(name));
3128
keyword->setDomain(domain);
@@ -56,15 +53,26 @@ class CoreValues {
5653

5754
///////////////////////////////////////////////////////////////////////
5855
// Run a command. All necessary information is passed in
59-
RuntimeValue* run(int code, ElementArray* value, SymbolArray* symbols, Functions* functions) {
56+
RuntimeValue* run(int code, Functions* functions, void* data) {
6057
// functions->showSymbolValues();
6158
int index = map[code];
6259
switch (index)
6360
{
64-
case SYMBOL:
65-
return core_symbol(functions);
66-
case CAT:
67-
return core_cat(functions);
61+
case SYMBOL: {
62+
Symbol* symbol = functions->getSymbol("name");
63+
return symbol->getValue();
64+
}
65+
case CAT: {
66+
RuntimeValueArray* runtimeValues = (RuntimeValueArray*)data;
67+
Text* value = new Text();
68+
for (int n = 0; n < runtimeValues->getSize(); n++) {
69+
value->append(runtimeValues->get(n)->getTextValue());
70+
}
71+
RuntimeValue* runtimeValue = new RuntimeValue();
72+
runtimeValue->setType(TEXT_VALUE);
73+
runtimeValue->setTextValue(value->getText());
74+
return runtimeValue;
75+
}
6876
default:
6977
print("Unknown keyword code %d in CoreValues\n", index);
7078
exit(1);

ecr/definitions.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ enum domains {
77
enum valueTypes {
88
TEXT_VALUE,
99
INT_VALUE,
10-
BOOL_VALUE,
11-
CAT_VALUE
10+
BOOL_VALUE
1211
};

ecr/domain/core/add.h

Lines changed: 0 additions & 5 deletions
This file was deleted.

ecr/domain/core/append.h

Lines changed: 0 additions & 5 deletions
This file was deleted.

ecr/domain/core/array.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

ecr/domain/core/begin.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

ecr/domain/core/cat.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

ecr/domain/core/clear.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

ecr/domain/core/close.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

ecr/domain/core/decrement.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

ecr/domain/core/delete.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

ecr/domain/core/divide.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)