Пример #1
0
/* utility functions */
Uint32 pygame_list_to_color(TP,tp_obj clr,SDL_Surface *s) {
    int r,g,b;
    r = tp_get(tp,clr,tp_number(0)).number.val;
    g = tp_get(tp,clr,tp_number(1)).number.val;
    b = tp_get(tp,clr,tp_number(2)).number.val;
    return SDL_MapRGB(s->format,r,g,b);
}
Пример #2
0
/*
 * init math module, namely, set its dictionary
 */
void math_init(TP)
{
    /*
     * new a module dict for math
     */
    tp_obj math_mod = tp_dict(tp);

    /*
     * initialize pi and e
     */
    math_pi = tp_number(M_PI);
    math_e  = tp_number(M_E);

    /*
     * bind math functions to math module
     */
    tp_set(tp, math_mod, tp_string("pi"), math_pi);
    tp_set(tp, math_mod, tp_string("e"), math_e);
    tp_set(tp, math_mod, tp_string("acos"), tp_fnc(tp, math_acos));
    tp_set(tp, math_mod, tp_string("asin"), tp_fnc(tp, math_asin));
    tp_set(tp, math_mod, tp_string("atan"), tp_fnc(tp, math_atan));
    tp_set(tp, math_mod, tp_string("atan2"), tp_fnc(tp, math_atan2));
    tp_set(tp, math_mod, tp_string("ceil"), tp_fnc(tp, math_ceil));
    tp_set(tp, math_mod, tp_string("cos"), tp_fnc(tp, math_cos));
    tp_set(tp, math_mod, tp_string("cosh"), tp_fnc(tp, math_cosh));
    tp_set(tp, math_mod, tp_string("degrees"), tp_fnc(tp, math_degrees));
    tp_set(tp, math_mod, tp_string("exp"), tp_fnc(tp, math_exp));
    tp_set(tp, math_mod, tp_string("fabs"), tp_fnc(tp, math_fabs));
    tp_set(tp, math_mod, tp_string("floor"), tp_fnc(tp, math_floor));
    tp_set(tp, math_mod, tp_string("fmod"), tp_fnc(tp, math_fmod));
    tp_set(tp, math_mod, tp_string("frexp"), tp_fnc(tp, math_frexp));
    tp_set(tp, math_mod, tp_string("hypot"), tp_fnc(tp, math_hypot));
    tp_set(tp, math_mod, tp_string("ldexp"), tp_fnc(tp, math_ldexp));
    tp_set(tp, math_mod, tp_string("log"), tp_fnc(tp, math_log));
    tp_set(tp, math_mod, tp_string("log10"), tp_fnc(tp, math_log10));
    tp_set(tp, math_mod, tp_string("modf"), tp_fnc(tp, math_modf));
    tp_set(tp, math_mod, tp_string("pow"), tp_fnc(tp, math_pow));
    tp_set(tp, math_mod, tp_string("radians"), tp_fnc(tp, math_radians));
    tp_set(tp, math_mod, tp_string("sin"), tp_fnc(tp, math_sin));
    tp_set(tp, math_mod, tp_string("sinh"), tp_fnc(tp, math_sinh));
    tp_set(tp, math_mod, tp_string("sqrt"), tp_fnc(tp, math_sqrt));
    tp_set(tp, math_mod, tp_string("tan"), tp_fnc(tp, math_tan));
    tp_set(tp, math_mod, tp_string("tanh"), tp_fnc(tp, math_tanh));

    /*
     * bind special attributes to math module
     */
    tp_set(tp, math_mod, tp_string("__doc__"), 
            tp_string(
                "This module is always available.  It provides access to the\n"
                "mathematical functions defined by the C standard."));
    tp_set(tp, math_mod, tp_string("__name__"), tp_string("math"));
    tp_set(tp, math_mod, tp_string("__file__"), tp_string(__FILE__));

    /*
     * bind to tiny modules[]
     */
    tp_set(tp, tp->modules, tp_string("math"), math_mod);
}
Пример #3
0
tp_obj tp_range(TP) {
    int a = TP_NUM();
    int b = TP_NUM();
    int c = TP_DEFAULT(tp_number(1)).number.val;
    tp_obj r = tp_list(tp);
    int i;
    for (i=a; i!=b; i+=c) { _tp_list_append(tp,r.list.val,tp_number(i)); }
    return r;
}
Пример #4
0
tp_obj tp_istype(TP) {
    tp_obj v = TP_OBJ();
    char *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(None,"is_type(%s,%s)",STR(v),t);
}
Пример #5
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));
}
Пример #6
0
tp_obj kolibri_socket_module(TP)
{
    tp_obj socket_mod = tp_dict(tp);

    tp_set(tp, socket_mod, tp_string("AF_INET"), tp_number(AF_INET));
    tp_set(tp, socket_mod, tp_string("SOCK_STREAM"), tp_number(SOCK_STREAM));
    tp_set(tp, socket_mod, tp_string("SOCK_DGRAM"), tp_number(SOCK_DGRAM));
    tp_set(tp, socket_mod, tp_string("inet_pton"), tp_fnc(tp, kolibri_inet_pton));
    tp_set(tp, socket_mod, tp_string("socket"), tp_fnc(tp, kolibri_socket));
    return socket_mod;
}
Пример #7
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(STR(v),'.')) { return tp_number(atof(STR(v))); }
        return(tp_number(strtol(STR(v),0,ord)));
    }
    tp_raise(None,"tp_float(%s)",STR(v));
}
Пример #8
0
/* Function: tp_len
 * Returns the length of an object.
 *
 * Returns the number of items in a list or dict, or the length of a string.
 */
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_string("(tp_len) TypeError: len() of unsized object"));
}
Пример #9
0
/* Function: tp_has
 * Checks if an object contains a key.
 *
 * Returns tp_True if self[k] exists, tp_False otherwise.
 */
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) {
        return tp_number(_tp_str_index(self,k)!=-1);
    } else if (type == TP_LIST) {
        return tp_number(_tp_list_find(tp,self.list.val,k)!=-1);
    }
    tp_raise(tp_None,tp_string("(tp_has) TypeError: iterable argument required"));
}
Пример #10
0
tp_obj tp_has(tp_vm *tp, tp_obj self, tp_obj k) {
    int type = obj_type(self);
    if (type == TP_DICT) {
        if (_tp_dict_find(tp, tp_dict_val(self), k) != -1) { return True; }
        return False;
    } else if (type == TP_STRING && obj_type(k) == TP_STRING) {
        char *p = strstr(STR(self),STR(k));
        return tp_number(tp, p != 0);
    } else if (type == TP_LIST) {
        return tp_number(tp, _tp_list_find(tp,tp_list_val(self),k)!=-1);
    }
    tp_raise(None,"tp_has(%s,%s)",STR(self),STR(k));
}
Пример #11
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 && v.string.len < 32) {
        char s[32]; memset(s,0,v.string.len+1);
        memcpy(s,v.string.val,v.string.len);
        if (strchr(s,'.')) { return tp_number(atof(s)); }
        return(tp_number(strtol(s,0,ord)));
    }
    tp_raise(tp_None,tp_string("(tp_float) TypeError: ?"));
}
Пример #12
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));
}
Пример #13
0
/* Socket connect method.
 *
 * Example:
 * s.connect('10.10.1.1', 7000) #Connects to 10.10.1.1:7000
 */
