Example #1
0
bool ToolBox::isdigit(std::string str, bool deci)
{
	bool point = false;
	bool expo = false;
	bool in = false;

	for (std::string::iterator it=str.begin(); it!=str.end(); ++it)
	{
		//std::cout << str.size() ;
		if (in == false && isnumber(it))
		{
			in = true;
		}
		if (!(isexpo(str, it, expo) || (isvirg(str, it, point, in) && deci) || issign(str, it, in) || isnumber(it)))
		{
			//std::cout << "return " << *it  <<"|"<< std::endl;
			return false;
		}
		//if (!((*it >= '0' && *it <= '9') || (*it == '.' && !point && deci == true && it!=str.end() - 1)
		//	|| (*it == '-' && in == false) || (*it == '+' && in == false) ))
		//	return false;
		//point = (*it == '.') ? true : point;
	}
	if (!in)
		return false;
	return true;
}
Example #2
0
void Lexer::ScanFloat()
{
	char ch = NextChar();
	if (isdigit(PeekAtChar()))
	{
		m_tokenType = FloatingConst;

		do
		{
			*tp++ = ch;
			ch = NextChar();
		} while (isdigit(ch));

		// Read the exponent, if any
		if (ch == 'e' || ch == 'd' || ch == 'q')
		{
			char peek = PeekAtChar();
			if (isdigit(peek) || issign(peek))
			{
				if (issign(peek))
				{
					NextChar();
					if (!isdigit(PeekAtChar()))
					{
						PushBack(peek);
						PushBack(ch);
						return;
					}

					ch = peek;
				}
				else
					ch = NextChar();

				*tp++ = 'e';

				do
				{
					*tp++ = ch;
					ch = NextChar();
				} while (isdigit(ch));
			}
		}
	}

	PushBack(ch);
}
Example #3
0
static int getop2(char *s)
{
    int c;

    /* skip the white space */
    while (isspace(c = *s = mygetchar()) && c != '\n')
        ;

    s[1] = '\0';

    if (!isdigit(c) && !issign(c) && c != '.') {
        return c;
    }

    /* scan the char after the sign */
    if (issign(c)) {
        if (isdigit(c = mygetchar())) {
            *++s = c;
        } else {
            myungetc(c);
            return *s;
        }
    }

    /* scan for the integral part */
    while (isdigit(c = mygetchar())) {
        *++s = c;
    }

    /* not a digit, is it a decimal point? */
    if (c == '.') {
        *++s = c;

        /* scan for the floating part */
        while (isdigit(c = mygetchar())) {
            *++s = c;
        }
    }

    if (c != EOF) {
        myungetc(c);
    }

    *++s = '\0';
    return NUMBER;
}
Example #4
0
static int
read_sign(const char **s,
	  char **b)
{
    int sign = '?';

    if (issign(**s)) {
	sign = **b = **s;
	(*s)++;
	(*b)++;
    }
    return sign;
}
Example #5
0
inline bool isFloat(const char* s) {
  if (!s) return false;

  if (!strcmp(s, "NaN")) return true;
  if (issign(*s)) s++;
  if (!strcmp(s, "Infinity")) return true;
  if (*s == '\0') return false;

  while (isdigit(*s)) s++;

  if (*s == '.') {
    s++;
    while (isdigit(*s)) s++;
  }

  if (*s == 'e' || *s == 'E') {
    s++;
    if (issign(*s)) s++;
    if (!isdigit(*s)) return false;
    while (isdigit(*s)) s++;
  }

  return *s == '\0';
}
Example #6
0
static int
read_comp(const char **s, int strict,
	  VALUE *ret, char **b)
{
    char *bb;
    int sign;
    VALUE num, num2;

    bb = *b;

    sign = read_sign(s, b);

    if (isimagunit(**s)) {
	(*s)++;
	num = INT2FIX((sign == '-') ? -1 : + 1);
	*ret = rb_complex_new2(ZERO, num);
	return 1; /* e.g. "i" */
    }

    if (!read_rat_nos(s, strict, b)) {
	**b = '\0';
	num = str2num(bb);
	*ret = rb_complex_new2(num, ZERO);
	return 0; /* e.g. "-" */
    }
    **b = '\0';
    num = str2num(bb);

    if (isimagunit(**s)) {
	(*s)++;
	*ret = rb_complex_new2(ZERO, num);
	return 1; /* e.g. "3i" */
    }

    if (**s == '@') {
	int st;

	(*s)++;
	bb = *b;
	st = read_rat(s, strict, b);
	**b = '\0';
	if (strlen(bb) < 1 ||
	    !isdecimal(*(bb + strlen(bb) - 1))) {
	    *ret = rb_complex_new2(num, ZERO);
	    return 0; /* e.g. "1@-" */
	}
	num2 = str2num(bb);
	*ret = rb_complex_polar(num, num2);
	if (!st)
	    return 0; /* e.g. "1@2." */
	else
	    return 1; /* e.g. "1@2" */
    }

    if (issign(**s)) {
	bb = *b;
	sign = read_sign(s, b);
	if (isimagunit(**s))
	    num2 = INT2FIX((sign == '-') ? -1 : + 1);
	else {
	    if (!read_rat_nos(s, strict, b)) {
		*ret = rb_complex_new2(num, ZERO);
		return 0; /* e.g. "1+xi" */
	    }
	    **b = '\0';
	    num2 = str2num(bb);
	}
	if (!isimagunit(**s)) {
	    *ret = rb_complex_new2(num, ZERO);
	    return 0; /* e.g. "1+3x" */
	}
	(*s)++;
	*ret = rb_complex_new2(num, num2);
	return 1; /* e.g. "1+2i" */
    }
    /* !(@, - or +) */
    {
	*ret = rb_complex_new2(num, ZERO);
	return 1; /* e.g. "3" */
    }
}
Example #7
0
inline bool isInteger(const char* s) {
  if (!s) return false;
  if (issign(*s)) s++;
  while (isdigit(*s)) s++;
  return *s == '\0';
}
Example #8
0
File: lread.c Project: cran/rioja
l_R(int poststar, int reqint)
#endif
{
	char s[FMAX+EXPMAXDIGS+4];
	register int ch;
	register char *sp, *spe, *sp1;
	long e, exp;
	int havenum, havestar, se;

	if (!poststar) {
		if (f__lcount > 0)
			return(0);
		f__lcount = 1;
		}
#ifdef Allow_TYQUAD
	f__llx = 0;
#endif
	f__ltype = 0;
	exp = 0;
	havestar = 0;
retry:
	sp1 = sp = s;
	spe = sp + FMAX;
	havenum = 0;

	switch(GETC(ch)) {
		case '-': *sp++ = ch; sp1++; spe++;
		case '+':
			GETC(ch);
		}
	while(ch == '0') {
		++havenum;
		GETC(ch);
		}
	while(isdigit(ch)) {
		if (sp < spe) *sp++ = ch;
		else ++exp;
		GETC(ch);
		}
	if (ch == '*' && !poststar) {
		if (sp == sp1 || exp || *s == '-') {
			errfl(f__elist->cierr,112,"bad repetition count");
			}
		poststar = havestar = 1;
		*sp = 0;
		f__lcount = atoi(s);
		goto retry;
		}
	if (ch == '.') {
#ifndef ALLOW_FLOAT_IN_INTEGER_LIST_INPUT
		if (reqint)
			errfl(f__elist->cierr,115,"invalid integer");
#endif
		GETC(ch);
		if (sp == sp1)
			while(ch == '0') {
				++havenum;
				--exp;
				GETC(ch);
				}
		while(isdigit(ch)) {
			if (sp < spe)
				{ *sp++ = ch; --exp; }
			GETC(ch);
			}
		}
	havenum += sp - sp1;
	se = 0;
	if (issign(ch))
		goto signonly;
	if (havenum && isexp(ch)) {
#ifndef ALLOW_FLOAT_IN_INTEGER_LIST_INPUT
		if (reqint)
			errfl(f__elist->cierr,115,"invalid integer");
#endif
		GETC(ch);
		if (issign(ch)) {
signonly:
			if (ch == '-') se = 1;
			GETC(ch);
			}
		if (!isdigit(ch)) {
bad:
			errfl(f__elist->cierr,112,"exponent field");
			}

		e = ch - '0';
		while(isdigit(GETC(ch))) {
			e = 10*e + ch - '0';
			if (e > EXPMAX)
				goto bad;
			}
		if (se)
			exp -= e;
		else
			exp += e;
		}
	(void) Ungetc(ch, f__cf);
	if (sp > sp1) {
		++havenum;
		while(*--sp == '0')
			++exp;
		if (exp)
			sprintf(sp+1, "e%ld", exp);
		else
			sp[1] = 0;
		f__lx = atof(s);
#ifdef Allow_TYQUAD
		if (reqint&2 && (se = sp - sp1 + exp) > 14 && se < 20) {
			/* Assuming 64-bit longint and 32-bit long. */
			if (exp < 0)
				sp += exp;
			if (sp1 <= sp) {
				f__llx = *sp1 - '0';
				while(++sp1 <= sp)
					f__llx = 10*f__llx + (*sp1 - '0');
				}
			while(--exp >= 0)
				f__llx *= 10;
			if (*s == '-')
				f__llx = -f__llx;
			}
#endif
		}
	else
		f__lx = 0.;
	if (havenum)
		f__ltype = TYLONG;
	else
		switch(ch) {
			case ',':
			case '/':
				break;
			default:
				if (havestar && ( ch == ' '
						||ch == '\t'
						||ch == '\n'))
					break;
				if (nml_read > 1) {
					f__lquit = 2;
					return 0;
					}
				errfl(f__elist->cierr,112,"invalid number");
			}
	return 0;
	}
