@@ -10,6 +10,7 @@ class CoreKeywords {
10
10
BEGIN,
11
11
CLEAR,
12
12
CLOSE,
13
+ DEBUG,
13
14
DECREMENT,
14
15
DELETE,
15
16
DIVIDE,
@@ -96,40 +97,49 @@ class CoreKeywords {
96
97
97
98
// /////////////////////////////////////////////////////////////////////
98
99
// 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) {
100
101
RuntimeValue* runtimeValue = nullptr ;
101
102
RuntimeValue* runtimeValue2 = nullptr ;
102
103
int next = runtime->getPC () + 1 ;
103
104
int index = map[code];
104
105
switch (index)
105
106
{
106
107
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 " );
110
111
if (runtimeValue2 == nullptr ) {
111
112
runtimeValue2 = target->getValue ();
112
113
}
113
- runtimeValue->setIntValue (runtimeValue->getIntValue () + runtimeValue2->getIntValue ());
114
- runtime->setSymbolValue (" target" , runtimeValue->copy ());
114
+ target->setIntValue (runtimeValue->getIntValue () + runtimeValue2->getIntValue ());
115
115
return next;
116
116
}
117
117
// case APPEND:
118
118
// case ARRAY:
119
119
// case BEGIN:
120
120
// case CLEAR:
121
121
// 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
+ }
123
134
// case DELETE:
124
135
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 " );
128
139
if (runtimeValue2 == nullptr ) {
129
140
runtimeValue2 = target->getValue ();
130
141
}
131
- runtimeValue->setIntValue (runtimeValue->getIntValue () / runtimeValue2->getIntValue ());
132
- runtime->setSymbolValue (" target" , runtimeValue->copy ());
142
+ target->setIntValue (runtimeValue->getIntValue () / runtimeValue2->getIntValue ());
133
143
return next;
134
144
}
135
145
// case DUMMY:
@@ -143,83 +153,120 @@ class CoreKeywords {
143
153
// case GO:
144
154
// case GOTO:
145
155
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
+ }
150
171
// case INIT:
151
172
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 " );
155
176
if (runtimeValue2 == nullptr ) {
156
177
runtimeValue2 = target->getValue ();
157
178
}
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);
160
185
return next;
161
186
}
162
- // case OBJECT:
163
187
// case OPEN:
164
188
// case POP:
165
189
// case POST:
166
190
case PRINT:{
167
- const char * buf = runtime->getTextValue (" value" );
191
+ const char * buf = runtime->getTextValue (elements, " value" );
168
192
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 ;
171
195
}
172
196
printf (" ->%s\n " , buf);
173
197
return next;
174
198
}
175
199
// case PUSH:
176
200
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 ;
180
204
}
181
205
// case READ:
182
206
// case REPLACE:
183
207
// case RETURN:
184
208
// 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
+ }
186
236
// case SPLIT:
187
237
// case STACK:
188
238
case STOP:
189
239
return STOPPED;
190
240
// case SYSTEM:
191
241
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 " );
195
245
if (runtimeValue2 == nullptr ) {
196
246
runtimeValue2 = target->getValue ();
197
247
}
198
- runtimeValue->setIntValue (runtimeValue2->getIntValue () - runtimeValue->getIntValue ());
199
- runtime->setSymbolValue (" target" , runtimeValue->copy ());
248
+ target->setIntValue (runtimeValue2->getIntValue () - runtimeValue->getIntValue ());
200
249
return next;
201
250
}
202
251
case VARIABLE:{
203
- Symbol* symbol = new Symbol (runtime->getCommand ()->getCommandProperty (" name" ));
252
+ Symbol* symbol = new Symbol (runtime->getCommand ()->getCommandProperty (elements, " name" ));
204
253
runtime->getSymbols ()->add (symbol);
205
254
return next;
206
255
}
207
256
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" );
210
259
long delay = runtimeValue->getIntValue () * atoi (multiplier->getText ());
211
260
runtime->getThreads ()->add (new Thread (delay, next));
212
261
return STOPPED;
213
262
}
214
263
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;
218
265
}
219
266
// case WRITE:
220
267
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 ;
223
270
}
224
271
}
225
272
@@ -235,6 +282,7 @@ class CoreKeywords {
235
282
add (" begin" );
236
283
add (" clear" );
237
284
add (" close" );
285
+ add (" debug" );
238
286
add (" decrement" );
239
287
add (" delete" );
240
288
add (" divide" );
0 commit comments