1 #define MAX_RPN_STACK 8
4 string rpn_stack[MAX_RPN_STACK];
8 return rpn_stack[rpn_sp];
10 print("rpn: stack underflow\n");
15 void rpn_push(string s) {
16 if(rpn_sp < MAX_RPN_STACK) {
17 rpn_stack[rpn_sp] = s;
20 print("rpn: stack overflow\n");
26 return rpn_stack[rpn_sp - 1];
28 print("rpn: empty stack\n");
33 void rpn_set(string s) {
35 rpn_stack[rpn_sp - 1] = s;
37 print("rpn: empty stack\n");
41 float rpn_getf() { return stof(rpn_get()); }
42 float rpn_popf() { return stof(rpn_pop()); }
43 void rpn_pushf(float f) { return rpn_push(ftos(f)); }
44 void rpn_setf(float f) { return rpn_set(ftos(f)); }
46 float GameCommand_Generic(string command)
49 argc = tokenize(command);
52 print(" rpn EXPRESSION... - a RPN calculator.\n");
53 print(" Operator description (x: string, s: set, f: float):\n");
54 print(" x pop -----------------------------> : removes the top\n");
55 print(" x dup -----------------------------> x x : duplicates the top\n");
56 print(" x x exch --------------------------> x x : swap the top two\n");
57 print(" /cvarname load --------------------> x : loads a cvar\n");
58 print(" /cvarname x def -------------------> : writes to a cvar\n");
59 print(" f f add|sub|mul|div|mod -----------> f : adds/... two numbers\n");
60 print(" f neg|abs|sgn|rand ----------------> f : negates/... a number\n");
61 print(" s s union|intersection|difference -> s : set operations\n");
62 print(" s shuffle -------------------------> s : randomly arrange elements\n");
63 print(" Set operations operate on 'such''strings' like g_maplist.\n");
64 print(" Unknown tokens insert their cvar value.\n");
79 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
81 rpncmd = argv(rpnpos);
84 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
86 } else if(substring(rpncmd, 0, 1) == "0") {
88 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
90 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
92 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
93 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
94 } else if(rpncmd == "def" || rpncmd == "=") {
98 registercvar(s2, "", 0);
100 registercvar(s2, "");
102 if(!rpn_error) // don't change cvars if a stack error had happened!
104 } else if(rpncmd == "load") {
105 rpn_set(cvar_string(rpn_get()));
106 } else if(rpncmd == "exch") {
111 } else if(rpncmd == "dup") {
113 } else if(rpncmd == "pop") {
115 } else if(rpncmd == "add" || rpncmd == "+") {
117 rpn_setf(rpn_getf() + f);
118 } else if(rpncmd == "sub" || rpncmd == "-") {
120 rpn_setf(rpn_getf() - f);
121 } else if(rpncmd == "mul" || rpncmd == "*") {
123 rpn_setf(rpn_getf() * f);
124 } else if(rpncmd == "div" || rpncmd == "/") {
126 rpn_setf(rpn_getf() / f);
127 } else if(rpncmd == "mod" || rpncmd == "%") {
130 rpn_setf(f2 - f * floor(f2 / f));
131 } else if(rpncmd == "abs") {
132 rpn_setf(fabs(rpn_getf()));
133 } else if(rpncmd == "sgn") {
141 } else if(rpncmd == "neg" || rpncmd == "~") {
142 rpn_setf(-rpn_getf());
143 } else if(rpncmd == "rand") {
144 rpn_setf(ceil(random() * rpn_getf()) - 1);
145 } else if(rpncmd == "union") {
150 f2 = tokenize(strcat(s, s2));
151 // tokens 0..(f-1) represent s
152 // tokens f..f2 represent s2
153 // UNION: add all tokens to s that are in s2 but not in s
155 for(i = 0; i < f; ++i)
156 s = strcat(s, "'", argv(i), "'");
157 for(i = f; i < f2; ++i) {
158 for(j = 0; j < f; ++j)
159 if(argv(i) == argv(j))
161 s = strcat(s, "'", argv(i), "'");
166 } else if(rpncmd == "intersection") {
171 f2 = tokenize(strcat(s, s2));
172 // tokens 0..(f-1) represent s
173 // tokens f..f2 represent s2
174 // INTERSECTION: keep only the tokens from s that are also in s2
176 for(i = 0; i < f; ++i) {
177 for(j = f; j < f2; ++j)
178 if(argv(i) == argv(j))
180 s = strcat(s, "'", argv(i), "'");
186 } else if(rpncmd == "difference") {
191 f2 = tokenize(strcat(s, s2));
192 // tokens 0..(f-1) represent s
193 // tokens f..f2 represent s2
194 // DIFFERENCE: keep only the tokens from s that are not in s2
196 for(i = 0; i < f; ++i) {
197 for(j = f; j < f2; ++j)
198 if(argv(i) == argv(j))
199 goto skip_difference;
200 s = strcat(s, "'", argv(i), "'");
205 } else if(rpncmd == "shuffle") {
210 for(i = 0; i < f - 1; ++i) {
211 // move a random item from i..f-1 to position i
213 f2 = ceil(random() * (f - i) + i) - 1;
214 for(j = 0; j < i; ++j)
215 s = strcat(s, "'", argv(j), "'");
216 s = strcat(s, "'", argv(f2), "'");
217 for(j = i; j < f; ++j)
219 s = strcat(s, "'", argv(j), "'");
225 } else if(rpncmd == "fexists_assert") {
229 f = fopen(s, FILE_READ);
233 print("rpn: ERROR: ", s, " does not exist!\n");
238 rpn_push(cvar_string(rpncmd));
246 print("rpn: still on stack: ", s, "\n");