Cube  Diff

Differences From Artifact [f1b5888486]:

To Artifact [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
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);
}

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

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

	conoutf(@"unknown alias lookup: %s", n + 1);
	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
		char *w[MAXWORDS];
		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] = "";
				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;
					// 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;
			char *c = w[0];
			OFString *c = w[0];
			// strip irc-style command prefix
			if (*c == '/')
				c++;
			if ([c hasPrefix:@"/"])
				c = [c substringFromIndex:1];
			// empty statement
			if (!*c)
			if (c.length == 0)
				continue;

			__kindof Identifier *identifier = identifiers[@(c)];
			__kindof Identifier *identifier = identifiers[c];
			if (identifier == nil) {
				@try {
				val = ATOI(c);
				if (!val && *c != '0')
					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:w
					    callWithArguments:arguments
					         numArguments:numargs
					               isDown:isdown];
				} else if ([identifier
				               isKindOfClass:[Variable
				                                 class]]) {
					// game defined variables
					if (isdown) {
						if (!w[1][0])
						if (w[1].length == 0)
							[identifier printValue];
						else
							[identifier
							    setValue:ATOI(
							                 w[1])];
							    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++) {
						@autoreleasepool {
							// set any arguments as
							// (global) arg values
							// so functions can
						// set any arguments as
						// (global) arg values so
						// functions can access them
							// access them
							OFString *t = [OFString
							    stringWithFormat:
							        @"arg%d", i];
							alias(t, @(w[i]));
						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