예제 #1
0
tp_obj tp_data(TP,int magic,void *v) {
    tp_obj r = {TP_DATA};
    r.data.info = (_tp_data*)tp_malloc(sizeof(_tp_data));
    r.data.val = v;
    r.data.magic = magic;
    return tp_track(tp,r);
}
예제 #2
0
tp_obj tp_replace(TP) {
    tp_obj s = TP_OBJ();
    tp_obj k = TP_OBJ();
    tp_obj v = TP_OBJ();
    tp_obj p = s;
    int i,n = 0;
    int c;
    int l;
    tp_obj rr;
    char *r;
    char *d;
    tp_obj z;
    while ((i = _tp_str_index(p,k)) != -1) {
        n += 1;
        p.string.val += i + k.string.len; p.string.len -= i + k.string.len;
    }
/*     fprintf(stderr,"ns: %d\n",n); */
    l = s.string.len + n * (v.string.len-k.string.len);
    rr = tp_string_t(tp,l);
    r = rr.string.info->s;
    d = r;
    z = p = s;
    while ((i = _tp_str_index(p,k)) != -1) {
        p.string.val += i; p.string.len -= i;
        memcpy(d,z.string.val,c=(p.string.val-z.string.val)); d += c;
        p.string.val += k.string.len; p.string.len -= k.string.len;
        memcpy(d,v.string.val,v.string.len); d += v.string.len;
        z = p;
    }
    memcpy(d,z.string.val,(s.string.val + s.string.len) - z.string.val);

    return tp_track(tp,rr);
}
예제 #3
0
tp_obj tp_fnc_new(TP,int t, void *v, tp_obj s, tp_obj g) {
    tp_obj r = {TP_FNC};
    _tp_fnc *info = (_tp_fnc*)tp_malloc(sizeof(_tp_fnc));
    info->self = s;
    info->globals = g;
    r.fnc.ftype = t;
    r.fnc.info = info;
    r.fnc.val = v;
    return tp_track(tp,r);
}
예제 #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) || 
               (a.type == TP_NUMBER && b.type == TP_STRING)) {
        if(a.type == TP_NUMBER) {
            tp_obj c = a; a = b; b = c;
        }
        int al = a.string.len; int n = b.number.val;
        if(n <= 0) {
            tp_obj r = tp_string_t(tp,0);
            return tp_track(tp,r);
        }
        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_string("(tp_mul) TypeError: ?"));
}
예제 #5
0
tp_obj tp_load(TP) {
    char *fname = TP_STR();
    struct stat stbuf; stat(fname, &stbuf); long l = stbuf.st_size;
    FILE *f;
    f = fopen(fname,"rb");
    if (!f) { tp_raise(None,"tp_load(%s)",fname); }
    tp_obj r = tp_string_t(tp,l);
    char *s = r.string.val;
    fread(s,1,l,f);
    fclose(f);
    return tp_track(tp,r);
}
예제 #6
0
tp_obj tp_printf(TP, char const *fmt,...) {
    int l;
    tp_obj r;
    char *s;
    va_list arg;
    va_start(arg, fmt);
    l = vsnprintf(NULL, 0, fmt,arg);
    r = tp_string_t(tp,l);
    s = r.string.info->s;
    va_end(arg);
    va_start(arg, fmt);
    vsprintf(s,fmt,arg);
    va_end(arg);
    return tp_track(tp,r);
}
예제 #7
0
파일: ops.c 프로젝트: abael/tinypy-kjk
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));
}
예제 #8
0
파일: ops.c 프로젝트: ashmew2/kolibriosSVN
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));
}
예제 #9
0
tp_obj tp_strip(TP) {
    tp_obj o = TP_TYPE(TP_STRING);
    char const *v = o.string.val; int l = o.string.len;
    int i; int a = l, b = 0;
    tp_obj r;
    char *s;
    for (i=0; i<l; i++) {
        if (v[i] != ' ' && v[i] != '\n' && v[i] != '\t' && v[i] != '\r') {
            a = _tp_min(a,i); b = _tp_max(b,i+1);
        }
    }
    if ((b-a) < 0) { return tp_string(""); }
    r = tp_string_t(tp,b-a);
    s = r.string.info->s;
    memcpy(s,v+a,b-a);
    return tp_track(tp,r);
}
예제 #10
0
tp_obj tp_load(TP) {
    FILE *f;
    long l;
    tp_obj r;
    char *s;
    char const *fname = TP_STR();
    struct stat stbuf;
    stat(fname, &stbuf);
    l = stbuf.st_size;
    f = fopen(fname,"rb");
    if (!f) {
        tp_raise(tp_None,"tp_load(%s)",fname);
    }
    r = tp_string_t(tp,l);
    s = r.string.info->s;
    fread(s,1,l,f);
    fclose(f);
    return tp_track(tp,r);
}
예제 #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_string("(tp_add) TypeError: ?"));
}
예제 #12
0
파일: ops.c 프로젝트: abael/tinypy-kjk
tp_obj tp_add(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(a) == obj_type(b)) {
        int al = tp_str_len(a), bl = tp_str_len(b);
        tp_obj r = tp_string_t(tp,al+bl);
        char *s = tp_str_val(r);
        memcpy(s, tp_str_val(a),al);
        memcpy(s+al, tp_str_val(b),bl);
        return tp_track(tp,r);
    } else if (obj_type(a) == TP_LIST && obj_type(a) == obj_type(b)) {
        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(None,"tp_add(%s,%s)",STR(a),STR(b));
}
예제 #13
0
파일: builtins.c 프로젝트: Saruta/tinypy
tp_obj tp_load(TP) {
    FILE *f;
    long l;
    tp_obj r;
    char *s;
    char fname[256]; tp_cstr(tp,TP_STR(),fname,256);
    struct stat stbuf;
    stat(fname, &stbuf);
    l = stbuf.st_size;
    f = fopen(fname,"rb");
    if (!f) {
        tp_raise(tp_None,tp_string("(tp_load) IOError: ?"));
    }
    r = tp_string_t(tp,l);
    s = r.string.info->s;
    fread(s,1,l,f);
/*    if (rr !=l) { printf("hmmn: %d %d\n",rr,(int)l); }*/
    fclose(f);
    return tp_track(tp,r);
}
예제 #14
0
tp_obj tp_join(TP) {
    tp_obj delim = TP_OBJ();
    tp_obj val = TP_OBJ();
    int l=0,i;
    tp_obj r;
    char *s;
    for (i=0; i<val.list.val->len; i++) {
        if (i!=0) { l += delim.string.len; }
        l += tp_str(tp,val.list.val->items[i]).string.len;
    }
    r = tp_string_t(tp,l);
    s = r.string.info->s;
    l = 0;
    for (i=0; i<val.list.val->len; i++) {
        tp_obj e;
        if (i!=0) {
            memcpy(s+l,delim.string.val,delim.string.len); l += delim.string.len;
        }
        e = tp_str(tp,val.list.val->items[i]);
        memcpy(s+l,e.string.val,e.string.len); l += e.string.len;
    }
    return tp_track(tp,r);
}
예제 #15
0
tp_obj tp_fpack(TP) {
    tp_num v = TP_NUM();
    tp_obj r = tp_string_t(tp,sizeof(tp_num));
    *(tp_num*)r.string.val = v; 
    return tp_track(tp,r);
}
예제 #16
0
/*
 * Create a new string which is a copy of some memory.
 * This is put into GC tracking for you.
 */
