1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
-
-
-
-
+
-
-
-
+
+
+
-
-
-
+
-
-
+
+
-
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
+
-
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
// 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"
#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;
static void
cleanup(char **string)
{
free(*string);
}
void
alias(OFString *name, OFString *action)
{
Alias *alias = identifiers[name];
Alias *alias = [Identifier identifierForName:name];
if (alias == nil) {
alias = [Alias aliasWithName:name action:action persisted:true];
if (alias == nil)
[Identifier addIdentifier:[Alias aliasWithName:name
action:action
if (identifiers == nil)
identifiers = [[OFMutableDictionary alloc] init];
persisted:true]];
identifiers[name] = alias;
} else {
else {
if ([alias isKindOfClass:Alias.class])
alias.action = action;
else
conoutf(
@"cannot redefine builtin %@ with an alias", name);
}
}
COMMAND(alias, ARG_2STR)
COMMAND(alias, ARG_2STR, ^(OFString *name, OFString *action) {
alias(name, action);
})
int
variable(OFString *name, int min, int cur, int max, int *storage,
void (*function)(), bool persisted)
{
Variable *variable = [Variable variableWithName:name
min:min
max:max
storage:storage
function:function
persisted:persisted];
[Identifier addIdentifier:[Variable variableWithName:name
min:min
max:max
storage:storage
function:function
persisted:persisted]];
if (identifiers == nil)
identifiers = [[OFMutableDictionary alloc] init];
identifiers[name] = variable;
return cur;
}
void
setvar(OFString *name, int i)
{
Variable *variable = [Identifier identifierForName:name];
*[identifiers[name] storage] = i;
if ([variable isKindOfClass:Variable.class])
*variable.storage = i;
}
int
getvar(OFString *name)
{
Variable *variable = [Identifier identifierForName:name];
if ([variable isKindOfClass:Variable.class])
return *variable.storage;
return *[identifiers[name] storage];
return 0;
}
bool
identexists(OFString *name)
{
return (identifiers[name] != nil);
return ([Identifier identifierForName:name] != nil);
}
OFString *
getalias(OFString *name)
{
Alias *alias = identifiers[name];
Alias *alias = [Identifier identifierForName:name];
if ([alias isKindOfClass:Alias.class])
return alias.action;
return nil;
}
bool
addcommand(OFString *name, void (*function)(), int argumentsTypes)
{
Command *command = [Command commandWithName:name
function:function
argumentsTypes:argumentsTypes];
if (identifiers == nil)
identifiers = [[OFMutableDictionary alloc] init];
identifiers[name] = command;
return false;
}
// parse any nested set of () or []
static char *
parseexp(char **p, int right)
{
int left = *(*p)++;
char *word = *p;
for (int brak = 1; brak;) {
|
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
-
-
-
-
-
+
+
+
+
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
+
+
+
-
+
+
+
-
-
+
-
-
+
-
-
+
-
-
+
-
-
+
-
-
+
-
-
+
-
-
+
-
-
-
+
+
+
+
+
-
+
-
-
+
-
-
+
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
+
-
-
+
-
-
-
+
-
-
+
-
-
-
+
-
-
+
-
-
-
+
-
+
-
+
-
-
-
-
+
-
-
+
-
-
-
+
-
-
+
-
-
-
+
-
-
+
-
-
-
+
-
-
+
-
-
-
+
-
-
-
+
+
-
-
-
+
-
-
+
-
|
@"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])
return;
[Identifier
enumerateIdentifiersUsingBlock:^(__kindof Identifier *identifier) {
if (![identifier isKindOfClass:Variable.class] ||
![identifier persisted])
return;
[stream writeFormat:@"%@ %d\n", identifier.name,
*[identifier storage]];
}];
[stream writeFormat:@"%@ %d\n", identifier.name,
*[identifier storage]];
}];
[stream writeString:@"\n"];
writebinds(stream);
[stream writeString:@"\n"];
[identifiers enumerateKeysAndObjectsUsingBlock:^(
OFString *name, __kindof Identifier *identifier, bool *stop) {
if (![identifier isKindOfClass:Alias.class] ||
[identifier.name hasPrefix:@"nextmap_"])
return;
[Identifier
enumerateIdentifiersUsingBlock:^(__kindof Identifier *identifier) {
if (![identifier isKindOfClass:Alias.class] ||
[identifier.name hasPrefix:@"nextmap_"])
return;
[stream writeFormat:@"alias \"%@\" [%@]\n", identifier.name,
[identifier action]];
}];
[stream writeFormat:@"alias \"%@\" [%@]\n", identifier.name,
[identifier action]];
}];
[stream close];
}
COMMAND(writecfg, ARG_NONE)
COMMAND(writecfg, ARG_NONE, ^{
writecfg();
})
// 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)
{
alias(name, [OFString stringWithFormat:@"%d", v]);
}
void
ifthen(OFString *cond, OFString *thenp, OFString *elsep)
COMMAND(if, ARG_3STR, ^(OFString *cond, OFString *thenp, OFString *elsep) {
{
execute((![cond hasPrefix:@"0"] ? thenp : elsep), true);
}
})
void
loopa(OFString *times, OFString *body)
COMMAND(loop, ARG_2STR, ^(OFString *times, OFString *body) {
{
int t = times.cube_intValue;
for (int i = 0; i < t; i++) {
intset(@"i", i);
execute(body, true);
}
}
})
void
whilea(OFString *cond, OFString *body)
COMMAND(while, ARG_2STR, ^(OFString *cond, OFString *body) {
{
while (execute(cond, true))
execute(body, true);
}
})
void
onrelease(bool on, OFString *body)
COMMAND(onrelease, ARG_DWN1, ^(bool on, OFString *body) {
{
if (!on)
execute(body, true);
}
})
void
concat(OFString *s)
{
alias(@"s", s);
}
void
concatword(OFString *s)
{
COMMAND(concat, ARG_VARI, ^(OFString *s) {
concat(s);
})
COMMAND(concatword, ARG_VARI, ^(OFString *s) {
concat([s stringByReplacingOccurrencesOfString:@" " withString:@""]);
}
})
int
listlen(OFString *a_)
COMMAND(listlen, ARG_1EST, ^(OFString *a_) {
{
const char *a = a_.UTF8String;
if (!*a)
return 0;
int n = 0;
while (*a)
if (*a++ == ' ')
n++;
return n + 1;
}
})
void
at(OFString *s_, OFString *pos)
COMMAND(at, ARG_2STR, ^(OFString *s_, OFString *pos) {
{
int n = pos.cube_intValue;
char *copy __attribute__((__cleanup__(cleanup))) =
strdup(s_.UTF8String);
char *s = copy;
for (int i = 0; i < n; i++) {
s += strcspn(s, " \0");
s += strspn(s, " ");
}
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)
COMMAND(concat, ARG_VARI)
COMMAND(concatword, ARG_VARI)
COMMAND(at, ARG_2STR)
COMMAND(listlen, ARG_1EST)
int
add(int a, int b)
COMMAND(+, ARG_2EXP, ^(int a, int b) {
{
return a + b;
}
})
COMMANDN(+, add, ARG_2EXP)
int
mul(int a, int b)
COMMAND(*, ARG_2EXP, ^(int a, int b) {
{
return a * b;
}
})
COMMANDN(*, mul, ARG_2EXP)
int
sub(int a, int b)
COMMAND(-, ARG_2EXP, ^(int a, int b) {
{
return a - b;
}
})
COMMANDN(-, sub, ARG_2EXP)
int
divi(int a, int b)
COMMAND(div, ARG_2EXP, ^(int a, int b) {
{
return b ? a / b : 0;
}
})
COMMANDN(div, divi, ARG_2EXP)
int
mod(int a, int b)
COMMAND(mod, ARG_2EXP, ^(int a, int b) {
{
return b ? a % b : 0;
})
}
COMMAND(mod, ARG_2EXP)
int
equal(int a, int b)
COMMAND(=, ARG_2EXP, ^(int a, int b) {
{
return (int)(a == b);
}
})
COMMANDN(=, equal, ARG_2EXP)
int
lt(int a, int b)
COMMAND(<, ARG_2EXP, ^(int a, int b) {
{
return (int)(a < b);
}
})
COMMANDN(<, lt, ARG_2EXP)
int
gt(int a, int b)
COMMAND(>, ARG_2EXP, ^(int a, int b) {
{
return (int)(a > b);
}
})
COMMANDN(>, gt, ARG_2EXP)
int
strcmpa(OFString *a, OFString *b)
COMMAND(strcmp, ARG_2EST, ^(OFString *a, OFString *b) {
{
return [a isEqual:b];
}
})
COMMANDN(strcmp, strcmpa, ARG_2EST)
int
rndn(int a)
COMMAND(rnd, ARG_1EXP, ^(int a) {
{
return a > 0 ? rnd(a) : 0;
}
return (a > 0 ? rnd(a) : 0);
})
COMMANDN(rnd, rndn, ARG_1EXP)
int
explastmillis()
COMMAND(millis, ARG_1EXP, ^(int unused) {
{
return lastmillis;
}
})
COMMANDN(millis, explastmillis, ARG_1EXP)
|