kolibrios/programs/develop/tinypy/tinypy/ops.c
jaeger 42affd404a tinypy: file reading support.
git-svn-id: svn://kolibrios.org@2001 a494cfbc-eb01-0410-851d-a64ba20cac60
2011-07-08 19:48:44 +00:00

275 lines
9.4 KiB
C

tp_obj tp_str(TP,tp_obj self) {
int type = self.type;
if (type == TP_STRING) {
return self;
}
if (type == TP_NUMBER) {
tp_num v = self.number.val;
if ((fabs(v)-fabs((long)v)) < 0.000001) { return tp_printf(tp,"%ld",(long)v); }
return tp_printf(tp,"%f",v);
} else if(type == TP_DICT) {
return tp_printf(tp,"<dict 0x%x>",self.dict.val);
} else if(type == TP_LIST) {
return tp_printf(tp,"<list 0x%x>",self.list.val);
} else if (type == TP_NONE) {
return tp_string("None");
} else if (type == TP_DATA) {
return tp_printf(tp,"<data 0x%x>",self.data.val);
} else if (type == TP_FNC) {
return tp_printf(tp,"<fnc 0x%x>",self.fnc.info);
}
return tp_string("<?>");
}
int tp_bool(TP,tp_obj v) {
switch(v.type) {
case TP_NUMBER: return v.number.val != 0;
case TP_NONE: return 0;
case TP_STRING: return v.string.len != 0;
case TP_LIST: return v.list.val->len != 0;
case TP_DICT: return v.dict.val->len != 0;
}
return 1;
}
tp_obj tp_has(TP,tp_obj self, tp_obj k) {
int type = self.type;
if (type == TP_DICT) {
if (_tp_dict_find(tp,self.dict.val,k) != -1) { return tp_True; }
return tp_False;
} else if (type == TP_STRING && k.type == TP_STRING) {
char *p = strstr(TP_CSTR(self),TP_CSTR(k));
return tp_number(p != 0);
} else if (type == TP_LIST) {
return tp_number(_tp_list_find(tp,self.list.val,k)!=-1);
}
tp_raise(tp_None,"tp_has(%s,%s)",TP_CSTR(self),TP_CSTR(k));
}
void tp_del(TP,tp_obj self, tp_obj k) {
int type = self.type;
if (type == TP_DICT) {
_tp_dict_del(tp,self.dict.val,k,"tp_del");
return;
}
tp_raise(,"tp_del(%s,%s)",TP_CSTR(self),TP_CSTR(k));
}
tp_obj tp_iter(TP,tp_obj self, tp_obj k) {
int type = self.type;
if (type == TP_LIST || type == TP_STRING) { return tp_get(tp,self,k); }
if (type == TP_DICT && k.type == TP_NUMBER) {
return self.dict.val->items[_tp_dict_next(tp,self.dict.val)].key;
}
tp_raise(tp_None,"tp_iter(%s,%s)",TP_CSTR(self),TP_CSTR(k));
}
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));
}
int tp_iget(TP,tp_obj *r, tp_obj self, tp_obj k) {
if (self.type == TP_DICT) {
int n = _tp_dict_find(tp,self.dict.val,k);
if (n == -1) { return 0; }
*r = self.dict.val->items[n].val;
tp_grey(tp,*r);
return 1;
}
if (self.type == TP_LIST && !self.list.val->len) { return 0; }
*r = tp_get(tp,self,k); tp_grey(tp,*r);
return 1;
}
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));
}
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_add(%s,%s)",TP_CSTR(a),TP_CSTR(b));
}
tp_obj tp_mul(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 && b.type == TP_NUMBER) {
int al = a.string.len; int n = b.number.val;
tp_obj r = tp_string_t(tp,al*n);
char *s = r.string.info->s;
int i; for (i=0; i<n; i++) { memcpy(s+al*i,a.string.val,al); }
return tp_track(tp,r);
}
tp_raise(tp_None,"tp_mul(%s,%s)",TP_CSTR(a),TP_CSTR(b));
}
tp_obj tp_len(TP,tp_obj self) {
int type = self.type;
if (type == TP_STRING) {
return tp_number(self.string.len);
} else if (type == TP_DICT) {
return tp_number(self.dict.val->len);
} else if (type == TP_LIST) {
return tp_number(self.list.val->len);
}
tp_raise(tp_None,"tp_len(%s)",TP_CSTR(self));
}
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 v = memcmp(a.string.val,b.string.val,_tp_min(a.string.len,b.string.len));
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_cmp(%s,%s)",TP_CSTR(a),TP_CSTR(b));
}
#define TP_OP(name,expr) \
tp_obj name(TP,tp_obj _a,tp_obj _b) { \
if (_a.type == TP_NUMBER && _a.type == _b.type) { \
tp_num a = _a.number.val; tp_num b = _b.number.val; \
return tp_number(expr); \
} \
tp_raise(tp_None,"%s(%s,%s)",#name,TP_CSTR(_a),TP_CSTR(_b)); \
}
TP_OP(tp_and,((long)a)&((long)b));
TP_OP(tp_or,((long)a)|((long)b));
TP_OP(tp_mod,((long)a)%((long)b));
TP_OP(tp_lsh,((long)a)<<((long)b));
TP_OP(tp_rsh,((long)a)>>((long)b));
TP_OP(tp_sub,a-b);
TP_OP(tp_div,a/b);
TP_OP(tp_pow,pow(a,b));
/**/