Beispiel #1
0
int tp_cmp(TP,tp_obj a, tp_obj b) {
    if (a.type != b.type) { return a.type-b.type; }
    switch(a.type) {
        case TP_NONE: return 0;
        case TP_NUMBER: return _tp_sign(a.number.val-b.number.val);
        case TP_STRING: {
            int l = _tp_min(a.string.len,b.string.len);
            int v = memcmp(a.string.val,b.string.val,l);
            if (v == 0) {
                v = a.string.len-b.string.len;
            }
            return v;
        }
        case TP_LIST: {
            int n,v; for(n=0;n<_tp_min(a.list.val->len,b.list.val->len);n++) {
        tp_obj aa = a.list.val->items[n]; tp_obj bb = b.list.val->items[n];
            if (aa.type == TP_LIST && bb.type == TP_LIST) { v = aa.list.val-bb.list.val; } else { v = tp_cmp(tp,aa,bb); }
            if (v) { return v; } }
            return a.list.val->len-b.list.val->len;
        }
        case TP_DICT: return a.dict.val - b.dict.val;
        case TP_FNC: return a.fnc.info - b.fnc.info;
        case TP_DATA: return (char*)a.data.val - (char*)b.data.val;
    }
    tp_raise(0,tp_string("(tp_cmp) TypeError: ?"));
}
Beispiel #2
0
/*
 * Create a new string which is a substring slice of another STRING.
 * Does not need to be put into GC tracking, as its parent is
 * already being tracked (supposedly).
 */
