3 #include <common/command/command.qh>
6 // ========================================
7 // RPN command code, written by divVerent
8 // Last updated: December 28th, 2011
9 // ========================================
15 return rpn_stack[rpn_sp];
17 LOG_INFO("rpn: stack underflow");
22 void rpn_push(string s)
24 if(rpn_sp < MAX_RPN_STACK) {
25 rpn_stack[rpn_sp] = s;
28 LOG_INFO("rpn: stack overflow");
35 return rpn_stack[rpn_sp - 1];
37 LOG_INFO("rpn: empty stack");
42 void rpn_set(string s)
45 rpn_stack[rpn_sp - 1] = s;
47 LOG_INFO("rpn: empty stack");
52 float rpn_getf() { return stof(rpn_get()); }
53 float rpn_popf() { return stof(rpn_pop()); }
54 void rpn_pushf(float f) { return rpn_push(sprintf("%.9g", f)); }
55 void rpn_setf(float f) { return rpn_set(sprintf("%.9g", f)); }
63 void GenericCommand_rpn(int request, int argc, string command)
67 case CMD_REQUEST_COMMAND:
69 float i, j, f, f2, f3, rpnpos;
76 db_put(rpn_db, "stack.pointer", "0");
77 db_put(rpn_db, "stack.pos", "-1");
84 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
86 rpncmd = argv(rpnpos);
89 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
91 } else if(substring(rpncmd, 0, 1) == "0") {
93 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
95 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
97 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
98 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
99 } else if(rpncmd == "clear") {
101 } else if(rpncmd == "def" || rpncmd == "=") {
108 registercvar(s2, "", 0);
110 registercvar(s2, "");
112 if(!rpn_error) // don't change cvars if a stack error had happened!
117 LOG_INFO("rpn: empty cvar name for 'def'");
120 } else if(rpncmd == "defs" || rpncmd == "@") {
124 while(rpn_sp > 1 && (j || i > 0))
126 s = strcat("/", rpn_pop(), " ", s);
133 registercvar(s2, "", 0);
135 registercvar(s2, "");
137 if(!rpn_error) // don't change cvars if a stack error had happened!
142 LOG_INFO("rpn: empty cvar name for 'defs'");
145 } else if(rpncmd == "load") {
146 rpn_set(cvar_string(rpn_get()));
147 } else if(rpncmd == "exch") {
152 } else if(rpncmd == "dup") {
154 } else if(rpncmd == "pop") {
156 } else if(rpncmd == "add" || rpncmd == "+") {
158 rpn_setf(rpn_getf() + f);
159 } else if(rpncmd == "sub" || rpncmd == "-") {
161 rpn_setf(rpn_getf() - f);
162 } else if(rpncmd == "mul" || rpncmd == "*") {
164 rpn_setf(rpn_getf() * f);
165 } else if(rpncmd == "div" || rpncmd == "/") {
167 rpn_setf(rpn_getf() / f);
168 } else if(rpncmd == "mod" || rpncmd == "%") {
171 rpn_setf(f2 - f * floor(f2 / f));
172 } else if(rpncmd == "pow" || rpncmd == "**") {
174 rpn_setf(rpn_getf() ** f);
175 } else if(rpncmd == "bitand" || rpncmd == "&") {
177 rpn_setf(rpn_getf() & f);
178 } else if(rpncmd == "bitor" || rpncmd == "|") {
180 rpn_setf(rpn_getf() | f);
181 } else if(rpncmd == "bitxor" || rpncmd == "^") {
183 rpn_setf(rpn_getf() ^ f);
184 } else if(rpncmd == "and" || rpncmd == "&&") {
186 rpn_setf(rpn_getf() && f);
187 } else if(rpncmd == "or" || rpncmd == "||") {
189 rpn_setf(rpn_getf() || f);
190 } else if(rpncmd == "xor" || rpncmd == "^^") {
192 rpn_setf(!rpn_getf() != !f);
193 } else if(rpncmd == "bitnot") {
194 rpn_setf(~rpn_popf());
195 } else if(rpncmd == "not") {
196 rpn_setf(!rpn_popf());
197 } else if(rpncmd == "abs") {
198 rpn_setf(fabs(rpn_getf()));
199 } else if(rpncmd == "sgn") {
207 } else if(rpncmd == "neg" || rpncmd == "~") {
208 rpn_setf(-rpn_getf());
209 } else if(rpncmd == "floor" || rpncmd == "f") {
210 rpn_setf(floor(rpn_getf()));
211 } else if(rpncmd == "ceil" || rpncmd == "c") {
212 rpn_setf(ceil(rpn_getf()));
213 } else if(rpncmd == "exp") {
214 rpn_setf(exp(rpn_getf()));
215 } else if(rpncmd == "log") {
216 rpn_setf(exp(rpn_getf()));
217 } else if(rpncmd == "sin") {
218 rpn_setf(sin(rpn_getf()));
219 } else if(rpncmd == "cos") {
220 rpn_setf(cos(rpn_getf()));
221 } else if(rpncmd == "max") {
224 rpn_setf(max(f2, f));
225 } else if(rpncmd == "min") {
228 rpn_setf(min(f2, f));
229 } else if(rpncmd == "bound") {
233 rpn_setf(bound(f3, f2, f));
234 } else if(rpncmd == "when") {
242 } else if(rpncmd == ">" || rpncmd == "gt") {
244 rpn_setf(rpn_getf() > f);
245 } else if(rpncmd == "<" || rpncmd == "lt") {
247 rpn_setf(rpn_getf() < f);
248 } else if(rpncmd == "==" || rpncmd == "eq") {
250 rpn_setf(rpn_getf() == f);
251 } else if(rpncmd == ">=" || rpncmd == "ge") {
253 rpn_setf(rpn_getf() >= f);
254 } else if(rpncmd == "<=" || rpncmd == "le") {
256 rpn_setf(rpn_getf() <= f);
257 } else if(rpncmd == "!=" || rpncmd == "ne") {
259 rpn_setf(rpn_getf() != f);
260 } else if(rpncmd == "rand") {
261 rpn_setf(ceil(random() * rpn_getf()) - 1);
262 } else if(rpncmd == "crc16") {
263 rpn_setf(crc16(false, rpn_get()));
264 } else if(rpncmd == "put") {
270 db_put(rpn_db, s, s2);
272 } else if(rpncmd == "get") {
275 rpn_push(db_get(rpn_db, s));
276 } else if(rpncmd == "dbpush") {
280 i = stof(db_get(rpn_db, "stack.pointer"));
281 db_put(rpn_db, "stack.pointer", ftos(i+1));
282 db_put(rpn_db, strcat("stack.", ftos(i)), s);
284 db_put(rpn_db, "stack.pos", "0");
286 } else if(rpncmd == "dbpop") {
287 i = stof(db_get(rpn_db, "stack.pointer"));
291 db_put(rpn_db, "stack.pointer", s);
292 rpn_push(db_get(rpn_db, strcat("stack.", s)));
293 j = stof(db_get(rpn_db, "stack.pos"));
295 db_put(rpn_db, "stack.pos", ftos(i-2));
298 LOG_INFO("rpn: database underflow");
300 } else if(rpncmd == "dbget") {
302 i = stof(db_get(rpn_db, "stack.pointer"));
305 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
308 LOG_INFO("rpn: database empty");
310 } else if(rpncmd == "dblen") {
311 rpn_push(db_get(rpn_db, "stack.pointer"));
312 } else if(rpncmd == "dbclr") {
314 rpn_db = db_create();
315 db_put(rpn_db, "stack.pointer", "0");
316 db_put(rpn_db, "stack.pos", "-1");
317 } else if(rpncmd == "dbsave") {
321 } else if(rpncmd == "dbload") {
328 } else if(rpncmd == "dbins") {
333 j = stof(db_get(rpn_db, "stack.pointer"));
334 i = stof(db_get(rpn_db, "stack.pos"));
339 db_put(rpn_db, "stack.pos", "0");
342 db_put(rpn_db, "stack.pointer", ftos(j+1));
343 for(--j; j >= i; --j)
345 db_put(rpn_db, strcat("stack.", ftos(j+1)),
346 db_get(rpn_db, (strcat("stack.", ftos(j))))
349 db_put(rpn_db, strcat("stack.", ftos(i)), s);
351 } else if(rpncmd == "dbext") {
352 j = stof(db_get(rpn_db, "stack.pointer"));
353 i = stof(db_get(rpn_db, "stack.pos"));
357 LOG_INFO("rpn: empty database");
360 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
361 db_put(rpn_db, "stack.pointer", ftos(j));
364 db_put(rpn_db, "stack.pos", ftos(j-1));
368 db_put(rpn_db, strcat("stack.", ftos(i)),
369 db_get(rpn_db, (strcat("stack.", ftos(i+1))))
375 } else if(rpncmd == "dbread") {
376 s = db_get(rpn_db, "stack.pos");
379 rpn_push(db_get(rpn_db, strcat("stack.", s)));
382 LOG_INFO("rpn: empty database");
384 } else if(rpncmd == "dbat") {
385 rpn_push(db_get(rpn_db, "stack.pos"));
386 } else if(rpncmd == "dbmov") {
387 j = stof(db_get(rpn_db, "stack.pointer"));
388 i = stof(db_get(rpn_db, "stack.pos"));
394 LOG_INFO("rpn: database cursor out of bounds");
399 db_put(rpn_db, "stack.pos", ftos(i));
402 } else if(rpncmd == "dbgoto") {
404 j = stof(db_get(rpn_db, "stack.pointer"));
408 LOG_INFO("rpn: empty database, cannot move cursor");
413 i = stof(db_get(rpn_db, "stack.pointer"))-1;
419 j = stof(db_get(rpn_db, "stack.pointer"));
422 LOG_INFO("rpn: database cursor destination out of bounds");
427 db_put(rpn_db, "stack.pos", ftos(i));
430 } else if(rpncmd == "union") {
434 f = tokenize_console(s);
435 f2 = tokenize_console(strcat(s, " ", s2));
436 // tokens 0..(f-1) represent s
437 // tokens f..f2 represent s2
438 // UNION: add all tokens to s that are in s2 but not in s
440 for(i = 0; i < f; ++i)
441 s = strcat(s, " ", argv(i));
442 for(i = f; i < f2; ++i) {
443 for(j = 0; j < f; ++j)
444 if(argv(i) == argv(j))
446 s = strcat(s, " ", argv(i));
449 if(substring(s, 0, 1) == " ")
450 s = substring(s, 1, 99999);
452 tokenize_console(command);
453 } else if(rpncmd == "intersection") {
457 f = tokenize_console(s);
458 f2 = tokenize_console(strcat(s, " ", s2));
459 // tokens 0..(f-1) represent s
460 // tokens f..f2 represent s2
461 // INTERSECTION: keep only the tokens from s that are also in s2
463 for(i = 0; i < f; ++i) {
464 for(j = f; j < f2; ++j)
465 if(argv(i) == argv(j))
467 s = strcat(s, " ", argv(i));
471 if(substring(s, 0, 1) == " ")
472 s = substring(s, 1, 99999);
474 tokenize_console(command);
475 } else if(rpncmd == "difference") {
479 f = tokenize_console(s);
480 f2 = tokenize_console(strcat(s, " ", s2));
481 // tokens 0..(f-1) represent s
482 // tokens f..f2 represent s2
483 // DIFFERENCE: keep only the tokens from s that are not in s2
485 for(i = 0; i < f; ++i) {
486 for(j = f; j < f2; ++j)
487 if(argv(i) == argv(j))
488 goto skip_difference;
489 s = strcat(s, " ", argv(i));
490 LABEL(skip_difference)
492 if(substring(s, 0, 1) == " ")
493 s = substring(s, 1, 99999);
495 tokenize_console(command);
496 } else if(rpncmd == "shuffle") {
499 f = tokenize_console(s);
501 for(i = 0; i < f - 1; ++i) {
502 // move a random item from i..f-1 to position i
504 f2 = floor(random() * (f - i) + i);
505 for(j = 0; j < i; ++j)
506 s = strcat(s, " ", argv(j));
507 s = strcat(s, " ", argv(f2));
508 for(j = i; j < f; ++j)
510 s = strcat(s, " ", argv(j));
511 f = tokenize_console(s);
514 if(substring(s, 0, 1) == " ")
515 s = substring(s, 1, 99999);
517 tokenize_console(command);
518 } else if(rpncmd == "fexists_assert") {
524 LOG_INFO("rpn: ERROR: ", s, " does not exist!");
528 } else if(rpncmd == "fexists") {
537 } else if(rpncmd == "localtime") {
538 rpn_set(strftime(true, rpn_get()));
539 } else if(rpncmd == "gmtime") {
540 rpn_set(strftime(false, rpn_get()));
541 } else if(rpncmd == "time") {
543 } else if(rpncmd == "digest") {
545 rpn_set(digest_hex(s, rpn_get()));
546 } else if(rpncmd == "sprintf1s") {
548 rpn_set(sprintf(s, rpn_get()));
549 } else if(rpncmd == "eval") {
551 command = strcat(s, substring(command, argv_end_index(rpnpos), -1));
552 argc = tokenize_console(command);
555 rpn_push(cvar_string(rpncmd));
563 LOG_INFO("rpn: still on stack: ", s);
571 case CMD_REQUEST_USAGE:
573 LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " rpn EXPRESSION...");
574 LOG_HELP(" Operator description (x: string, s: set, f: float):");
575 LOG_HELP(" x pop -----------------------------> : removes the top");
576 LOG_HELP(" x dup -----------------------------> x x : duplicates the top");
577 LOG_HELP(" x x exch --------------------------> x x : swap the top two");
578 LOG_HELP(" /cvarname load --------------------> x : loads a cvar");
579 LOG_HELP(" /cvarname x def -------------------> : writes to a cvar");
580 LOG_HELP(" f f add|sub|mul|div|mod|pow -------> f : adds/... two numbers");
581 LOG_HELP(" f f and|or|xor|bitand|bitor|bitxor > f : logical and bitwise operations");
582 LOG_HELP(" f f eq|ne|gt|ge|lt|le|max|min -----> f : compares two numbers");
583 LOG_HELP(" f neg|abs|sgn|rand|floor|ceil------> f : negates/... a number");
584 LOG_HELP(" f not|bitnot ----------------------> f : logical and bitwise negation");
585 LOG_HELP(" f exp|log|sin|cos -----------------> f : exponential function & Co.");
586 LOG_HELP(" f f f bound -----------------------> f : bounds the middle number");
587 LOG_HELP(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise");
588 LOG_HELP(" s s union|intersection|difference -> s : set operations");
589 LOG_HELP(" s shuffle -------------------------> s : randomly arrange elements");
590 LOG_HELP(" /key /value put -------------------> : set a database key");
591 LOG_HELP(" /key get --------------------------> s : get a database value");
592 LOG_HELP(" x dbpush --------------------------> : pushes the top onto the database");
593 LOG_HELP(" dbpop|dbget -----------------------> x : removes/reads DB's top");
594 LOG_HELP(" dblen|dbat ------------------------> f : gets the DB's size/cursor pos");
595 LOG_HELP(" dbclr -----------------------------> : clear the DB");
596 LOG_HELP(" s dbsave|dbload--------------------> : save/load the DB to/from a file");
597 LOG_HELP(" x dbins ---------------------------> : moves the top into the DB");
598 LOG_HELP(" dbext|dbread ----------------------> x : extract/get from the DB's cursor");
599 LOG_HELP(" f dbmov|dbgoto --------------------> : move or set the DB's cursor");
600 LOG_HELP(" s localtime -----------------------> s : formats the current local time");
601 LOG_HELP(" s gmtime --------------------------> s : formats the current UTC time");
602 LOG_HELP(" time ------------------------------> f : seconds since VM start");
603 LOG_HELP(" s /MD4 digest ---------------------> s : MD4 digest");
604 LOG_HELP(" s /SHA256 digest ------------------> s : SHA256 digest");
605 LOG_HELP(" s /formatstring sprintf1s ---------> s : sprintf with 1 string (pad, cut)");
606 LOG_HELP(" s eval ----------------------------> : does something eval");
607 LOG_HELP(" Set operations operate on 'such''strings'.");
608 LOG_HELP(" Unknown tokens insert their cvar value.");