︙ | | |
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
-
-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
-
|
p--;
conoutf(@"missing \"%c\"", right);
return NULL;
}
}
char *s = strndup(word, p - word - 1);
if (left == '(') {
@autoreleasepool {
OFString *t;
@try {
OFString *t;
@try {
t = [OFString
stringWithFormat:@"%d", execute(@(s))];
} @finally {
free(s);
}
s = strdup(t.UTF8String);
t = [OFString stringWithFormat:@"%d", execute(@(s))];
} @finally {
free(s);
}
s = strdup(t.UTF8String);
}
}
return s;
}
// parse single argument, including expressions
char *
parseword(char *&p)
|
︙ | | |
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
+
-
+
-
-
+
-
-
+
-
-
-
-
+
+
+
-
|
char *word = p;
p += strcspn(p, "; \t\r\n\0");
if (p - word == 0)
return NULL;
return strndup(word, p - word);
}
// find value of ident referenced with $ in exp
OFString *
lookup(OFString *n) // find value of ident referenced with $ in exp
lookup(OFString *n)
{
@autoreleasepool {
__kindof Identifier *identifier =
__kindof Identifier *identifier = identifiers[[n substringFromIndex:1]];
identifiers[[n substringFromIndex:1]];
if ([identifier isKindOfClass:Variable.class]) {
if ([identifier isKindOfClass:Variable.class]) {
return [OFString
stringWithFormat:@"%d", *[identifier storage]];
} else if ([identifier isKindOfClass:Alias.class])
return [identifier action];
return [OFString stringWithFormat:@"%d", *[identifier storage]];
} else if ([identifier isKindOfClass:Alias.class])
return [identifier action];
}
conoutf(@"unknown alias lookup: %@", [n substringFromIndex:1]);
return n;
}
int
executeIdentifier(__kindof Identifier *identifier,
|
︙ | | |
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
-
+
-
-
-
-
+
+
-
-
+
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
-
|
return 0;
}
// all evaluation happens here, recursively
int
execute(OFString *string, bool isDown)
{
@autoreleasepool {
std::unique_ptr<char> copy(strdup(string.UTF8String));
char *p = copy.get();
const int MAXWORDS = 25; // limit, remove
OFString *w[MAXWORDS];
int val = 0;
for (bool cont = true; cont;) {
// for each ; seperated statement
int numargs = MAXWORDS;
loopi(MAXWORDS)
{
// collect all argument values
w[i] = @"";
if (i > numargs)
continue;
// parse and evaluate exps
char *s = parseword(p);
if (!s) {
numargs = i;
s = strdup("");
}
@try {
if (*s == '$')
// substitute variables
w[i] = lookup(@(s));
else
w[i] = @(s);
} @finally {
free(s);
}
}
std::unique_ptr<char> copy(strdup(string.UTF8String));
char *p = copy.get();
const int MAXWORDS = 25; // limit, remove
OFString *w[MAXWORDS];
int val = 0;
for (bool cont = true; cont;) {
// for each ; seperated statement
int numargs = MAXWORDS;
loopi(MAXWORDS)
{
// collect all argument values
w[i] = @"";
if (i > numargs)
continue;
// parse and evaluate exps
char *s = parseword(p);
if (!s) {
numargs = i;
s = strdup("");
}
@try {
if (*s == '$')
// substitute variables
w[i] = lookup(@(s));
else
w[i] = @(s);
} @finally {
free(s);
}
}
p += strcspn(p, ";\n\0");
// more statements if this isn't the end of the string
cont = *p++ != 0;
OFString *c = w[0];
// strip irc-style command prefix
if ([c hasPrefix:@"/"]) {
c = [c substringFromIndex:1];
w[0] = c;
}
// empty statement
if (c.length == 0)
continue;
p += strcspn(p, ";\n\0");
// more statements if this isn't the end of the string
cont = *p++ != 0;
OFString *c = w[0];
// strip irc-style command prefix
if ([c hasPrefix:@"/"]) {
c = [c substringFromIndex:1];
w[0] = c;
}
// empty statement
if (c.length == 0)
continue;
val = executeIdentifier(identifiers[c],
[OFArray arrayWithObjects:w count:numargs], isDown);
}
val = executeIdentifier(identifiers[c],
[OFArray arrayWithObjects:w count:numargs], isDown);
}
return val;
return val;
}
}
// tab-completion of all identifiers
int completesize = 0, completeidx = 0;
void
resetcomplete()
{
completesize = 0;
}
void
complete(OFMutableString *s)
{
@autoreleasepool {
if (![s hasPrefix:@"/"])
[s insertString:@"/" atIndex:0];
if (![s hasPrefix:@"/"])
[s insertString:@"/" atIndex:0];
if (s.length == 1)
return;
if (s.length == 1)
return;
if (!completesize) {
completesize = s.length - 1;
completeidx = 0;
}
if (!completesize) {
completesize = s.length - 1;
completeidx = 0;
}
__block int idx = 0;
[identifiers enumerateKeysAndObjectsUsingBlock:^(
OFString *name, Identifier *identifier, bool *stop) {
if (strncmp(identifier.name.UTF8String,
s.UTF8String + 1, completesize) == 0 &&
idx++ == completeidx)
[s replaceCharactersInRange:OFMakeRange(
__block int idx = 0;
[identifiers enumerateKeysAndObjectsUsingBlock:^(
OFString *name, Identifier *identifier, bool *stop) {
if (strncmp(identifier.name.UTF8String, s.UTF8String + 1,
completesize) == 0 &&
idx++ == completeidx)
[s replaceCharactersInRange:OFMakeRange(1, s.length - 1)
1, s.length - 1)
withString:identifier.name];
}];
withString:identifier.name];
}];
completeidx++;
completeidx++;
if (completeidx >= idx)
completeidx = 0;
if (completeidx >= idx)
completeidx = 0;
}
}
bool
execfile(OFIRI *cfgfile)
{
@autoreleasepool {
OFString *command;
@try {
command = [OFString stringWithContentsOfIRI:cfgfile];
} @catch (OFOpenItemFailedException *e) {
return false;
} @catch (OFReadFailedException *e) {
return false;
}
OFString *command;
@try {
command = [OFString stringWithContentsOfIRI:cfgfile];
} @catch (OFOpenItemFailedException *e) {
return false;
} @catch (OFReadFailedException *e) {
return false;
}
execute(command);
return true;
execute(command);
return true;
}
}
void
exec(OFString *cfgfile)
{
@autoreleasepool {
if (!execfile([Cube.sharedInstance.userDataIRI
IRIByAppendingPathComponent:cfgfile]) &&
!execfile([Cube.sharedInstance.gameDataIRI
IRIByAppendingPathComponent:cfgfile]))
conoutf(@"could not read \"%@\"", cfgfile);
if (!execfile([Cube.sharedInstance.userDataIRI
IRIByAppendingPathComponent:cfgfile]) &&
!execfile([Cube.sharedInstance.gameDataIRI
IRIByAppendingPathComponent:cfgfile]))
conoutf(@"could not read \"%@\"", cfgfile);
}
}
void
writecfg()
{
OFStream *stream;
@try {
|
︙ | | |
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
-
-
+
-
-
-
+
-
-
-
-
+
+
+
+
-
|
// below the commands that implement a small imperative language. thanks to the
// semantics of () and [] expressions, any control construct can be defined
// trivially.
void
intset(OFString *name, int v)
{
@autoreleasepool {
alias(name, [OFString stringWithFormat:@"%d", v]);
alias(name, [OFString stringWithFormat:@"%d", v]);
}
}
void
ifthen(OFString *cond, OFString *thenp, OFString *elsep)
{
execute((![cond hasPrefix:@"0"] ? thenp : elsep));
}
void
loopa(OFString *times, OFString *body)
{
@autoreleasepool {
int t = times.cube_intValue;
int t = times.cube_intValue;
loopi(t)
{
intset(@"i", i);
execute(body);
loopi(t)
{
intset(@"i", i);
execute(body);
}
}
}
void
whilea(OFString *cond, OFString *body)
{
while (execute(cond))
|
︙ | | |
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
-
-
+
-
-
+
+
-
-
-
-
+
+
+
+
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
|
{
concat([s stringByReplacingOccurrencesOfString:@" " withString:@""]);
}
int
listlen(OFString *a_)
{
@autoreleasepool {
const char *a = a_.UTF8String;
const char *a = a_.UTF8String;
if (!*a)
return 0;
if (!*a)
return 0;
int n = 0;
while (*a)
if (*a++ == ' ')
n++;
int n = 0;
while (*a)
if (*a++ == ' ')
n++;
return n + 1;
return n + 1;
}
}
void
at(OFString *s_, OFString *pos)
{
@autoreleasepool {
int n = pos.cube_intValue;
std::unique_ptr<char> copy(strdup(s_.UTF8String));
char *s = copy.get();
loopi(n) s += strspn(s += strcspn(s, " \0"), " ");
s[strcspn(s, " \0")] = 0;
concat(@(s));
int n = pos.cube_intValue;
std::unique_ptr<char> copy(strdup(s_.UTF8String));
char *s = copy.get();
loopi(n) s += strspn(s += strcspn(s, " \0"), " ");
s[strcspn(s, " \0")] = 0;
concat(@(s));
}
}
COMMANDN(loop, loopa, ARG_2STR)
COMMANDN(while, whilea, ARG_2STR)
COMMANDN(if, ifthen, ARG_3STR)
COMMAND(onrelease, ARG_DWN1)
COMMAND(exec, ARG_1STR)
|
︙ | | |