tp_obj tp_string_sub(TP, tp_obj s, int a, int b) {
    int l = s.string.len;
    a = _tp_max(0,(a<0?l+a:a)); b = _tp_min(l,(b<0?l+b:b));
    tp_obj r = s;
    r.string.val += a;
    r.string.len = b-a;
    return r;
}
Beispiel #3
0
int tp_cmp(tp_vm *tp, tp_obj a, tp_obj b) {
    if (obj_type(a) != obj_type(b)) {
        return obj_type(a) - obj_type(b);
    }

    switch (obj_type(a)) {
        case TP_NONE: return 0;
        case TP_NUMBER: return _tp_sign(tp_number_val(a) - tp_number_val(b));
        case TP_STRING: {
            int minlen = _tp_min(tp_str_len(a), tp_str_len(b));
            int v = memcmp(tp_str_val(a), tp_str_val(b), minlen);
            if (v == 0) { 
                v = tp_str_len(a) - tp_str_len(b);
            }
            return v;
        }
        case TP_LIST: {
            int n,v;
            int minlen = _tp_min(tp_list_val(a)->len,tp_list_val(b)->len);
            for (n=0; n < minlen; n++) {
                tp_obj aa = tp_list_val(a)->items[n];
                tp_obj bb = tp_list_val(b)->items[n];
                if (obj_type(aa) == TP_LIST && obj_type(bb) == TP_LIST) {
                    v = tp_list_val(aa) - tp_list_val(bb);
                } else { 
                    v = tp_cmp(tp,aa,bb); 
                }
                if (v) { 
                    return v; 
                } 
            }
            return tp_list_val(a)->len - tp_list_val(b)->len;
        }
        case TP_DICT: return tp_dict_val(a) - tp_dict_val(b);
        case TP_FNC: return tp_fnc_val(a) - tp_fnc_val(b);
        case TP_DATA: return (char*)tp_data_val(a) - (char*)tp_data_val(b);
    }
    tp_raise(0,"tp_cmp(%s,%s)",STR(a),STR(b));
}
Beispiel #4
0
tp_obj tp_strip(TP) {
    tp_obj o = TP_TYPE(TP_STRING);
    char const *v = o.string.val; int l = o.string.len;
    int i; int a = l, b = 0;
    tp_obj r;
    char *s;
    for (i=0; i<l; i++) {
        if (v[i] != ' ' && v[i] != '\n' && v[i] != '\t' && v[i] != '\r') {
            a = _tp_min(a,i); b = _tp_max(b,i+1);
        }
    }
    if ((b-a) < 0) { return tp_string(""); }
    r = tp_string_t(tp,b-a);
    s = r.string.info->s;
    memcpy(s,v+a,b-a);
    return tp_track(tp,r);
}
Beispiel #5
0
tp_obj tp_get(TP,tp_obj self, tp_obj k) {
    int type = self.type;
    tp_obj r;

    if (type == TP_DICT) {
        return _tp_dict_get(tp,self.dict.val,k,"tp_get");
    } else if (type == TP_LIST) {
        if (k.type == TP_NUMBER) {
            int l = tp_len(tp,self).number.val;
            int n = k.number.val;
            n = (n<0?l+n:n);
            return _tp_list_get(tp,self.list.val,n,"tp_get");
        } else if (k.type == TP_STRING) {
            if (strcmp("append",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_append);
            } else if (strcmp("pop",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_pop);
            } else if (strcmp("index",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_index);
            } else if (strcmp("sort",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_sort);
            } else if (strcmp("extend",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_extend);
            } else if (strcmp("*",TP_CSTR(k)) == 0) {
                tp_params_v(tp,1,self);
                r = tp_copy(tp);
                self.list.val->len=0;
                return r;
            }
        } else if (k.type == TP_NONE) {
            return _tp_list_pop(tp,self.list.val,0,"tp_get");
        }
    } else if (type == TP_STRING) {
        if (k.type == TP_NUMBER) {
            int l = self.string.len;
            int n = k.number.val;
            n = (n<0?l+n:n);
            if (n >= 0 && n < l) {
                return tp_string_n(tp->chars[(unsigned char)self.string.val[n]],1);
            }
        } else if (k.type == TP_STRING) {
            if (strcmp("join",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_join);
            } else if (strcmp("split",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_split);
            } else if (strcmp("index",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_str_index);
            } else if (strcmp("strip",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_strip);
            } else if (strcmp("replace",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_replace);
            }
        }
    }

    if (k.type == TP_LIST) {
        int a,b,l;
        tp_obj tmp;
        l = tp_len(tp,self).number.val;
        tmp = tp_get(tp,k,tp_number(0));
        if (tmp.type == TP_NUMBER) {
            a = tmp.number.val;
        }
        else if(tmp.type == TP_NONE) {
            a = 0;
        }
        else {
            tp_raise(tp_None,"%s is not a number",TP_CSTR(tmp));
        }
        tmp = tp_get(tp,k,tp_number(1));
        if (tmp.type == TP_NUMBER) {
            b = tmp.number.val;
        }
        else if(tmp.type == TP_NONE) {
            b = l;
        }
        else {
            tp_raise(tp_None,"%s is not a number",TP_CSTR(tmp));
        }
        a = _tp_max(0,(a<0?l+a:a));
        b = _tp_min(l,(b<0?l+b:b));
        if (type == TP_LIST) {
            return tp_list_n(tp,b-a,&self.list.val->items[a]);
        } else if (type == TP_STRING) {
            tp_obj r = tp_string_t(tp,b-a);
            char *ptr = r.string.info->s;
            memcpy(ptr,self.string.val+a,b-a);
            ptr[b-a]=0;
            return tp_track(tp,r);
        }
    }

    tp_raise(tp_None,"tp_get(%s,%s)",TP_CSTR(self),TP_CSTR(k));
}
Beispiel #6
0
tp_obj tp_get(tp_vm *tp, tp_obj self, tp_obj k) {
    int type = obj_type(self);
    tp_obj r;
    if (type == TP_DICT) {
        return _tp_dict_get(tp, tp_dict_val(self), k, "tp_get");
    } else if (type == TP_LIST) {
        if (obj_type(k) == TP_NUMBER) {
            int l = tp_number_val(tp_len(tp,self));
            int n = tp_number_val(k);
            n = (n<0?l+n:n);
            return _tp_list_get(tp,tp_list_val(self),n,"tp_get");
        } else if (obj_type(k) == TP_STRING) {
            if (strcmp("append",STR(k)) == 0) {
                return tp_method(tp,self,tp_append);
            } else if (strcmp("pop",STR(k)) == 0) {
                return tp_method(tp,self,tp_pop);
            } else if (strcmp("index",STR(k)) == 0) {
                return tp_method(tp,self,tp_index);
            } else if (strcmp("sort",STR(k)) == 0) {
                return tp_method(tp,self,tp_sort);
            } else if (strcmp("extend",STR(k)) == 0) {
                return tp_method(tp,self,tp_extend);
            } else if (strcmp("*",STR(k)) == 0) {
                tp_params_v(tp,1,self); 
                r = tp_copy(tp);
                tp_list_val(self)->len=0;
                return r;
            }
        } else if (obj_type(k) == TP_NONE) {
            return _tp_list_pop(tp,tp_list_val(self),0,"tp_get");
        }
    } else if (type == TP_STRING) {
        if (obj_type(k) == TP_NUMBER) {
            int l = tp_str_len(self);
            int n = tp_number_val(k);
            n = (n<0?l+n:n);
            if (n >= 0 && n < l) { 
                return tp_string_n(tp, tp->chars[(unsigned char)tp_str_val(self)[n]],1);
            }
        } else if (obj_type(k) == TP_STRING) {
            if (strcmp("join",STR(k)) == 0) {
                return tp_method(tp,self,tp_join);
            } else if (strcmp("split",STR(k)) == 0) {
                return tp_method(tp,self,tp_split);
            } else if (strcmp("index",STR(k)) == 0) {
                return tp_method(tp,self,tp_str_index);
            } else if (strcmp("strip",STR(k)) == 0) {
                return tp_method(tp,self,tp_strip);
            } else if (strcmp("replace",STR(k)) == 0) {
                return tp_method(tp,self,tp_replace);
            }
        }
    } else if (type == TP_DATA) {
        return tp_data_meta(self)->get(tp,self,k);
    }

    if (obj_type(k) == TP_LIST) {
        int a,b,l;
        tp_obj tmp;
        l = tp_number_val(tp_len(tp,self));
        tmp = tp_get(tp, k, tp_number(tp, 0));
        if (obj_type(tmp) == TP_NUMBER) { 
            a = tp_number_val(tmp); 
        } else if (obj_type(tmp) == TP_NONE) { 
            a = 0; 
        } else { 
            tp_raise(None,"%s is not a number",STR(tmp)); 
        }
        tmp = tp_get(tp, k, tp_number(tp, 1));
        if (obj_type(tmp) == TP_NUMBER) {
            b = tp_number_val(tmp);
        } else if (obj_type(tmp) == TP_NONE) {
            b = l;
        } else {
            tp_raise(None,"%s is not a number",STR(tmp));
        }
        a = _tp_max(0,(a<0?l+a:a));
        b = _tp_min(l,(b<0?l+b:b));
        if (type == TP_LIST) {
            return tp_list_n(tp,b-a, &tp_list_val(self)->items[a]);
        } else if (type == TP_STRING) {
            tp_obj r = tp_string_t(tp, b-a);
            char *ptr = tp_str_val(r);
            memcpy(ptr,tp_str_val(self)+a,b-a);
            ptr[b-a]=0;
            return tp_track(tp,r);
        }
    }

    tp_raise(None,"tp_get(%s,%s)",STR(self),STR(k));
}
Beispiel #7
0
/* Function: tp_get
 * Attribute lookup.
 * 
 * This returns the result of using self[k] in actual code. It works for
 * dictionaries (including classes and instantiated objects), lists and strings.
 *
 * As a special case, if self is a list, self[None] will return the first
 * element in the list and subsequently remove it from the list.
 */
