Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1913 jaeger 1
tp_obj tp_str(TP,tp_obj self) {
2
    int type = self.type;
3
    if (type == TP_STRING) {
4
      return self;
5
    }
6
    if (type == TP_NUMBER) {
7
        tp_num v = self.number.val;
8
        if ((fabs(v)-fabs((long)v)) < 0.000001) { return tp_printf(tp,"%ld",(long)v); }
9
        return tp_printf(tp,"%f",v);
10
    } else if(type == TP_DICT) {
11
        return tp_printf(tp,"",self.dict.val);
12
    } else if(type == TP_LIST) {
13
        return tp_printf(tp,"",self.list.val);
14
    } else if (type == TP_NONE) {
15
        return tp_string("None");
16
    } else if (type == TP_DATA) {
17
        return tp_printf(tp,"",self.data.val);
18
    } else if (type == TP_FNC) {
19
        return tp_printf(tp,"",self.fnc.info);
20
    }
21
    return tp_string("");
22
}
23
 
24
int tp_bool(TP,tp_obj v) {
25
    switch(v.type) {
26
        case TP_NUMBER: return v.number.val != 0;
27
        case TP_NONE: return 0;
28
        case TP_STRING: return v.string.len != 0;
29
        case TP_LIST: return v.list.val->len != 0;
30
        case TP_DICT: return v.dict.val->len != 0;
31
    }
32
    return 1;
33
}
34
 
35
 
36
tp_obj tp_has(TP,tp_obj self, tp_obj k) {
37
    int type = self.type;
38
    if (type == TP_DICT) {
39
        if (_tp_dict_find(tp,self.dict.val,k) != -1) { return tp_True; }
40
        return tp_False;
41
    } else if (type == TP_STRING && k.type == TP_STRING) {
42
        char *p = strstr(TP_CSTR(self),TP_CSTR(k));
43
        return tp_number(p != 0);
44
    } else if (type == TP_LIST) {
45
        return tp_number(_tp_list_find(tp,self.list.val,k)!=-1);
46
    }
47
    tp_raise(tp_None,"tp_has(%s,%s)",TP_CSTR(self),TP_CSTR(k));
48
}
49
 
50
void tp_del(TP,tp_obj self, tp_obj k) {
51
    int type = self.type;
52
    if (type == TP_DICT) {
53
        _tp_dict_del(tp,self.dict.val,k,"tp_del");
54
        return;
55
    }
56
    tp_raise(,"tp_del(%s,%s)",TP_CSTR(self),TP_CSTR(k));
57
}
58
 
59
 
60
tp_obj tp_iter(TP,tp_obj self, tp_obj k) {
61
    int type = self.type;
62
    if (type == TP_LIST || type == TP_STRING) { return tp_get(tp,self,k); }
63
    if (type == TP_DICT && k.type == TP_NUMBER) {
64
        return self.dict.val->items[_tp_dict_next(tp,self.dict.val)].key;
65
    }
66
    tp_raise(tp_None,"tp_iter(%s,%s)",TP_CSTR(self),TP_CSTR(k));
67
}
68
 
