5 // ========================================
6 // RPN command code, written by divVerent
7 // Last updated: December 28th, 2011
8 // ========================================
14 return rpn_stack[rpn_sp];
16 LOG_INFO("rpn: stack underflow");
21 void rpn_push(string s)
23 if(rpn_sp < MAX_RPN_STACK) {
24 rpn_stack[rpn_sp] = s;
27 LOG_INFO("rpn: stack overflow");
34 return rpn_stack[rpn_sp - 1];
36 LOG_INFO("rpn: empty stack");
41 void rpn_set(string s)
44 rpn_stack[rpn_sp - 1] = s;
46 LOG_INFO("rpn: empty stack");
51 float rpn_getf() { return stof(rpn_get()); }
52 float rpn_popf() { return stof(rpn_pop()); }
53 void rpn_pushf(float f) { return rpn_push(sprintf("%.9g", f)); }
54 void rpn_setf(float f) { return rpn_set(sprintf("%.9g", f)); }
56 void GenericCommand_rpn(int request, int argc, string command)
60 case CMD_REQUEST_COMMAND:
62 float i, j, f, f2, f3, rpnpos;
69 db_put(rpn_db, "stack.pointer", "0");
70 db_put(rpn_db, "stack.pos", "-1");
77 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
79 rpncmd = argv(rpnpos);
82 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
84 } else if(substring(rpncmd, 0, 1) == "0") {
86 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
88 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
90 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
91 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
92 } else if(rpncmd == "clear") {
94 } else if(rpncmd == "def" || rpncmd == "=") {
101 registercvar(s2, "", 0);
103 registercvar(s2, "");
105 if(!rpn_error) // don't change cvars if a stack error had happened!
110 LOG_INFO("rpn: empty cvar name for 'def'");
113 } else if(rpncmd == "defs" || rpncmd == "@") {
117 while(rpn_sp > 1 && (j || i > 0))
119 s = strcat("/", rpn_pop(), " ", s);
126 registercvar(s2, "", 0);
128 registercvar(s2, "");
130 if(!rpn_error) // don't change cvars if a stack error had happened!
135 LOG_INFO("rpn: empty cvar name for 'defs'");
138 } else if(rpncmd == "load") {
139 rpn_set(cvar_string(rpn_get()));
140 } else if(rpncmd == "exch") {
145 } else if(rpncmd == "dup") {
147 } else if(rpncmd == "pop") {
149 } else if(rpncmd == "add" || rpncmd == "+") {
151 rpn_setf(rpn_getf() + f);
152 } else if(rpncmd == "sub" || rpncmd == "-") {
154 rpn_setf(rpn_getf() - f);
155 } else if(rpncmd == "mul" || rpncmd == "*") {
157 rpn_setf(rpn_getf() * f);
158 } else if(rpncmd == "div" || rpncmd == "/") {
160 rpn_setf(rpn_getf() / f);
161 } else if(rpncmd == "mod" || rpncmd == "%") {
164 rpn_setf(f2 - f * floor(f2 / f));
165 } else if(rpncmd == "pow" || rpncmd == "**") {
167 rpn_setf(rpn_getf() ** f);
168 } else if(rpncmd == "bitand" || rpncmd == "&") {
170 rpn_setf(rpn_getf() & f);
171 } else if(rpncmd == "bitor" || rpncmd == "|") {
173 rpn_setf(rpn_getf() | f);
174 } else if(rpncmd == "bitxor" || rpncmd == "^") {
176 rpn_setf(rpn_getf() ^ f);
177 } else if(rpncmd == "and" || rpncmd == "&&") {
179 rpn_setf(rpn_getf() && f);
180 } else if(rpncmd == "or" || rpncmd == "||") {
182 rpn_setf(rpn_getf() || f);
183 } else if(rpncmd == "xor" || rpncmd == "^^") {
185 rpn_setf(!rpn_getf() != !f);
186 } else if(rpncmd == "bitnot") {
187 rpn_setf(~rpn_popf());
188 } else if(rpncmd == "not") {
189 rpn_setf(!rpn_popf());
190 } else if(rpncmd == "abs") {
191 rpn_setf(fabs(rpn_getf()));
192 } else if(rpncmd == "sgn") {
200 } else if(rpncmd == "neg" || rpncmd == "~") {
201 rpn_setf(-rpn_getf());
202 } else if(rpncmd == "floor" || rpncmd == "f") {
203 rpn_setf(floor(rpn_getf()));
204 } else if(rpncmd == "ceil" || rpncmd == "c") {
205 rpn_setf(ceil(rpn_getf()));
206 } else if(rpncmd == "exp") {
207 rpn_setf(exp(rpn_getf()));
208 } else if(rpncmd == "log") {
209 rpn_setf(exp(rpn_getf()));
210 } else if(rpncmd == "sin") {
211 rpn_setf(sin(rpn_getf()));
212 } else if(rpncmd == "cos") {
213 rpn_setf(cos(rpn_getf()));
214 } else if(rpncmd == "max") {
217 rpn_setf(max(f2, f));
218 } else if(rpncmd == "min") {
221 rpn_setf(min(f2, f));
222 } else if(rpncmd == "bound") {
226 rpn_setf(bound(f3, f2, f));
227 } else if(rpncmd == "when") {
235 } else if(rpncmd == ">" || rpncmd == "gt") {
237 rpn_setf(rpn_getf() > f);
238 } else if(rpncmd == "<" || rpncmd == "lt") {
240 rpn_setf(rpn_getf() < f);
241 } else if(rpncmd == "==" || rpncmd == "eq") {
243 rpn_setf(rpn_getf() == f);
244 } else if(rpncmd == ">=" || rpncmd == "ge") {
246 rpn_setf(rpn_getf() >= f);
247 } else if(rpncmd == "<=" || rpncmd == "le") {
249 rpn_setf(rpn_getf() <= f);
250 } else if(rpncmd == "!=" || rpncmd == "ne") {
252 rpn_setf(rpn_getf() != f);
253 } else if(rpncmd == "rand") {
254 rpn_setf(ceil(random() * rpn_getf()) - 1);
255 } else if(rpncmd == "crc16") {
256 rpn_setf(crc16(false, rpn_get()));
257 } else if(rpncmd == "put") {
263 db_put(rpn_db, s, s2);
265 } else if(rpncmd == "get") {
268 rpn_push(db_get(rpn_db, s));
269 } else if(rpncmd == "dbpush") {
273 i = stof(db_get(rpn_db, "stack.pointer"));
274 db_put(rpn_db, "stack.pointer", ftos(i+1));
275 db_put(rpn_db, strcat("stack.", ftos(i)), s);
277 db_put(rpn_db, "stack.pos", "0");
279 } else if(rpncmd == "dbpop") {
280 i = stof(db_get(rpn_db, "stack.pointer"));
284 db_put(rpn_db, "stack.pointer", s);
285 rpn_push(db_get(rpn_db, strcat("stack.", s)));
286 j = stof(db_get(rpn_db, "stack.pos"));
288 db_put(rpn_db, "stack.pos", ftos(i-2));
291 LOG_INFO("rpn: database underflow");
293 } else if(rpncmd == "dbget") {
295 i = stof(db_get(rpn_db, "stack.pointer"));
298 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
301 LOG_INFO("rpn: database empty");
303 } else if(rpncmd == "dblen") {
304 rpn_push(db_get(rpn_db, "stack.pointer"));
305 } else if(rpncmd == "dbclr") {
307 rpn_db = db_create();
308 db_put(rpn_db, "stack.pointer", "0");
309 db_put(rpn_db, "stack.pos", "-1");
310 } else if(rpncmd == "dbsave") {
314 } else if(rpncmd == "dbload") {
321 } else if(rpncmd == "dbins") {
326 j = stof(db_get(rpn_db, "stack.pointer"));
327 i = stof(db_get(rpn_db, "stack.pos"));
332 db_put(rpn_db, "stack.pos", "0");
335 db_put(rpn_db, "stack.pointer", ftos(j+1));
336 for(--j; j >= i; --j)
338 db_put(rpn_db, strcat("stack.", ftos(j+1)),
339 db_get(rpn_db, (strcat("stack.", ftos(j))))
342 db_put(rpn_db, strcat("stack.", ftos(i)), s);
344 } else if(rpncmd == "dbext") {
345 j = stof(db_get(rpn_db, "stack.pointer"));
346 i = stof(db_get(rpn_db, "stack.pos"));
350 LOG_INFO("rpn: empty database");
353 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
354 db_put(rpn_db, "stack.pointer", ftos(j));
357 db_put(rpn_db, "stack.pos", ftos(j-1));
361 db_put(rpn_db, strcat("stack.", ftos(i)),
362 db_get(rpn_db, (strcat("stack.", ftos(i+1))))
368 } else if(rpncmd == "dbread") {
369 s = db_get(rpn_db, "stack.pos");
372 rpn_push(db_get(rpn_db, strcat("stack.", s)));
375 LOG_INFO("rpn: empty database");
377 } else if(rpncmd == "dbat") {
378 rpn_push(db_get(rpn_db, "stack.pos"));
379 } else if(rpncmd == "dbmov") {
380 j = stof(db_get(rpn_db, "stack.pointer"));
381 i = stof(db_get(rpn_db, "stack.pos"));
387 LOG_INFO("rpn: database cursor out of bounds");
392 db_put(rpn_db, "stack.pos", ftos(i));
395 } else if(rpncmd == "dbgoto") {
397 j = stof(db_get(rpn_db, "stack.pointer"));
401 LOG_INFO("rpn: empty database, cannot move cursor");
406 i = stof(db_get(rpn_db, "stack.pointer"))-1;
412 j = stof(db_get(rpn_db, "stack.pointer"));
415 LOG_INFO("rpn: database cursor destination out of bounds");
420 db_put(rpn_db, "stack.pos", ftos(i));
423 } else if(rpncmd == "union") {
427 f = tokenize_console(s);
428 f2 = tokenize_console(strcat(s, " ", s2));
429 // tokens 0..(f-1) represent s
430 // tokens f..f2 represent s2
431 // UNION: add all tokens to s that are in s2 but not in s
433 for(i = 0; i < f; ++i)
434 s = strcat(s, " ", argv(i));
435 for(i = f; i < f2; ++i) {
436 for(j = 0; j < f; ++j)
437 if(argv(i) == argv(j))
439 s = strcat(s, " ", argv(i));
442 if(substring(s, 0, 1) == " ")
443 s = substring(s, 1, 99999);
445 tokenize_console(command);
446 } else if(rpncmd == "intersection") {
450 f = tokenize_console(s);
451 f2 = tokenize_console(strcat(s, " ", s2));
452 // tokens 0..(f-1) represent s
453 // tokens f..f2 represent s2
454 // INTERSECTION: keep only the tokens from s that are also in s2
456 for(i = 0; i < f; ++i) {
457 for(j = f; j < f2; ++j)
458 if(argv(i) == argv(j))
460 s = strcat(s, " ", argv(i));
464 if(substring(s, 0, 1) == " ")
465 s = substring(s, 1, 99999);
467 tokenize_console(command);
468 } else if(rpncmd == "difference") {
472 f = tokenize_console(s);
473 f2 = tokenize_console(strcat(s, " ", s2));
474 // tokens 0..(f-1) represent s
475 // tokens f..f2 represent s2
476 // DIFFERENCE: keep only the tokens from s that are not in s2
478 for(i = 0; i < f; ++i) {
479 for(j = f; j < f2; ++j)
480 if(argv(i) == argv(j))
481 goto skip_difference;
482 s = strcat(s, " ", argv(i));
483 LABEL(skip_difference)
485 if(substring(s, 0, 1) == " ")
486 s = substring(s, 1, 99999);
488 tokenize_console(command);
489 } else if(rpncmd == "shuffle") {
492 f = tokenize_console(s);
494 for(i = 0; i < f - 1; ++i) {
495 // move a random item from i..f-1 to position i
497 f2 = floor(random() * (f - i) + i);
498 for(j = 0; j < i; ++j)
499 s = strcat(s, " ", argv(j));
500 s = strcat(s, " ", argv(f2));
501 for(j = i; j < f; ++j)
503 s = strcat(s, " ", argv(j));
504 f = tokenize_console(s);
507 if(substring(s, 0, 1) == " ")
508 s = substring(s, 1, 99999);
510 tokenize_console(command);
511 } else if(rpncmd == "fexists_assert") {
517 LOG_INFO("rpn: ERROR: ", s, " does not exist!");
521 } else if(rpncmd == "fexists") {
530 } else if(rpncmd == "localtime") {
531 rpn_set(strftime(true, rpn_get()));
532 } else if(rpncmd == "gmtime") {
533 rpn_set(strftime(false, rpn_get()));
534 } else if(rpncmd == "time") {
536 } else if(rpncmd == "digest") {
538 rpn_set(digest_hex(s, rpn_get()));
539 } else if(rpncmd == "sprintf1s") {
541 rpn_set(sprintf(s, rpn_get()));
542 } else if(rpncmd == "eval") {
544 command = strcat(s, substring(command, argv_end_index(rpnpos), -1));
545 argc = tokenize_console(command);
548 rpn_push(cvar_string(rpncmd));
556 LOG_INFO("rpn: still on stack: ", s);
564 case CMD_REQUEST_USAGE:
566 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " rpn EXPRESSION...");
567 LOG_INFO(" Operator description (x: string, s: set, f: float):");
568 LOG_INFO(" x pop -----------------------------> : removes the top");
569 LOG_INFO(" x dup -----------------------------> x x : duplicates the top");
570 LOG_INFO(" x x exch --------------------------> x x : swap the top two");
571 LOG_INFO(" /cvarname load --------------------> x : loads a cvar");
572 LOG_INFO(" /cvarname x def -------------------> : writes to a cvar");
573 LOG_INFO(" f f add|sub|mul|div|mod|pow -------> f : adds/... two numbers");
574 LOG_INFO(" f f and|or|xor|bitand|bitor|bitxor > f : logical and bitwise operations");
575 LOG_INFO(" f f eq|ne|gt|ge|lt|le|max|min -----> f : compares two numbers");
576 LOG_INFO(" f neg|abs|sgn|rand|floor|ceil------> f : negates/... a number");
577 LOG_INFO(" f not|bitnot ----------------------> f : logical and bitwise negation");
578 LOG_INFO(" f exp|log|sin|cos -----------------> f : exponential function & Co.");
579 LOG_INFO(" f f f bound -----------------------> f : bounds the middle number");
580 LOG_INFO(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise");
581 LOG_INFO(" s s union|intersection|difference -> s : set operations");
582 LOG_INFO(" s shuffle -------------------------> s : randomly arrange elements");
583 LOG_INFO(" /key /value put -------------------> : set a database key");
584 LOG_INFO(" /key get --------------------------> s : get a database value");
585 LOG_INFO(" x dbpush --------------------------> : pushes the top onto the database");
586 LOG_INFO(" dbpop|dbget -----------------------> x : removes/reads DB's top");
587 LOG_INFO(" dblen|dbat ------------------------> f : gets the DB's size/cursor pos");
588 LOG_INFO(" dbclr -----------------------------> : clear the DB");
589 LOG_INFO(" s dbsave|dbload--------------------> : save/load the DB to/from a file");
590 LOG_INFO(" x dbins ---------------------------> : moves the top into the DB");
591 LOG_INFO(" dbext|dbread ----------------------> x : extract/get from the DB's cursor");
592 LOG_INFO(" f dbmov|dbgoto --------------------> : move or set the DB's cursor");
593 LOG_INFO(" s localtime -----------------------> s : formats the current local time");
594 LOG_INFO(" s gmtime --------------------------> s : formats the current UTC time");
595 LOG_INFO(" time ------------------------------> f : seconds since VM start");
596 LOG_INFO(" s /MD4 digest ---------------------> s : MD4 digest");
597 LOG_INFO(" s /SHA256 digest ------------------> s : SHA256 digest");
598 LOG_INFO(" s /formatstring sprintf1s ---------> s : sprintf with 1 string (pad, cut)");
599 LOG_INFO(" s eval ----------------------------> : does something eval");
600 LOG_INFO(" Set operations operate on 'such''strings'.");
601 LOG_INFO(" Unknown tokens insert their cvar value.");