Cube  Diff

Differences From Artifact [4117ae0f6a]:

To Artifact [116e44fb5b]:


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
// 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];

	if (alias == nil) {
		alias = [Alias aliasWithName:name action:action persisted:true];

		if (identifiers == nil)
			identifiers = [[OFMutableDictionary alloc] init];

		identifiers[name] = alias;
	} else {
		if ([alias isKindOfClass:Alias.class])
			alias.action = action;
		else
			conoutf(
			    @"cannot redefine builtin %@ with an alias", name);
	}
}

COMMAND(alias, ARG_2STR)



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];

	if (identifiers == nil)
		identifiers = [[OFMutableDictionary alloc] init];

	identifiers[name] = variable;

	return cur;
}

void
setvar(OFString *name, int i)
{

	*[identifiers[name] storage] = i;


}

int
getvar(OFString *name)
{





	return *[identifiers[name] storage];
}

bool
identexists(OFString *name)
{
	return (identifiers[name] != nil);
}

OFString *
getalias(OFString *name)
{
	Alias *alias = identifiers[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;) {











<
<
<









|

|
|
|
<
<
|
<
|







>
|
>
>





|
|
|
|
|
|
<
<
<
<
<
<






>
|
>
>





>
>
>
>
>
|





|





|







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







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"




static void
cleanup(char **string)
{
	free(*string);
}

void
alias(OFString *name, OFString *action)
{
	Alias *alias = [Identifier identifierForName:name];

	if (alias == nil)
		[Identifier addIdentifier:[Alias aliasWithName:name
		                                        action:action


		                                     persisted:true]];

	else {
		if ([alias isKindOfClass:Alias.class])
			alias.action = action;
		else
			conoutf(
			    @"cannot redefine builtin %@ with an alias", name);
	}
}

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)
{
	[Identifier addIdentifier:[Variable variableWithName:name
	                                                 min:min
	                                                 max:max
	                                             storage:storage
	                                            function:function
	                                           persisted:persisted]];






	return cur;
}

void
setvar(OFString *name, int i)
{
	Variable *variable = [Identifier identifierForName:name];

	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 0;
}

bool
identexists(OFString *name)
{
	return ([Identifier identifierForName:name] != nil);
}

OFString *
getalias(OFString *name)
{
	Alias *alias = [Identifier identifierForName:name];

	if ([alias isKindOfClass:Alias.class])
		return alias.action;

	return nil;
}
















// parse any nested set of () or []
static char *
parseexp(char **p, int right)
{
	int left = *(*p)++;
	char *word = *p;
	for (int brak = 1; brak;) {
164
165
166
167
168
169
170
171

172
173
174
175
176
177
178
	return strndup(word, *p - word);
}

// find value of ident referenced with $ in exp
OFString *
lookup(OFString *n)
{
	__kindof Identifier *identifier = identifiers[[n substringFromIndex:1]];


	if ([identifier isKindOfClass:Variable.class]) {
		return [OFString stringWithFormat:@"%d", *[identifier storage]];
	} else if ([identifier isKindOfClass:Alias.class])
		return [identifier action];

	conoutf(@"unknown alias lookup: %@", [n substringFromIndex:1]);







|
>







148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
	return strndup(word, *p - word);
}

// find value of ident referenced with $ in exp
OFString *
lookup(OFString *n)
{
	__kindof Identifier *identifier =
	    [Identifier identifierForName:[n substringFromIndex:1]];

	if ([identifier isKindOfClass:Variable.class]) {
		return [OFString stringWithFormat:@"%d", *[identifier storage]];
	} else if ([identifier isKindOfClass:Alias.class])
		return [identifier action];

	conoutf(@"unknown alias lookup: %@", [n substringFromIndex:1]);
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
			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







|







256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
			c = [c substringFromIndex:1];
			w[0] = c;
		}
		// empty statement
		if (c.length == 0)
			continue;

		val = executeIdentifier([Identifier identifierForName:c],
		    [OFArray arrayWithObjects:w count:numargs], isDown);
	}

	return val;
}

// tab-completion of all identifiers
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

	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(1, s.length - 1)
			                 withString:identifier.name];
	}];








|
|







288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

	if (!completesize) {
		completesize = s.length - 1;
		completeidx = 0;
	}

	__block int idx = 0;
	[Identifier enumerateIdentifiersUsingBlock:^(
	    __kindof Identifier *identifier) {
		if (strncmp(identifier.name.UTF8String, s.UTF8String + 1,
		        completesize) == 0 &&
		    idx++ == completeidx)
			[s replaceCharactersInRange:OFMakeRange(1, s.length - 1)
			                 withString:identifier.name];
	}];

343
344
345
346
347
348
349




350
351
352
353
354
355
356
{
	if (!execfile([Cube.sharedInstance.userDataIRI
	        IRIByAppendingPathComponent:cfgfile]) &&
	    !execfile([Cube.sharedInstance.gameDataIRI
	        IRIByAppendingPathComponent:cfgfile]))
		conoutf(@"could not read \"%@\"", cfgfile);
}





