Exemplo n.º 1
0
static int cin_get_num(RNum *num, RNumCalc *nc, RNumCalcValue *n) {
	double d;
	char str[R_NUMCALC_STRSZ]; // TODO: move into the heap?
	int i = 0;
	char c;
	str[0] = 0;
	while (cin_get (num, nc, &c)) {
		if (c != '_' && c!=':' && c!='.' && !isalnum ((ut8)c)) {
			cin_putback (num, nc, c);
			break;
		}
		if (i < R_NUMCALC_STRSZ) {
			str[i++] = c;
		}
	}
	str[i] = 0;
	*n = Nset (r_num_get (num, str));
	if (IS_DIGIT (*str) && strchr (str, '.')) {
		if (sscanf (str, "%lf", &d) < 1) {
			return 0;
		}
		if (n->n < d) {
			*n = Nsetf (d);
		}
		n->d = d;
	}
	return 1;
}
Exemplo n.º 2
0
static int cin_get_num(RNum *num, RNumCalc *nc, RNumCalcValue *n) {
	double d;
	char str[R_NUMCALC_STRSZ];
	int i = 0;
	char c;
	str[0] = 0;
	while (cin_get (num, nc, &c)) {
		if (c!=':' && c!='.' && !isalnum ((unsigned char)c)) {
			cin_putback (num, nc, c);
			break;
		}
		if (i<R_NUMCALC_STRSZ) {
			str[i++] = c;
		}
	}
	str[i] = 0;
	*n = Nset (r_num_get (num, str));
	if (*str>='0' && *str<='9' && strchr (str, '.')) {
		if (sscanf (str, "%lf", &d)<1)
			return 0;
		*n = Nsetf (d);
	}
#if 0
// XXX: use r_num_get here
	if (str[0]=='0' && str[1]=='x') {
		ut64 x = 0;
		if (sscanf (str+2, "%llx", &x)<1)
			return 0;
		*n = Nset (x);
	} 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);
	}
#endif
	return 1;
}
Exemplo n.º 3
0
static int cin_get_num(NumValue *n) {
	double d;
	char str[128];
	int i = 0;
	char c;
	str[0] = 0;
	while (cin_get (&c)) {
		if (c!=':' && c!='.' && !isalnum (c)) {
			cin_putback (c);
			break;
		}
		if (i<STRSZ)
			str[i++] = c;
	}
	str[i] = 0;
	*n = Nset (r_num_get (calc_num, str));
	if (*str>='0' && *str<='9' && strchr (str, '.')) {
		if (sscanf (str, "%lf", &d)<1)
			return 0;
		*n = Nsetf (d);
	}
#if 0
// XXX: use r_num_get here
	if (str[0]=='0' && str[1]=='x') {
		ut64 x = 0;
		if (sscanf (str+2, "%llx", &x)<1)
			return 0;
		*n = Nset (x);
	} 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);
	}
