5 return rpn_stack[rpn_sp];
7 print("rpn: stack underflow\n");
12 void rpn_push(string s)
14 if(rpn_sp < MAX_RPN_STACK) {
15 rpn_stack[rpn_sp] = s;
18 print("rpn: stack overflow\n");
25 return rpn_stack[rpn_sp - 1];
27 print("rpn: empty stack\n");
32 void rpn_set(string s)
35 rpn_stack[rpn_sp - 1] = s;
37 print("rpn: empty stack\n");
42 float rpn_getf() { return stof(rpn_get()); }
43 float rpn_popf() { return stof(rpn_pop()); }
44 void rpn_pushf(float f) { return rpn_push(ftos(f)); }
45 void rpn_setf(float f) { return rpn_set(ftos(f)); }
47 void GenericCommand_rpn(float request, float argc, string command)
51 case CMD_REQUEST_COMMAND:
53 float i, j, f, n, f2, f3, rpnpos;
55 string s, s2, c, rpncmd;
60 db_put(rpn_db, "stack.pointer", "0");
61 db_put(rpn_db, "stack.pos", "-1");
71 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
73 rpncmd = argv(rpnpos);
76 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
78 } else if(substring(rpncmd, 0, 1) == "0") {
80 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
82 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
84 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
85 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
86 } else if(rpncmd == "clear") {
88 } else if(rpncmd == "def" || rpncmd == "=") {
95 registercvar(s2, "", 0);
99 if(!rpn_error) // don't change cvars if a stack error had happened!
104 print("rpn: empty cvar name for 'def'\n");
107 } else if(rpncmd == "defs" || rpncmd == "@") {
111 while(rpn_sp > 1 && (j || i > 0))
113 s = strcat("/", rpn_pop(), " ", s);
120 registercvar(s2, "", 0);
122 registercvar(s2, "");
124 if(!rpn_error) // don't change cvars if a stack error had happened!
129 print("rpn: empty cvar name for 'defs'\n");
132 } else if(rpncmd == "load") {
133 rpn_set(cvar_string(rpn_get()));
134 } else if(rpncmd == "exch") {
139 } else if(rpncmd == "dup") {
141 } else if(rpncmd == "pop") {
143 } else if(rpncmd == "add" || rpncmd == "+") {
145 rpn_setf(rpn_getf() + f);
146 } else if(rpncmd == "sub" || rpncmd == "-") {
148 rpn_setf(rpn_getf() - f);
149 } else if(rpncmd == "mul" || rpncmd == "*") {
151 rpn_setf(rpn_getf() * f);
152 } else if(rpncmd == "div" || rpncmd == "/") {
154 rpn_setf(rpn_getf() / f);
155 } else if(rpncmd == "mod" || rpncmd == "%") {
158 rpn_setf(f2 - f * floor(f2 / f));
159 } else if(rpncmd == "abs") {
160 rpn_setf(fabs(rpn_getf()));
161 } else if(rpncmd == "sgn") {
169 } else if(rpncmd == "neg" || rpncmd == "~") {
170 rpn_setf(-rpn_getf());
171 } else if(rpncmd == "floor" || rpncmd == "f") {
172 rpn_setf(floor(rpn_getf()));
173 } else if(rpncmd == "ceil" || rpncmd == "c") {
174 rpn_setf(ceil(rpn_getf()));
175 } else if(rpncmd == "max") {
178 rpn_setf(max(f2, f));
179 } else if(rpncmd == "min") {
182 rpn_setf(min(f2, f));
183 } else if(rpncmd == "bound") {
187 rpn_setf(bound(f3, f2, f));
188 } else if(rpncmd == "when") {
196 } else if(rpncmd == ">" || rpncmd == "gt") {
198 rpn_setf(rpn_getf() > f);
199 } else if(rpncmd == "<" || rpncmd == "lt") {
201 rpn_setf(rpn_getf() < f);
202 } else if(rpncmd == "==" || rpncmd == "eq") {
204 rpn_setf(rpn_getf() == f);
205 } else if(rpncmd == ">=" || rpncmd == "ge") {
207 rpn_setf(rpn_getf() >= f);
208 } else if(rpncmd == "<=" || rpncmd == "le") {
210 rpn_setf(rpn_getf() <= f);
211 } else if(rpncmd == "!=" || rpncmd == "ne") {
213 rpn_setf(rpn_getf() != f);
214 } else if(rpncmd == "rand") {
215 rpn_setf(ceil(random() * rpn_getf()) - 1);
216 } else if(rpncmd == "crc16") {
217 rpn_setf(crc16(FALSE, rpn_get()));
218 } else if(rpncmd == "put") {
224 db_put(rpn_db, s, s2);
226 } else if(rpncmd == "get") {
229 rpn_push(db_get(rpn_db, s));
230 } else if(rpncmd == "dbpush") {
234 i = stof(db_get(rpn_db, "stack.pointer"));
235 db_put(rpn_db, "stack.pointer", ftos(i+1));
236 db_put(rpn_db, strcat("stack.", ftos(i)), s);
239 db_put(rpn_db, "stack.pos", "0");
240 } else if(rpncmd == "dbpop") {
241 i = stof(db_get(rpn_db, "stack.pointer"));
245 db_put(rpn_db, "stack.pointer", s);
246 rpn_push(db_get(rpn_db, strcat("stack.", s)));
247 j = stof(db_get(rpn_db, "stack.pos"));
249 db_put(rpn_db, "stack.pos", ftos(i-2));
252 print("rpn: database underflow\n");
254 } else if(rpncmd == "dbget") {
256 i = stof(db_get(rpn_db, "stack.pointer"));
259 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
262 print("rpn: database empty\n");
264 } else if(rpncmd == "dblen") {
265 rpn_push(db_get(rpn_db, "stack.pointer"));
266 } else if(rpncmd == "dbclr") {
268 rpn_db = db_create();
269 db_put(rpn_db, "stack.pointer", "0");
270 db_put(rpn_db, "stack.pos", "-1");
271 } else if(rpncmd == "dbsave") {
275 } else if(rpncmd == "dbload") {
282 } else if(rpncmd == "dbins") {
287 j = stof(db_get(rpn_db, "stack.pointer"));
288 i = stof(db_get(rpn_db, "stack.pos"));
293 db_put(rpn_db, "stack.pos", "0");
296 db_put(rpn_db, "stack.pointer", ftos(j+1));
297 for(--j; j >= i; --j)
299 db_put(rpn_db, strcat("stack.", ftos(j+1)),
300 db_get(rpn_db, (strcat("stack.", ftos(j))))
303 db_put(rpn_db, strcat("stack.", ftos(i)), s);
305 } else if(rpncmd == "dbext") {
306 j = stof(db_get(rpn_db, "stack.pointer"));
307 i = stof(db_get(rpn_db, "stack.pos"));
311 print("rpn: empty database\n");
314 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
315 db_put(rpn_db, "stack.pointer", ftos(j));
318 db_put(rpn_db, "stack.pos", ftos(j-1));
322 db_put(rpn_db, strcat("stack.", ftos(i)),
323 db_get(rpn_db, (strcat("stack.", ftos(i+1))))
329 } else if(rpncmd == "dbread") {
330 s = db_get(rpn_db, "stack.pos");
333 rpn_push(db_get(rpn_db, strcat("stack.", s)));
336 print("rpn: empty database\n");
338 } else if(rpncmd == "dbat") {
339 rpn_push(db_get(rpn_db, "stack.pos"));
340 } else if(rpncmd == "dbmov") {
341 j = stof(db_get(rpn_db, "stack.pointer"));
342 i = stof(db_get(rpn_db, "stack.pos"));
348 print("rpn: database cursor out of bounds\n");
353 db_put(rpn_db, "stack.pos", ftos(i));
356 } else if(rpncmd == "dbgoto") {
358 j = stof(db_get(rpn_db, "stack.pointer"));
362 print("rpn: empty database, cannot move cursor\n");
367 i = stof(db_get(rpn_db, "stack.pointer"))-1;
373 j = stof(db_get(rpn_db, "stack.pointer"));
376 print("rpn: database cursor destination out of bounds\n");
381 db_put(rpn_db, "stack.pos", ftos(i));
384 } else if(rpncmd == "union") {
388 f = tokenize_console(s);
389 f2 = tokenize_console(strcat(s, " ", s2));
390 // tokens 0..(f-1) represent s
391 // tokens f..f2 represent s2
392 // UNION: add all tokens to s that are in s2 but not in s
394 for(i = 0; i < f; ++i)
395 s = strcat(s, " ", argv(i));
396 for(i = f; i < f2; ++i) {
397 for(j = 0; j < f; ++j)
398 if(argv(i) == argv(j))
400 s = strcat(s, " ", argv(i));
403 if(substring(s, 0, 1) == " ")
404 s = substring(s, 1, 99999);
406 tokenize_console(command);
407 } else if(rpncmd == "intersection") {
411 f = tokenize_console(s);
412 f2 = tokenize_console(strcat(s, " ", s2));
413 // tokens 0..(f-1) represent s
414 // tokens f..f2 represent s2
415 // INTERSECTION: keep only the tokens from s that are also in s2
417 for(i = 0; i < f; ++i) {
418 for(j = f; j < f2; ++j)
419 if(argv(i) == argv(j))
421 s = strcat(s, " ", argv(i));
425 if(substring(s, 0, 1) == " ")
426 s = substring(s, 1, 99999);
428 tokenize_console(command);
429 } else if(rpncmd == "difference") {
433 f = tokenize_console(s);
434 f2 = tokenize_console(strcat(s, " ", s2));
435 // tokens 0..(f-1) represent s
436 // tokens f..f2 represent s2
437 // DIFFERENCE: keep only the tokens from s that are not in s2
439 for(i = 0; i < f; ++i) {
440 for(j = f; j < f2; ++j)
441 if(argv(i) == argv(j))
442 goto skip_difference;
443 s = strcat(s, " ", argv(i));
446 if(substring(s, 0, 1) == " ")
447 s = substring(s, 1, 99999);
449 tokenize_console(command);
450 } else if(rpncmd == "shuffle") {
453 f = tokenize_console(s);
455 for(i = 0; i < f - 1; ++i) {
456 // move a random item from i..f-1 to position i
458 f2 = floor(random() * (f - i) + i);
459 for(j = 0; j < i; ++j)
460 s = strcat(s, " ", argv(j));
461 s = strcat(s, " ", argv(f2));
462 for(j = i; j < f; ++j)
464 s = strcat(s, " ", argv(j));
465 f = tokenize_console(s);
468 if(substring(s, 0, 1) == " ")
469 s = substring(s, 1, 99999);
471 tokenize_console(command);
472 } else if(rpncmd == "fexists_assert") {
478 print("rpn: ERROR: ", s, " does not exist!\n");
482 } else if(rpncmd == "fexists") {
491 } else if(rpncmd == "localtime") {
492 rpn_set(strftime(TRUE, rpn_get()));
493 } else if(rpncmd == "gmtime") {
494 rpn_set(strftime(FALSE, rpn_get()));
495 } else if(rpncmd == "time") {
497 } else if(rpncmd == "digest") {
499 rpn_set(digest_hex(s, rpn_get()));
500 } else if(rpncmd == "sprintf1s") {
502 rpn_set(sprintf(s, rpn_get()));
504 rpn_push(cvar_string(rpncmd));
512 print("rpn: still on stack: ", s, "\n");
520 case CMD_REQUEST_USAGE:
522 print(" rpn EXPRESSION... - a RPN calculator.\n");
523 print(" Operator description (x: string, s: set, f: float):\n");
524 print(" x pop -----------------------------> : removes the top\n");
525 print(" x dup -----------------------------> x x : duplicates the top\n");
526 print(" x x exch --------------------------> x x : swap the top two\n");
527 print(" /cvarname load --------------------> x : loads a cvar\n");
528 print(" /cvarname x def -------------------> : writes to a cvar\n");
529 print(" f f add|sub|mul|div|mod|max|min ---> f : adds/... two numbers\n");
530 print(" f f eq|ne|gt|ge|lt|le -------------> f : compares two numbers\n");
531 print(" f neg|abs|sgn|rand|floor|ceil------> f : negates/... a number\n");
532 print(" f f f bound -----------------------> f : bounds the middle number\n");
533 print(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise\n");
534 print(" s s union|intersection|difference -> s : set operations\n");
535 print(" s shuffle -------------------------> s : randomly arrange elements\n");
536 print(" /key /value put -------------------> : set a database key\n");
537 print(" /key get --------------------------> s : get a database value\n");
538 print(" x dbpush --------------------------> : pushes the top onto the database\n");
539 print(" dbpop|dbget -----------------------> x : removes/reads DB's top\n");
540 print(" dblen|dbat ------------------------> f : gets the DB's size/cursor pos\n");
541 print(" dbclr -----------------------------> : clear the DB\n");
542 print(" s dbsave|dbload--------------------> : save/load the DB to/from a file\n");
543 print(" x dbins ---------------------------> : moves the top into the DB\n");
544 print(" dbext|dbread ----------------------> x : extract/get from the DB's cursor\n");
545 print(" f dbmov|dbgoto --------------------> : move or set the DB's cursor\n");
546 print(" s localtime -----------------------> s : formats the current local time\n");
547 print(" s gmtime --------------------------> s : formats the current UTC time\n");
548 print(" time ------------------------------> f : seconds since VM start\n");
549 print(" s /MD4 digest ---------------------> s : MD4 digest\n");
550 print(" s /SHA256 digest ------------------> s : SHA256 digest\n");
551 print(" s /formatstring sprintf1s ---------> s : sprintf with 1 string (pad, cut)\n");
552 print(" Set operations operate on 'such''strings'.\n");
553 print(" Unknown tokens insert their cvar value.\n");
559 float GameCommand_Generic(string command)
561 if(argv(0) == "help")
563 print(" rpn EXPRESSION... - a RPN calculator.\n");
564 print(" Operator description (x: string, s: set, f: float):\n");
565 print(" x pop -----------------------------> : removes the top\n");
566 print(" x dup -----------------------------> x x : duplicates the top\n");
567 print(" x x exch --------------------------> x x : swap the top two\n");
568 print(" /cvarname load --------------------> x : loads a cvar\n");
569 print(" /cvarname x def -------------------> : writes to a cvar\n");
570 print(" f f add|sub|mul|div|mod|max|min ---> f : adds/... two numbers\n");
571 print(" f f eq|ne|gt|ge|lt|le -------------> f : compares two numbers\n");
572 print(" f neg|abs|sgn|rand|floor|ceil------> f : negates/... a number\n");
573 print(" f f f bound -----------------------> f : bounds the middle number\n");
574 print(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise\n");
575 print(" s s union|intersection|difference -> s : set operations\n");
576 print(" s shuffle -------------------------> s : randomly arrange elements\n");
577 print(" /key /value put -------------------> : set a database key\n");
578 print(" /key get --------------------------> s : get a database value\n");
579 print(" x dbpush --------------------------> : pushes the top onto the database\n");
580 print(" dbpop|dbget -----------------------> x : removes/reads DB's top\n");
581 print(" dblen|dbat ------------------------> f : gets the DB's size/cursor pos\n");
582 print(" dbclr -----------------------------> : clear the DB\n");
583 print(" s dbsave|dbload--------------------> : save/load the DB to/from a file\n");
584 print(" x dbins ---------------------------> : moves the top into the DB\n");
585 print(" dbext|dbread ----------------------> x : extract/get from the DB's cursor\n");
586 print(" f dbmov|dbgoto --------------------> : move or set the DB's cursor\n");
587 print(" s localtime -----------------------> s : formats the current local time\n");
588 print(" s gmtime --------------------------> s : formats the current UTC time\n");
589 print(" time ------------------------------> f : seconds since VM start\n");
590 print(" s /MD4 digest ---------------------> s : MD4 digest\n");
591 print(" s /SHA256 digest ------------------> s : SHA256 digest\n");
592 print(" s /formatstring sprintf1s ---------> s : sprintf with 1 string (pad, cut)\n");
593 print(" Set operations operate on 'such''strings'.\n");
594 print(" Unknown tokens insert their cvar value.\n");
595 print(" maplist add map\n");
596 print(" maplist remove map\n");
597 print(" maplist shuffle\n");
598 print(" maplist cleanup\n");
599 print(" maplist maplist\n");
600 print(" maplist lsmaps\n");
601 print(" maplist lsnewmaps\n");
602 print(" addtolist variable addedvalue\n");
604 print(" rankings (map argument optional)\n");
605 print(" settemp cvar value\n");
606 print(" settemp_restore\n");
610 else if(argv(0) == "rpn")