tp_obj kolibri_connect(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    tp_obj  remote_addr_obj = TP_OBJ();
    __u32  remote_addr;
    __u32  remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
    __u32  local_port  = tp_get(tp, self, tp_string("local_port")).number.val;
    __u32  socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
    int  s = -1; /* Socket descriptor */


    if (remote_addr_obj.type == TP_NUMBER)
        remote_addr = remote_addr_obj.number.val;
    else if (remote_addr_obj.type == TP_STRING)
        inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);

    if (socktype == SOCK_STREAM)
        s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 1);
    else if (socktype == SOCK_DGRAM)
        s = __menuet__open_UDP_socket(local_port, remote_port, remote_addr);
    if (s >= 0)
    {
        tp_set(tp, self, tp_string("socket"), tp_number(s));
        return tp_True;
    }
    else
        return tp_False;
}
Пример #14
0
Файл: mem.c Проект: Saruta/pyr0
tp_obj mem_used(TP)
{
	u32 ret = (u32)sbrk(0);
	ret -= (u32)brk_base;

	return tp_number(ret);
}
Пример #15
0
tp_obj tp_range(TP) {
    int a,b,c,i;
    tp_obj r = tp_list(tp);
    switch (tp->params.list.val->len) {
        case 1: a = 0; b = TP_NUM(); c = 1; break;
        case 2:
        case 3: a = TP_NUM(); b = TP_NUM(); c = TP_DEFAULT(tp_number(1)).number.val; break;
        default: return r;
    }
    if (c != 0) {
        for (i=a; (c>0) ? i<b : i>b; i+=c) {
            _tp_list_append(tp,r.list.val,tp_number(i));
        }
    }
    return r;
}
Пример #16
0
Файл: math.c Проект: Saruta/pyr0
tp_obj math_acos(TP)
{
	double x = TP_NUM();
	double r = 0.0;
	r = acos(x);
	return (tp_number(r));
}
Пример #17
0
Файл: math.c Проект: Saruta/pyr0
tp_obj math_sqrt(TP)
{
	double x = TP_NUM();
	double r = 0.0;
	r = sqrt(x);
	return (tp_number(r));
}
Пример #18
0
Файл: math.c Проект: Saruta/pyr0
tp_obj math_ceil(TP)
{
	double x = TP_NUM();
	double r = 0.0;
	r = ceil(x);
	return (tp_number(r));
}
Пример #19
0
Файл: math.c Проект: Saruta/pyr0
tp_obj math_exp(TP)
{
	double x = TP_NUM();
	double r = 0.0;
	r = exp(x);
	return (tp_number(r));
}
Пример #20
0
Файл: math.c Проект: Saruta/pyr0
tp_obj math_tan(TP)
{
	double x = TP_NUM();
	double r = 0.0;
	r = tan(x);
	return (tp_number(r));
}
Пример #21
0
tp_obj tp_len(tp_vm *tp, tp_obj self) {
    double len = _tp_len(self);
    if (-1.0 == len) {
        tp_raise(None, "tp_len(%s)", STR(self));
    }
    return tp_number(tp, len);
}
Пример #22
0
Файл: mem.c Проект: Saruta/pyr0
tp_obj mem_available(TP)
{
	u32 ret = (u32)sbrk(0);
	ret = (u32)brk_limit - (u32)ret;

	return tp_number(ret);
}
Пример #23
0
/* Socket listen method.
 * 
 * Example:
 * s.listen('10.10.1.1', 5000)
 */
