1 // ========================================
2 // RPN command code, written by divVerent
3 // Last updated: December 28th, 2011
4 // ========================================
10 return rpn_stack[rpn_sp];
12 print("rpn: stack underflow\n");
17 void rpn_push(string s)
19 if(rpn_sp < MAX_RPN_STACK) {
20 rpn_stack[rpn_sp] = s;
23 print("rpn: stack overflow\n");
30 return rpn_stack[rpn_sp - 1];
32 print("rpn: empty stack\n");
37 void rpn_set(string s)
40 rpn_stack[rpn_sp - 1] = s;
42 print("rpn: empty stack\n");
47 float rpn_getf() { return stof(rpn_get()); }
48 float rpn_popf() { return stof(rpn_pop()); }
49 void rpn_pushf(float f) { return rpn_push(ftos(f)); }
50 void rpn_setf(float f) { return rpn_set(ftos(f)); }
52 void GenericCommand_rpn(float request, float argc, string command)
56 case CMD_REQUEST_COMMAND:
58 float i, j, f, f2, f3, rpnpos;
65 db_put(rpn_db, "stack.pointer", "0");
66 db_put(rpn_db, "stack.pos", "-1");
73 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
75 rpncmd = argv(rpnpos);
78 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
80 } else if(substring(rpncmd, 0, 1) == "0") {
82 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
84 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
86 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
87 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
88 } else if(rpncmd == "clear") {
90 } else if(rpncmd == "def" || rpncmd == "=") {
97 registercvar(s2, "", 0);
101 if(!rpn_error) // don't change cvars if a stack error had happened!
106 print("rpn: empty cvar name for 'def'\n");
109 } else if(rpncmd == "defs" || rpncmd == "@") {
113 while(rpn_sp > 1 && (j || i > 0))
115 s = strcat("/", rpn_pop(), " ", s);
122 registercvar(s2, "", 0);
124 registercvar(s2, "");
126 if(!rpn_error) // don't change cvars if a stack error had happened!
131 print("rpn: empty cvar name for 'defs'\n");
134 } else if(rpncmd == "load") {
135 rpn_set(cvar_string(rpn_get()));
136 } else if(rpncmd == "exch") {
141 } else if(rpncmd == "dup") {
143 } else if(rpncmd == "pop") {
145 } else if(rpncmd == "add" || rpncmd == "+") {
147 rpn_setf(rpn_getf() + f);
148 } else if(rpncmd == "sub" || rpncmd == "-") {
150 rpn_setf(rpn_getf() - f);
151 } else if(rpncmd == "mul" || rpncmd == "*") {
153 rpn_setf(rpn_getf() * f);
154 } else if(rpncmd == "div" || rpncmd == "/") {
156 rpn_setf(rpn_getf() / f);
157 } else if(rpncmd == "mod" || rpncmd == "%") {
160 rpn_setf(f2 - f * floor(f2 / f));
161 } else if(rpncmd == "abs") {
162 rpn_setf(fabs(rpn_getf()));
163 } else if(rpncmd == "sgn") {
171 } else if(rpncmd == "neg" || rpncmd == "~") {
172 rpn_setf(-rpn_getf());
173 } else if(rpncmd == "floor" || rpncmd == "f") {
174 rpn_setf(floor(rpn_getf()));
175 } else if(rpncmd == "ceil" || rpncmd == "c") {
176 rpn_setf(ceil(rpn_getf()));
177 } else if(rpncmd == "max") {
180 rpn_setf(max(f2, f));
181 } else if(rpncmd == "min") {
184 rpn_setf(min(f2, f));
185 } else if(rpncmd == "bound") {
189 rpn_setf(bound(f3, f2, f));
190 } else if(rpncmd == "when") {
198 } else if(rpncmd == ">" || rpncmd == "gt") {
200 rpn_setf(rpn_getf() > f);
201 } else if(rpncmd == "<" || rpncmd == "lt") {
203 rpn_setf(rpn_getf() < f);
204 } else if(rpncmd == "==" || rpncmd == "eq") {
206 rpn_setf(rpn_getf() == f);
207 } else if(rpncmd == ">=" || rpncmd == "ge") {
209 rpn_setf(rpn_getf() >= f);
210 } else if(rpncmd == "<=" || rpncmd == "le") {
212 rpn_setf(rpn_getf() <= f);
213 } else if(rpncmd == "!=" || rpncmd == "ne") {
215 rpn_setf(rpn_getf() != f);
216 } else if(rpncmd == "rand") {
217 rpn_setf(ceil(random() * rpn_getf()) - 1);
218 } else if(rpncmd == "crc16") {
219 rpn_setf(crc16(FALSE, rpn_get()));
220 } else if(rpncmd == "put") {
226 db_put(rpn_db, s, s2);
228 } else if(rpncmd == "get") {
231 rpn_push(db_get(rpn_db, s));
232 } else if(rpncmd == "dbpush") {
236 i = stof(db_get(rpn_db, "stack.pointer"));
237 db_put(rpn_db, "stack.pointer", ftos(i+1));
238 db_put(rpn_db, strcat("stack.", ftos(i)), s);
241 db_put(rpn_db, "stack.pos", "0");
242 } else if(rpncmd == "dbpop") {
243 i = stof(db_get(rpn_db, "stack.pointer"));
247 db_put(rpn_db, "stack.pointer", s);
248 rpn_push(db_get(rpn_db, strcat("stack.", s)));
249 j = stof(db_get(rpn_db, "stack.pos"));
251 db_put(rpn_db, "stack.pos", ftos(i-2));
254 print("rpn: database underflow\n");
256 } else if(rpncmd == "dbget") {
258 i = stof(db_get(rpn_db, "stack.pointer"));
261 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
264 print("rpn: database empty\n");
266 } else if(rpncmd == "dblen") {
267 rpn_push(db_get(rpn_db, "stack.pointer"));
268 } else if(rpncmd == "dbclr") {
270 rpn_db = db_create();
271 db_put(rpn_db, "stack.pointer", "0");
272 db_put(rpn_db, "stack.pos", "-1");
273 } else if(rpncmd == "dbsave") {
277 } else if(rpncmd == "dbload") {
284 } else if(rpncmd == "dbins") {
289 j = stof(db_get(rpn_db, "stack.pointer"));
290 i = stof(db_get(rpn_db, "stack.pos"));
295 db_put(rpn_db, "stack.pos", "0");
298 db_put(rpn_db, "stack.pointer", ftos(j+1));
299 for(--j; j >= i; --j)
301 db_put(rpn_db, strcat("stack.", ftos(j+1)),
302 db_get(rpn_db, (strcat("stack.", ftos(j))))
305 db_put(rpn_db, strcat("stack.", ftos(i)), s);
307 } else if(rpncmd == "dbext") {
308 j = stof(db_get(rpn_db, "stack.pointer"));
309 i = stof(db_get(rpn_db, "stack.pos"));
313 print("rpn: empty database\n");
316 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
317 db_put(rpn_db, "stack.pointer", ftos(j));
320 db_put(rpn_db, "stack.pos", ftos(j-1));
324 db_put(rpn_db, strcat("stack.", ftos(i)),
325 db_get(rpn_db, (strcat("stack.", ftos(i+1))))
331 } else if(rpncmd == "dbread") {
332 s = db_get(rpn_db, "stack.pos");
335 rpn_push(db_get(rpn_db, strcat("stack.", s)));
338 print("rpn: empty database\n");
340 } else if(rpncmd == "dbat") {
341 rpn_push(db_get(rpn_db, "stack.pos"));
342 } else if(rpncmd == "dbmov") {
343 j = stof(db_get(rpn_db, "stack.pointer"));
344 i = stof(db_get(rpn_db, "stack.pos"));
350 print("rpn: database cursor out of bounds\n");
355 db_put(rpn_db, "stack.pos", ftos(i));
358 } else if(rpncmd == "dbgoto") {
360 j = stof(db_get(rpn_db, "stack.pointer"));
364 print("rpn: empty database, cannot move cursor\n");
369 i = stof(db_get(rpn_db, "stack.pointer"))-1;
375 j = stof(db_get(rpn_db, "stack.pointer"));
378 print("rpn: database cursor destination out of bounds\n");
383 db_put(rpn_db, "stack.pos", ftos(i));
386 } else if(rpncmd == "union") {
390 f = tokenize_console(s);
391 f2 = tokenize_console(strcat(s, " ", s2));
392 // tokens 0..(f-1) represent s
393 // tokens f..f2 represent s2
394 // UNION: add all tokens to s that are in s2 but not in s
396 for(i = 0; i < f; ++i)
397 s = strcat(s, " ", argv(i));
398 for(i = f; i < f2; ++i) {
399 for(j = 0; j < f; ++j)
400 if(argv(i) == argv(j))
402 s = strcat(s, " ", argv(i));
405 if(substring(s, 0, 1) == " ")
406 s = substring(s, 1, 99999);
408 tokenize_console(command);
409 } else if(rpncmd == "intersection") {
413 f = tokenize_console(s);
414 f2 = tokenize_console(strcat(s, " ", s2));
415 // tokens 0..(f-1) represent s
416 // tokens f..f2 represent s2
417 // INTERSECTION: keep only the tokens from s that are also in s2
419 for(i = 0; i < f; ++i) {
420 for(j = f; j < f2; ++j)
421 if(argv(i) == argv(j))
423 s = strcat(s, " ", argv(i));
427 if(substring(s, 0, 1) == " ")
428 s = substring(s, 1, 99999);
430 tokenize_console(command);
431 } else if(rpncmd == "difference") {
435 f = tokenize_console(s);
436 f2 = tokenize_console(strcat(s, " ", s2));
437 // tokens 0..(f-1) represent s
438 // tokens f..f2 represent s2
439 // DIFFERENCE: keep only the tokens from s that are not in s2
441 for(i = 0; i < f; ++i) {
442 for(j = f; j < f2; ++j)
443 if(argv(i) == argv(j))
444 goto skip_difference;
445 s = strcat(s, " ", argv(i));
448 if(substring(s, 0, 1) == " ")
449 s = substring(s, 1, 99999);
451 tokenize_console(command);
452 } else if(rpncmd == "shuffle") {
455 f = tokenize_console(s);
457 for(i = 0; i < f - 1; ++i) {
458 // move a random item from i..f-1 to position i
460 f2 = floor(random() * (f - i) + i);
461 for(j = 0; j < i; ++j)
462 s = strcat(s, " ", argv(j));
463 s = strcat(s, " ", argv(f2));
464 for(j = i; j < f; ++j)
466 s = strcat(s, " ", argv(j));
467 f = tokenize_console(s);
470 if(substring(s, 0, 1) == " ")
471 s = substring(s, 1, 99999);
473 tokenize_console(command);
474 } else if(rpncmd == "fexists_assert") {
480 print("rpn: ERROR: ", s, " does not exist!\n");
484 } else if(rpncmd == "fexists") {
493 } else if(rpncmd == "localtime") {
494 rpn_set(strftime(TRUE, rpn_get()));
495 } else if(rpncmd == "gmtime") {
496 rpn_set(strftime(FALSE, rpn_get()));
497 } else if(rpncmd == "time") {
499 } else if(rpncmd == "digest") {
501 rpn_set(digest_hex(s, rpn_get()));
502 } else if(rpncmd == "sprintf1s") {
504 rpn_set(sprintf(s, rpn_get()));
506 rpn_push(cvar_string(rpncmd));
514 print("rpn: still on stack: ", s, "\n");
522 case CMD_REQUEST_USAGE:
524 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " rpn EXPRESSION...\n"));
525 print(" Operator description (x: string, s: set, f: float):\n");
526 print(" x pop -----------------------------> : removes the top\n");
527 print(" x dup -----------------------------> x x : duplicates the top\n");
528 print(" x x exch --------------------------> x x : swap the top two\n");
529 print(" /cvarname load --------------------> x : loads a cvar\n");
530 print(" /cvarname x def -------------------> : writes to a cvar\n");
531 print(" f f add|sub|mul|div|mod|max|min ---> f : adds/... two numbers\n");
532 print(" f f eq|ne|gt|ge|lt|le -------------> f : compares two numbers\n");
533 print(" f neg|abs|sgn|rand|floor|ceil------> f : negates/... a number\n");
534 print(" f f f bound -----------------------> f : bounds the middle number\n");
535 print(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise\n");
536 print(" s s union|intersection|difference -> s : set operations\n");
537 print(" s shuffle -------------------------> s : randomly arrange elements\n");
538 print(" /key /value put -------------------> : set a database key\n");
539 print(" /key get --------------------------> s : get a database value\n");
540 print(" x dbpush --------------------------> : pushes the top onto the database\n");
541 print(" dbpop|dbget -----------------------> x : removes/reads DB's top\n");
542 print(" dblen|dbat ------------------------> f : gets the DB's size/cursor pos\n");
543 print(" dbclr -----------------------------> : clear the DB\n");
544 print(" s dbsave|dbload--------------------> : save/load the DB to/from a file\n");
545 print(" x dbins ---------------------------> : moves the top into the DB\n");
546 print(" dbext|dbread ----------------------> x : extract/get from the DB's cursor\n");
547 print(" f dbmov|dbgoto --------------------> : move or set the DB's cursor\n");
548 print(" s localtime -----------------------> s : formats the current local time\n");
549 print(" s gmtime --------------------------> s : formats the current UTC time\n");
550 print(" time ------------------------------> f : seconds since VM start\n");
551 print(" s /MD4 digest ---------------------> s : MD4 digest\n");
552 print(" s /SHA256 digest ------------------> s : SHA256 digest\n");
553 print(" s /formatstring sprintf1s ---------> s : sprintf with 1 string (pad, cut)\n");
554 print(" Set operations operate on 'such''strings'.\n");
555 print(" Unknown tokens insert their cvar value.\n");