Exemplo n.º 1
0
void tp_set(tp_vm *tp, tp_obj self, tp_obj k, tp_obj v) {
    objtype type = obj_type(self);
    assert(type <= TP_DATA);

    if (type == TP_DICT) {
        _tp_dict_set(tp, tp_dict_val(self), k, v);
        return;
    } else if (type == TP_LIST) {
        if (obj_type(k) == TP_NUMBER) {
            _tp_list_set(tp, tp_list_val(self), tp_number_val(k), v, "tp_set");
            return;
        } else if (obj_type(k) == TP_NONE) {
            _tp_list_append(tp, tp_list_val(self),v);
            return;
        } else if (obj_type(k) == TP_STRING) {
            if (strcmp("*", STR(k)) == 0) {
                tp_params_v(tp, 2, self, v); 
                tp_extend(tp);
                return;
            }
        }
    } else if (type == TP_DATA) {
        tp_data_meta(self)->set(tp, self, k, v);
        return;
    }
    tp_raise(,"tp_set(%s,%s,%s)",STR(self),STR(k),STR(v));
}
Exemplo n.º 2
0
/* Function: tp_set
 * Attribute modification.
 * 
 * This is the counterpart of tp_get, it does the same as self[k] = v would do
 * in actual tinypy code.
 */
void tp_set(TP,tp_obj self, tp_obj k, tp_obj v) {
    int type = self.type;

    if (type == TP_DICT) {
        TP_META_BEGIN(self,"__set__");
            tp_call(tp,meta,tp_params_v(tp,2,k,v));
            return;
        TP_META_END;
        _tp_dict_set(tp,self.dict.val,k,v);
        return;
    } else if (type == TP_LIST) {
        if (k.type == TP_NUMBER) {
            _tp_list_set(tp,self.list.val,k.number.val,v,"tp_set");
            return;
        } else if (k.type == TP_NONE) {
            _tp_list_append(tp,self.list.val,v);
            return;
        } else if (k.type == TP_STRING) {
            if (tp_cmp(tp,tp_string("*"),k) == 0) {
                tp_params_v(tp,2,self,v); tp_extend(tp);
                return;
            }
        }
    }
    tp_raise(,tp_string("(tp_set) TypeError: object does not support item assignment"));
}
Exemplo n.º 3
0
tp_obj tp_add(TP,tp_obj a, tp_obj b) {
    if (a.type == TP_NUMBER && a.type == b.type) {
        return tp_number(a.number.val+b.number.val);
    } else if (a.type == TP_STRING && a.type == b.type) {
        int al = a.string.len, bl = b.string.len;
        tp_obj r = tp_string_t(tp,al+bl);
        char *s = r.string.info->s;
        memcpy(s,a.string.val,al); memcpy(s+al,b.string.val,bl);
        return tp_track(tp,r);
    } else if (a.type == TP_LIST && a.type == b.type) {
        tp_obj r;
        tp_params_v(tp,1,a);
        r = tp_copy(tp);
        tp_params_v(tp,2,r,b);
        tp_extend(tp);
        return r;
    }
    tp_raise(tp_None,tp_string("(tp_add) TypeError: ?"));
}
Exemplo n.º 4
0
tp_obj tp_add(tp_vm *tp, tp_obj a, tp_obj b) {
    if (obj_type(a) == TP_NUMBER && obj_type(a) == obj_type(b)) {
        return tp_number(tp, tp_number_val(a) + tp_number_val(b));
    } else if (obj_type(a) == TP_STRING && obj_type(a) == obj_type(b)) {
        int al = tp_str_len(a), bl = tp_str_len(b);
        tp_obj r = tp_string_t(tp,al+bl);
        char *s = tp_str_val(r);
        memcpy(s, tp_str_val(a),al);
        memcpy(s+al, tp_str_val(b),bl);
        return tp_track(tp,r);
    } else if (obj_type(a) == TP_LIST && obj_type(a) == obj_type(b)) {
        tp_obj r;
        tp_params_v(tp,1,a); 
        r = tp_copy(tp);
        tp_params_v(tp,2,r,b); 
        tp_extend(tp);
        return r;
    }
    tp_raise(None,"tp_add(%s,%s)",STR(a),STR(b));
}
Exemplo n.º 5
0
void tp_set(TP,tp_obj self, tp_obj k, tp_obj v) {
    int type;

    type = self.type;
    if (type == TP_DICT) {
        _tp_dict_set(tp,self.dict.val,k,v);
        return;
    } else if (type == TP_LIST) {
        if (k.type == TP_NUMBER) {
            _tp_list_set(tp,self.list.val,k.number.val,v,"tp_set");
            return;
        } else if (k.type == TP_NONE) {
            _tp_list_append(tp,self.list.val,v);
            return;
        } else if (k.type == TP_STRING) {
            if (strcmp("*",TP_CSTR(k)) == 0) {
                tp_params_v(tp,2,self,v);
                tp_extend(tp);
                return;
            }
        }
    }
    tp_raise(,"tp_set(%s,%s,%s)",TP_CSTR(self),TP_CSTR(k),TP_CSTR(v));
}