+#include "command.qh"
+#include "rpn.qh"
+
+
// ========================================
// RPN command code, written by divVerent
// Last updated: December 28th, 2011
// ========================================
-string rpn_pop()
+string rpn_pop()
{
if(rpn_sp > 0) {
--rpn_sp;
return rpn_stack[rpn_sp];
} else {
- print("rpn: stack underflow\n");
- rpn_error = TRUE;
+ LOG_INFO("rpn: stack underflow\n");
+ rpn_error = true;
return "";
}
}
-void rpn_push(string s)
+void rpn_push(string s)
{
if(rpn_sp < MAX_RPN_STACK) {
rpn_stack[rpn_sp] = s;
++rpn_sp;
} else {
- print("rpn: stack overflow\n");
- rpn_error = TRUE;
+ LOG_INFO("rpn: stack overflow\n");
+ rpn_error = true;
}
}
-string rpn_get()
+string rpn_get()
{
if(rpn_sp > 0) {
return rpn_stack[rpn_sp - 1];
} else {
- print("rpn: empty stack\n");
- rpn_error = TRUE;
+ LOG_INFO("rpn: empty stack\n");
+ rpn_error = true;
return "";
}
}
-void rpn_set(string s)
+void rpn_set(string s)
{
if(rpn_sp > 0) {
rpn_stack[rpn_sp - 1] = s;
} else {
- print("rpn: empty stack\n");
- rpn_error = TRUE;
+ LOG_INFO("rpn: empty stack\n");
+ rpn_error = true;
}
}
float rpn_getf() { return stof(rpn_get()); }
float rpn_popf() { return stof(rpn_pop()); }
-void rpn_pushf(float f) { return rpn_push(ftos(f)); }
-void rpn_setf(float f) { return rpn_set(ftos(f)); }
+void rpn_pushf(float f) { return rpn_push(sprintf("%.9g", f)); }
+void rpn_setf(float f) { return rpn_set(sprintf("%.9g", f)); }
void GenericCommand_rpn(float request, float argc, string command)
{
float i, j, f, f2, f3, rpnpos;
//vector rgb;
string s, s2, rpncmd;
-
+
if(!rpn_db)
{
rpn_db = db_create();
db_put(rpn_db, "stack.pointer", "0");
db_put(rpn_db, "stack.pos", "-1");
}
-
+
if(argc >= 2)
{
rpn_sp = 0;
- rpn_error = FALSE;
+ rpn_error = false;
for(rpnpos = 1; rpnpos < argc; ++rpnpos)
{
rpncmd = argv(rpnpos);
}
else
{
- print("rpn: empty cvar name for 'def'\n");
- rpn_error = TRUE;
+ LOG_INFO("rpn: empty cvar name for 'def'\n");
+ rpn_error = true;
}
} else if(rpncmd == "defs" || rpncmd == "@") {
s = "";
}
else
{
- print("rpn: empty cvar name for 'defs'\n");
- rpn_error = TRUE;
+ LOG_INFO("rpn: empty cvar name for 'defs'\n");
+ rpn_error = true;
}
} else if(rpncmd == "load") {
rpn_set(cvar_string(rpn_get()));
f = rpn_popf();
f2 = rpn_getf();
rpn_setf(f2 - f * floor(f2 / f));
+ } else if(rpncmd == "pow" || rpncmd == "**") {
+ f = rpn_popf();
+ rpn_setf(pow(rpn_getf(), f));
+ } else if(rpncmd == "bitand" || rpncmd == "&") {
+ f = rpn_popf();
+ rpn_setf(rpn_getf() & f);
+ } else if(rpncmd == "bitor" || rpncmd == "|") {
+ f = rpn_popf();
+ rpn_setf(rpn_getf() | f);
+ } else if(rpncmd == "bitxor" || rpncmd == "^") {
+ f = rpn_popf();
+ rpn_setf(rpn_getf() ^ f);
+ } else if(rpncmd == "and" || rpncmd == "&&") {
+ f = rpn_popf();
+ rpn_setf(rpn_getf() && f);
+ } else if(rpncmd == "or" || rpncmd == "||") {
+ f = rpn_popf();
+ rpn_setf(rpn_getf() || f);
+ } else if(rpncmd == "xor" || rpncmd == "^^") {
+ f = rpn_popf();
+ rpn_setf(!rpn_getf() != !f);
+ } else if(rpncmd == "bitnot") {
+ rpn_setf(~rpn_popf());
+ } else if(rpncmd == "not") {
+ rpn_setf(!rpn_popf());
} else if(rpncmd == "abs") {
rpn_setf(fabs(rpn_getf()));
} else if(rpncmd == "sgn") {
rpn_setf(floor(rpn_getf()));
} else if(rpncmd == "ceil" || rpncmd == "c") {
rpn_setf(ceil(rpn_getf()));
+ } else if(rpncmd == "exp") {
+ rpn_setf(exp(rpn_getf()));
+ } else if(rpncmd == "log") {
+ rpn_setf(exp(rpn_getf()));
+ } else if(rpncmd == "sin") {
+ rpn_setf(sin(rpn_getf()));
+ } else if(rpncmd == "cos") {
+ rpn_setf(cos(rpn_getf()));
} else if(rpncmd == "max") {
f = rpn_popf();
f2 = rpn_getf();
rpn_setf(bound(f3, f2, f));
} else if(rpncmd == "when") {
f = rpn_popf();
- f2 = rpn_popf();
- f3 = rpn_getf();
+ s = rpn_pop();
+ s2 = rpn_get();
if(f)
- rpn_setf(f3);
+ rpn_set(s2);
else
- rpn_setf(f2);
+ rpn_set(s);
} else if(rpncmd == ">" || rpncmd == "gt") {
f = rpn_popf();
rpn_setf(rpn_getf() > f);
} else if(rpncmd == "rand") {
rpn_setf(ceil(random() * rpn_getf()) - 1);
} else if(rpncmd == "crc16") {
- rpn_setf(crc16(FALSE, rpn_get()));
+ rpn_setf(crc16(false, rpn_get()));
} else if(rpncmd == "put") {
s2 = rpn_pop();
if (!rpn_error)
i = stof(db_get(rpn_db, "stack.pointer"));
db_put(rpn_db, "stack.pointer", ftos(i+1));
db_put(rpn_db, strcat("stack.", ftos(i)), s);
+ if(!i)
+ db_put(rpn_db, "stack.pos", "0");
}
- if(!i)
- db_put(rpn_db, "stack.pos", "0");
} else if(rpncmd == "dbpop") {
i = stof(db_get(rpn_db, "stack.pointer"));
if(i)
db_put(rpn_db, "stack.pos", ftos(i-2));
} else {
rpn_error = 1;
- print("rpn: database underflow\n");
+ LOG_INFO("rpn: database underflow\n");
}
} else if(rpncmd == "dbget") {
-
+
i = stof(db_get(rpn_db, "stack.pointer"));
if(i)
{
rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
} else {
rpn_error = 1;
- print("rpn: database empty\n");
+ LOG_INFO("rpn: database empty\n");
}
} else if(rpncmd == "dblen") {
rpn_push(db_get(rpn_db, "stack.pointer"));
{
j = stof(db_get(rpn_db, "stack.pointer"));
i = stof(db_get(rpn_db, "stack.pos"));
-
+
if(i < 0)
{
i = 0;
db_put(rpn_db, "stack.pos", "0");
}
-
+
db_put(rpn_db, "stack.pointer", ftos(j+1));
for(--j; j >= i; --j)
{
i = stof(db_get(rpn_db, "stack.pos"));
if(!j)
{
- rpn_error = TRUE;
- print("rpn: empty database\n");
+ rpn_error = true;
+ LOG_INFO("rpn: empty database\n");
} else {
--j;
rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
rpn_push(db_get(rpn_db, strcat("stack.", s)));
} else {
rpn_error = 1;
- print("rpn: empty database\n");
+ LOG_INFO("rpn: empty database\n");
}
} else if(rpncmd == "dbat") {
rpn_push(db_get(rpn_db, "stack.pos"));
{
if(i < 0 || i >= j)
{
- print("rpn: database cursor out of bounds\n");
- rpn_error = TRUE;
+ LOG_INFO("rpn: database cursor out of bounds\n");
+ rpn_error = true;
}
if(!rpn_error)
{
j = stof(db_get(rpn_db, "stack.pointer"));
if(!j)
{
- rpn_error = TRUE;
- print("rpn: empty database, cannot move cursor\n");
+ rpn_error = true;
+ LOG_INFO("rpn: empty database, cannot move cursor\n");
}
if(!rpn_error)
{
i = 0;
else
i = stof(s);
-
+
j = stof(db_get(rpn_db, "stack.pointer"));
if(i < 0 || i >= j)
{
- print("rpn: database cursor destination out of bounds\n");
- rpn_error = TRUE;
+ LOG_INFO("rpn: database cursor destination out of bounds\n");
+ rpn_error = true;
}
if(!rpn_error)
{
// tokens f..f2 represent s2
// UNION: add all tokens to s that are in s2 but not in s
s = "";
- for(i = 0; i < f; ++i)
+ for(i = 0; i < f; ++i)
s = strcat(s, " ", argv(i));
for(i = f; i < f2; ++i) {
for(j = 0; j < f; ++j)
if(argv(i) == argv(j))
goto skip_union;
s = strcat(s, " ", argv(i));
- :skip_union
+LABEL(skip_union)
}
if(substring(s, 0, 1) == " ")
s = substring(s, 1, 99999);
if(argv(i) == argv(j))
goto skip_difference;
s = strcat(s, " ", argv(i));
- :skip_difference
+LABEL(skip_difference)
}
if(substring(s, 0, 1) == " ")
s = substring(s, 1, 99999);
{
if (!fexists(s))
{
- print("rpn: ERROR: ", s, " does not exist!\n");
- rpn_error = TRUE;
+ LOG_INFO("rpn: ERROR: ", s, " does not exist!\n");
+ rpn_error = true;
}
}
} else if(rpncmd == "fexists") {
rpn_setf(0);
}
} else if(rpncmd == "localtime") {
- rpn_set(strftime(TRUE, rpn_get()));
+ rpn_set(strftime(true, rpn_get()));
} else if(rpncmd == "gmtime") {
- rpn_set(strftime(FALSE, rpn_get()));
+ rpn_set(strftime(false, rpn_get()));
} else if(rpncmd == "time") {
rpn_pushf(time);
} else if(rpncmd == "digest") {
} else if(rpncmd == "sprintf1s") {
s = rpn_pop();
rpn_set(sprintf(s, rpn_get()));
+ } else if(rpncmd == "eval") {
+ s = rpn_pop();
+ command = strcat(s, substring(command, argv_end_index(rpnpos), -1));
+ argc = tokenize_console(command);
+ rpnpos = -1;
} else {
rpn_push(cvar_string(rpncmd));
}
while(rpn_sp > 0)
{
s = rpn_pop();
- print("rpn: still on stack: ", s, "\n");
+ LOG_INFO("rpn: still on stack: ", s, "\n");
}
}
return;
}
-
+
default:
case CMD_REQUEST_USAGE:
{
- print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " rpn EXPRESSION...\n"));
- print(" Operator description (x: string, s: set, f: float):\n");
- print(" x pop -----------------------------> : removes the top\n");
- print(" x dup -----------------------------> x x : duplicates the top\n");
- print(" x x exch --------------------------> x x : swap the top two\n");
- print(" /cvarname load --------------------> x : loads a cvar\n");
- print(" /cvarname x def -------------------> : writes to a cvar\n");
- print(" f f add|sub|mul|div|mod|max|min ---> f : adds/... two numbers\n");
- print(" f f eq|ne|gt|ge|lt|le -------------> f : compares two numbers\n");
- print(" f neg|abs|sgn|rand|floor|ceil------> f : negates/... a number\n");
- print(" f f f bound -----------------------> f : bounds the middle number\n");
- print(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise\n");
- print(" s s union|intersection|difference -> s : set operations\n");
- print(" s shuffle -------------------------> s : randomly arrange elements\n");
- print(" /key /value put -------------------> : set a database key\n");
- print(" /key get --------------------------> s : get a database value\n");
- print(" x dbpush --------------------------> : pushes the top onto the database\n");
- print(" dbpop|dbget -----------------------> x : removes/reads DB's top\n");
- print(" dblen|dbat ------------------------> f : gets the DB's size/cursor pos\n");
- print(" dbclr -----------------------------> : clear the DB\n");
- print(" s dbsave|dbload--------------------> : save/load the DB to/from a file\n");
- print(" x dbins ---------------------------> : moves the top into the DB\n");
- print(" dbext|dbread ----------------------> x : extract/get from the DB's cursor\n");
- print(" f dbmov|dbgoto --------------------> : move or set the DB's cursor\n");
- print(" s localtime -----------------------> s : formats the current local time\n");
- print(" s gmtime --------------------------> s : formats the current UTC time\n");
- print(" time ------------------------------> f : seconds since VM start\n");
- print(" s /MD4 digest ---------------------> s : MD4 digest\n");
- print(" s /SHA256 digest ------------------> s : SHA256 digest\n");
- print(" s /formatstring sprintf1s ---------> s : sprintf with 1 string (pad, cut)\n");
- print(" Set operations operate on 'such''strings'.\n");
- print(" Unknown tokens insert their cvar value.\n");
+ LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " rpn EXPRESSION...\n"));
+ LOG_INFO(" Operator description (x: string, s: set, f: float):\n");
+ LOG_INFO(" x pop -----------------------------> : removes the top\n");
+ LOG_INFO(" x dup -----------------------------> x x : duplicates the top\n");
+ LOG_INFO(" x x exch --------------------------> x x : swap the top two\n");
+ LOG_INFO(" /cvarname load --------------------> x : loads a cvar\n");
+ LOG_INFO(" /cvarname x def -------------------> : writes to a cvar\n");
+ LOG_INFO(" f f add|sub|mul|div|mod|pow -------> f : adds/... two numbers\n");
+ LOG_INFO(" f f and|or|xor|bitand|bitor|bitxor > f : logical and bitwise operations\n");
+ LOG_INFO(" f f eq|ne|gt|ge|lt|le|max|min -----> f : compares two numbers\n");
+ LOG_INFO(" f neg|abs|sgn|rand|floor|ceil------> f : negates/... a number\n");
+ LOG_INFO(" f not|bitnot ----------------------> f : logical and bitwise negation\n");
+ LOG_INFO(" f exp|log|sin|cos -----------------> f : exponential function & Co.\n");
+ LOG_INFO(" f f f bound -----------------------> f : bounds the middle number\n");
+ LOG_INFO(" f1 f2 b when ----------------------> f : f1 if b, f2 otherwise\n");
+ LOG_INFO(" s s union|intersection|difference -> s : set operations\n");
+ LOG_INFO(" s shuffle -------------------------> s : randomly arrange elements\n");
+ LOG_INFO(" /key /value put -------------------> : set a database key\n");
+ LOG_INFO(" /key get --------------------------> s : get a database value\n");
+ LOG_INFO(" x dbpush --------------------------> : pushes the top onto the database\n");
+ LOG_INFO(" dbpop|dbget -----------------------> x : removes/reads DB's top\n");
+ LOG_INFO(" dblen|dbat ------------------------> f : gets the DB's size/cursor pos\n");
+ LOG_INFO(" dbclr -----------------------------> : clear the DB\n");
+ LOG_INFO(" s dbsave|dbload--------------------> : save/load the DB to/from a file\n");
+ LOG_INFO(" x dbins ---------------------------> : moves the top into the DB\n");
+ LOG_INFO(" dbext|dbread ----------------------> x : extract/get from the DB's cursor\n");
+ LOG_INFO(" f dbmov|dbgoto --------------------> : move or set the DB's cursor\n");
+ LOG_INFO(" s localtime -----------------------> s : formats the current local time\n");
+ LOG_INFO(" s gmtime --------------------------> s : formats the current UTC time\n");
+ LOG_INFO(" time ------------------------------> f : seconds since VM start\n");
+ LOG_INFO(" s /MD4 digest ---------------------> s : MD4 digest\n");
+ LOG_INFO(" s /SHA256 digest ------------------> s : SHA256 digest\n");
+ LOG_INFO(" s /formatstring sprintf1s ---------> s : sprintf with 1 string (pad, cut)\n");
+ LOG_INFO(" s eval ----------------------------> : does something eval\n");
+ LOG_INFO(" Set operations operate on 'such''strings'.\n");
+ LOG_INFO(" Unknown tokens insert their cvar value.\n");
return;
}
}
-}
\ No newline at end of file
+}