]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/rpn.qc
Make it compilable :D
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / command / rpn.qc
1 string rpn_pop() 
2 {
3         if(rpn_sp > 0) {
4                 --rpn_sp;
5                 return rpn_stack[rpn_sp];
6         } else {
7                 print("rpn: stack underflow\n");
8                 rpn_error = TRUE;
9                 return "";
10         }
11 }
12 void rpn_push(string s) 
13 {
14         if(rpn_sp < MAX_RPN_STACK) {
15                 rpn_stack[rpn_sp] = s;
16                 ++rpn_sp;
17         } else {
18                 print("rpn: stack overflow\n");
19                 rpn_error = TRUE;
20         }
21 }
22 string rpn_get() 
23 {
24         if(rpn_sp > 0) {
25                 return rpn_stack[rpn_sp - 1];
26         } else {
27                 print("rpn: empty stack\n");
28                 rpn_error = TRUE;
29                 return "";
30         }
31 }
32 void rpn_set(string s) 
33 {
34         if(rpn_sp > 0) {
35                 rpn_stack[rpn_sp - 1] = s;
36         } else {
37                 print("rpn: empty stack\n");
38                 rpn_error = TRUE;
39         }
40 }
41
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)); }
46
47 void GenericCommand_rpn(float request, float argc, string command)
48 {
49         switch(request)
50         {
51                 case CMD_REQUEST_COMMAND:
52                 {
53                         float i, j, f, n, f2, f3, rpnpos;
54                         vector rgb;
55                         string s, s2, c, rpncmd;
56                         
57                         if(!rpn_db)
58                         {
59                                 rpn_db = db_create();
60                                 db_put(rpn_db, "stack.pointer", "0");
61                                 db_put(rpn_db, "stack.pos", "-1");
62                         }
63                         
64                         if(argc >= 2)
65                         {
66                                 rpn_sp = 0;
67                                 rpn_error = FALSE;
68                                 for(rpnpos = 1; rpnpos < argc; ++rpnpos)
69                                 {
70                                         rpncmd = argv(rpnpos);
71                                         f = strlen(rpncmd);
72                                         if(rpncmd == "") {
73                                         } else if(stof(substring(rpncmd, 0, 1)) > 0) {
74                                                 rpn_push(rpncmd);
75                                         } else if(substring(rpncmd, 0, 1) == "0") {
76                                                 rpn_push(rpncmd);
77                                         } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
78                                                 rpn_push(rpncmd);
79                                         } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
80                                                 rpn_push(rpncmd);
81                                         } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
82                                                 rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
83                                         } else if(rpncmd == "clear") {
84                                                 rpn_sp = 0;
85                                         } else if(rpncmd == "def" || rpncmd == "=") {
86                                                 s = rpn_pop();
87                                                 s2 = rpn_pop();
88
89                                                 if(s2 != "")
90                                                 {
91                                                         #ifdef MENUQC
92                                                         registercvar(s2, "", 0);
93                                                         #else
94                                                         registercvar(s2, "");
95                                                         #endif
96                                                         if(!rpn_error) // don't change cvars if a stack error had happened!
97                                                                 cvar_set(s2, s);
98                                                 }
99                                                 else
100                                                 {
101                                                         print("rpn: empty cvar name for 'def'\n");
102                                                         rpn_error = TRUE;
103                                                 }
104                                         } else if(rpncmd == "defs" || rpncmd == "@") {
105                                                 s = "";
106                                                 i = rpn_popf();
107                                                 j = (i == 0);
108                                                 while(rpn_sp > 1 && (j || i > 0))
109                                                 {
110                                                         s = strcat("/", rpn_pop(), " ", s);
111                                                         --i;
112                                                 }
113                                                 s2 = rpn_pop();
114                                                 if(s2 != "")
115                                                 {
116                                                         #ifdef MENUQC
117                                                         registercvar(s2, "", 0);
118                                                         #else
119                                                         registercvar(s2, "");
120                                                         #endif
121                                                         if(!rpn_error) // don't change cvars if a stack error had happened!
122                                                                 cvar_set(s2, s);
123                                                 }
124                                                 else
125                                                 {
126                                                         print("rpn: empty cvar name for 'defs'\n");
127                                                         rpn_error = TRUE;
128                                                 }
129                                         } else if(rpncmd == "load") {
130                                                 rpn_set(cvar_string(rpn_get()));
131                                         } else if(rpncmd == "exch") {
132                                                 s = rpn_pop();
133                                                 s2 = rpn_get();
134                                                 rpn_set(s);
135                                                 rpn_push(s2);
136                                         } else if(rpncmd == "dup") {
137                                                 rpn_push(rpn_get());
138                                         } else if(rpncmd == "pop") {
139                                                 rpn_pop();
140                                         } else if(rpncmd == "add" || rpncmd == "+") {
141                                                 f = rpn_popf();
142                                                 rpn_setf(rpn_getf() + f);
143                                         } else if(rpncmd == "sub" || rpncmd == "-") {
144                                                 f = rpn_popf();
145                                                 rpn_setf(rpn_getf() - f);
146                                         } else if(rpncmd == "mul" || rpncmd == "*") {
147                                                 f = rpn_popf();
148                                                 rpn_setf(rpn_getf() * f);
149                                         } else if(rpncmd == "div" || rpncmd == "/") {
150                                                 f = rpn_popf();
151                                                 rpn_setf(rpn_getf() / f);
152                                         } else if(rpncmd == "mod" || rpncmd == "%") {
153                                                 f = rpn_popf();
154                                                 f2 = rpn_getf();
155                                                 rpn_setf(f2 - f * floor(f2 / f));
156                                         } else if(rpncmd == "abs") {
157                                                 rpn_setf(fabs(rpn_getf()));
158                                         } else if(rpncmd == "sgn") {
159                                                 f = rpn_getf();
160                                                 if(f < 0)
161                                                         rpn_set("-1");
162                                                 else if(f > 0)
163                                                         rpn_set("1");
164                                                 else
165                                                         rpn_set("0");
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") {
173                                                 f = rpn_popf();
174                                                 f2 = rpn_getf();
175                                                 rpn_setf(max(f2, f));
176                                         } else if(rpncmd == "min") {
177                                                 f = rpn_popf();
178                                                 f2 = rpn_getf();
179                                                 rpn_setf(min(f2, f));
180                                         } else if(rpncmd == "bound") {
181                                                 f = rpn_popf();
182                                                 f2 = rpn_popf();
183                                                 f3 = rpn_getf();
184                                                 rpn_setf(bound(f3, f2, f));
185                                         } else if(rpncmd == "when") {
186                                                 f = rpn_popf();
187                                                 f2 = rpn_popf();
188                                                 f3 = rpn_getf();
189                                                 if(f)
190                                                         rpn_setf(f3);
191                                                 else
192                                                         rpn_setf(f2);
193                                         } else if(rpncmd == ">" || rpncmd == "gt") {
194                                                 f = rpn_popf();
195                                                 rpn_setf(rpn_getf() > f);
196                                         } else if(rpncmd == "<" || rpncmd == "lt") {
197                                                 f = rpn_popf();
198                                                 rpn_setf(rpn_getf() < f);
199                                         } else if(rpncmd == "==" || rpncmd == "eq") {
200                                                 f = rpn_popf();
201                                                 rpn_setf(rpn_getf() == f);
202                                         } else if(rpncmd == ">=" || rpncmd == "ge") {
203                                                 f = rpn_popf();
204                                                 rpn_setf(rpn_getf() >= f);
205                                         } else if(rpncmd == "<=" || rpncmd == "le") {
206                                                 f = rpn_popf();
207                                                 rpn_setf(rpn_getf() <= f);
208                                         } else if(rpncmd == "!=" || rpncmd == "ne") {
209                                                 f = rpn_popf();
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") {
216                                                 s2 = rpn_pop();
217                                                 if (!rpn_error)
218                                                 {
219                                                         s = rpn_pop();
220                                                         if (!rpn_error)
221                                                                 db_put(rpn_db, s, s2);
222                                                 }
223                                         } else if(rpncmd == "get") {
224                                                 s = rpn_pop();
225                                                 if (!rpn_error)
226                                                         rpn_push(db_get(rpn_db, s));
227                                         } else if(rpncmd == "dbpush") {
228                                                 s = rpn_pop();
229                                                 if(!rpn_error)
230                                                 {
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);
234                                                 }
235                                                 if(!i)
236                                                         db_put(rpn_db, "stack.pos", "0");
237                                         } else if(rpncmd == "dbpop") {
238                                                 i = stof(db_get(rpn_db, "stack.pointer"));
239                                                 if(i)
240                                                 {
241                                                         s = ftos(i-1);
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"));
245                                                         if(j >= i)
246                                                                 db_put(rpn_db, "stack.pos", ftos(i-2));
247                                                 } else {
248                                                         rpn_error = 1;
249                                                         print("rpn: database underflow\n");
250                                                 }
251                                         } else if(rpncmd == "dbget") {
252                                                 
253                                                 i = stof(db_get(rpn_db, "stack.pointer"));
254                                                 if(i)
255                                                 {
256                                                         rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
257                                                 } else {
258                                                         rpn_error = 1;
259                                                         print("rpn: database empty\n");
260                                                 }
261                                         } else if(rpncmd == "dblen") {
262                                                 rpn_push(db_get(rpn_db, "stack.pointer"));
263                                         } else if(rpncmd == "dbclr") {
264                                                 db_close(rpn_db);
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") {
269                                                 s = rpn_pop();
270                                                 if(!rpn_error)
271                                                         db_save(rpn_db, s);
272                                         } else if(rpncmd == "dbload") {
273                                                 s = rpn_pop();
274                                                 if(!rpn_error)
275                                                 {
276                                                         db_close(rpn_db);
277                                                         rpn_db = db_load(s);
278                                                 }
279                                         } else if(rpncmd == "dbins") {
280                                                 s = rpn_pop();
281                                                 if(!rpn_error)
282                                                         //if(rpn_sp > 0)
283                                                 {
284                                                         j = stof(db_get(rpn_db, "stack.pointer"));
285                                                         i = stof(db_get(rpn_db, "stack.pos"));
286                                                         
287                                                         if(i < 0)
288                                                         {
289                                                                 i = 0;
290                                                                 db_put(rpn_db, "stack.pos", "0");
291                                                         }
292                                                         
293                                                         db_put(rpn_db, "stack.pointer", ftos(j+1));
294                                                         for(--j; j >= i; --j)
295                                                         {
296                                                                 db_put(rpn_db, strcat("stack.", ftos(j+1)),
297                                                                                          db_get(rpn_db, (strcat("stack.", ftos(j))))
298                                                                         );
299                                                         }
300                                                         db_put(rpn_db, strcat("stack.", ftos(i)), s);
301                                                 }
302                                         } else if(rpncmd == "dbext") {
303                                                 j = stof(db_get(rpn_db, "stack.pointer"));
304                                                 i = stof(db_get(rpn_db, "stack.pos"));
305                                                 if(!j)
306                                                 {
307                                                         rpn_error = TRUE;
308                                                         print("rpn: empty database\n");
309                                                 } else {
310                                                         --j;
311                                                         rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
312                                                         db_put(rpn_db, "stack.pointer", ftos(j));
313                                                         if(i == j)
314                                                         {
315                                                                 db_put(rpn_db, "stack.pos", ftos(j-1));
316                                                         } else {
317                                                                 while(i < j)
318                                                                 {
319                                                                         db_put(rpn_db, strcat("stack.", ftos(i)),
320                                                                                                  db_get(rpn_db, (strcat("stack.", ftos(i+1))))
321                                                                                 );
322                                                                         ++i;
323                                                                 }
324                                                         }
325                                                 }
326                                         } else if(rpncmd == "dbread") {
327                                                 s = db_get(rpn_db, "stack.pos");
328                                                 if(stof(s) >= 0)
329                                                 {
330                                                         rpn_push(db_get(rpn_db, strcat("stack.", s)));
331                                                 } else {
332                                                         rpn_error = 1;
333                                                         print("rpn: empty database\n");
334                                                 }
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"));
340                                                 i += rpn_popf();
341                                                 if(!rpn_error)
342                                                 {
343                                                         if(i < 0 || i >= j)
344                                                         {
345                                                                 print("rpn: database cursor out of bounds\n");
346                                                                 rpn_error = TRUE;
347                                                         }
348                                                         if(!rpn_error)
349                                                         {
350                                                                 db_put(rpn_db, "stack.pos", ftos(i));
351                                                         }
352                                                 }
353                                         } else if(rpncmd == "dbgoto") {
354                                                 s = rpn_pop();
355                                                 j = stof(db_get(rpn_db, "stack.pointer"));
356                                                 if(!j)
357                                                 {
358                                                         rpn_error = TRUE;
359                                                         print("rpn: empty database, cannot move cursor\n");
360                                                 }
361                                                 if(!rpn_error)
362                                                 {
363                                                         if(s == "end")
364                                                                 i = stof(db_get(rpn_db, "stack.pointer"))-1;
365                                                         else if(s == "beg")
366                                                                 i = 0;
367                                                         else
368                                                                 i = stof(s);
369                                                         
370                                                         j = stof(db_get(rpn_db, "stack.pointer"));
371                                                         if(i < 0 || i >= j)
372                                                         {
373                                                                 print("rpn: database cursor destination out of bounds\n");
374                                                                 rpn_error = TRUE;
375                                                         }
376                                                         if(!rpn_error)
377                                                         {
378                                                                 db_put(rpn_db, "stack.pos", ftos(i));
379                                                         }
380                                                 }
381                                         } else if(rpncmd == "union") {
382                                                 // s s2 union
383                                                 s2 = rpn_pop();
384                                                 s = rpn_get();
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
390                                                 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))
396                                                                         goto skip_union;
397                                                         s = strcat(s, " ", argv(i));
398                                                         :skip_union
399                                                 }
400                                                 if(substring(s, 0, 1) == " ")
401                                                         s = substring(s, 1, 99999);
402                                                 rpn_set(s);
403                                                 tokenize_console(command);
404                                         } else if(rpncmd == "intersection") {
405                                                 // s s2 intersection
406                                                 s2 = rpn_pop();
407                                                 s = rpn_get();
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
413                                                 s = "";
414                                                 for(i = 0; i < f; ++i) {
415                                                         for(j = f; j < f2; ++j)
416                                                                 if(argv(i) == argv(j))
417                                                                 {
418                                                                         s = strcat(s, " ", argv(i));
419                                                                         break;
420                                                                 }
421                                                 }
422                                                 if(substring(s, 0, 1) == " ")
423                                                         s = substring(s, 1, 99999);
424                                                 rpn_set(s);
425                                                 tokenize_console(command);
426                                         } else if(rpncmd == "difference") {
427                                                 // s s2 difference
428                                                 s2 = rpn_pop();
429                                                 s = rpn_get();
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
435                                                 s = "";
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));
441                                                         :skip_difference
442                                                 }
443                                                 if(substring(s, 0, 1) == " ")
444                                                         s = substring(s, 1, 99999);
445                                                 rpn_set(s);
446                                                 tokenize_console(command);
447                                         } else if(rpncmd == "shuffle") {
448                                                 // s shuffle
449                                                 s = rpn_get();
450                                                 f = tokenize_console(s);
451
452                                                 for(i = 0; i < f - 1; ++i) {
453                                                         // move a random item from i..f-1 to position i
454                                                         s = "";
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)
460                                                                 if(j != f2)
461                                                                         s = strcat(s, " ", argv(j));
462                                                         f = tokenize_console(s);
463                                                 }
464
465                                                 if(substring(s, 0, 1) == " ")
466                                                         s = substring(s, 1, 99999);
467                                                 rpn_set(s);
468                                                 tokenize_console(command);
469                                         } else if(rpncmd == "fexists_assert") {
470                                                 s = rpn_pop();
471                                                 if(!rpn_error)
472                                                 {
473                                                         if (!fexists(s))
474                                                         {
475                                                                 print("rpn: ERROR: ", s, " does not exist!\n");
476                                                                 rpn_error = TRUE;
477                                                         }
478                                                 }
479                                         } else if(rpncmd == "fexists") {
480                                                 s = rpn_get();
481                                                 if(!rpn_error)
482                                                 {
483                                                         if (fexists(s))
484                                                                 rpn_setf(1);
485                                                         else
486                                                                 rpn_setf(0);
487                                                 }
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") {
493                                                 rpn_pushf(time);
494                                         } else if(rpncmd == "digest") {
495                                                 s = rpn_pop();
496                                                 rpn_set(digest_hex(s, rpn_get()));
497                                         } else if(rpncmd == "sprintf1s") {
498                                                 s = rpn_pop();
499                                                 rpn_set(sprintf(s, rpn_get()));
500                                         } else {
501                                                 rpn_push(cvar_string(rpncmd));
502                                         }
503                                         if(rpn_error)
504                                                 break;
505                                 }
506                                 while(rpn_sp > 0)
507                                 {
508                                         s = rpn_pop();
509                                         print("rpn: still on stack: ", s, "\n");
510                                 }
511                         }
512
513                         return;
514                 }
515                         
516                 default:
517                 case CMD_REQUEST_USAGE:
518                 {
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");
551                         return;
552                 }
553         }
554 }