void
writecfg()
{
	OFStream *stream;
	@try {
		OFIRI *IRI = [Cube.sharedInstance.userDataIRI







>
>
>
>







328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
{
	if (!execfile([Cube.sharedInstance.userDataIRI
	        IRIByAppendingPathComponent:cfgfile]) &&
	    !execfile([Cube.sharedInstance.gameDataIRI
	        IRIByAppendingPathComponent:cfgfile]))
		conoutf(@"could not read \"%@\"", cfgfile);
}

COMMAND(exec, ARG_1STR, ^(OFString *cfgfile) {
	exec(cfgfile);
})

void
writecfg()
{
	OFStream *stream;
	@try {
		OFIRI *IRI = [Cube.sharedInstance.userDataIRI
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
	                    @"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;

		[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;

		[stream writeFormat:@"alias \"%@\" [%@]\n", identifier.name,
		        [identifier action]];
	}];

	[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)
{
	alias(name, [OFString stringWithFormat:@"%d", v]);
}

void
ifthen(OFString *cond, OFString *thenp, OFString *elsep)
{
	execute((![cond hasPrefix:@"0"] ? thenp : elsep), true);
}

void
loopa(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)
{
	while (execute(cond, true))
		execute(body, true);
}

void
onrelease(bool on, OFString *body)
{
	if (!on)
		execute(body, true);
}

void
concat(OFString *s)
{
	alias(@"s", s);
}

void
concatword(OFString *s)

{

	concat([s stringByReplacingOccurrencesOfString:@" " withString:@""]);
}

int
listlen(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)
{
	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)
{
	return a + b;
}
COMMANDN(+, add, ARG_2EXP)

int
mul(int a, int b)
{
	return a * b;
}
COMMANDN(*, mul, ARG_2EXP)

int
sub(int a, int b)
{
	return a - b;
}
COMMANDN(-, sub, ARG_2EXP)

int
divi(int a, int b)
{
	return b ? a / b : 0;
}
COMMANDN(div, divi, ARG_2EXP)

int
mod(int a, int b)
{
	return b ? a % b : 0;

}
COMMAND(mod, ARG_2EXP)

int
equal(int a, int b)
{
	return (int)(a == b);
}
COMMANDN(=, equal, ARG_2EXP)

int
lt(int a, int b)
{
	return (int)(a < b);
}
COMMANDN(<, lt, ARG_2EXP)

int
gt(int a, int b)
{
	return (int)(a > b);
}
COMMANDN(>, gt, ARG_2EXP)

int
strcmpa(OFString *a, OFString *b)
{
	return [a isEqual:b];
}
COMMANDN(strcmp, strcmpa, ARG_2EST)

int
rndn(int a)
{
	return a > 0 ? rnd(a) : 0;
}
COMMANDN(rnd, rndn, ARG_1EXP)

int
explastmillis()
{
	return lastmillis;
}
COMMANDN(millis, explastmillis, ARG_1EXP)







|
|
|
|
|

|
|
|





|
|
|
|
|

|
|
|




|
>
>











<
|
<

|

<
|
<






|

<
|
<


|

<
|
<


|







|
|
>
|
>

|

<
|
<











|

<
|
<










|

<
<
<
<
<
<
<
<
<
<
<
|
<

|
<

<
|
<

|
<

<
|
<

|
<

<
|
<

|
<

<
|
<

>
|
<
<
<
|
<

|
<

<
|
<

|
<

<
|
<

|
<

<
|
<

|
<

<
|
<
|
|
<

<
|
<

|
<
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"];

	[Identifier
	    enumerateIdentifiersUsingBlock:^(__kindof Identifier *identifier) {
		    if (![identifier isKindOfClass:Variable.class] ||
		        ![identifier persisted])
			    return;

		    [stream writeFormat:@"%@ %d\n", identifier.name,
		            *[identifier storage]];
	    }];
	[stream writeString:@"\n"];

	writebinds(stream);
	[stream writeString:@"\n"];

	[Identifier
	    enumerateIdentifiersUsingBlock:^(__kindof Identifier *identifier) {
		    if (![identifier isKindOfClass:Alias.class] ||
		        [identifier.name hasPrefix:@"nextmap_"])
			    return;

		    [stream writeFormat:@"alias \"%@\" [%@]\n", identifier.name,
		            [identifier action]];
	    }];

	[stream close];
}

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]);
}


COMMAND(if, ARG_3STR, ^(OFString *cond, OFString *thenp, OFString *elsep) {

	execute((![cond hasPrefix:@"0"] ? thenp : elsep), true);
})


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);
	}
})


COMMAND(while, ARG_2STR, ^(OFString *cond, OFString *body) {

	while (execute(cond, true))
		execute(body, true);
})


COMMAND(onrelease, ARG_DWN1, ^(bool on, OFString *body) {

	if (!on)
		execute(body, true);
})

void
concat(OFString *s)
{
	alias(@"s", s);
}

COMMAND(concat, ARG_VARI, ^(OFString *s) {
	concat(s);
})

COMMAND(concatword, ARG_VARI, ^(OFString *s) {
	concat([s stringByReplacingOccurrencesOfString:@" " withString:@""]);
})


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;
})


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));
})












COMMAND(+, ARG_2EXP, ^(int a, int b) {

	return a + b;
})



COMMAND(*, ARG_2EXP, ^(int a, int b) {

	return a * b;
})



COMMAND(-, ARG_2EXP, ^(int a, int b) {

	return a - b;
})



COMMAND(div, ARG_2EXP, ^(int a, int b) {

	return b ? a / b : 0;
})



COMMAND(mod, ARG_2EXP, ^(int a, int b) {

	return b ? a % b : 0;
})




COMMAND(=, ARG_2EXP, ^(int a, int b) {

	return (int)(a == b);
})



COMMAND(<, ARG_2EXP, ^(int a, int b) {

	return (int)(a < b);
})



COMMAND(>, ARG_2EXP, ^(int a, int b) {

	return (int)(a > b);
})



COMMAND(strcmp, ARG_2EST, ^(OFString *a, OFString *b) {

	return [a isEqual:b];
})



COMMAND(rnd, ARG_1EXP, ^(int a) {

	return (a > 0 ? rnd(a) : 0);
})



COMMAND(millis, ARG_1EXP, ^(int unused) {

	return lastmillis;
})