示例#1
0
文件: functions.c 项目: af-inet/RCS
struct Token *function_execute(struct Token *statement, struct List *scope){

	int header = statement->list->tokens[0]->type;

	if( token_is_operator(header) ) return function_operator(statement,scope);
	if(header == T_PRINT)           return function_print(statement,scope);
	if(header == T_IF)              return function_if(statement,scope);
	if(header == T_EXPRESSION)      return function_expression(statement,scope);
	if(header == T_STRING)          return statement->list->tokens[1];
	if(header == T_EQUALS)          return function_assignment(statement,scope);
	if(header == T_LOOKUP)          return function_lookup(statement,scope);
	if(header == T_NOT)             return function_not(statement,scope);

	printf("(ERROR:function_execute) Statement was not a valid function.\n");
	return token_create(T_NUMBER,0);
}
示例#2
0
static int expr_lex(const char **str, token_t *tok)
{
    int n=0;
    char c = **str;
    const char *s;
    int integer_found = 0;

    if (c==0) {
        tok->type = TOK_END;
        return 0;
    }

  again:

    if (isdigit(c)) {
        s = *str;
        do {
            c = (*(++*str));
        } while (c && isdigit(c));
        n = atoi(s);
        integer_found = 1;
        if (c!='.' && c!='e') {
            tok->i = n;
            tok->type = TOK_INT;
            return 0;
        }
    }

    switch (c) {
    case '.':
        c = (*(++*str));
        if (!isdigit(c) && c!='e' && integer_found) {
            tok->type = TOK_FLOAT;
            tok->f = (float)n;
            return 0;
        }
        if (!isdigit(c) && c!='e')
            break;
        do {
            c = (*(++*str));
        } while (c && isdigit(c));
        if (c!='e') {
            tok->f = atof(s);
            tok->type = TOK_FLOAT;
            return 0;
        }
    case 'e':
        if (!integer_found) {
            printf("unexpected `e' outside float\n");
            break;
        }
        c = (*(++*str));
        if (c!='-' && !isdigit(c)) {
            printf("Incomplete scientific notation `%s'.\n",s);
            break;
        }
        if (c=='-')
            c = (*(++*str));
        while (c && isdigit(c))
            c = (*(++*str));
        tok->type = TOK_FLOAT;
        tok->f = atof(s);
        return 0;
    case '+':
    case '-':
    case '/':
    case '*':
    case '%':
    case '=':
        tok->type = TOK_OP;
        tok->op = c;
        ++*str;
        return 0;
    case '(':
        tok->type = TOK_OPEN_PAREN;
        ++*str;
        return 0;
    case ')':
        tok->type = TOK_CLOSE_PAREN;
        ++*str;
        return 0;
    case 'x':
    case 'y':
        tok->type = TOK_VAR;
        tok->var = c;
        ++*str;
        return 0;
    case '[':
        tok->type = TOK_OPEN_SQUARE;
        ++*str;
        return 0;
    case ']':
        tok->type = TOK_CLOSE_SQUARE;
        ++*str;
        return 0;
    case '{':
        tok->type = TOK_OPEN_CURLY;
        ++*str;
        return 0;
    case '}':
        tok->type = TOK_CLOSE_CURLY;
        ++*str;
        return 0;
    case ' ':
    case '\t':
    case '\r':
    case '\n':
        c = (*(++*str));
        goto again;
    case ',':
        tok->type = TOK_COMMA;
        ++*str;
        return 0;
    default:
        if (!isalpha(c)) {
            printf("unknown character '%c' in lexer\n", c);
            break;
        }
        s = *str;
        while (c && (isalpha(c) || isdigit(c)))
            c = (*(++*str));
        tok->type = TOK_FUNC;
        tok->func = function_lookup(s, *str-s);
        return 0;
    }

    return 1;
}