#endif
	return 1;
}
Exemplo n.º 4
0
static RNumCalcToken get_token(RNum *num, RNumCalc *nc) {
	char ch = 0, c = 0;

	do {
		if (!cin_get (num, nc, &ch)) {
			return nc->curr_tok = RNCEND;
		}
	} while (ch != '\n' && isspace ((ut8)ch));

	switch (ch) {
	case 0:
	case ';':
	case '\n':
		return nc->curr_tok = RNCEND;
	case '+':    // added for ++name and name++
		if (cin_get (num, nc, &c) && c == '+') {
			return nc->curr_tok = RNCINC;
		}
		cin_putback (num, nc, c);
		return nc->curr_tok = (RNumCalcToken) ch;
	// 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
	case '-':
		if (cin_get (num, nc, &c) && c == '-') {
			return nc->curr_tok = RNCDEC;
		}
		cin_putback (num, nc, c);
		return nc->curr_tok = (RNumCalcToken) ch;
	case '^':
	case '&':
	case '|':
	case '*':
	case '%':
	case '/':
	case '(':
	case ')':
	case '<':
	case '>':
	case '=':
		return nc->curr_tok = (RNumCalcToken) ch;
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
	case '.':
		cin_putback (num, nc, ch);
		if (!cin_get_num (num, nc, &nc->number_value)) {
			error (num, nc, "invalid number conversion");
			return 1;
		}
		return nc->curr_tok = RNCNUMBER;

#define isvalidchar(x) \
	(isalnum(x) || x==':' || x=='$' || x=='.' || x=='_' || x=='?' || x=='\\' \
	|| x==' ' || x=='[' || x==']' || x=='}' || x=='{' || (x>='0'&&x<='9'))

	default:
		{
			int i = 0;
#define stringValueAppend(x) { \
	const size_t max = sizeof (nc->string_value) - 1; \
	if (i < max) nc->string_value[i++] = x; \
	else nc->string_value[max] = 0; \
}
			stringValueAppend(ch);
			if (ch == '[') {
				while (cin_get (num, nc, &ch) && ch!=']') {
					if (i > R_NUMCALC_STRSZ - 1) {
						error (num, nc, "string too long");
						return 0;
					}
					stringValueAppend(ch);
				}
				stringValueAppend(ch);
			} else {
				while (cin_get (num, nc, &ch) && isvalidchar ((unsigned char)ch)) {
					if (i >= R_NUMCALC_STRSZ) {
						error (num, nc, "string too long");
						return 0;
					}
					stringValueAppend(ch);
				}
			}
			stringValueAppend(0);
			if (ch!='\'') {
				cin_putback (num, nc, ch);
			}
			return nc->curr_tok = RNCNAME;
		}
	}
}
Exemplo n.º 5
0
static RNumCalcToken get_token(RNum *num, RNumCalc *nc) {
	char ch = 0, c = 0;

	do { if (!cin_get (num, nc, &ch)) return nc->curr_tok = RNCEND;
	} while (ch!='\n' && isspace ((unsigned char)ch));

	switch (ch) {
	case 0:
	case ';':
	case '\n':
		return nc->curr_tok = RNCEND;
	case '+':    // added for ++name and name++
		if (cin_get (num, nc, &c) && c == '+')
			return nc->curr_tok = RNCINC;
		cin_putback (num, nc, c);
		return nc->curr_tok = (RNumCalcToken) ch;
	// 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
	case '-':
		if (cin_get (num, nc, &c) && c == '-')
			return nc->curr_tok = RNCDEC;
		cin_putback (num, nc, c);
		return nc->curr_tok = (RNumCalcToken) ch;
	case '^':
	case '&':
	case '|':
	case '*':
	case '%':
	case '/':
	case '(':
	case ')':
	case '=':
		return nc->curr_tok = (RNumCalcToken) ch;
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
	case '.':
		cin_putback (num, nc, ch);
		if (!cin_get_num (num, nc, &nc->number_value)) {
			error (num, nc, "invalid number conversion");
			return 1;
		}
		return nc->curr_tok = RNCNUMBER;

#define isvalidchar(x) \
	(isalnum(x) || x==':' || x=='$' || x=='.' || x=='_' || x=='?' || x=='\\' \
	|| x==' ' || x=='[' || x==']' || x=='}' || x=='{' || x=='/' || (x>='0'&&x<='9'))

	default:
		{
			int i = 0;
			nc->string_value[i++] = ch;
			if (ch == '[') {
				while (cin_get (num, nc, &ch) && ch!=']') {
					if (i>=R_NUMCALC_STRSZ) {
						error (num, nc, "string too long");
						return 0;
					}
					nc->string_value[i++] = ch;
				}
				nc->string_value[i++] = ch;
			} else {
				while (cin_get (num, nc, &ch) && isvalidchar ((unsigned char)ch)) {
					if (i>=R_NUMCALC_STRSZ) {
						error (num, nc, "string too long");
						return 0;
					}
					nc->string_value[i++] = ch;
				}
			}
			nc->string_value[i] = 0;
			cin_putback (num, nc, ch);
			return nc->curr_tok = RNCNAME;
		}
/*
 * Unreacheable code:
		error (num, nc, "bad token");
		return nc->curr_tok = RNCPRINT;
*/
	}
}
Exemplo n.º 6
0
static RNumToken get_token() {
	char c, ch;

	do { if (!cin_get (&ch)) return curr_tok = END;
	} while (ch!='\n' && isspace (ch));

	switch (ch) {
	case 0:
	case ';':
	case '\n':
		return curr_tok = END;
	case '+':    // added for ++name and name++
		if (cin_get (&c) && c == '+')
			return curr_tok = INC;
		cin_putback (c);
		return curr_tok = (RNumToken) ch;
	case '-':
		if (cin_get (&c) && c == '-')
			return curr_tok = DEC;
		cin_putback (c);
		return curr_tok = (RNumToken) ch;
	case '*':
	case '/':
	case '(':
	case ')':
	case '=':
		return curr_tok = (RNumToken) ch;
	case '0':case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
	case '.':
		cin_putback (ch);
		if (!cin_get_num (&number_value)) {
			error ("invalid number conversion");
			return 1;
		}
		return curr_tok = NUMBER;
	default:
#define isvalidchar(x) \
	(isalnum(x) || x==':' || x=='$' || x=='.' || x=='_' || x=='?' || x=='\\' \
	|| x==' ' || x=='[' || x==']' || x=='}' || x=='{' || x=='/' || (x>='0'&&x<='9'))
{
			int i = 0;
			string_value[i++] = ch;
			if (ch == '[') {
				while (cin_get (&ch) && ch!=']') {
					if (i>=STRSZ) {
						error ("string too long");
						return 0;
					}
					string_value[i++] = ch;
				}
				string_value[i++] = ch;
			} else {
				while (cin_get (&ch) && isvalidchar (ch)) {
					if (i>=STRSZ) {
						error ("string too long");
						return 0;
					}
					string_value[i++] = ch;
				}
			}
			string_value[i] = 0;
			cin_putback (ch);

			return curr_tok = NAME;
}
		//}
		error ("bad token");
		return curr_tok = PRINT;
	}
}