1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// command.cpp: implements the parsing and execution of a tiny script language
// which is largely backwards compatible with the quake console language.
#include "cube.h"
#include <memory>
#import "Alias.h"
#import "Command.h"
#import "Identifier.h"
#import "Variable.h"
// contains ALL vars/commands/aliases
static OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers;
void
alias(OFString *name, OFString *action)
|
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// command.cpp: implements the parsing and execution of a tiny script language
// which is largely backwards compatible with the quake console language.
#include "cube.h"
#include <memory>
#import "Alias.h"
#import "Command.h"
#import "Identifier.h"
#import "OFString+Cube.h"
#import "Variable.h"
// contains ALL vars/commands/aliases
static OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers;
void
alias(OFString *name, OFString *action)
|
︙ | | | ︙ | |
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
} else if ([identifier isKindOfClass:Alias.class])
return [identifier action];
}
conoutf(@"unknown alias lookup: %@", [n substringFromIndex:1]);
return n;
}
// all evaluation happens here, recursively
int
execute(OFString *string, bool isDown)
{
@autoreleasepool {
std::unique_ptr<char> copy(strdup(string.UTF8String));
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
} else if ([identifier isKindOfClass:Alias.class])
return [identifier action];
}
conoutf(@"unknown alias lookup: %@", [n substringFromIndex:1]);
return n;
}
int
executeIdentifier(__kindof Identifier *identifier,
OFArray<OFString *> *arguments, bool isDown)
{
if (identifier == nil) {
@try {
return [arguments[0] intValueWithBase:0];
} @catch (OFInvalidFormatException *e) {
conoutf(@"unknown command: %@", arguments[0]);
return 0;
} @catch (OFOutOfRangeException *e) {
conoutf(@"invalid value: %@", arguments[0]);
return 0;
}
}
if ([identifier isKindOfClass:Command.class])
// game defined commands use very ad-hoc function signature,
// and just call it
return [identifier callWithArguments:arguments isDown:isDown];
if ([identifier isKindOfClass:Variable.class]) {
if (!isDown)
return 0;
// game defined variables
if (arguments.count < 2 || arguments[1].length == 0)
[identifier printValue];
else
[identifier
setValue:[arguments[1] cube_intValueWithBase:0]];
}
if ([identifier isKindOfClass:Alias.class]) {
// alias, also used as functions and (global) variables
for (int i = 1; i < arguments.count; i++) {
// set any arguments as (global) arg values so
// functions can access them
OFString *t = [OFString stringWithFormat:@"arg%d", i];
alias(t, arguments[i]);
}
return execute([identifier action], isDown);
}
return 0;
}
// all evaluation happens here, recursively
int
execute(OFString *string, bool isDown)
{
@autoreleasepool {
std::unique_ptr<char> copy(strdup(string.UTF8String));
|
︙ | | | ︙ | |
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
|
c = [c substringFromIndex:1];
w[0] = c;
}
// empty statement
if (c.length == 0)
continue;
__kindof Identifier *identifier = identifiers[c];
if (identifier == nil) {
@try {
val = [c intValueWithBase:0];
} @catch (OFInvalidFormatException *e) {
conoutf(@"unknown command: %@", c);
}
} else {
if ([identifier isKindOfClass:Command.class]) {
// game defined commands use very
// ad-hoc function signature, and just
// call it
OFArray<OFString *> *arguments =
[[OFArray alloc]
initWithObjects:w
count:numargs];
val = [identifier
callWithArguments:arguments
isDown:isDown];
} else if ([identifier
isKindOfClass:Variable.class]) {
// game defined variables
if (isDown) {
if (w[1].length == 0)
[identifier printValue];
else
[identifier
setValue:
[w[1]
intValueWithBase:
0]];
}
} else if ([identifier
isKindOfClass:Alias.class]) {
// alias, also used as functions and
// (global) variables
for (int i = 1; i < numargs; i++) {
// set any arguments as
// (global) arg values so
// functions can access them
OFString *t = [OFString
stringWithFormat:@"arg%d",
i];
alias(t, w[i]);
}
val = execute(
[identifier action], isDown);
}
}
}
return val;
}
}
// tab-completion of all identifiers
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
<
|
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
c = [c substringFromIndex:1];
w[0] = c;
}
// empty statement
if (c.length == 0)
continue;
val = executeIdentifier(identifiers[c],
[OFArray arrayWithObjects:w count:numargs], isDown);
}
return val;
}
}
// tab-completion of all identifiers
|
︙ | | | ︙ | |
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
IRIByAppendingPathComponent:@"config.cfg"];
stream = [[OFIRIHandler handlerForIRI:IRI] openItemAtIRI:IRI
mode:@"w"];
} @catch (id e) {
return;
}
[stream writeString:
@"// automatically written on exit, do not modify\n"
@"// delete this file to have defaults.cfg overwrite these "
@"settings\n"
@"// modify settings in game, or put settings in "
@"autoexec.cfg to override anything\n"
@"\n"];
writeclientinfo(stream);
[stream writeString:@"\n"];
[identifiers enumerateKeysAndObjectsUsingBlock:^(
OFString *name, __kindof Identifier *identifier, bool *stop) {
if (![identifier isKindOfClass:Variable.class] ||
![identifier persisted])
|
<
|
|
|
|
|
|
|
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
IRIByAppendingPathComponent:@"config.cfg"];
stream = [[OFIRIHandler handlerForIRI:IRI] openItemAtIRI:IRI
mode:@"w"];
} @catch (id e) {
return;
}
[stream writeString:@"// automatically written on exit, do not modify\n"
@"// delete this file to have defaults.cfg "
@"overwrite these settings\n"
@"// modify settings in game, or put settings in "
@"autoexec.cfg to override anything\n"
@"\n"];
writeclientinfo(stream);
[stream writeString:@"\n"];
[identifiers enumerateKeysAndObjectsUsingBlock:^(
OFString *name, __kindof Identifier *identifier, bool *stop) {
if (![identifier isKindOfClass:Variable.class] ||
![identifier persisted])
|
︙ | | | ︙ | |
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
|
[stream close];
}
COMMAND(writecfg, ARG_NONE)
// 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]);
}
}
void
ifthen(OFString *cond, OFString *thenp, OFString *elsep)
{
execute((![cond hasPrefix:@"0"] ? thenp : elsep));
}
void
loopa(OFString *times, OFString *body)
{
@autoreleasepool {
int t = times.intValue;
loopi(t)
{
intset(@"i", i);
execute(body);
}
}
|
<
|
>
|
|
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
450
|
[stream close];
}
COMMAND(writecfg, ARG_NONE)
// 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]);
}
}
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;
loopi(t)
{
intset(@"i", i);
execute(body);
}
}
|
︙ | | | ︙ | |
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
}
}
void
at(OFString *s_, OFString *pos)
{
@autoreleasepool {
int n = pos.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)
|
|
<
|
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
}
}
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));
}
}
COMMANDN(loop, loopa, ARG_2STR)
|
︙ | | | ︙ | |