Cube  Diff

Differences From Artifact [a0d92d1a92]:

To Artifact [ddbc7f66e7]:


1
2
3
4


5
6
7
8
9
10
11
// 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"



enum { ID_VAR, ID_COMMAND, ID_ALIAS };

@interface Ident : OFObject
@property (nonatomic) int type; // one of ID_* above
@property (copy, nonatomic) OFString *name;
@property (nonatomic) int min, max;           // ID_VAR




>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
// 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>

enum { ID_VAR, ID_COMMAND, ID_ALIAS };

@interface Ident : OFObject
@property (nonatomic) int type; // one of ID_* above
@property (copy, nonatomic) OFString *name;
@property (nonatomic) int min, max;           // ID_VAR
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
113
114
115
116
	return newstring(n);
}

// contains ALL vars/commands/aliases
OFMutableDictionary<OFString *, Ident *> *idents;

void
alias(char *name, char *action)
{
	@autoreleasepool {
		Ident *b = idents[@(name)];

		if (!b) {
			Ident *b = [[Ident alloc] init];
			b.type = ID_ALIAS;
			b.name = @(name);
			b.action = @(action);
			b.persist = true;

			idents[b.name] = b;
		} else {
			if (b.type == ID_ALIAS)
				b.action = @(action);
			else
				conoutf(
				    @"cannot redefine builtin %s with an alias",
				    name);
		}
	}
}

COMMAND(alias, ARG_2STR)

int
variable(char *name, int min, int cur, int max, int *storage, void (*fun)(),
    bool persist)
{
	if (idents == nil)
		idents = [[OFMutableDictionary alloc] init];

	@autoreleasepool {
		Ident *v = [[Ident alloc] init];
		v.type = ID_VAR;
		v.name = @(name);
		v.min = min;
		v.max = max;
		v.storage = storage;
		v.fun = fun;
		v.persist = persist;

		idents[v.name] = v;
	}

	return cur;
}

void
setvar(char *name, int i)
{
	@autoreleasepool {
		*idents[@(name)].storage = i;
	}
}

int
getvar(char *name)
{
	@autoreleasepool {
		return *idents[@(name)].storage;
	}
}

bool
identexists(char *name)
{
	@autoreleasepool {
		return (idents[@(name)] != nil);
	}
}

OFString *
getalias(OFString *name)
{
	Ident *i = idents[name];
	return i != nil && i.type == ID_ALIAS ? i.action : nil;







|

<
|

|
|
|
|
|
|

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



|





<
|
|
|
|
|
|
|
|

|
<





|

<
|
<



|

<
|
<



|

<
|
<







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
	return newstring(n);
}

// contains ALL vars/commands/aliases
OFMutableDictionary<OFString *, Ident *> *idents;

void
alias(OFString *name, OFString *action)
{

	Ident *b = idents[name];

	if (b == nil) {
		Ident *b = [[Ident alloc] init];
		b.type = ID_ALIAS;
		b.name = name;
		b.action = action;
		b.persist = true;

		idents[b.name] = b;
	} else {
		if (b.type == ID_ALIAS)
			b.action = action;
		else
			conoutf(
			    @"cannot redefine builtin %s with an alias", name.UTF8String);

	}
}


COMMAND(alias, ARG_2STR)

int
variable(OFString *name, int min, int cur, int max, int *storage, void (*fun)(),
    bool persist)
{
	if (idents == nil)
		idents = [[OFMutableDictionary alloc] init];


	Ident *v = [[Ident alloc] init];
	v.type = ID_VAR;
	v.name = name;
	v.min = min;
	v.max = max;
	v.storage = storage;
	v.fun = fun;
	v.persist = persist;

	idents[name] = v;


	return cur;
}

void
setvar(OFString *name, int i)
{

	*idents[name].storage = i;

}

int
getvar(OFString *name)
{

	return *idents[name].storage;

}

bool
identexists(OFString *name)
{

	return (idents[name] != nil);

}

