示例#1
0
static inline bool strint(const char *s, int &result) {
  try {
    result = eval_integer(s);
    return true;
  } catch(const char*) {
    result = 0;
    return false;
  }
}
示例#2
0
int xkas::eval(const char *&s, int depth) {
	if(!*s) throw "nall::bad_eval";
	while(*s == ' ' || *s == '\t') s++; //trim whitespace
	int value = 0, x = *s, y = *(s + 1);

	if(*s == '(') {
		value = eval(++s, 1);
		if(*s++ != ')') throw "nall::bad_eval";
	}

	else if(x == '!') value = !eval(++s, 14);
	else if(x == '~') value = ~eval(++s, 14);
	else if(x == '+') value = +eval(++s, 14);
	else if(x == '-') value = -eval(++s, 14);

	else if(x == '$' || x == '%' || x == '\'' || x == '.' || x == '_'
			|| (x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z') || (x >= '0' && x <= '9')
	) value = eval_integer(s);

	else throw "nall::bad_eval";

	for(;;) {
		while(*s == ' ' || *s == '\t') s++; //trim whitespace
		if(!*s) break;
		x = *s, y = *(s + 1);

		if(depth >= 13) break;
		if(x == '*') { value *= eval(++s, 13); continue; }
		if(x == '/') { value /= eval(++s, 13); continue; }
		if(x == '%') { value %= eval(++s, 13); continue; }

		if(depth >= 12) break;
		if(x == '+') { value += eval(++s, 12); continue; }
		if(x == '-') { value -= eval(++s, 12); continue; }

		if(depth >= 11) break;
		if(x == '<' && y == '<') { value <<= eval(++++s, 11); continue; }
		if(x == '>' && y == '>') { value >>= eval(++++s, 11); continue; }

		if(depth >= 10) break;
		if(x == '<' && y == '=') { value = value <= eval(++++s, 10); continue; }
		if(x == '>' && y == '=') { value = value >= eval(++++s, 10); continue; }
		if(x == '<') { value = value < eval(++s, 10); continue; }
		if(x == '>') { value = value > eval(++s, 10); continue; }

		if(depth >= 9) break;
		if(x == '=' && y == '=') { value = value == eval(++++s, 9); continue; }
		if(x == '!' && y == '=') { value = value != eval(++++s, 9); continue; }

		if(depth >= 8) break;
		if(x == '&' && y != '&') { value = value & eval(++s, 8); continue; }

		if(depth >= 7) break;
		if(x == '^' && y != '^') { value = value ^ eval(++s, 7); continue; }

		if(depth >= 6) break;
		if(x == '|' && y != '|') { value = value | eval(++s, 6); continue; }

		if(depth >= 5) break;
		if(x == '&' && y == '&') { value = eval(++++s, 5) && value; continue; }

		if(depth >= 4) break;
		if(x == '^' && y == '^') { value = (!eval(++++s, 4) != !value); continue; }

		if(depth >= 3) break;
		if(x == '|' && y == '|') { value = eval(++++s, 3) || value; continue; }

		if(x == '?') {
			int lhs = eval(++s, 2);
			if(*s != ':') throw "nall::bad_eval";
			int rhs = eval(++s, 2);
			value = value ? lhs : rhs;
			continue;
		}
		if(depth >= 2) break;

		if(depth > 0 && x == ')') break;

		throw "nall::bad_eval";
	}
示例#3
0
文件: math.hpp 项目: ben401/OpenEmu
static int eval(const char *&s, int depth = 0) {
  while(*s == ' ' || *s == '\t') s++; //trim whitespace
  if(!*s) throw "unrecognized_token";
  int value = 0, x = *s, y = *(s + 1);

  if(*s == '(') {
    value = eval(++s, 1);
    if(*s++ != ')') throw "mismatched_group";
  }

  else if(x == '!') value = !eval(++s, 13);
  else if(x == '~') value = ~eval(++s, 13);
  else if(x == '+') value = +eval(++s, 13);
  else if(x == '-') value = -eval(++s, 13);

  else if((x >= '0' && x <= '9') || x == '\'') value = eval_integer(s);

  else throw "unrecognized_token";

  while(true) {
    while(*s == ' ' || *s == '\t') s++; //trim whitespace
    if(!*s) break;
    x = *s, y = *(s + 1);

    if(depth >= 13) break;
    if(x == '*') { value *= eval(++s, 13); continue; }
    if(x == '/') { value /= eval(++s, 13); continue; }
    if(x == '%') { value %= eval(++s, 13); continue; }

    if(depth >= 12) break;
    if(x == '+') { value += eval(++s, 12); continue; }
    if(x == '-') { value -= eval(++s, 12); continue; }

    if(depth >= 11) break;
    if(x == '<' && y == '<') { value <<= eval(++++s, 11); continue; }
    if(x == '>' && y == '>') { value >>= eval(++++s, 11); continue; }

    if(depth >= 10) break;
    if(x == '<' && y == '=') { value = value <= eval(++++s, 10); continue; }
    if(x == '>' && y == '=') { value = value >= eval(++++s, 10); continue; }
    if(x == '<') { value = value < eval(++s, 10); continue; }
    if(x == '>') { value = value > eval(++s, 10); continue; }

    if(depth >= 9) break;
    if(x == '=' && y == '=') { value = value == eval(++++s, 9); continue; }
    if(x == '!' && y == '=') { value = value != eval(++++s, 9); continue; }

    if(depth >= 8) break;
    if(x == '&' && y != '&') { value = value & eval(++s, 8); continue; }

    if(depth >= 7) break;
    if(x == '^' && y != '^') { value = value ^ eval(++s, 7); continue; }

    if(depth >= 6) break;
    if(x == '|' && y != '|') { value = value | eval(++s, 6); continue; }

    if(depth >= 5) break;
    if(x == '&' && y == '&') { value = eval(++++s, 5) && value; continue; }

    if(depth >= 4) break;
    if(x == '^' && y == '^') { value = (!eval(++++s, 4) != !value); continue; }

    if(depth >= 3) break;
    if(x == '|' && y == '|') { value = eval(++++s, 3) || value; continue; }

    if(x == '?') {
      int lhs = eval(++s, 2);
      if(*s != ':') throw "mismatched_ternary";
      int rhs = eval(++s, 2);
      value = value ? lhs : rhs;
      continue;
    }
    if(depth >= 2) break;

    if(depth > 0 && x == ')') break;

    throw "unrecognized_token";
  }