Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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,"<dict 0x%x>",self.dict.val);
  12.     } else if(type == TP_LIST) {
  13.         return tp_printf(tp,"<list 0x%x>",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,"<data 0x%x>",self.data.val);
  18.     } else if (type == TP_FNC) {
  19.         return tp_printf(tp,"<fnc 0x%x>",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;
  72.     /*con_printf("tp_get %s %s\n", TP_CSTR(self), TP_CSTR(k));*/
  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.  
  146.     con_printf("Raising exception\n");
  147.     tp_raise(tp_None,"tp_get(%s,%s)",TP_CSTR(self),TP_CSTR(k));
  148. }
  149.  
  150. int tp_iget(TP,tp_obj *r, tp_obj self, tp_obj k) {
  151.     if (self.type == TP_DICT) {
  152.         int n = _tp_dict_find(tp,self.dict.val,k);
  153.         if (n == -1) { return 0; }
  154.         *r = self.dict.val->items[n].val;
  155.         tp_grey(tp,*r);
  156.         return 1;
  157.     }
  158.     if (self.type == TP_LIST && !self.list.val->len) { return 0; }
  159.     *r = tp_get(tp,self,k); tp_grey(tp,*r);
  160.     return 1;
  161. }
  162.  
  163. void tp_set(TP,tp_obj self, tp_obj k, tp_obj v) {
  164.     int type;
  165.     con_printf("vm is %x self is %x k is %x v is %x", tp, self, k, v);
  166.     type = self.type;
  167.     if (type == TP_DICT) {
  168.         _tp_dict_set(tp,self.dict.val,k,v);
  169.         return;
  170.     } else if (type == TP_LIST) {
  171.         if (k.type == TP_NUMBER) {
  172.             _tp_list_set(tp,self.list.val,k.number.val,v,"tp_set");
  173.             return;
  174.         } else if (k.type == TP_NONE) {
  175.             _tp_list_append(tp,self.list.val,v);
  176.             return;
  177.         } else if (k.type == TP_STRING) {
  178.             if (strcmp("*",TP_CSTR(k)) == 0) {
  179.                 tp_params_v(tp,2,self,v); tp_extend(tp);
  180.                 return;
  181.             }
  182.         }
  183.     }
  184.     tp_raise(,"tp_set(%s,%s,%s)",TP_CSTR(self),TP_CSTR(k),TP_CSTR(v));
  185. }
  186.  
  187. tp_obj tp_add(TP,tp_obj a, tp_obj b) {
  188.     if (a.type == TP_NUMBER && a.type == b.type) {
  189.         return tp_number(a.number.val+b.number.val);
  190.     } else if (a.type == TP_STRING && a.type == b.type) {
  191.         int al = a.string.len, bl = b.string.len;
  192.         tp_obj r = tp_string_t(tp,al+bl);
  193.         char *s = r.string.info->s;
  194.         memcpy(s,a.string.val,al); memcpy(s+al,b.string.val,bl);
  195.         return tp_track(tp,r);
  196.     } else if (a.type == TP_LIST && a.type == b.type) {
  197.         tp_obj r;
  198.         tp_params_v(tp,1,a);
  199.         r = tp_copy(tp);
  200.         tp_params_v(tp,2,r,b);
  201.         tp_extend(tp);
  202.         return r;
  203.     }
  204.     tp_raise(tp_None,"tp_add(%s,%s)",TP_CSTR(a),TP_CSTR(b));
  205. }
  206.  
  207. tp_obj tp_mul(TP,tp_obj a, tp_obj b) {
  208.     if (a.type == TP_NUMBER && a.type == b.type) {
  209.         return tp_number(a.number.val*b.number.val);
  210.     } else if (a.type == TP_STRING && b.type == TP_NUMBER) {
  211.         int al = a.string.len; int n = b.number.val;
  212.         tp_obj r = tp_string_t(tp,al*n);
  213.         char *s = r.string.info->s;
  214.         int i; for (i=0; i<n; i++) { memcpy(s+al*i,a.string.val,al); }
  215.         return tp_track(tp,r);
  216.     }
  217.     tp_raise(tp_None,"tp_mul(%s,%s)",TP_CSTR(a),TP_CSTR(b));
  218. }
  219.  
  220.  
  221. tp_obj tp_len(TP,tp_obj self) {
  222.     int type = self.type;
  223.     if (type == TP_STRING) {
  224.         return tp_number(self.string.len);
  225.     } else if (type == TP_DICT) {
  226.         return tp_number(self.dict.val->len);
  227.     } else if (type == TP_LIST) {
  228.         return tp_number(self.list.val->len);
  229.     }
  230.     tp_raise(tp_None,"tp_len(%s)",TP_CSTR(self));
  231. }
  232.  
  233. int tp_cmp(TP,tp_obj a, tp_obj b) {
  234.     if (a.type != b.type) { return a.type-b.type; }
  235.     switch(a.type) {
  236.         case TP_NONE: return 0;
  237.         case TP_NUMBER: return _tp_sign(a.number.val-b.number.val);
  238.         case TP_STRING: {
  239.             int v = memcmp(a.string.val,b.string.val,_tp_min(a.string.len,b.string.len));
  240.             if (v == 0) { v = a.string.len-b.string.len; }
  241.             return v;
  242.         }
  243.         case TP_LIST: {
  244.             int n,v; for(n=0;n<_tp_min(a.list.val->len,b.list.val->len);n++) {
  245.         tp_obj aa = a.list.val->items[n]; tp_obj bb = b.list.val->items[n];
  246.             if (aa.type == TP_LIST && bb.type == TP_LIST) { v = aa.list.val-bb.list.val; } else { v = tp_cmp(tp,aa,bb); }
  247.             if (v) { return v; } }
  248.             return a.list.val->len-b.list.val->len;
  249.         }
  250.         case TP_DICT: return a.dict.val - b.dict.val;
  251.         case TP_FNC: return a.fnc.info - b.fnc.info;
  252.         case TP_DATA: return (char*)a.data.val - (char*)b.data.val;
  253.     }
  254.     tp_raise(0,"tp_cmp(%s,%s)",TP_CSTR(a),TP_CSTR(b));
  255. }
  256.  
  257. #define TP_OP(name,expr) \
  258.     tp_obj name(TP,tp_obj _a,tp_obj _b) { \
  259.     if (_a.type == TP_NUMBER && _a.type == _b.type) { \
  260.         tp_num a = _a.number.val; tp_num b = _b.number.val; \
  261.         return tp_number(expr); \
  262.     } \
  263.     tp_raise(tp_None,"%s(%s,%s)",#name,TP_CSTR(_a),TP_CSTR(_b)); \
  264. }
  265.  
  266. TP_OP(tp_and,((long)a)&((long)b));
  267. TP_OP(tp_or,((long)a)|((long)b));
  268. TP_OP(tp_mod,((long)a)%((long)b));
  269. TP_OP(tp_lsh,((long)a)<<((long)b));
  270. TP_OP(tp_rsh,((long)a)>>((long)b));
  271. TP_OP(tp_sub,a-b);
  272. TP_OP(tp_div,a/b);
  273. TP_OP(tp_pow,pow(a,b));
  274.  
  275.  
  276. /**/
  277.