2014-02-26 17:31:20 +00:00
|
|
|
/* ported to C by pancake for r2 in 2012-2014 */
|
2012-02-14 04:26:41 +00:00
|
|
|
// TODO: integrate floating point support
|
|
|
|
// TODO: do not use global variables
|
2012-01-31 01:45:17 +00:00
|
|
|
/*
|
|
|
|
Reference Chapter 6:
|
|
|
|
"The C++ Programming Language", Special Edition.
|
|
|
|
Bjarne Stroustrup,Addison-Wesley Pub Co; 3 edition (February 15, 2000)
|
|
|
|
ISBN: 0201700735
|
|
|
|
*/
|
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
#include <r_types.h>
|
2012-02-14 03:41:40 +00:00
|
|
|
#include <r_util.h>
|
2012-01-31 01:45:17 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/* accessors */
|
2012-08-10 09:30:27 +00:00
|
|
|
static inline RNumCalcValue Nset(ut64 v) { RNumCalcValue n; n.d = (double)v; n.n = v; return n; }
|
|
|
|
static inline RNumCalcValue Nsetf(double v) { RNumCalcValue n; n.d = v; n.n = (ut64)v; return n; }
|
|
|
|
static inline RNumCalcValue Naddf(RNumCalcValue n, double v) { n.d += v; n.n += (ut64)v; return n; }
|
|
|
|
static inline RNumCalcValue Naddi(RNumCalcValue n, ut64 v) { n.d += (double)v; n.n += v; return n; }
|
|
|
|
static inline RNumCalcValue Nsubi(RNumCalcValue n, ut64 v) { n.d -= (double)v; n.n -= v; return n; }
|
2014-02-26 17:31:20 +00:00
|
|
|
static inline RNumCalcValue Nneg(RNumCalcValue n) { n.n = ~n.n; return n; }
|
|
|
|
static inline RNumCalcValue Norr(RNumCalcValue n, RNumCalcValue v) { n.d = v.d; n.n |= v.n; return n; }
|
|
|
|
static inline RNumCalcValue Nxor(RNumCalcValue n, RNumCalcValue v) { n.d = v.d; n.n ^= v.n; return n; }
|
|
|
|
static inline RNumCalcValue Nand(RNumCalcValue n, RNumCalcValue v) { n.d = v.d; n.n &= v.n; return n; }
|
2012-08-10 09:30:27 +00:00
|
|
|
static inline RNumCalcValue Nadd(RNumCalcValue n, RNumCalcValue v) { n.d += v.d; n.n += v.n; return n; }
|
|
|
|
static inline RNumCalcValue Nsub(RNumCalcValue n, RNumCalcValue v) { n.d -= v.d; n.n -= v.n; return n; }
|
|
|
|
static inline RNumCalcValue Nmul(RNumCalcValue n, RNumCalcValue v) { n.d *= v.d; n.n *= v.n; return n; }
|
|
|
|
static inline RNumCalcValue Ndiv(RNumCalcValue n, RNumCalcValue v) {
|
2012-05-31 00:41:45 +00:00
|
|
|
if (v.d) n.d /= v.d; else n.d = 0;
|
|
|
|
if (v.n) n.n /= v.n; else n.n = 0;
|
|
|
|
return n;
|
|
|
|
}
|
2012-01-31 01:45:17 +00:00
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
static RNumCalcValue expr(RNum*, RNumCalc*, int);
|
|
|
|
static RNumCalcValue term(RNum*, RNumCalc*, int);
|
|
|
|
static void error(RNum*, RNumCalc*, const char *);
|
|
|
|
static RNumCalcValue prim(RNum*, RNumCalc*, int);
|
|
|
|
static RNumCalcToken get_token(RNum*, RNumCalc*);
|
2012-02-14 03:41:40 +00:00
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
static void error(RNum *num, RNumCalc *nc, const char *s) {
|
|
|
|
nc->errors++;
|
|
|
|
nc->calc_err = s;
|
2012-02-14 03:41:40 +00:00
|
|
|
//fprintf (stderr, "error: %s\n", s);
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
static RNumCalcValue expr(RNum *num, RNumCalc *nc, int get) {
|
|
|
|
RNumCalcValue left = term (num, nc, get);
|
2012-01-31 01:45:17 +00:00
|
|
|
for (;;) {
|
2014-02-26 17:31:20 +00:00
|
|
|
switch (nc->curr_tok) {
|
|
|
|
case RNCPLUS: left = Nadd (left, term (num, nc, 1)); break;
|
|
|
|
case RNCMINUS: left = Nsub (left, term (num, nc, 1)); break;
|
|
|
|
case RNCXOR: left = Nxor (left, term (num, nc, 1)); break;
|
|
|
|
case RNCORR: left = Norr (left, term (num, nc, 1)); break;
|
|
|
|
case RNCAND: left = Nand (left, term (num, nc, 1)); break;
|
|
|
|
default:
|
|
|
|
return left;
|
|
|
|
}
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
2013-04-02 10:45:16 +00:00
|
|
|
return left;
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
static RNumCalcValue term(RNum *num, RNumCalc *nc, int get) {
|
|
|
|
RNumCalcValue left = prim (num, nc, get);
|
2012-01-31 01:45:17 +00:00
|
|
|
for (;;) {
|
2012-08-10 09:30:27 +00:00
|
|
|
if (nc->curr_tok == RNCMUL) {
|
|
|
|
left = Nmul (left, prim (num, nc, 1));
|
2012-01-31 01:45:17 +00:00
|
|
|
} else
|
2012-08-10 09:30:27 +00:00
|
|
|
if (nc->curr_tok == RNCDIV) {
|
|
|
|
RNumCalcValue d = prim (num, nc, 1);
|
2012-01-31 01:45:17 +00:00
|
|
|
if (!d.d) {
|
2013-12-28 01:34:15 +00:00
|
|
|
//error (num, nc, "divide by 0");
|
2012-01-31 01:45:17 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
left = Ndiv (left, d);
|
|
|
|
} else return left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
static RNumCalcValue prim(RNum *num, RNumCalc *nc, int get) {
|
|
|
|
RNumCalcValue v = {0};
|
|
|
|
if (get) get_token (num, nc);
|
|
|
|
switch (nc->curr_tok) {
|
|
|
|
case RNCNUMBER:
|
|
|
|
v = nc->number_value;
|
|
|
|
get_token (num, nc);
|
2012-01-31 01:45:17 +00:00
|
|
|
return v;
|
2012-08-10 09:30:27 +00:00
|
|
|
case RNCNAME:
|
|
|
|
//fprintf (stderr, "error: unknown keyword (%s)\n", nc->string_value);
|
|
|
|
//double& v = table[nc->string_value];
|
|
|
|
r_str_chop (nc->string_value);
|
|
|
|
v = Nset (r_num_get (num, nc->string_value));
|
|
|
|
get_token (num, nc);
|
|
|
|
if (nc->curr_tok == RNCASSIGN)
|
|
|
|
v = expr (num, nc, 1);
|
|
|
|
if (nc->curr_tok == RNCINC) Naddi (v, 1);
|
|
|
|
if (nc->curr_tok == RNCDEC) Nsubi (v, 1);
|
2012-01-31 01:45:17 +00:00
|
|
|
return v;
|
2014-02-26 17:31:20 +00:00
|
|
|
case RNCNEG:
|
|
|
|
v = nc->number_value;
|
|
|
|
get_token (num, nc);
|
|
|
|
return Nneg (nc->number_value); //prim (num, nc, 1), 1);
|
2012-08-10 09:30:27 +00:00
|
|
|
case RNCINC: return Naddi (prim (num, nc, 1), 1);
|
|
|
|
case RNCDEC: return Naddi (prim (num, nc, 1), -1);
|
2014-02-26 17:31:20 +00:00
|
|
|
case RNCORR: return Norr (v, prim (num, nc, 1));
|
2012-08-10 09:30:27 +00:00
|
|
|
case RNCMINUS: return Nsub (v, prim (num, nc, 1));
|
|
|
|
case RNCLEFTP:
|
|
|
|
v = expr (num, nc, 1);
|
|
|
|
if (nc->curr_tok == RNCRIGHTP)
|
|
|
|
get_token (num, nc);
|
|
|
|
else error (num, nc, " ')' expected");
|
|
|
|
case RNCEND:
|
|
|
|
case RNCPLUS:
|
|
|
|
case RNCMUL:
|
|
|
|
case RNCDIV:
|
|
|
|
case RNCPRINT:
|
|
|
|
case RNCASSIGN:
|
|
|
|
case RNCRIGHTP:
|
2012-02-15 22:42:27 +00:00
|
|
|
return v;
|
2012-08-10 09:30:27 +00:00
|
|
|
//default: error (num, nc, "primary expected");
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
static void cin_putback (RNum *num, RNumCalc *nc, char c) {
|
|
|
|
nc->oc = c;
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
R_API const char *r_num_calc_index (RNum *num, const char *p) {
|
|
|
|
if (num == NULL)
|
|
|
|
return NULL;
|
2012-06-27 23:27:40 +00:00
|
|
|
if (p) {
|
2012-08-10 09:30:27 +00:00
|
|
|
num->nc.calc_buf = p;
|
2012-12-20 10:31:38 +00:00
|
|
|
num->nc.calc_len = strlen (p);
|
2012-08-10 09:30:27 +00:00
|
|
|
num->nc.calc_i = 0;
|
2012-06-27 23:27:40 +00:00
|
|
|
}
|
2012-12-20 10:31:38 +00:00
|
|
|
//if (num->nc.calc_i>num->nc.calc_len) return NULL;
|
2012-08-10 09:30:27 +00:00
|
|
|
return num->nc.calc_buf + num->nc.calc_i;
|
2012-06-27 23:27:40 +00:00
|
|
|
}
|
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
static int cin_get(RNum *num, RNumCalc *nc, char *c) {
|
|
|
|
if (nc->oc) {
|
|
|
|
*c = nc->oc;
|
|
|
|
nc->oc = 0;
|
2012-01-31 01:45:17 +00:00
|
|
|
} else {
|
2012-08-10 09:30:27 +00:00
|
|
|
if (!nc->calc_buf)
|
2012-01-31 01:45:17 +00:00
|
|
|
return 0;
|
2012-12-20 10:31:38 +00:00
|
|
|
//if (nc->calc_i>nc->calc_len) return 0;
|
2012-08-10 09:30:27 +00:00
|
|
|
*c = nc->calc_buf[nc->calc_i];
|
|
|
|
if (*c) nc->calc_i++;
|
2012-02-14 03:41:40 +00:00
|
|
|
else return 0;
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
static int cin_get_num(RNum *num, RNumCalc *nc, RNumCalcValue *n) {
|
2012-01-31 01:45:17 +00:00
|
|
|
double d;
|
2012-10-31 09:42:49 +00:00
|
|
|
char str[R_NUMCALC_STRSZ];
|
2012-01-31 01:45:17 +00:00
|
|
|
int i = 0;
|
|
|
|
char c;
|
|
|
|
str[0] = 0;
|
2012-08-10 09:30:27 +00:00
|
|
|
while (cin_get (num, nc, &c)) {
|
2012-03-07 00:34:57 +00:00
|
|
|
if (c!=':' && c!='.' && !isalnum (c)) {
|
2012-08-10 09:30:27 +00:00
|
|
|
cin_putback (num, nc, c);
|
2012-01-31 01:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-08-10 09:30:27 +00:00
|
|
|
if (i<R_NUMCALC_STRSZ)
|
2012-01-31 01:45:17 +00:00
|
|
|
str[i++] = c;
|
|
|
|
}
|
|
|
|
str[i] = 0;
|
2012-08-10 09:30:27 +00:00
|
|
|
*n = Nset (r_num_get (num, str));
|
2012-02-14 04:26:41 +00:00
|
|
|
if (*str>='0' && *str<='9' && strchr (str, '.')) {
|
|
|
|
if (sscanf (str, "%lf", &d)<1)
|
|
|
|
return 0;
|
|
|
|
*n = Nsetf (d);
|
|
|
|
}
|
2012-02-14 03:41:40 +00:00
|
|
|
#if 0
|
|
|
|
// XXX: use r_num_get here
|
2012-01-31 01:45:17 +00:00
|
|
|
if (str[0]=='0' && str[1]=='x') {
|
|
|
|
ut64 x = 0;
|
|
|
|
if (sscanf (str+2, "%llx", &x)<1)
|
|
|
|
return 0;
|
|
|
|
*n = Nset (x);
|
2012-02-14 03:41:40 +00:00
|
|
|
} else
|
|
|
|
if (strchr (str, '.')) {
|
|
|
|
if (sscanf (str, "%lf", &d)<1)
|
|
|
|
return 0;
|
|
|
|
*n = Nsetf (d);
|
|
|
|
} else {
|
|
|
|
ut64 u;
|
|
|
|
if (sscanf (str, "%"PFMT64d, &u)<1)
|
|
|
|
return 0;
|
|
|
|
*n = Nset (u);
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
2012-02-14 03:41:40 +00:00
|
|
|
#endif
|
2012-01-31 01:45:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
static RNumCalcToken get_token(RNum *num, RNumCalc *nc) {
|
2013-01-04 13:51:21 +00:00
|
|
|
char ch = 0, c = 0;
|
2012-01-31 01:45:17 +00:00
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
do { if (!cin_get (num, nc, &ch)) return nc->curr_tok = RNCEND;
|
2012-01-31 01:45:17 +00:00
|
|
|
} while (ch!='\n' && isspace (ch));
|
|
|
|
|
|
|
|
switch (ch) {
|
2012-06-27 23:27:40 +00:00
|
|
|
case 0:
|
2012-01-31 01:45:17 +00:00
|
|
|
case ';':
|
|
|
|
case '\n':
|
2012-08-10 09:30:27 +00:00
|
|
|
return nc->curr_tok = RNCEND;
|
2012-01-31 01:45:17 +00:00
|
|
|
case '+': // added for ++name and name++
|
2012-08-10 09:30:27 +00:00
|
|
|
if (cin_get (num, nc, &c) && c == '+')
|
|
|
|
return nc->curr_tok = RNCINC;
|
|
|
|
cin_putback (num, nc, c);
|
|
|
|
return nc->curr_tok = (RNumCalcToken) ch;
|
2014-02-26 17:31:20 +00:00
|
|
|
// negate hack
|
|
|
|
case '~':
|
|
|
|
if (cin_get (num, nc, &c) && c == '-')
|
|
|
|
return nc->curr_tok = RNCNEG;
|
|
|
|
cin_putback (num, nc, c);
|
|
|
|
return nc->curr_tok = (RNumCalcToken) ch;
|
|
|
|
// negative number
|
2012-01-31 01:45:17 +00:00
|
|
|
case '-':
|
2012-08-10 09:30:27 +00:00
|
|
|
if (cin_get (num, nc, &c) && c == '-')
|
|
|
|
return nc->curr_tok = RNCDEC;
|
|
|
|
cin_putback (num, nc, c);
|
|
|
|
return nc->curr_tok = (RNumCalcToken) ch;
|
2014-02-26 17:31:20 +00:00
|
|
|
case '^':
|
|
|
|
case '&':
|
|
|
|
case '|':
|
2012-01-31 01:45:17 +00:00
|
|
|
case '*':
|
|
|
|
case '/':
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '=':
|
2012-08-10 09:30:27 +00:00
|
|
|
return nc->curr_tok = (RNumCalcToken) ch;
|
2014-02-26 17:31:20 +00:00
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
2012-01-31 01:45:17 +00:00
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
case '.':
|
2012-08-10 09:30:27 +00:00
|
|
|
cin_putback (num, nc, ch);
|
|
|
|
if (!cin_get_num (num, nc, &nc->number_value)) {
|
|
|
|
error (num, nc, "invalid number conversion");
|
2012-01-31 01:45:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2012-08-10 09:30:27 +00:00
|
|
|
return nc->curr_tok = RNCNUMBER;
|
2012-01-31 01:45:17 +00:00
|
|
|
default:
|
2012-02-15 10:11:25 +00:00
|
|
|
#define isvalidchar(x) \
|
2012-03-07 00:34:57 +00:00
|
|
|
(isalnum(x) || x==':' || x=='$' || x=='.' || x=='_' || x=='?' || x=='\\' \
|
2012-06-27 23:27:40 +00:00
|
|
|
|| x==' ' || x=='[' || x==']' || x=='}' || x=='{' || x=='/' || (x>='0'&&x<='9'))
|
2012-02-14 03:41:40 +00:00
|
|
|
{
|
2012-01-31 01:45:17 +00:00
|
|
|
int i = 0;
|
2012-08-10 09:30:27 +00:00
|
|
|
nc->string_value[i++] = ch;
|
2012-06-27 23:27:40 +00:00
|
|
|
if (ch == '[') {
|
2012-08-10 09:30:27 +00:00
|
|
|
while (cin_get (num, nc, &ch) && ch!=']') {
|
|
|
|
if (i>=R_NUMCALC_STRSZ) {
|
|
|
|
error (num, nc, "string too long");
|
2012-06-27 23:27:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-08-10 09:30:27 +00:00
|
|
|
nc->string_value[i++] = ch;
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
2012-08-10 09:30:27 +00:00
|
|
|
nc->string_value[i++] = ch;
|
2012-06-27 23:27:40 +00:00
|
|
|
} else {
|
2012-08-10 09:30:27 +00:00
|
|
|
while (cin_get (num, nc, &ch) && isvalidchar (ch)) {
|
|
|
|
if (i>=R_NUMCALC_STRSZ) {
|
|
|
|
error (num, nc, "string too long");
|
2012-06-27 23:27:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-08-10 09:30:27 +00:00
|
|
|
nc->string_value[i++] = ch;
|
2012-06-27 23:27:40 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-10 09:30:27 +00:00
|
|
|
nc->string_value[i] = 0;
|
|
|
|
cin_putback (num, nc, ch);
|
|
|
|
return nc->curr_tok = RNCNAME;
|
2014-02-26 17:31:20 +00:00
|
|
|
}
|
2012-08-10 09:30:27 +00:00
|
|
|
error (num, nc, "bad token");
|
|
|
|
return nc->curr_tok = RNCPRINT;
|
2012-01-31 01:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-10 09:30:27 +00:00
|
|
|
static void load_token(RNum *num, RNumCalc *nc, const char *s) {
|
|
|
|
nc->calc_i = 0;
|
2012-12-20 10:31:38 +00:00
|
|
|
nc->calc_len = 0;
|
2012-08-10 09:30:27 +00:00
|
|
|
nc->calc_buf = s;
|
|
|
|
nc->calc_err = NULL;
|
2012-02-14 03:41:40 +00:00
|
|
|
}
|
|
|
|
|
2012-02-14 04:26:41 +00:00
|
|
|
R_API ut64 r_num_calc (RNum *num, const char *str, const char **err) {
|
2012-08-10 09:30:27 +00:00
|
|
|
RNumCalcValue n;
|
|
|
|
RNumCalc *nc, nc_local;
|
2012-12-20 10:31:38 +00:00
|
|
|
if (!str || !*str)
|
2012-02-14 04:26:41 +00:00
|
|
|
return 0LL;
|
2012-08-10 09:30:27 +00:00
|
|
|
|
|
|
|
if (num == NULL)
|
|
|
|
nc = &nc_local;
|
|
|
|
else nc = &num->nc;
|
|
|
|
|
|
|
|
/* init */
|
|
|
|
nc->curr_tok = RNCPRINT;
|
|
|
|
nc->number_value.d = 0.0;
|
|
|
|
nc->number_value.n = 0LL;
|
|
|
|
nc->errors = 0;
|
|
|
|
nc->oc = 0;
|
|
|
|
nc->calc_err = NULL;
|
|
|
|
nc->calc_i = 0;
|
2012-12-20 10:31:38 +00:00
|
|
|
nc->calc_len = 0;
|
2012-08-10 09:30:27 +00:00
|
|
|
nc->calc_buf = NULL;
|
|
|
|
|
|
|
|
load_token (num, nc, str);
|
|
|
|
get_token (num, nc);
|
|
|
|
n = expr (num, nc, 0);
|
|
|
|
if (err) *err = nc->calc_err;
|
|
|
|
//if (nc->curr_tok == RNCEND) return 0LL; // XXX: Error
|
|
|
|
//if (nc->curr_tok == RNCPRINT) //return 0LL; // XXX: the fuck
|
|
|
|
// n = expr (num, nc, 0);
|
2012-02-14 04:26:41 +00:00
|
|
|
if (n.d != ((double)(ut64)n.d)) {
|
|
|
|
if (num) num->fvalue = n.d;
|
|
|
|
} else if (num) num->fvalue = (double)n.n;
|
2012-02-14 03:41:40 +00:00
|
|
|
return n.n;
|
|
|
|
}
|
2012-06-27 23:27:40 +00:00
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
int main(int argc, char* argv[]) {
|
2012-08-10 09:30:27 +00:00
|
|
|
RNumCalcValue n;
|
|
|
|
RNumCalc nc;
|
2012-06-27 23:27:40 +00:00
|
|
|
while (!feof (stdin)) {
|
2012-08-10 09:30:27 +00:00
|
|
|
get_token (nc);
|
|
|
|
if (nc.curr_tok == RNCEND) break;
|
|
|
|
if (nc.curr_tok == RNCPRINT) continue;
|
|
|
|
n = expr (num, nc, 0);
|
2012-06-27 23:27:40 +00:00
|
|
|
if (n.d == ((double)(int)n.d))
|
|
|
|
printf ("%llx\n", n.n);
|
|
|
|
else printf ("%lf\n", n.d);
|
|
|
|
}
|
2012-08-10 09:30:27 +00:00
|
|
|
return nc->errors;
|
2012-06-27 23:27:40 +00:00
|
|
|
}
|
|
|
|
#endif
|