Cube  Check-in [22520cd0d9]

Overview
Comment:Clean up argument passing of commands
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 22520cd0d9f9e9a106f9680d90f4c5ac87da8db1c1c304342d6c637557012b22
User & Date: js on 2025-03-07 23:29:58
Other Links: manifest | tags
Context
2025-03-07
23:33
Fix newent accepting hex check-in: 291f7a7146 user: js tags: trunk
23:29
Clean up argument passing of commands check-in: 22520cd0d9 user: js tags: trunk
22:44
Make execute() take an OFString check-in: acc50da079 user: js tags: trunk
Changes

Modified src/Command.h from [40b405f271] to [d308bef816].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import "Identifier.h"

OF_ASSUME_NONNULL_BEGIN

@interface Command : Identifier
@property (readonly, nonatomic) void (*function)();
@property (readonly, nonatomic) int argumentsTypes;

- (instancetype)initWithName:(OFString *)name OF_UNAVAILABLE;
- (instancetype)initWithName:(OFString *)name
                    function:(void (*)())function
              argumentsTypes:(int)argumentsTypes;
- (int)callWithArguments:(char *_Nonnull *_Nonnull)arguments
            numArguments:(size_t)numArguments
                  isDown:(bool)isDown;
@end

OF_ASSUME_NONNULL_END












<
<
|



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


13
14
15
16
#import "Identifier.h"

OF_ASSUME_NONNULL_BEGIN

@interface Command : Identifier
@property (readonly, nonatomic) void (*function)();
@property (readonly, nonatomic) int argumentsTypes;

- (instancetype)initWithName:(OFString *)name OF_UNAVAILABLE;
- (instancetype)initWithName:(OFString *)name
                    function:(void (*)())function
              argumentsTypes:(int)argumentsTypes;


- (int)callWithArguments:(OFArray<OFString *> *)arguments isDown:(bool)isDown;
@end

OF_ASSUME_NONNULL_END

Modified src/Command.mm from [b83be0b4d8] to [3d87b31463].

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


113
114
115
116
117
118

	_function = function;
	_argumentsTypes = argumentsTypes;

	return self;
}

- (int)callWithArguments:(char **)arguments
            numArguments:(size_t)numArguments
                  isDown:(bool)isDown
{
	switch (_argumentsTypes) {
	case ARG_1INT:
		if (isDown)
			((void(__cdecl *)(int))_function)(ATOI(arguments[1]));

		break;
	case ARG_2INT:
		if (isDown)
			((void(__cdecl *)(int, int))_function)(
			    ATOI(arguments[1]), ATOI(arguments[2]));

		break;
	case ARG_3INT:
		if (isDown)
			((void(__cdecl *)(int, int, int))_function)(
			    ATOI(arguments[1]), ATOI(arguments[2]),

			    ATOI(arguments[3]));
		break;
	case ARG_4INT:
		if (isDown)
			((void(__cdecl *)(int, int, int, int))_function)(
			    ATOI(arguments[1]), ATOI(arguments[2]),

			    ATOI(arguments[3]), ATOI(arguments[4]));

		break;
	case ARG_NONE:
		if (isDown)
			((void(__cdecl *)())_function)();
		break;
	case ARG_1STR:
		if (isDown)
			((void(__cdecl *)(OFString *))_function)(
			    @(arguments[1]));
		break;
	case ARG_2STR:
		if (isDown)
			((void(__cdecl *)(OFString *, OFString *))_function)(
			    @(arguments[1]), @(arguments[2]));
		break;
	case ARG_3STR:
		if (isDown)
			((void(__cdecl *)(
			    OFString *, OFString *, OFString *))_function)(
			    @(arguments[1]), @(arguments[2]), @(arguments[3]));
		break;
	case ARG_5STR:
		if (isDown)
			((void(__cdecl *)(OFString *, OFString *, OFString *,
			    OFString *, OFString *))_function)(@(arguments[1]),
			    @(arguments[2]), @(arguments[3]), @(arguments[4]),
			    @(arguments[5]));
		break;
	case ARG_DOWN:
		((void(__cdecl *)(bool))_function)(isDown);
		break;
	case ARG_DWN1:
		((void(__cdecl *)(bool, OFString *))_function)(
		    isDown, @(arguments[1]));
		break;
	case ARG_1EXP:
		if (isDown)
			return ((int(__cdecl *)(int))_function)(
			    execute(@(arguments[1])));
		break;
	case ARG_2EXP:
		if (isDown)
			return ((int(__cdecl *)(int, int))_function)(
			    execute(@(arguments[1])), execute(@(arguments[2])));
		break;
	case ARG_1EST:
		if (isDown)
			return ((int(__cdecl *)(OFString *))_function)(
			    @(arguments[1]));
		break;
	case ARG_2EST:
		if (isDown)
			return (
			    (int(__cdecl *)(OFString *, OFString *))_function)(
			    @(arguments[1]), @(arguments[2]));
		break;
	case ARG_VARI:
		if (isDown) {
			// limit, remove
			string r;
			r[0] = 0;
			for (int i = 1; i < numArguments; i++) {
				// make string-list out of all arguments
				strcat_s(r, arguments[i]);
				if (i == numArguments - 1)
					break;
				strcat_s(r, " ");
			}
			((void(__cdecl *)(OFString *))_function)(@(r));
		}


		break;
	}

	return 0;
}
@end







