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");
68 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
70 rpncmd = argv(rpnpos);
73 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
75 } else if(substring(rpncmd, 0, 1) == "0") {
77 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
79 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
81 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
82 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
83 } else if(rpncmd == "clear") {
85 } else if(rpncmd == "def" || rpncmd == "=") {
92 registercvar(s2, "", 0);
96 if(!rpn_error) // don't change cvars if a stack error had happened!
101 print("rpn: empty cvar name for 'def'\n");
104 } else if(rpncmd == "defs" || rpncmd == "@") {
108 while(rpn_sp > 1 && (j || i > 0))
110 s = strcat("/", rpn_pop(), " ", s);
117 registercvar(s2, "", 0);
119 registercvar(s2, "");
121 if(!rpn_error) // don't change cvars if a stack error had happened!
126 print("rpn: empty cvar name for 'defs'\n");
129 } else if(rpncmd == "load") {
130 rpn_set(cvar_string(rpn_get()));
131 } else if(rpncmd == "exch") {
136 } else if(rpncmd == "dup") {
138 } else if(rpncmd == "pop") {
140 } else if(rpncmd == "add" || rpncmd == "+") {
142 rpn_setf(rpn_getf() + f);
143 } else if(rpncmd == "sub" || rpncmd == "-") {
145 rpn_setf(rpn_getf() - f);
146 } else if(rpncmd == "mul" || rpncmd == "*") {
148 rpn_setf(rpn_getf() * f);
149 } else if(rpncmd == "div" || rpncmd == "/") {
151 rpn_setf(rpn_getf() / f);
152 } else if(rpncmd == "mod" || rpncmd == "%") {
155 rpn_setf(f2 - f * floor(f2 / f));
156 } else if(rpncmd == "abs") {
157 rpn_setf(fabs(rpn_getf()));
158 } else if(rpncmd == "sgn") {
166 } else if(rpncmd == "neg" || rpncmd == "~") {
167 rpn_setf(-rpn_getf());
168 } else if(rpncmd == "floor" || rpncmd == "f") {
169 rpn_setf(floor(rpn_getf()));
170 } else if(rpncmd == "ceil" || rpncmd == "c") {
171 rpn_setf(ceil(rpn_getf()));
172 } else if(rpncmd == "max") {
175 rpn_setf(max(f2, f));
176 } else if(rpncmd == "min") {
179 rpn_setf(min(f2, f));
180 } else if(rpncmd == "bound") {
184 rpn_setf(bound(f3, f2, f));
185 } else if(rpncmd == "when") {
193 } else if(rpncmd == ">" || rpncmd == "gt") {
195 rpn_setf(rpn_getf() > f);
196 } else if(rpncmd == "<" || rpncmd == "lt") {
198 rpn_setf(rpn_getf() < f);
199 } else if(rpncmd == "==" || rpncmd == "eq") {
201 rpn_setf(rpn_getf() == f);
202 } else if(rpncmd == ">=" || rpncmd == "ge") {
204 rpn_setf(rpn_getf() >= f);
205 } else if(rpncmd == "<=" || rpncmd == "le") {
207 rpn_setf(rpn_getf() <= f);
208 } else if(rpncmd == "!=" || rpncmd == "ne") {
210 rpn_setf(rpn_getf() != f);
211 } else if(rpncmd == "rand") {
212 rpn_setf(ceil(random() * rpn_getf()) - 1);
213 } else if(rpncmd == "crc16") {
214 rpn_setf(crc16(FALSE, rpn_get()));
215 } else if(rpncmd == "put") {
221 db_put(rpn_db, s, s2);
223 } else if(rpncmd == "get") {
226 rpn_push(db_get(rpn_db, s));
227 } else if(rpncmd == "dbpush") {
231 i = stof(db_get(rpn_db, "stack.pointer"));
232 db_put(rpn_db, "stack.pointer", ftos(i+1));
233 db_put(rpn_db, strcat("stack.", ftos(i)), s);
236 db_put(rpn_db, "stack.pos", "0");
237 } else if(rpncmd == "dbpop") {
238 i = stof(db_get(rpn_db, "stack.pointer"));
242 db_put(rpn_db, "stack.pointer", s);
243 rpn_push(db_get(rpn_db, strcat("stack.", s)));
244 j = stof(db_get(rpn_db, "stack.pos"));
246 db_put(rpn_db, "stack.pos", ftos(i-2));
249 print("rpn: database underflow\n");
251 } else if(rpncmd == "dbget") {
253 i = stof(db_get(rpn_db, "stack.pointer"));
256 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
259 print("rpn: database empty\n");
261 } else if(rpncmd == "dblen") {
262 rpn_push(db_get(rpn_db, "stack.pointer"));
263 } else if(rpncmd == "dbclr") {
265 rpn_db = db_create();
266 db_put(rpn_db, "stack.pointer", "0");
267 db_put(rpn_db, "stack.pos", "-1");
268 } else if(rpncmd == "dbsave") {
272 } else if(rpncmd == "dbload") {
279 } else if(rpncmd == "dbins") {
284 j = stof(db_get(rpn_db, "stack.pointer"));
285 i = stof(db_get(rpn_db, "stack.pos"));
290 db_put(rpn_db, "stack.pos", "0");
293 db_put(rpn_db, "stack.pointer", ftos(j+1));
294 for(--j; j >= i; --j)
296 db_put(rpn_db, strcat("stack.", ftos(j+1)),
297 db_get(rpn_db, (strcat("stack.", ftos(j))))
300 db_put(rpn_db, strcat("stack.", ftos(i)), s);
302 } else if(rpncmd == "dbext") {
303 j = stof(db_get(rpn_db, "stack.pointer"));
304 i = stof(db_get(rpn_db, "stack.pos"));
308 print("rpn: empty database\n");
311 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
312 db_put(rpn_db, "stack.pointer", ftos(j));
315 db_put(rpn_db, "stack.pos", ftos(j-1));
319 db_put(rpn_db, strcat("stack.", ftos(i)),
320 db_get(rpn_db, (strcat("stack.", ftos(i+1))))
326 } else if(rpncmd == "dbread") {
327 s = db_get(rpn_db, "stack.pos");
330 rpn_push(db_get(rpn_db, strcat("stack.", s)));
333 print("rpn: empty database\n");
335 } else if(rpncmd == "dbat") {
336 rpn_push(db_get(rpn_db, "stack.pos"));
337 } else if(rpncmd == "dbmov") {
338 j = stof(db_get(rpn_db, "stack.pointer"));
339 i = stof(db_get(rpn_db, "stack.pos"));
345 print("rpn: database cursor out of bounds\n");
350 db_put(rpn_db, "stack.pos", ftos(i));
353 } else if(rpncmd == "dbgoto") {
355 j = stof(db_get(rpn_db, "stack.pointer"));
359 print("rpn: empty database, cannot move cursor\n");
364 i = stof(db_get(rpn_db, "stack.pointer"))-1;
370 j = stof(db_get(rpn_db, "stack.pointer"));
373 print("rpn: database cursor destination out of bounds\n");
378 db_put(rpn_db, "stack.pos", ftos(i));
381 } else if(rpncmd == "union") {
385 f = tokenize_console(s);
386 f2 = tokenize_console(strcat(s, " ", s2));
387 // tokens 0..(f-1) represent s
388 // tokens f..f2 represent s2
389 // UNION: add all tokens to s that are in s2 but not in s
391 for(i = 0; i < f; ++i)
392 s = strcat(s, " ", argv(i));
393 for(i = f; i < f2; ++i) {
394 for(j = 0; j < f; ++j)
395 if(argv(i) == argv(j))
397 s = strcat(s, " ", argv(i));
400 if(substring(s, 0, 1) == " ")
401 s = substring(s, 1, 99999);
403 tokenize_console(command);
404 } else if(rpncmd == "intersection") {
408 f = tokenize_console(s);
409 f2 = tokenize_console(strcat(s, " ", s2));
410 // tokens 0..(f-1) represent s
411 // tokens f..f2 represent s2
412 // INTERSECTION: keep only the tokens from s that are also in s2
414 for(i = 0; i < f; ++i) {
415 for(j = f; j < f2; ++j)
416 if(argv(i) == argv(j))
418 s = strcat(s, " ", argv(i));
422 if(substring(s, 0, 1) == " ")
423 s = substring(s, 1, 99999);
425 tokenize_console(command);
426 } else if(rpncmd == "difference") {
430 f = tokenize_console(s);
431 f2 = tokenize_console(strcat(s, " ", s2));
432 // tokens 0..(f-1) represent s
433 // tokens f..f2 represent s2
434 // DIFFERENCE: keep only the tokens from s that are not in s2
436 for(i = 0; i < f; ++i) {
437 for(j = f; j < f2; ++j)
438 if(argv(i) == argv(j))
439 goto skip_difference;
440 s = strcat(s, " ", argv(i));
443 if(substring(s, 0, 1) == " ")
444 s = substring(s, 1, 99999);
446 tokenize_console(command);
447 } else if(rpncmd == "shuffle") {
450 f = tokenize_console(s);
452 for(i = 0; i < f - 1; ++i) {
453 // move a random item from i..f-1 to position i
455 f2 = floor(random() * (f - i) + i);
456 for(j = 0; j < i; ++j)
457 s = strcat(s, " ", argv(j));
458 s = strcat(s, " ", argv(f2));
459 for(j = i; j < f; ++j)
461 s = strcat(s, " ", argv(j));
462 f = tokenize_console(s);
465 if(substring(s, 0, 1) == " ")
466 s = substring(s, 1, 99999);
468 tokenize_console(command);
469 } else if(rpncmd == "fexists_assert") {
475 print("rpn: ERROR: ", s, " does not exist!\n");
479 } else if(rpncmd == "fexists") {
488 } else if(rpncmd == "localtime") {
489 rpn_set(strftime(TRUE, rpn_get()));
490 } else if(rpncmd == "gmtime") {
491 rpn_set(strftime(FALSE, rpn_get()));
492 } else if(rpncmd == "time") {
494 } else if(rpncmd == "digest") {
496 rpn_set(digest_hex(s, rpn_get()));
497 } else if(rpncmd == "sprintf1s") {
499 rpn_set(sprintf(s, rpn_get()));
501 rpn_push(cvar_string(rpncmd));
509 print("rpn: still on stack: ", s, "\n");
517 case CMD_REQUEST_USAGE:
519 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " rpn EXPRESSION...\n"));
520 print(" Operator description (x: string, s: set, f: float):\n");
521 print(" x pop -----------------------------> : removes the top\n");
522 print(" x dup -----------------------------> x x : duplicates the top\n");
523 print(" x x exch --------------------------> x x : swap the top two\n");
524 print(" /cvarname load --------------------> x : loads a cvar\n");
525 print(" /cvarname x def -------------------> : writes to a cvar\n");
526 print(" f f add|sub|mul|div|mod|max|min ---> f : adds/... two numbers\n");
527 print(" f f eq|ne|gt|ge|lt|le -------------> f : compares two numbers\n");
528 print(" f neg|abs|sgn|rand|floor|ceil------> f : negates/... a number\n");
529 print(" f f f bound -----------------------> f : bounds the middle number\n");
530 print(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise\n");
531 print(" s s union|intersection|difference -> s : set operations\n");
532 print(" s shuffle -------------------------> s : randomly arrange elements\n");
533 print(" /key /value put -------------------> : set a database key\n");
534 print(" /key get --------------------------> s : get a database value\n");
535 print(" x dbpush --------------------------> : pushes the top onto the database\n");
536 print(" dbpop|dbget -----------------------> x : removes/reads DB's top\n");
537 print(" dblen|dbat ------------------------> f : gets the DB's size/cursor pos\n");
538 print(" dbclr -----------------------------> : clear the DB\n");
539 print(" s dbsave|dbload--------------------> : save/load the DB to/from a file\n");
540 print(" x dbins ---------------------------> : moves the top into the DB\n");
541 print(" dbext|dbread ----------------------> x : extract/get from the DB's cursor\n");
542 print(" f dbmov|dbgoto --------------------> : move or set the DB's cursor\n");
543 print(" s localtime -----------------------> s : formats the current local time\n");
544 print(" s gmtime --------------------------> s : formats the current UTC time\n");
545 print(" time ------------------------------> f : seconds since VM start\n");
546 print(" s /MD4 digest ---------------------> s : MD4 digest\n");
547 print(" s /SHA256 digest ------------------> s : SHA256 digest\n");
548 print(" s /formatstring sprintf1s ---------> s : sprintf with 1 string (pad, cut)\n");
549 print(" Set operations operate on 'such''strings'.\n");
550 print(" Unknown tokens insert their cvar value.\n");