tp_obj kolibri_listen(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    tp_obj remote_addr_obj = TP_OBJ();
    __u32  remote_addr;
    __u32  remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
    __u32  local_port  = tp_get(tp, self, tp_string("local_port")).number.val;
    __u32  socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
    int  s = -1; /* Socket descriptor */

    if (socktype != SOCK_STREAM)
        tp_raise(tp_None, "IOError: attempt to listen on non-TCP socket", tp_None);

    if (remote_addr_obj.type == TP_NUMBER)
        remote_addr = remote_addr_obj.number.val;
    else if (remote_addr_obj.type == TP_STRING)
        inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);

    if ((s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 0)) >= 0)
    {
        tp_set(tp, self, tp_string("socket"), tp_number(s));
        return tp_True;
    }
    else
        return tp_False;
}
Пример #24
0
tp_obj tp_ord(TP) {
    tp_obj s = TP_STR();
    if (s.string.len != 1) {
        tp_raise(tp_None,tp_string("(tp_ord) TypeError: ord() expected a character"));
    }
    return tp_number((unsigned char)s.string.val[0]);
}
Пример #25
0
tp_obj tp_str_index(TP) {
    tp_obj s = TP_OBJ();
    tp_obj v = TP_OBJ();
    int n = _tp_str_index(s,v);
    if (n >= 0) { return tp_number(n); }
    tp_raise(tp_None,tp_string("(tp_str_index) ValueError: substring not found"));
}
Пример #26
0
/* Socket bind method.
 *
 * In KolibriOS it just sets local address and port.
 *
 * Example:
 * s.bind('10.10.1.2', 6000) #Connects to 10.10.1.2:6000
 */
tp_obj kolibri_bind(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    tp_obj local_addr_obj = TP_OBJ();
    __u32  local_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
    __u32  local_addr;

    if (local_addr_obj.type == TP_NUMBER)
        local_addr = local_addr_obj.number.val;
    else if (local_addr_obj.type == TP_STRING)
        inet_pton(tp, (const char *)local_addr_obj.string.val, local_addr_obj.string.len, &local_addr);

    tp_set(tp, self, tp_string("local_addr"), tp_number(local_addr));
    tp_set(tp, self, tp_string("local_port"), tp_number(local_port));
    return tp_None;
}
Пример #27
0
/* Converter from string presentation to binary address. */
static tp_obj kolibri_inet_pton(TP)
{
    tp_obj obj;
    __u32 addr;
    obj = TP_TYPE(TP_STRING);
    inet_pton(tp, (char *)obj.string.val, (int)obj.string.len, &addr);
    return tp_number(addr);
}
Пример #28
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));
}
Пример #29
0
tp_obj tp_mul(tp_vm *tp, tp_obj a, tp_obj b) {
    if (obj_type(a) == TP_NUMBER && obj_type(a) == obj_type(b)) {
        return tp_number(tp, tp_number_val(a) * tp_number_val(b));
    } else if (obj_type(a) == TP_STRING && obj_type(b) == TP_NUMBER) {
        int al = tp_str_len(a);
        int n = tp_number_val(b);
        tp_obj r = tp_string_t(tp,al*n);
        char *s = tp_str_val(r);
        int i; 
        for (i=0; i<n; i++) { 
            memcpy(s+al*i, tp_str_val(a),al);
        }
        return tp_track(tp,r);
    }
    tp_raise(None,"tp_mul(%s,%s)",STR(a),STR(b));
}
Пример #30
0
tp_obj tp_istype(TP) {
    tp_obj v = TP_OBJ();
    tp_obj t = TP_STR();
    if (tp_cmp(tp,t,tp_string("string")) == 0) { return tp_number(v.type == TP_STRING); }
    if (tp_cmp(tp,t,tp_string("list")) == 0) { return tp_number(v.type == TP_LIST); }
    if (tp_cmp(tp,t,tp_string("dict")) == 0) { return tp_number(v.type == TP_DICT); }
    if (tp_cmp(tp,t,tp_string("number")) == 0) { return tp_number(v.type == TP_NUMBER); }
    if (tp_cmp(tp,t,tp_string("fnc")) == 0) { return tp_number(v.type == TP_FNC && (v.fnc.ftype&2) == 0); }
    if (tp_cmp(tp,t,tp_string("method")) == 0) { return tp_number(v.type == TP_FNC && (v.fnc.ftype&2) != 0); }
    tp_raise(tp_None,tp_string("(is_type) TypeError: ?"));
}