Cube  Check-in [dab8760d1e]

Overview
Comment:commands.mm: More cleanups
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: dab8760d1e340f33b5e97238f44ea13a1bc930770a24fe586735a689c06f1951
User & Date: js on 2025-03-08 14:04:23
Other Links: manifest | tags
Context
2025-03-08
14:48
Fix concatword check-in: caec4df75e user: js tags: trunk
14:04
commands.mm: More cleanups check-in: dab8760d1e user: js tags: trunk
13:12
Pad arguments for commands check-in: 852e3a48c3 user: js tags: trunk
Changes

Modified src/commands.mm from [b241a73010] to [07eb61aa1c].

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
// 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"

void
itoa(char *s, int i)
{
	sprintf_s(s)("%d", i);
}

char *
exchangestr(char *o, const char *n)
{
	free(o);
	return strdup(n);
}

// contains ALL vars/commands/aliases
static OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers;

void
alias(OFString *name, OFString *action)
{
	Alias *alias = identifiers[name];












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







1
2
3
4
5
6
7
8
9
10
11
12













13
14
15
16
17
18
19
// 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)
{
	Alias *alias = identifiers[name];
109
110
111
112
113
114
115

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139



140


141
142

143
144
145
146

147
148
149
150
151
152
153
154
155
		identifiers = [[OFMutableDictionary alloc] init];

	identifiers[name] = command;

	return false;
}


char *
parseexp(char *&p, int right) // parse any nested set of () or []
{
	int left = *p++;
	char *word = p;
	for (int brak = 1; brak;) {
		int c = *p++;
		if (c == '\r')
			*(p - 1) = ' '; // hack
		if (c == left)
			brak++;
		else if (c == right)
			brak--;
		else if (!c) {
			p--;
			conoutf(@"missing \"%c\"", right);
			return NULL;
		}
	}
	char *s = strndup(word, p - word - 1);
	if (left == '(') {
		string t;
		// evaluate () exps directly, and substitute result
		@autoreleasepool {



			itoa(t, execute(@(s)));


		}
		s = exchangestr(s, t);

	}
	return s;
}


char *
parseword(char *&p) // parse single argument, including expressions
{
	p += strspn(p, " \t\r");
	if (p[0] == '/' && p[1] == '/')
		p += strcspn(p, "\n\0");
	if (*p == '\"') {
		p++;
		char *word = p;







>

|



















<
<

>
>
>
|
>
>
|
|
>




>

|







96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
		identifiers = [[OFMutableDictionary alloc] init];

	identifiers[name] = command;

	return false;
}

// parse any nested set of () or []
char *
parseexp(char *&p, int right)
{
	int left = *p++;
	char *word = p;
	for (int brak = 1; brak;) {
		int c = *p++;
		if (c == '\r')
			*(p - 1) = ' '; // hack
		if (c == left)
			brak++;
		else if (c == right)
			brak--;
		else if (!c) {
			p--;
			conoutf(@"missing \"%c\"", right);
			return NULL;
		}
	}
	char *s = strndup(word, p - word - 1);
	if (left == '(') {


		@autoreleasepool {
			OFString *t;
			@try {
				t = [OFString
				    stringWithFormat:@"%d", execute(@(s))];
			} @finally {
				free(s);
			}
			s = strdup(t.UTF8String);
		}
	}
	return s;
}

// parse single argument, including expressions
char *
parseword(char *&p)
{
	p += strspn(p, " \t\r");
	if (p[0] == '/' && p[1] == '/')
		p += strcspn(p, "\n\0");
	if (*p == '\"') {
		p++;
		char *word = p;
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
			return [identifier action];
	}

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


int
execute(
    OFString *string, bool isdown) // all evaluation happens here, recursively
{
	@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 = "";
				}

				if (*s == '$')
					// substitute variables
					w[i] = lookup(@(s));
				else
					w[i] = @(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







>

|
<




















|

>
|
|
|
|
|
>
>
>







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
			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));
		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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
					// 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:
							        (int)[w[1]
							            longLongValueWithBase:







|




|







243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
					// 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:
							        (int)[w[1]
							            longLongValueWithBase:
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
						// (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
					val = execute(
					    [[identifier action] copy], isdown);
					break;
				}
			}
		}

		return val;
	}







<
<

|







271
272
273
274
275
276
277


278
279
280
281
282
283
284
285
286
						// (global) arg values so
						// functions can access them
						OFString *t = [OFString
						    stringWithFormat:@"arg%d",
						    i];
						alias(t, w[i]);
					}


					val = execute(
					    [identifier action], isDown);
					break;
				}
			}
		}

		return val;
	}