void hvm_restore_execution_point(hefesto_func_list_ctx **function, struct stacked_function_execution_point_ctx *execution_point) { hefesto_var_list_ctx *vp, *vp_to, *curr_vlist_from_p, *curr_vlist_to_p; hefesto_int_t state; for (state = 0; state < 2; state++) { switch (state) { case 0: curr_vlist_from_p = execution_point->args; curr_vlist_to_p = (*function)->args; break; case 1: curr_vlist_from_p = execution_point->vars; curr_vlist_to_p = (*function)->vars; break; } for (vp = curr_vlist_from_p; vp; vp = vp->next) { vp_to = get_hefesto_var_list_ctx_name(vp->name, curr_vlist_to_p); if (vp_to != NULL && vp_to->contents != NULL) { del_hefesto_common_list_ctx(vp_to->contents); vp_to->contents = NULL; } if (vp->contents != NULL) { vp_to->contents = cp_hefesto_common_list_ctx(vp->contents); } } } hvm_set_current_executed_function(execution_point->curr_code); hvm_set_last_executed_function(execution_point->last_code); }
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; }
static void *hvm_attrib(hefesto_command_list_ctx *cmd, hefesto_var_list_ctx **lo_vars, hefesto_var_list_ctx **gl_vars, hefesto_func_list_ctx *functions) { hefesto_var_list_ctx *var = get_hefesto_var_list_ctx_name(cmd->params->data, *lo_vars); void *expr_result = NULL; size_t out_sz; hefesto_type_t etype; if (var == NULL) var = get_hefesto_var_list_ctx_name(cmd->params->data, *gl_vars); if (var) { etype = var->type; expr_result = expr_eval(cmd->expr, lo_vars, gl_vars, functions, &etype, &out_sz); switch (var->type) { case HEFESTO_VAR_TYPE_STRING: case HEFESTO_VAR_TYPE_INT: //printf("OUTSZ: %d\n", out_sz); if (etype != HEFESTO_VAR_TYPE_NONE) { var = assign_data_to_hefesto_var(var, expr_result, out_sz); } //printf("OK!\n"); break; case HEFESTO_VAR_TYPE_FILE_DESCRIPTOR: var = assign_data_to_hefesto_var_file_type(var, expr_result); // don't close the (FILE *) it were pointed by the assignment // above. //close_hefesto_file_handle((hefesto_file_handle *)expr_result, 0); expr_result = NULL; break; case HEFESTO_VAR_TYPE_LIST: del_hefesto_common_list_ctx(var->contents); var->contents = cp_hefesto_common_list_ctx((etype == HEFESTO_VAR_TYPE_LIST) ? (hefesto_common_list_ctx *)expr_result : NULL); if (etype == HEFESTO_VAR_TYPE_LIST) { var->subtype = get_hsl_list_subtype((hefesto_common_list_ctx *)expr_result); /*if (get_hefesto_common_list_ctx_count((hefesto_common_list_ctx *)expr_result) > 0) { if (is_hefesto_numeric_constant(((hefesto_common_list_ctx *)expr_result)->data)) { var->subtype = HEFESTO_VAR_TYPE_INT; } else { var->subtype = HEFESTO_VAR_TYPE_STRING; } }*/ del_hefesto_common_list_ctx( (hefesto_common_list_ctx *)expr_result); expr_result = NULL; } break; } if (expr_result) free(expr_result); } return NULL; }
void hvm_init_function_args(const char *args, hefesto_var_list_ctx **lo_vars, hefesto_var_list_ctx **gl_vars, hefesto_func_list_ctx *functions, hefesto_func_list_ctx **function) { char *expr; char *expr_pfix; void *expr_result; hefesto_type_t etype; size_t sz; hefesto_var_list_ctx *vp = (*function)->vars, *ap = (*function)->args, *vlp = NULL; size_t offset = 0; //hefesto_instruction_code_t c_intr = hvm_get_current_executed_instruction(); char *temp = (char *) hefesto_mloc(HEFESTO_MAX_BUFFER_SIZE); strncpy(temp, args, HEFESTO_MAX_BUFFER_SIZE); strcat(temp, ";"); //printf("FUNCTION: %s [%s]\n", (*function)->name, temp); for (expr = temp; *expr != 0 && *expr != '('; expr++); offset = expr - args + 1; expr = get_arg_from_call(args, &offset); while (vp != NULL && *expr != 0) { if (ap->contents != NULL) { del_hefesto_common_list_ctx(ap->contents); ap->contents = NULL; } ap->contents = add_data_to_hefesto_common_list_ctx(ap->contents, expr, strlen(expr)); // if (c_intr != HEFESTO_ATTRIB && c_intr != HEFESTO_RET && // c_intr != HEFESTO_IF && c_intr != HEFESTO_WHILE) { // expr_pfix = infix2postfix(expr, strlen(expr), 1); // free(expr); // } else { expr_pfix = expr; // } etype = vp->type; //printf("\tF-ARG: %s\n", expr_pfix); // INFO(Santiago): // truque sujo, aqui gl_vars precisa ser varrida primeiro, lo_vars nem // precisa pois argumentos nao serao formados por variaveis ou // expressoes com variaveis definidas no corpo dessa mesma funcao // prestes a ser executada. //expr_result = expr_eval(expr_pfix, (*lo_vars == vp) ? gl_vars : lo_vars, gl_vars, functions, &etype, &sz); /*if (hvm_get_last_executed_function() != NULL) { printf("last -> %s\n", hvm_get_last_executed_function()->name); } printf("current -> %s\n", (*function)->name);*/ expr_result = expr_eval(expr_pfix, (hvm_get_last_executed_function() == *function) ? &(*function)->vars : lo_vars, gl_vars, functions, &etype, &sz); if (*expr_pfix == '$') { vlp = get_hefesto_var_list_ctx_name(expr_pfix + 1, *lo_vars); if (vlp == NULL) { vlp = get_hefesto_var_list_ctx_name(expr_pfix + 1, *gl_vars); } } free(expr_pfix); if (vp->contents != NULL) { del_hefesto_common_list_ctx(vp->contents); vp->contents = NULL; } if (etype != HEFESTO_VAR_TYPE_LIST) { vp->contents = add_data_to_hefesto_common_list_ctx(vp->contents, expr_result, (etype == HEFESTO_VAR_TYPE_STRING) ? strlen((char*)expr_result) : sizeof(hefesto_int_t)); free(expr_result); } else { if (vlp != NULL) { vp->subtype = vlp->subtype; } else { vp->subtype = get_hsl_list_subtype((hefesto_common_list_ctx *)expr_result); /*if (((hefesto_common_list_ctx *)expr_result)->data && is_hefesto_numeric_constant(((hefesto_common_list_ctx *) expr_result)->data)) { vp->subtype = HEFESTO_VAR_TYPE_INT; } else { vp->subtype = HEFESTO_VAR_TYPE_STRING; }*/ } vp->contents = cp_hefesto_common_list_ctx((hefesto_common_list_ctx *)expr_result); del_hefesto_common_list_ctx((hefesto_common_list_ctx *)expr_result); } vp = vp->next; ap = ap->next; expr = get_arg_from_call(args, &offset); } free(expr); // we must to clear local stack initializing all with null or zero. for (; vp != NULL; vp = vp->next) { if (vp->type == HEFESTO_VAR_TYPE_FILE_DESCRIPTOR && vp->contents) { close_hefesto_file_handle((hefesto_file_handle *) vp->contents->data); } del_hefesto_common_list_ctx(vp->contents); new_hefesto_common_list_ctx(vp->contents); if (vp->type == HEFESTO_VAR_TYPE_LIST) { vp->subtype = HEFESTO_VAR_TYPE_NONE; vp->contents->is_dummy_item = 1; } } }