Example #1
0
void exec_neq(){
    Odescr *e1 = pop_tstack();
    Odescr *e2 = pop_tstack();
    push_bool(odescr_equal(e1,e2)!=0);
    free_odescr(e1);
    free_odescr(e2);
}
void T_pop_tstack(void)
{
    StackItemType item;
    
    if ( !_G_stack.top ||
         !_G_stack.bottom) {
        fprintf(stderr, "[x] Global stack is not ready. \n");

        return ;
    }

    __print_title("Pop Stack");
    while ( !is_empty_tstack(&_G_stack)) {
        if ( FALSE == pop_tstack(&_G_stack,
                                 &item)) {
            fprintf(stderr, "[x] Pop from stack fail. \n");
        } else {
            fprintf(stdout, "[*] Pop <%4d> from stack top success. \n", item);
        }

        if ( FALSE == traverse_tstack(&_G_stack,
                                      __print_stack)) {
            fprintf(stderr, "[x] Traverse stack fail. \n");
        }
        putchar('\n');        
    }
}
Example #3
0
void exec_ist(){
    Odescr *val = pop_tstack();
    Odescr *dest_addr = pop_tstack();
    Odescr *dest = dest_addr->original;
    
    if(dest->mode == EMB) {
        //printf("A\n");
        dest->inst = val->inst;
    } else{
        if(val->mode == EMB){
            //printf("B\n");
            char *instance = dest_addr->inst.sval;
            
            switch(val->size){
                case sizeof(int): { // works with float too
                    int *inst = (int *)instance;
                    inst[0] = val->inst.ival;
                    break;
                }
                case sizeof(char): {
                    char *inst = (char *)instance;
                    inst[0] = val->inst.cval;
                    break;
                }
                case sizeof(char *): {
                    char **inst = (char **)instance;
                    inst[0] = val->inst.sval;
                    break;
                }
                default: {
                    machine_error("Unrecognized size for IST.");
                    break;
                }
            }
        } else {
            //printf("C\n");
            memcpy(dest_addr->inst.sval, val->inst.sval, val->size);
        }
    }
    
    free_odescr(val);
    freemem((char *)dest_addr, sizeof(Odescr));
    // pop expression result
    // pop address
    // *adress = result
}
Example #4
0
void exec_gfread(int oid, char* format){
    Odescr *specifier = pop_tstack();
    FILE *in_file = fopen(specifier->inst.sval, "r");
    if(in_file == NULL){
        machine_error("Could not open input file.");
    }
    exec_gread(oid, format, 0, in_file);
    free_odescr(specifier);
    fclose(in_file);
}
Example #5
0
void exec_fwr(char* format){
    Odescr *specifier = pop_tstack();
    FILE *out = fopen(specifier->inst.sval, "w");
    if(out == NULL){
        machine_error("Could not open output file.");
    }
    exec_wr(format, 0, out);
    free_odescr(specifier);
    fclose(out);
}
Example #6
0
void exec_gsto(int oid){
    Odescr *val = pop_tstack();
    Odescr *dest = &astack[0]->objects[oid-1];
    if(dest->mode == EMB){
        dest->inst = val->inst;
    } else{
        memcpy(dest->inst.sval, val->inst.sval, dest->size);
    }
    free_odescr(val);
}
Example #7
0
void exec_sil(int field_size){
    Odescr *op = pop_tstack();
    Odescr *op2 = push_tstack();
    op2->size = field_size;
    op2->mode = STA;
    op2->inst.sval = newmem(op2->size);
    memcpy(op2->inst.sval, op->inst.sval, op2->size);
    // only dealloc the object and not the inst
    freemem((char*)op, sizeof(Odescr));
}
Example #8
0
void exec_ixa(int elem_size){
    Odescr *expr_val = pop_tstack();
    Odescr *op = top_tstack();
    op->inst.sval+=elem_size*expr_val->inst.ival;
    op->size=elem_size;
    free_odescr(expr_val);
    // pop number of elements so "skip"
    // pop address
    // sum
    // push on stack
}
Example #9
0
void exec_cat(int num_fields, int size){
    int size_done = 0;
    int fields_done = 0;
    char *instance = newmem(size);
    while(fields_done < num_fields){
        Odescr *op = pop_tstack();
        int dest_start = size - size_done - op->size;
        // if there is an error reverse it, starting from 0 -- using size_done as index
        
        if(op->mode == EMB){
            switch(op->size){
                case sizeof(char): instance[dest_start] = op->inst.cval; break;
                case sizeof(int): *((int *)(instance+dest_start)) = op->inst.ival; break;
                case sizeof(char *):{
                    char *actual_address = instance+dest_start;
                    
                    char** act = (char **)actual_address;
                    act[0] = op->inst.sval;

                    //printf("%s %s\n",op->inst.sval, act[0]);
                    //printf("%p %s\n%p %s\n\n", act[0],act[0], op->inst.sval,op->inst.sval);
                    break;
                }
                default: machine_error("Unknown type for op->size"); break;
            }
        } else{
            memcpy(instance + dest_start, op->inst.sval, op->size);
        }
        size_done += op->size;
        fields_done++;
        free_odescr(op);
    }
    Odescr *op2 = push_tstack();
    op2->size = size;
    op2->mode = STA;
    op2->inst.sval = instance;
    // this allocates space on the instance stack and than copies byte by byte all the elements on the stack
    // taking them to the tstack (?)
    /*
        TSTACK:
        expr1_res
        expr2_res
        ...
    
        INSTANCE -> expr1_res, expr2_res, ...
    
        creates a single record on the tstack with SIZE, STA and with the pointer to the allocated instance
        
    */
}
Example #10
0
void exec_eil(int field_size){
    Odescr *address = pop_tstack();
    switch(field_size){
        case sizeof(int):{
            push_int(*((int *)address->inst.sval)); break;
        }
        case sizeof(char):{
            push_char(*((char *)address->inst.sval)); break;
        }
        case sizeof(char *):{
            char** act = (char **)address->inst.sval;
            push_string(act[0]); break;
        }
        default:{
            machine_error("EIL field_size error."); break;
        }
    }
    freemem((char*)address, sizeof(Odescr));
    // only dealloc the object and not the inst
}
Example #11
0
void exec_tpop(int num_objects){
    while(num_objects--) {
        Odescr *op = pop_tstack();
        free_odescr(op);
    }
}
Example #12
0
void exec_fwrite(char* format){
    exec_fwr(format);
    Odescr *op = pop_tstack();
    free_odescr(op);
}