Example #1
0
File: eval.c Project: crab2313/m1
static void
eval_expr(m1_expression *e) {
    if (e == NULL) 
        return;
        
    switch (e->type) {
        case EXPR_NUMBER:
            eval_number(e->expr.l->value.fval);
            break;
        case EXPR_INT:
            eval_int(e->expr.l->value.ival);
            break;
        case EXPR_BINARY:
            eval_binary(e->expr.b);
            break;
        case EXPR_UNARY:
            eval_unary(e->expr.u);
            break;
        case EXPR_FUNCALL:
            eval_funcall(e->expr.f);
            break;
        case EXPR_ASSIGN:
            eval_assign(e->expr.a);
            break;
        case EXPR_IF:   
            eval_if(e->expr.i);
            break;
        case EXPR_WHILE:
            eval_while(e->expr.w);
            break;
        case EXPR_DOWHILE:
            eval_dowhile(e->expr.w);
            break;
        case EXPR_FOR:
            eval_for(e->expr.o);
            break;
        case EXPR_RETURN:
            eval_return(e->expr.e);
            break;
        case EXPR_NULL:
            eval_null();
            break;
        case EXPR_DEREF:
            eval_deref(e->expr.t);
            break;
        case EXPR_ADDRESS:
            eval_address(e->expr.t);
            break;
        case EXPR_OBJECT:
            eval_obj(e->expr.t);
            break;
        case EXPR_BREAK:
            eval_break();
            break;            
        case EXPR_CONTINUE:
            eval_continue();
            break;
        case EXPR_CONSTDECL:
        case EXPR_VARDECL:
            break;
        default:
            fprintf(stderr, "unknown expr type");   
            exit(EXIT_FAILURE);
    }   

}
bool eval(string express) {
    filter_useless_char(express);
    trim(express);
    if (check_string("{",express.c_str()) && INVALID_VALUE!=express.find('}')) {  //  inside code block ..
        for (unsigned long code_block_end_index=get_matching_outside_right_brace(express.substr(1),0),
                           code_block_start_index=express.find('{');
                           INVALID_VALUE!=code_block_start_index && INVALID_VALUE!=code_block_end_index;
                           code_block_end_index=get_matching_outside_right_brace(express.substr(1),0),
                           code_block_start_index=express.find('{')) {
            string code_block(express.substr(code_block_start_index+1,code_block_end_index-code_block_start_index));
            while (INVALID_VALUE!=code_block.find(';')) {
                if (!eval(code_block.substr(0,code_block.find(';'))))
                    return false;
                code_block=code_block.substr(code_block.find(';')+1);
            }
            if (!code_block_start_index)
                break;
            express=express.substr(0,code_block_start_index-1)+express.substr(code_block_end_index+1);
        }
        return true;
    }
    bool base_javascript_syntax_execute_result=true;
    if (check_string("for",express.c_str())) {  //  base JavaScript syntax ..
        base_javascript_syntax_execute_result=eval_for(express);
    } else if (check_string("if",express.c_str())) {
        base_javascript_syntax_execute_result=eval_if(express);
    }
    if (check_string("function",express.c_str())) {
        base_javascript_syntax_execute_result=eval_decleare_function(express);
        if (base_javascript_syntax_execute_result)
            return eval(express);
    } else if (check_string("return",express.c_str())) {
        return eval_function_return(express);
    }

    string next_express;
    if (INVALID_VALUE!=express.find(';')) {  //  put data ..
        next_express=express.substr(express.find(';')+1);
        express=express.substr(0,express.find(';'));
    }
    if (eval_function(express)) {
    } else if (INVALID_VALUE!=express.find('=')) {  //  var asd=123 or var4='asd'
        char calcu_flag=express[express.find('=')-1];
        if ('+'==calcu_flag) {
            if (INVALID_VALUE!=express.find("+=")) {  //  asd+=123 -> asd=asd+123
                string variant_name(express.substr(0,express.find("+=")));
                trim(variant_name);
                if (!eval(variant_name+"="+variant_name+"+"+express.substr(express.find("+=")+2)))
                    return false;
            }
        } else if ('-'==calcu_flag) {
            if (INVALID_VALUE!=express.find("-=")) {  //  asd+=123 -> asd=asd+123
                string variant_name(express.substr(0,express.find("-=")));
                trim(variant_name);
                if (!eval(variant_name+"="+variant_name+"-"+express.substr(express.find("-=")+2)))
                    return false;
            }
        } else if ('*'==calcu_flag) {
            if (INVALID_VALUE!=express.find("*=")) {  //  asd+=123 -> asd=asd+123
                string variant_name(express.substr(0,express.find("*=")));
                trim(variant_name);
                if (!eval(variant_name+"="+variant_name+"*"+express.substr(express.find("*=")+2)))
                    return false;
            }
        } else if ('/'==calcu_flag) {
            if (INVALID_VALUE!=express.find("/=")) {  //  asd+=123 -> asd=asd+123
                string variant_name(express.substr(0,express.find("/=")));
                trim(variant_name);
                if (!eval(variant_name+"="+variant_name+"/"+express.substr(express.find("/=")+2)))
                    return false;
            }
        } else {
            string variant_name;  //  asd
            string calcu_express;
            if (check_string("var",express.c_str())) {
                if (INVALID_VALUE!=express.find('=')) {
                    variant_name=express.substr(3,express.find('=')-3);
                    calcu_express=express.substr(express.find('=')+1);
                } else {
                    variant_name=express.substr(3,express.find(';')-3);
                }
            } else {
                if (INVALID_VALUE!=express.find('=')) {
                    variant_name=express.substr(0,express.find('='));
                    calcu_express=express.substr(express.find('=')+1);
                } else {
                    variant_name=express.substr(3,express.find(';')-3);
                }
            }
            trim(variant_name);

            if (!calcu_express.empty())
                if (!express_calcu(calcu_express))
                    return false;
            if (EXPRESSION_ARRAY==get_express_type(variant_name)) {
                unsigned long calcu_result=0;
                support_javascript_variant_type calcu_result_type=NONE;
                get_variant(JAVASCRIPT_VARIANT_KEYNAME_CALCULATION_RESULT,(void*)&calcu_result,&calcu_result_type);
                string array_index_express(variant_name.substr(variant_name.find('[')+1,variant_name.find(']')-variant_name.find('[')-1));
                if (!express_calcu(array_index_express))
                    return false;
                unsigned long array_index=0;
                support_javascript_variant_type array_index_type=NONE;
                get_variant(JAVASCRIPT_VARIANT_KEYNAME_CALCULATION_RESULT,(void*)&array_index,&array_index_type);
                variant_name=variant_name.substr(0,variant_name.find('['));
                trim(variant_name);
                if (INT_ARRAY==get_variant_type(variant_name) && NUMBER!=calcu_result_type)
                    return false;
                set_variant_array(variant_name,array_index,(void*)calcu_result,calcu_result_type);
            } else {
                copy_variant(variant_name,JAVASCRIPT_VARIANT_KEYNAME_CALCULATION_RESULT);
            }
        }
    }
    if (!next_express.empty())
        return eval(next_express);
    return base_javascript_syntax_execute_result;
}