示例#1
0
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));
}
示例#2
0
tp_obj tp_float(TP) {
    tp_obj v = TP_OBJ();
    int ord = TP_DEFAULT(tp_number(0)).number.val;
    int type = v.type;
    if (type == TP_NUMBER) { return v; }
    if (type == TP_STRING) {
        if (strchr(TP_CSTR(v),'.')) { return tp_number(atof(TP_CSTR(v))); }
        return(tp_number(strtol(TP_CSTR(v),0,ord)));
    }
    tp_raise(tp_None,"tp_float(%s)",TP_CSTR(v));
}
示例#3
0
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));
}
示例#4
0
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));
}
示例#5
0
static PyObject *
Tinypy_exec(TinypyObject *self, PyObject *args)
{    
    tp_obj obj;
    tp_vm *tp = self->vm;
    PyObject *ret, *TinypyError, *TinypyModule, *TinypyDict;
    const char *code;
    
    if (!PyArg_ParseTuple(args, "s|d", &code, &tp->time_limit)) {
        return NULL;
    }
    
    tp->time_elapsed = 0;
    tp->mem_exceeded = 0;
    TinypyModule = PyImport_ImportModule("tinypy");
    TinypyDict = PyModule_GetDict(TinypyModule);
    TinypyError = PyDict_GetItemString(TinypyDict, "error");
    if(setjmp(tp->nextexpr)) {
        --(tp->cur);
        PyErr_SetString(TinypyError, TP_CSTR(tp->ex));      
        return NULL;
    }
    tp->clocks = clock();
    obj = tp_eval(tp, code, tp->builtins);
    ret = Tinypy_ConvertObj(obj);
    return ret;
}
示例#6
0
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));
}
示例#7
0
tp_obj tp_istype(TP) {
    tp_obj v = TP_OBJ();
    char const *t = TP_STR();
    if (strcmp("string",t) == 0) { return tp_number(v.type == TP_STRING); }
    if (strcmp("list",t) == 0) { return tp_number(v.type == TP_LIST); }
    if (strcmp("dict",t) == 0) { return tp_number(v.type == TP_DICT); }
    if (strcmp("number",t) == 0) { return tp_number(v.type == TP_NUMBER); }
    tp_raise(tp_None,"is_type(%s,%s)",TP_CSTR(v),t);
}
示例#8
0
tp_obj tp_copy(TP) {
    tp_obj r = TP_OBJ();
    int type = r.type;
    if (type == TP_LIST) {
        return _tp_list_copy(tp,r);
    } else if (type == TP_DICT) {
        return _tp_dict_copy(tp,r);
    }
    tp_raise(tp_None,"tp_copy(%s)",TP_CSTR(r));
}
示例#9
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 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));
}
示例#10
0
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));
}
示例#11
0
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));
}
示例#12
0
tp_obj tp_print(TP) {
    int n = 0;
    tp_obj e;
    TP_LOOP(e)
        if (n) { printf(" "); }
        printf("%s",TP_CSTR(e));
        n += 1;
    TP_END;
    printf("\n");
    return tp_None;
}
示例#13
0
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));
}
示例#14
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));
}