69
tp_obj tp_get(TP,tp_obj self, tp_obj k) {
70
    int type = self.type;
71
    tp_obj r;
2001 jaeger 72
 
1913 jaeger 73
    if (type == TP_DICT) {
74
        return _tp_dict_get(tp,self.dict.val,k,"tp_get");
75
    } else if (type == TP_LIST) {
76
        if (k.type == TP_NUMBER) {
77
            int l = tp_len(tp,self).number.val;
78
            int n = k.number.val;
79
            n = (n<0?l+n:n);
80
            return _tp_list_get(tp,self.list.val,n,"tp_get");
81
        } else if (k.type == TP_STRING) {
82
            if (strcmp("append",TP_CSTR(k)) == 0) {
83
                return tp_method(tp,self,tp_append);
84
            } else if (strcmp("pop",TP_CSTR(k)) == 0) {
85
                return tp_method(tp,self,tp_pop);
86
            } else if (strcmp("index",TP_CSTR(k)) == 0) {
87
                return tp_method(tp,self,tp_index);
88
            } else if (strcmp("sort",TP_CSTR(k)) == 0) {
89
                return tp_method(tp,self,tp_sort);
90
            } else if (strcmp("extend",TP_CSTR(k)) == 0) {
91
                return tp_method(tp,self,tp_extend);
92
            } else if (strcmp("*",TP_CSTR(k)) == 0) {
93
                tp_params_v(tp,1,self);
94
                r = tp_copy(tp);
95
                self.list.val->len=0;
96
                return r;
97
            }
98
        } else if (k.type == TP_NONE) {
99
            return _tp_list_pop(tp,self.list.val,0,"tp_get");
100
        }
101
    } else if (type == TP_STRING) {
102
        if (k.type == TP_NUMBER) {
103
            int l = self.string.len;
104
            int n = k.number.val;
105
            n = (n<0?l+n:n);
106
            if (n >= 0 && n < l) { return tp_string_n(tp->chars[(unsigned char)self.string.val[n]],1); }
107
        } else if (k.type == TP_STRING) {
108
            if (strcmp("join",TP_CSTR(k)) == 0) {
109
                return tp_method(tp,self,tp_join);
110
            } else if (strcmp("split",TP_CSTR(k)) == 0) {
111
                return tp_method(tp,self,tp_split);
112
            } else if (strcmp("index",TP_CSTR(k)) == 0) {
113
                return tp_method(tp,self,tp_str_index);
114
            } else if (strcmp("strip",TP_CSTR(k)) == 0) {
115
                return tp_method(tp,self,tp_strip);
116
            } else if (strcmp("replace",TP_CSTR(k)) == 0) {
117
                return tp_method(tp,self,tp_replace);
118
            }
119
        }
120
    }
121
 
122
    if (k.type == TP_LIST) {
123
        int a,b,l;
124
        tp_obj tmp;
125
        l = tp_len(tp,self).number.val;
126
        tmp = tp_get(tp,k,tp_number(0));
127
        if (tmp.type == TP_NUMBER) { a = tmp.number.val; }
128
        else if(tmp.type == TP_NONE) { a = 0; }
129
        else { tp_raise(tp_None,"%s is not a number",TP_CSTR(tmp)); }
130
        tmp = tp_get(tp,k,tp_number(1));
131
        if (tmp.type == TP_NUMBER) { b = tmp.number.val; }
132
        else if(tmp.type == TP_NONE) { b = l; }
133
        else { tp_raise(tp_None,"%s is not a number",TP_CSTR(tmp)); }
134
        a = _tp_max(0,(a<0?l+a:a)); b = _tp_min(l,(b<0?l+b:b));
135
        if (type == TP_LIST) {
136
            return tp_list_n(tp,b-a,&self.list.val->items[a]);
137
        } else if (type == TP_STRING) {
138
            tp_obj r = tp_string_t(tp,b-a);
139
            char *ptr = r.string.info->s;
140
            memcpy(ptr,self.string.val+a,b-a); ptr[b-a]=0;
141
            return tp_track(tp,r);
142
        }
143
    }
144
 
145
    tp_raise(tp_None,"tp_get(%s,%s)",TP_CSTR(self),TP_CSTR(k));
146
}
147
 
148
int tp_iget(TP,tp_obj *r, tp_obj self, tp_obj k) {
149
    if (self.type == TP_DICT) {
150
        int n = _tp_dict_find(tp,self.dict.val,k);
151
        if (n == -1) { return 0; }
152
        *r = self.dict.val->items[n].val;
153
        tp_grey(tp,*r);
154
        return 1;
155
    }
156
    if (self.type == TP_LIST && !self.list.val->len) { return 0; }
157
    *r = tp_get(tp,self,k); tp_grey(tp,*r);
158
    return 1;
159
}
160
 
161
void tp_set(TP,tp_obj self, tp_obj k, tp_obj v) {
162
    int type;
2001 jaeger 163
 
1913 jaeger 164
    type = self.type;
165
    if (type == TP_DICT) {
166
        _tp_dict_set(tp,self.dict.val,k,v);
167
        return;
168
    } else if (type == TP_LIST) {
169
        if (k.type == TP_NUMBER) {
170
            _tp_list_set(tp,self.list.val,k.number.val,v,"tp_set");
171
            return;
172
        } else if (k.type == TP_NONE) {
173
            _tp_list_append(tp,self.list.val,v);
174
            return;
175
        } else if (k.type == TP_STRING) {
176
            if (strcmp("*",TP_CSTR(k)) == 0) {
177
                tp_params_v(tp,2,self,v); tp_extend(tp);
178
                return;
179
            }
180
        }
181
    }
182
    tp_raise(,"tp_set(%s,%s,%s)",TP_CSTR(self),TP_CSTR(k),TP_CSTR(v));
183
}
184
 