|
<
<




|
>




|
>




|
>
|




|
>
|
>







|
<




|





|




|
|
|






|




|




|




|



|
|
<


|

<
<
<
<
<
<
<
<
<
|
<
>
>






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

	_function = function;
	_argumentsTypes = argumentsTypes;

	return self;
}

- (int)callWithArguments:(OFArray<OFString *> *)arguments isDown:(bool)isDown


{
	switch (_argumentsTypes) {
	case ARG_1INT:
		if (isDown)
			((void(__cdecl *)(int))_function)(
			    (int)[arguments[1] longLongValueWithBase:0]);
		break;
	case ARG_2INT:
		if (isDown)
			((void(__cdecl *)(int, int))_function)(
			    (int)[arguments[1] longLongValueWithBase:0],
			    (int)[arguments[2] longLongValueWithBase:0]);
		break;
	case ARG_3INT:
		if (isDown)
			((void(__cdecl *)(int, int, int))_function)(
			    (int)[arguments[1] longLongValueWithBase:0],
			    (int)[arguments[2] longLongValueWithBase:0],
			    (int)[arguments[3] longLongValueWithBase:0]);
		break;
	case ARG_4INT:
		if (isDown)
			((void(__cdecl *)(int, int, int, int))_function)(
			    (int)[arguments[1] longLongValueWithBase:0],
			    (int)[arguments[2] longLongValueWithBase:0],
			    (int)[arguments[3] longLongValueWithBase:0],
			    (int)[arguments[4] longLongValueWithBase:0]);
		break;
	case ARG_NONE:
		if (isDown)
			((void(__cdecl *)())_function)();
		break;
	case ARG_1STR:
		if (isDown)
			((void(__cdecl *)(OFString *))_function)(arguments[1]);

		break;
	case ARG_2STR:
		if (isDown)
			((void(__cdecl *)(OFString *, OFString *))_function)(
			    arguments[1], arguments[2]);
		break;
	case ARG_3STR:
		if (isDown)
			((void(__cdecl *)(
			    OFString *, OFString *, OFString *))_function)(
			    arguments[1], arguments[2], arguments[3]);
		break;
	case ARG_5STR:
		if (isDown)
			((void(__cdecl *)(OFString *, OFString *, OFString *,
			    OFString *, OFString *))_function)(arguments[1],
			    arguments[2], arguments[3], arguments[4],
			    arguments[5]);
		break;
	case ARG_DOWN:
		((void(__cdecl *)(bool))_function)(isDown);
		break;
	case ARG_DWN1:
		((void(__cdecl *)(bool, OFString *))_function)(
		    isDown, arguments[1]);
		break;
	case ARG_1EXP:
		if (isDown)
			return ((int(__cdecl *)(int))_function)(
			    execute(arguments[1]));
		break;
	case ARG_2EXP:
		if (isDown)
			return ((int(__cdecl *)(int, int))_function)(
			    execute(arguments[1]), execute(arguments[2]));
		break;
	case ARG_1EST:
		if (isDown)
			return ((int(__cdecl *)(OFString *))_function)(
			    arguments[1]);
		break;
	case ARG_2EST:
		if (isDown)
			return ((int(__cdecl *)(OFString *,
			    OFString *))_function)(arguments[1], arguments[2]);

		break;
	case ARG_VARI:
		if (isDown)
			// limit, remove









			((void(__cdecl *)(OFString *))_function)([[arguments

			    objectsInRange:OFMakeRange(1, arguments.count - 1)]
			    componentsJoinedByString:@" "]);
		break;
	}

	return 0;
}
@end