OFString *
getalias(OFString *name)
{
	Ident *i = idents[name];
	return i != nil && i.type == ID_ALIAS ? i.action : nil;
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
							    ATOI(w[4]));
						break;
					case ARG_NONE:
						if (isdown)
							((void(__cdecl *)())
							        ID.fun)();
						break;
					case ARG_1CSTR:
						if (isdown)

							((void(__cdecl *)(


							    char *))ID.fun)(
							    w[1]);


						break;
					case ARG_2STR:
						if (isdown)

							((void(__cdecl *)(

							    char *,

							    char *))ID.fun)(

							    w[1], w[2]);


						break;
					case ARG_3STR:
						if (isdown)

							((void(__cdecl *)(

							    char *, char *,


							    char *))ID.fun)(


							    w[1], w[2], w[3]);


						break;
					case ARG_5STR:
						if (isdown)

							((void(__cdecl *)(

							    char *, char *,
							    char *, char *,



							    char *))ID.fun)(
							    w[1], w[2], w[3],



							    w[4], w[5]);


						break;
					case ARG_DOWN:
						((void(__cdecl *)(bool))ID.fun)(
						    isdown);
						break;
					case ARG_DWN1:
						((void(__cdecl *)(







|
|
>
|
>
>
|
|
>
>


|
>
|
>
|
>
|
>
|
>
>


|
>
|
>
|
>
>
|
>
>
|
>
>


|
>
|
>
|
|
>
>
>
|
|
>
>
>
|
>
>







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
							    ATOI(w[4]));
						break;
					case ARG_NONE:
						if (isdown)
							((void(__cdecl *)())
							        ID.fun)();
						break;
					case ARG_1STR:
						if (isdown) {
							@autoreleasepool {
								((void(
								    __cdecl *)(
								    OFString *))
								        ID.fun)(
								    @(w[1]));
							}
						}
						break;
					case ARG_2STR:
						if (isdown) {
							@autoreleasepool {
								((void(
								    __cdecl *)(
								    OFString *,
								    OFString *))
								        ID.fun)(
								    @(w[1]),
								    @(w[2]));
							}
						}
						break;
					case ARG_3STR:
						if (isdown) {
							@autoreleasepool {
								((void(
								    __cdecl *)(
								    OFString *,
								    OFString *,
								    OFString *))
								        ID.fun)(
								    @(w[1]),
								    @(w[2]),
								    @(w[3]));
							}
						}
						break;
					case ARG_5STR:
						if (isdown) {
							@autoreleasepool {
								((void(
								    __cdecl *)(
								    OFString *,
								    OFString *,
								    OFString *,
								    OFString *,
								    OFString *))
								        ID.fun)(
								    @(w[1]),
								    @(w[2]),
								    @(w[3]),
								    @(w[4]),
								    @(w[5]));
							}
						}
						break;
					case ARG_DOWN:
						((void(__cdecl *)(bool))ID.fun)(
						    isdown);
						break;
					case ARG_DWN1:
						((void(__cdecl *)(
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
								strcat_s(
								    r, " ");
							}
							((void(__cdecl *)(
							    char *))ID.fun)(r);
							break;
						}
					case ARG_1STR:
						if (isdown) {
							@autoreleasepool {
								((void(
								    __cdecl *)(
								    OFString *))
								        ID.fun)(
								    @(w[1]));
							}
						}
						break;
					}
					break;

				// game defined variables
				case ID_VAR:
					if (isdown) {
						if (!w[1][0])







<
<
<
<
<
<
<
<
<
<
<







398
399
400
401
402
403
404











405
406
407
408
409
410
411
								strcat_s(
								    r, " ");
							}
							((void(__cdecl *)(
							    char *))ID.fun)(r);
							break;
						}











					}
					break;

				// game defined variables
				case ID_VAR:
					if (isdown) {
						if (!w[1][0])
475
476
477
478
479
480
481

482
483

484


485
486

487
488
489
490
491
492
493
					}
					break;

				// alias, also used as functions and (global)
				// variables
				case ID_ALIAS:
					for (int i = 1; i < numargs; i++) {

						// set any arguments as
						// (global) arg values so

						// functions can access them


						sprintf_sd(t)("arg%d", i);
						alias(t, w[i]);

					}
					// create new string here because alias
					// could rebind itself
					char *action =
					    newstring(ID.action.UTF8String);
					val = execute(action, isdown);
					gp()->deallocstr(action);







>
|
|
>
|
>
>
|
|
>







483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
					}
					break;

				// alias, also used as functions and (global)
				// variables
				case ID_ALIAS:
					for (int i = 1; i < numargs; i++) {
						@autoreleasepool {
							// set any arguments as
							// (global) arg values
							// so functions can
							// access them
							OFString *t = [OFString
							    stringWithFormat:
							        @"arg%d", i];
							alias(t, @(w[i]));
						}
					}
					// create new string here because alias
					// could rebind itself
					char *action =
					    newstring(ID.action.UTF8String);
					val = execute(action, isdown);
					gp()->deallocstr(action);
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
	}];
	completeidx++;
	if (completeidx >= idx)
		completeidx = 0;
}