185
tp_obj tp_add(TP,tp_obj a, tp_obj b) {
186
    if (a.type == TP_NUMBER && a.type == b.type) {
187
        return tp_number(a.number.val+b.number.val);
188
    } else if (a.type == TP_STRING && a.type == b.type) {
189
        int al = a.string.len, bl = b.string.len;
190
        tp_obj r = tp_string_t(tp,al+bl);
191
        char *s = r.string.info->s;
192
        memcpy(s,a.string.val,al); memcpy(s+al,b.string.val,bl);
193
        return tp_track(tp,r);
194
    } else if (a.type == TP_LIST && a.type == b.type) {
195
        tp_obj r;
196
        tp_params_v(tp,1,a);
197
        r = tp_copy(tp);
198
        tp_params_v(tp,2,r,b);
199
        tp_extend(tp);
200
        return r;
201
    }
202
    tp_raise(tp_None,"tp_add(%s,%s)",TP_CSTR(a),TP_CSTR(b));
203
}
204
 
205
tp_obj tp_mul(TP,tp_obj a, tp_obj b) {
206
    if (a.type == TP_NUMBER && a.type == b.type) {
207
        return tp_number(a.number.val*b.number.val);
208
    } else if (a.type == TP_STRING && b.type == TP_NUMBER) {
209
        int al = a.string.len; int n = b.number.val;
210
        tp_obj r = tp_string_t(tp,al*n);
211
        char *s = r.string.info->s;
212
        int i; for (i=0; i
213
        return tp_track(tp,r);
214
    }
215
    tp_raise(tp_None,"tp_mul(%s,%s)",TP_CSTR(a),TP_CSTR(b));
216
}
217
 
218
 
219
tp_obj tp_len(TP,tp_obj self) {
220
    int type = self.type;
221
    if (type == TP_STRING) {
222
        return tp_number(self.string.len);
223
    } else if (type == TP_DICT) {
224
        return tp_number(self.dict.val->len);
225
    } else if (type == TP_LIST) {
226
        return tp_number(self.list.val->len);
227
    }
228
    tp_raise(tp_None,"tp_len(%s)",TP_CSTR(self));
229
}
230
 
231
int tp_cmp(TP,tp_obj a, tp_obj b) {
232
    if (a.type != b.type) { return a.type-b.type; }
233
    switch(a.type) {
234
        case TP_NONE: return 0;
235
        case TP_NUMBER: return _tp_sign(a.number.val-b.number.val);
236
        case TP_STRING: {
237
            int v = memcmp(a.string.val,b.string.val,_tp_min(a.string.len,b.string.len));
238
            if (v == 0) { v = a.string.len-b.string.len; }
239
            return v;
240
        }
241
        case TP_LIST: {
242
            int n,v; for(n=0;n<_tp_min(a.list.val->len,b.list.val->len);n++) {
243
        tp_obj aa = a.list.val->items[n]; tp_obj bb = b.list.val->items[n];
244
            if (aa.type == TP_LIST && bb.type == TP_LIST) { v = aa.list.val-bb.list.val; } else { v = tp_cmp(tp,aa,bb); }
245
            if (v) { return v; } }
246
            return a.list.val->len-b.list.val->len;
247
        }
248
        case TP_DICT: return a.dict.val - b.dict.val;
249
        case TP_FNC: return a.fnc.info - b.fnc.info;
250
        case TP_DATA: return (char*)a.data.val - (char*)b.data.val;
251
    }
252
    tp_raise(0,"tp_cmp(%s,%s)",TP_CSTR(a),TP_CSTR(b));
253
}
254
 
255
#define TP_OP(name,expr) \
256
    tp_obj name(TP,tp_obj _a,tp_obj _b) { \
257
    if (_a.type == TP_NUMBER && _a.type == _b.type) { \
258
        tp_num a = _a.number.val; tp_num b = _b.number.val; \
259
        return tp_number(expr); \
260
    } \
261
    tp_raise(tp_None,"%s(%s,%s)",#name,TP_CSTR(_a),TP_CSTR(_b)); \
262
}
263
 
264
TP_OP(tp_and,((long)a)&((long)b));
265
TP_OP(tp_or,((long)a)|((long)b));
266
TP_OP(tp_mod,((long)a)%((long)b));
267
TP_OP(tp_lsh,((long)a)<<((long)b));
268
TP_OP(tp_rsh,((long)a)>>((long)b));
269
TP_OP(tp_sub,a-b);
270
TP_OP(tp_div,a/b);
271
TP_OP(tp_pow,pow(a,b));
272
 
273
 
274
/**/