int main(int argc, char *argv[]) { tp_vm *tp = tp_init(argc,argv); /* INIT */ tp_call(tp,"py2bc","tinypy",tp_None); tp_deinit(tp); return(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")); }
tp_obj tp_object_new(TP) { tp_obj klass = TP_TYPE(TP_DICT); tp_obj self = tp_object(tp); self.dict.val->meta = klass; TP_META_BEGIN(self,"__init__"); tp_call(tp,meta,tp->params); TP_META_END; return self; }
/* 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: ?")); }