Ejemplo n.º 1
0
static void *hvm_str_at(const char *method,
                        hefesto_common_list_ctx **string_var,
                        hefesto_type_t *otype, hefesto_var_list_ctx **lo_vars,
                        hefesto_var_list_ctx **gl_vars,
                        hefesto_func_list_ctx *functions) {

    size_t index, offset = 0, outsz;
    hefesto_type_t etype = HEFESTO_VAR_TYPE_INT;
    char *result;
    void *index_p;
    char *arg;
    const char *m;

    if (string_var == NULL || (*string_var) == NULL ||
        (*string_var)->data == NULL) return NULL;

    for (m = method; *m != '(' && *m != 0; m++);

    arg = get_next_call_args(m+1, &offset);

    index_p = expr_eval(arg, lo_vars, gl_vars, functions, &etype, &outsz);
    index = *(size_t *)index_p;
    free(index_p);
    free(arg);

    result =  (char *) hefesto_mloc(sizeof(char)*2);
    if (strlen((char *)(*string_var)->data) > 0) {
        *result = *((char *)(*string_var)->data +
                        (index % strlen((char *)(*string_var)->data)));
    } else {
        *result = 0;
    }
    *(result+1) = 0;
    *otype = HEFESTO_VAR_TYPE_STRING;

    return result;

}
Ejemplo n.º 2
0
char *infix2postfix(const char *expr, const size_t esize, const hefesto_int_t main_call) {

    const char *efinis = expr + esize, *e;
    char *t, *term, *tt;
    hefesto_common_stack_ctx *sp = NULL, *dp = NULL;
    hefesto_common_list_ctx *lp = NULL, *l;
    hefesto_int_t p, tp;
    size_t sz;
    hefesto_int_t bracket = 0;
    char *indexing;
    char *iexpr;
    char *args;
    size_t offset, count;
    hefesto_int_t state = 0;

    term = (char *) hefesto_mloc(HEFESTO_MAX_BUFFER_SIZE);
    memset(term, 0, HEFESTO_MAX_BUFFER_SIZE);

    t = term;
    *t = 0;

    for (e = expr; e != efinis; e++) {

        if (*e == 0) break;

        if (*e == '\n' || *e == '\r' || *e == '\t' || *e == ' ') continue;

        if (*e != 0 && *e != ' ' && *e != '\n' && *e != '\r' && *e != '\t' &&
            *e != '(' && *e != ')' && !is_op(*e) && *e != '[' &&
            !is_hefesto_string_tok(*e)) {
            *t = *e;
            t++;
            *t = 0;
            continue;
        } else if (is_hefesto_string_tok(*e)) {
            offset = 0;
            iexpr = get_next_string(e, &offset);
            e += offset;
            for (indexing = iexpr; *indexing != 0; indexing++, t++)
            *t = *indexing;
            free(iexpr);
            *t = 0;
        } else if (*e == '(' && !is_hefesto_numeric_constant(term) &&
               !is_hefesto_string(term) && *term && *term != '(' &&
               !is_op(*term)) {
            bracket++;
            offset = 0;
            args = get_next_call_args(e, &offset);

            if (*args) {
                iexpr = infix2postfix_function_args(args, strlen(args));
                tt = t;
                for (indexing = iexpr; *indexing != 0; indexing++, t++)
                    *t = *indexing;
                *t = 0;
                free(iexpr);
                count = 0;
                for (t = tt; *t != 0; t++) {
                    if (*t == '(') {
                        count++;
                    } else if (*t == ')') {
                        count--;
                    } else if (*t == '"') {
                        t++;
                        while (*t != '"' && *t != 0) {
                            t++;
                            if (*t == '\\') t += 2;
                        }
                    }
                }
                //  WARN(Santiago): null buffer position skipping avoidance.
                while (*t == 0 && t != term) {
                    t--;
                }
                t++;
                tt = ((char *)e + offset);
                while (is_hefesto_blank(*tt)) tt++;
                // can't add one more ')' because this is a function that is an
                // argument of another function
                if (*tt == ',') count--;
                while (count > 0) {
                    *t = ')';
                    t++;
                    count--;
                }
                *t = 0;
            }
            e += offset;
            free(args);
            if (*e == ')') e++;
        } else if ((*e == '-' || *e == '+') && t == term && isdigit(*(e+1)) &&
                   state == 0) {
            *t = *e;
            t++;
            continue;
        }

        if ((is_op(*e) || *e == '(' || *e == ')') && t == term) {
            *t = *e;
            t++;
            *t = 0;
            if (is_op(*(e+1)) && *e != '(' && *e != ')') { // composed operands
                *t = *(e+1);
                e++;
                t++;
                *t = 0;
            }
            state = 0;
        } else if ((e + 1) != efinis) e--;

        if (strcmp(term,"(") == 0) {
            sp = hefesto_common_stack_ctx_push(sp, (void *) term, t - term,
                                               HEFESTO_VAR_TYPE_UNTYPED);
        } else if(strcmp(term, ")") == 0) {
            t = (char *)hefesto_common_stack_ctx_data_on_top(sp);
            if (t && strcmp(t, "(") != 0) {
                while (sp && sp->data != NULL && strcmp(sp->data, "(") != 0) {
                    lp = add_data_to_hefesto_common_list_ctx(lp,
                                    hefesto_common_stack_ctx_data_on_top(sp),
                                    hefesto_common_stack_ctx_dsize_on_top(sp));
                    dp = hefesto_common_stack_ctx_on_top(sp);
                    sp = hefesto_common_stack_ctx_pop(sp);
                    del_hefesto_common_stack_ctx(dp);
                }
            }
            dp = hefesto_common_stack_ctx_on_top(sp);
            sp = hefesto_common_stack_ctx_pop(sp);
            del_hefesto_common_stack_ctx(dp);
        } else if ((p = get_op_precedence(term)) != -1) {

            tp = get_op_precedence(hefesto_common_stack_ctx_data_on_top(sp)); 

            while (tp != -1 && tp >= p) {
                lp = add_data_to_hefesto_common_list_ctx(lp,
                                    hefesto_common_stack_ctx_data_on_top(sp),
                                    hefesto_common_stack_ctx_dsize_on_top(sp));
                dp = hefesto_common_stack_ctx_on_top(sp);
                sp = hefesto_common_stack_ctx_pop(sp);
                del_hefesto_common_stack_ctx(dp);
                tp = get_op_precedence(hefesto_common_stack_ctx_data_on_top(sp)); 
            }

            sp = hefesto_common_stack_ctx_push(sp, (void *) term, t - term,
                                                   HEFESTO_VAR_TYPE_UNTYPED);
        } else {
            lp = add_data_to_hefesto_common_list_ctx(lp, term, t - term);
            state = 1;
        }
        t = term;
        *t = 0;

    }

    if (*term) {
        sp = hefesto_common_stack_ctx_push(sp, (void *) term, t - term,
                                           HEFESTO_VAR_TYPE_UNTYPED);
    }

    while (!hefesto_common_stack_ctx_empty(sp)) {
        lp = add_data_to_hefesto_common_list_ctx(lp,
                        hefesto_common_stack_ctx_data_on_top(sp),
                        hefesto_common_stack_ctx_dsize_on_top(sp));
        dp = hefesto_common_stack_ctx_on_top(sp);
        sp = hefesto_common_stack_ctx_pop(sp);
        del_hefesto_common_stack_ctx(dp);
    }

    free(term);

    for (p = 0, sz = 0, l = lp; l; l = l->next, p++) {
        sz += l->dsize;
    }

    sz += (size_t) p;

    term = (char *) hefesto_mloc(sz);
    *term = 0;

    for (l = lp; l; l = l->next) {
        if (*(char *)l->data == '(') continue;
        strncat(term, (char *) l->data, sz - 1);
        if (l->next && *(char *)l->next->data != '[')
        strncat(term, " ", sz - 1);
    }

    del_hefesto_common_list_ctx(lp);
    return term;

}