tp_obj tp_string_copy(TP, const char *s, int n) {
    tp_obj r = tp_string_t(tp,n);
    memcpy(r.string.info->s,s,n);
    return tp_track(tp,r);
}
예제 #17
0
파일: ops.c 프로젝트: ashmew2/kolibriosSVN
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));
}
예제 #18
0
파일: ops.c 프로젝트: abael/tinypy-kjk
tp_obj tp_get(tp_vm *tp, tp_obj self, tp_obj k) {
    int type = obj_type(self);
    tp_obj r;
    if (type == TP_DICT) {
        return _tp_dict_get(tp, tp_dict_val(self), k, "tp_get");
    } else if (type == TP_LIST) {
        if (obj_type(k) == TP_NUMBER) {
            int l = tp_number_val(tp_len(tp,self));
            int n = tp_number_val(k);
            n = (n<0?l+n:n);
            return _tp_list_get(tp,tp_list_val(self),n,"tp_get");
        } else if (obj_type(k) == TP_STRING) {
            if (strcmp("append",STR(k)) == 0) {
                return tp_method(tp,self,tp_append);
            } else if (strcmp("pop",STR(k)) == 0) {
                return tp_method(tp,self,tp_pop);
            } else if (strcmp("index",STR(k)) == 0) {
                return tp_method(tp,self,tp_index);
            } else if (strcmp("sort",STR(k)) == 0) {
                return tp_method(tp,self,tp_sort);
            } else if (strcmp("extend",STR(k)) == 0) {
                return tp_method(tp,self,tp_extend);
            } else if (strcmp("*",STR(k)) == 0) {
                tp_params_v(tp,1,self); 
                r = tp_copy(tp);
                tp_list_val(self)->len=0;
                return r;
            }
        } else if (obj_type(k) == TP_NONE) {
            return _tp_list_pop(tp,tp_list_val(self),0,"tp_get");
        }
    } else if (type == TP_STRING) {
        if (obj_type(k) == TP_NUMBER) {
            int l = tp_str_len(self);
            int n = tp_number_val(k);
            n = (n<0?l+n:n);
            if (n >= 0 && n < l) { 
                return tp_string_n(tp, tp->chars[(unsigned char)tp_str_val(self)[n]],1);
            }
        } else if (obj_type(k) == TP_STRING) {
            if (strcmp("join",STR(k)) == 0) {
                return tp_method(tp,self,tp_join);
            } else if (strcmp("split",STR(k)) == 0) {
                return tp_method(tp,self,tp_split);
            } else if (strcmp("index",STR(k)) == 0) {
                return tp_method(tp,self,tp_str_index);
            } else if (strcmp("strip",STR(k)) == 0) {
                return tp_method(tp,self,tp_strip);
            } else if (strcmp("replace",STR(k)) == 0) {
                return tp_method(tp,self,tp_replace);
            }
        }
    } else if (type == TP_DATA) {
        return tp_data_meta(self)->get(tp,self,k);
    }

    if (obj_type(k) == TP_LIST) {
        int a,b,l;
        tp_obj tmp;
        l = tp_number_val(tp_len(tp,self));
        tmp = tp_get(tp, k, tp_number(tp, 0));
        if (obj_type(tmp) == TP_NUMBER) { 
            a = tp_number_val(tmp); 
        } else if (obj_type(tmp) == TP_NONE) { 
            a = 0; 
        } else { 
            tp_raise(None,"%s is not a number",STR(tmp)); 
        }
        tmp = tp_get(tp, k, tp_number(tp, 1));
        if (obj_type(tmp) == TP_NUMBER) {
            b = tp_number_val(tmp);
        } else if (obj_type(tmp) == TP_NONE) {
            b = l;
        } else {
            tp_raise(None,"%s is not a number",STR(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, &tp_list_val(self)->items[a]);
        } else if (type == TP_STRING) {
            tp_obj r = tp_string_t(tp, b-a);
            char *ptr = tp_str_val(r);
            memcpy(ptr,tp_str_val(self)+a,b-a);
            ptr[b-a]=0;
            return tp_track(tp,r);
        }
    }

    tp_raise(None,"tp_get(%s,%s)",STR(self),STR(k));
}