bool
execfile(char *cfgfile)
{

	string s;
	strcpy_s(s, cfgfile);
	char *buf = loadfile(path(s), NULL);
	if (!buf)
		return false;
	execute(buf);
	free(buf);
	return true;
}


void
exec(char *cfgfile)
{
	if (!execfile(cfgfile))

		conoutf(@"could not read \"%s\"", cfgfile);


}

void
writecfg()
{
	FILE *f = fopen("config.cfg", "w");
	if (!f)







|

>
|
|
|
|
|
|
|
|
|
|
>

|

|
>
|
>
>







550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
	}];
	completeidx++;
	if (completeidx >= idx)
		completeidx = 0;
}

bool
execfile(OFString *cfgfile)
{
	@autoreleasepool {
		string s;
		strcpy_s(s, cfgfile.UTF8String);
		char *buf = loadfile(path(s), NULL);
		if (!buf)
			return false;
		execute(buf);
		free(buf);
		return true;
	}
}

void
exec(OFString *cfgfile)
{
	if (!execfile(cfgfile)) {
		@autoreleasepool {
			conoutf(@"could not read \"%s\"", cfgfile.UTF8String);
		}
	}
}

void
writecfg()
{
	FILE *f = fopen("config.cfg", "w");
	if (!f)
596
597
598
599
600
601
602
603
604
605
606
607
608
609

610
611
612


613
614

615


616
617
618



619
620
621
622
623
624
625
626

627
628
629




630
631
632


633
634
635
636
637
638
639
640
641
642
643

644

645
646
647
648
649
650
651
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(char *name, int v)
{
	string b;
	itoa(b, v);
	alias(name, b);
}


void
ifthen(char *cond, char *thenp, char *elsep)
{


	execute(cond[0] != '0' ? thenp : elsep);
}




void
loopa(char *times, char *body)
{



	int t = atoi(times);
	loopi(t)
	{
		intset("i", i);
		execute(body);
	}
}


void
whilea(char *cond, char *body)
{




	while (execute(cond))
		execute(body);
} // can't get any simpler than this :)



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

void
concat(char *s)
{

	alias("s", s);

}

void
concatword(char *s)
{
	for (char *a = s, *b = s; *a = *b; b++)
		if (*a != ' ')







|

<
|
|
|
|
>

|

>
>
|
|
>
|
>
>

|

>
>
>
|
|
|
|
|
|
|
|
>

|

>
>
>
>
|
|
<
>
>











>
|
>







614
615
616
617
618
619
620
621
622

623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662

663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
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)
{
	@autoreleasepool {
		std::unique_ptr<char> cmd(strdup(
		    (cond.UTF8String[0] != '0' ? thenp : elsep).UTF8String));

		execute(cmd.get());
	}
}

void
loopa(OFString *times, OFString *body_)
{
	@autoreleasepool {
		int t = (int)times.longLongValue;
		std::unique_ptr<char> body(strdup(body_.UTF8String));

		loopi(t)
		{
			intset(@"i", i);
			execute(body.get());
		}
	}
}

void
whilea(OFString *cond_, OFString *body_)
{
	@autoreleasepool {
		std::unique_ptr<char> cond(strdup(cond_.UTF8String));
		std::unique_ptr<char> body(strdup(body_.UTF8String));

		while (execute(cond.get()))
			execute(body.get());

	}
}

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

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

void
concatword(char *s)
{
	for (char *a = s, *b = s; *a = *b; b++)
		if (*a != ' ')
662
663
664
665
666
667
668
669
670

671



672
673
674

675
676
677
678
679
680
681
682
683
684
685
686
687
688
	while (*a)
		if (*a++ == ' ')
			n++;
	return n + 1;
}

void
at(char *s, char *pos)
{

	int n = atoi(pos);



	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_1CSTR)
COMMAND(concat, ARG_VARI)
COMMAND(concatword, ARG_VARI)
COMMAND(at, ARG_2STR)
COMMAND(listlen, ARG_1EST)

int
add(int a, int b)







|

>
|
>
>
>
|
|
|
>






|







696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
	while (*a)
		if (*a++ == ' ')
			n++;
	return n + 1;
}

void
at(OFString *s_, OFString *pos)
{
	@autoreleasepool {
		int n = (int)pos.longLongValue;
		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)
COMMAND(concat, ARG_VARI)
COMMAND(concatword, ARG_VARI)
COMMAND(at, ARG_2STR)
COMMAND(listlen, ARG_1EST)

int
add(int a, int b)