Example #9
0
/*
 * Gets the next token from the `lexfile` FILE stream.
 * Side effects:
 *  - If the TokenType has an associated string, it is found in global `lexstr`.
 *  - If the TokenType has an associated integer value, look in global `lexint`.
 */
TokenType next_tok(void) {
    if (!curr_char) eat(); // Eat first char.

    while (curr_char != EOF) {
        lo_col = curr_col; // Save first col of the token.

        // Newline.
        if (curr_char == '\n') {
            eat();
            return TOK_NL;
        }

        // Skip whitespace.
        if (isspace(curr_char)) {
            eat();
            continue;
        }

        // Skip comments until next line.
        if (curr_char == ';') {
            do {
                eat();
            } while (curr_char != '\n' && curr_char != EOF);
            continue;
        }

        // id ::= [A-Za-z$_][A-Za-z_$0-9]*
        // reg ::= r([0-9]|1[0-5])[abcd] | rs | re[0-6] | rk[0-7]
        // label ::= <nonopcode id>:
        if (is_idstart(curr_char)) {
            int i;
            // TODO: Make this loop prettier.
            for (i = 0; curr_char != EOF; i++) {
                lexstr[i] = curr_char;
                if (is_idcont(peek())) {
                    eat();
                } else {
                    break;
                }
            }
            lexstr[++i] = '\0';
            eat(); // Advance to next char after the identifier.

            // Register?
            if (lexstr[0] == 'r') {

                // Long or short
                if (is_long_reg(lexstr))
                    return TOK_GL_REG;
                if (is_short_reg(lexstr))
                    return TOK_GS_REG;

                // Extra
                if (is_extra_reg(lexstr))
                    return TOK_E_REG;

                // Kernel
                if (is_kernel_reg(lexstr))
                    return TOK_K_REG;
            }

            // Directive?
            if (is_dtv(lexstr)) {
                return TOK_DATA_SEG;
            }

            // Instruction?
            if (isInstruction(lexstr))
                return TOK_INSTR;

            // Label?
            if (curr_char == ':') {
                eat(); // Eat the ':'
                return TOK_LABEL;
            }

            // Plain identfier
            return TOK_ID;
        }

        // chr_lit ::= '[^\\']'
        if (curr_char == '\'') {
            eat(); // Get inner char.
            if (curr_char == '\\') {
                lexstr[0] = escape(eat());
            } else {
                lexstr[0] = curr_char;
            }
            lexstr[1] = '\0';
            eat(); // Reach the closing quote.

            // Error: for situations like '\'
            if (curr_char != '\'') {
                jas_err("Character literal missing closing quote.",
                         curr_line, lo_col, curr_col);
                return TOK_UNK;
            }

            eat(); // Get rid of ' and advance.
            return TOK_CHR_LIT;
        }

        // str_lit ::= "(\\.|[^\\"])*"
        if (curr_char == '"') {
            eat(); // Get first char of string.

            // Let by escape chars, but not single \ or ".
            int i;
            for (i = 0; curr_char != '"'; i++) {
                // Check that we don't close reach EOF before the close ".
                if (curr_char == EOF) {
                    jas_err("EOF while parsing string literal.",
                            curr_line, curr_col, curr_col);
                    return TOK_UNK;
                }

                if (curr_char == '\\') {
                    lexstr[i] = escape(eat());
                } else {
                    lexstr[i] = curr_char;
                }
                eat();
            }
            lexstr[i] = '\0';

            eat(); // Get rid of the " and advance.
            return TOK_STR_LIT;
        }

        // num_lit ::= [+-][1-9][0-9]* | [+-]0[0-7]* | [+-]0x[0-9A-Fa-f]+
        //          |  [+-]0b[01]+
        if ((issign(curr_char) && isdigit(peek())) || isdigit(curr_char)) {
            int base = 10;  // Numeric base for interpreting the literal.
            int chars_read; // Keep track of how many columns we move forward.
            int sign = +1;

            // Grab sign if it exists.
            if (issign(curr_char)) {
                sign = (curr_char == '+' ? +1 : -1);
                eat(); // Get next number character.
            }

            // Choose base by prefix:
            if (curr_char == '0') {
                int next = peek();
                if (next == 'x' || next == 'X') {
                    base = HEX_BASE;
                } else if (next == 'b' || next == 'B') {
                    base = BIN_BASE;
                } else {
                    base = OCT_BASE;
                }
            }

            // Re-place sign back into lexstr for strtol.
            if (sign == +1) {
                lexstr[0] = '+';
            } else {
                lexstr[0] = '-';
            }

            // Copy in whole num literal, keep track of columns.
            chars_read = fgets_base(lexstr + 1, lexfile, base);
            curr_col += chars_read;

            // Convert to integer value, using saved sign.
            lexint = sign * strtol(lexstr, NULL, base);

            // Check for `int` size (we can support max of 32 bits)
            if (lexint < INT_MIN || UINT_MAX < lexint) {
                jas_err("Integer larger than 32 bits.",
                        curr_line, lo_col, curr_col);
            }

            return TOK_NUM;
        }

        // Let by various punctuation:
        switch (curr_char) {
            case ',': eat(); return TOK_COMMA;
            case '.': eat(); return TOK_DOT;
            case '+': eat(); return TOK_PLUS;
            case '-': eat(); return TOK_MINUS;
            case '[': eat(); return TOK_LBRACKET;
            case ']': eat(); return TOK_RBRACKET;
        }

        jas_err("Unknown character encountered.", curr_line, lo_col, lo_col);
        eat(); // Advance to next char.
    }

    return TOK_EOF;
}
Example #10
0
l_R(int poststar)
#endif
{
	char s[FMAX+EXPMAXDIGS+4];
	register int ch;
	register char *sp, *spe, *sp1;
	long e, exp;
	int havenum, havestar, se;

	if (!poststar) {
		if (f__lcount > 0)
			return(0);
		f__lcount = 1;
		}
	f__ltype = 0;
	exp = 0;
	havestar = 0;
retry:
	sp1 = sp = s;
	spe = sp + FMAX;
	havenum = 0;

	switch(GETC(ch)) {
		case '-': *sp++ = ch; sp1++; spe++;
		case '+':
			GETC(ch);
		}
	while(ch == '0') {
		++havenum;
		GETC(ch);
		}
	while(isdigit(ch)) {
		if (sp < spe) *sp++ = ch;
		else ++exp;
		GETC(ch);
		}
	if (ch == '*' && !poststar) {
		if (sp == sp1 || exp || *s == '-') {
			errfl(f__elist->cierr,112,"bad repetition count");
			}
		poststar = havestar = 1;
		*sp = 0;
		f__lcount = atoi(s);
		goto retry;
		}
	if (ch == '.') {
		GETC(ch);
		if (sp == sp1)
			while(ch == '0') {
				++havenum;
				--exp;
				GETC(ch);
				}
		while(isdigit(ch)) {
			if (sp < spe)
				{ *sp++ = ch; --exp; }
			GETC(ch);
			}
		}
	havenum += sp - sp1;
	se = 0;
	if (issign(ch))
		goto signonly;
	if (havenum && isexp(ch)) {
		GETC(ch);
		if (issign(ch)) {
signonly:
			if (ch == '-') se = 1;
			GETC(ch);
			}
		if (!isdigit(ch)) {
bad:
			errfl(f__elist->cierr,112,"exponent field");
			}

		e = ch - '0';
		while(isdigit(GETC(ch))) {
			e = 10*e + ch - '0';
			if (e > EXPMAX)
				goto bad;
			}
		if (se)
			exp -= e;
		else
			exp += e;
		}
	(void) Ungetc(ch, f__cf);
	if (sp > sp1) {
		++havenum;
		while(*--sp == '0')
			++exp;
		if (exp)
			sprintf(sp+1, "e%ld", exp);
		else
			sp[1] = 0;
		f__lx = atof(s);
		}
	else
		f__lx = 0.;
	if (havenum)
		f__ltype = TYLONG;
	else
		switch(ch) {
			case ',':
			case '/':
				break;
			default:
				if (havestar && ( ch == ' '
						||ch == '\t'
						||ch == '\n'))
					break;
				if (nml_read > 1) {
					f__lquit = 2;
					return 0;
					}
				errfl(f__elist->cierr,112,"invalid number");
			}
	return 0;
	}