tp_obj tp_get(TP,tp_obj self, tp_obj k) {
    int type = self.type;
    tp_obj r;
    if (type == TP_DICT) {
        TP_META_BEGIN(self,"__get__");
            return tp_call(tp,meta,tp_params_v(tp,1,k));
        TP_META_END;
        if (self.dict.dtype && _tp_lookup(tp,self,k,&r)) { return r; }
        return _tp_dict_get(tp,self.dict.val,k,"tp_get");
    } else if (type == TP_LIST) {
        if (k.type == TP_NUMBER) {
            int l = tp_len(tp,self).number.val;
            int n = k.number.val;
            n = (n<0?l+n:n);
            return _tp_list_get(tp,self.list.val,n,"tp_get");
        } else if (k.type == TP_STRING) {
            if (tp_cmp(tp,tp_string("append"),k) == 0) {
                return tp_method(tp,self,tp_append);
            } else if (tp_cmp(tp,tp_string("pop"),k) == 0) {
                return tp_method(tp,self,tp_pop);
            } else if (tp_cmp(tp,tp_string("index"),k) == 0) {
                return tp_method(tp,self,tp_index);
            } else if (tp_cmp(tp,tp_string("sort"),k) == 0) {
                return tp_method(tp,self,tp_sort);
            } else if (tp_cmp(tp,tp_string("extend"),k) == 0) {
                return tp_method(tp,self,tp_extend);
            } else if (tp_cmp(tp,tp_string("*"),k) == 0) {
                tp_params_v(tp,1,self);
                r = tp_copy(tp);
                self.list.val->len=0;
                return r;
            }
        } else if (k.type == TP_NONE) {
            return _tp_list_pop(tp,self.list.val,0,"tp_get");
        }
    } else if (type == TP_STRING) {
        if (k.type == TP_NUMBER) {
            int l = self.string.len;
            int n = k.number.val;
            n = (n<0?l+n:n);
            if (n >= 0 && n < l) { return tp_string_n(tp->chars[(unsigned char)self.string.val[n]],1); }
        } else if (k.type == TP_STRING) {
            if (tp_cmp(tp,tp_string("join"),k) == 0) {
                return tp_method(tp,self,tp_join);
            } else if (tp_cmp(tp,tp_string("split"),k) == 0) {
                return tp_method(tp,self,tp_split);
            } else if (tp_cmp(tp,tp_string("index"),k) == 0) {
                return tp_method(tp,self,tp_str_index);
            } else if (tp_cmp(tp,tp_string("strip"),k) == 0) {
                return tp_method(tp,self,tp_strip);
            } else if (tp_cmp(tp,tp_string("replace"),k) == 0) {
                return tp_method(tp,self,tp_replace);
            }
        }
    }

    if (k.type == TP_LIST) {
        int a,b,l;
        tp_obj tmp;
        l = tp_len(tp,self).number.val;
        tmp = tp_get(tp,k,tp_number(0));
        if (tmp.type == TP_NUMBER) { a = tmp.number.val; }
        else if(tmp.type == TP_NONE) { a = 0; }
        else { tp_raise(tp_None,tp_string("(tp_get) TypeError: indices must be numbers")); }
        tmp = tp_get(tp,k,tp_number(1));
        if (tmp.type == TP_NUMBER) { b = tmp.number.val; }
        else if(tmp.type == TP_NONE) { b = l; }
        else { tp_raise(tp_None,tp_string("(tp_get) TypeError: indices must be numbers")); }
        a = _tp_max(0,(a<0?l+a:a)); b = _tp_min(l,(b<0?l+b:b));
        if (type == TP_LIST) {
            return tp_list_n(tp,b-a,&self.list.val->items[a]);
        } else if (type == TP_STRING) {
/*            tp_obj r = tp_string_t(tp,b-a);
            char *ptr = r.string.info->s;
            memcpy(ptr,self.string.val+a,b-a); ptr[b-a]=0;
            return tp_track(tp,r);*/
            return tp_string_slice(tp,self,a,b);
        }
    }

    tp_raise(tp_None,tp_string("(tp_get) TypeError: ?"));
}