Modified src/commands.mm from [f1b5888486] to [5c63a6ff95].

166
167
168
169
170
171
172
173
174
175
176
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
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
289
	char *word = p;
	p += strcspn(p, "; \t\r\n\0");
	if (p - word == 0)
		return NULL;
	return newstring(word, p - word);
}

char *
lookup(char *n) // find value of ident referenced with $ in exp
{
	@autoreleasepool {
		__kindof Identifier *identifier = identifiers[@(n + 1)];


		if ([identifier isKindOfClass:[Variable class]]) {
			string t;
			itoa(t, *[identifier storage]);
			return exchangestr(n, t);
		} else if ([identifier isKindOfClass:[Alias class]])
			return exchangestr(n, [identifier action].UTF8String);
	}

	conoutf(@"unknown alias lookup: %s", n + 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
		char *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 == '$')
					s = lookup(s); // substitute variables


				w[i] = s;
			}

			p += strcspn(p, ";\n\0");
			// more statements if this isn't the end of the string
			cont = *p++ != 0;
			char *c = w[0];
			// strip irc-style command prefix
			if (*c == '/')
				c++;
			// empty statement
			if (!*c)
				continue;

			__kindof Identifier *identifier = identifiers[@(c)];
			if (identifier == nil) {

				val = ATOI(c);
				if (!val && *c != '0')
					conoutf(@"unknown command: %s", c);

			} else {
				if ([identifier
				        isKindOfClass:[Command class]]) {
					// game defined commands use very
					// ad-hoc function signature, and just
					// call it




					val = [identifier
					    callWithArguments:w
					         numArguments:numargs
					               isDown:isdown];
				} else if ([identifier
				               isKindOfClass:[Variable
				                                 class]]) {
					// game defined variables
					if (isdown) {
						if (!w[1][0])
							[identifier printValue];
						else
							[identifier
							    setValue:ATOI(
							                 w[1])];


					}
				} else if ([identifier
				               isKindOfClass:[Alias class]]) {
					// alias, also used as functions and
					// (global) variables
					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
					val = execute(
					    [[identifier action] copy], isdown);
					break;
				}
			}
			loopj(numargs) gp()->deallocstr(w[j]);
		}

		return val;
	}
}

// tab-completion of all identifiers







|
|


|
>


|
|
<

|


|











|







|









|
>
>
|





|

|
|

|


|

>
|
|

>






>
>
>
>

|
<






|



|
|
>
>






<
|
|
|
<
|
|
|
|
<








<







166
167
168
169
170
171
172
173
174
175
176
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
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
289
290
291
292
293
294
	char *word = p;
	p += strcspn(p, "; \t\r\n\0");
	if (p - word == 0)
		return NULL;
	return newstring(word, p - word);
}

OFString *
lookup(OFString *n) // find value of ident referenced with $ in exp
{
	@autoreleasepool {
		__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]);
	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
			if ([c hasPrefix:@"/"])
				c = [c substringFromIndex:1];
			// empty statement
			if (c.length == 0)
				continue;

			__kindof Identifier *identifier = identifiers[c];
			if (identifier == nil) {
				@try {
					val = (int)[c longLongValueWithBase:0];
				} @catch (OFInvalidFormatException *e) {
					conoutf(@"unknown command: %s", 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 + 1];
					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:
							                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]);

					}
					// create new string here because alias
					// could rebind itself
					val = execute(
					    [[identifier action] copy], isdown);
					break;
				}
			}

		}

		return val;
	}
}

// tab-completion of all identifiers