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
|
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"
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];
|
︙ | | |
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
|
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) // parse any nested set of () or []
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 == '(') {
string t;
// evaluate () exps directly, and substitute result
@autoreleasepool {
OFString *t;
@try {
t = [OFString
itoa(t, execute(@(s)));
}
s = exchangestr(s, t);
stringWithFormat:@"%d", execute(@(s))];
} @finally {
free(s);
}
s = strdup(t.UTF8String);
}
}
return s;
}
// parse single argument, including expressions
char *
parseword(char *&p) // parse single argument, including expressions
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
|
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(
execute(OFString *string, bool isDown)
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 = "";
s = strdup("");
}
@try {
if (*s == '$')
// substitute variables
w[i] = lookup(@(s));
else
w[i] = @(s);
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
|
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];
isDown:isDown];
} else if ([identifier
isKindOfClass:[Variable
class]]) {
// game defined variables
if (isdown) {
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
|
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]);
}
// create new string here because alias
// could rebind itself
val = execute(
[[identifier action] copy], isdown);
[identifier action], isDown);
break;
}
}
}
return val;
}
|
︙ | | |