Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1913 | jaeger | 1 | tp_obj tp_print(TP) { |
2 | int n = 0; |
||
3 | tp_obj e; |
||
4 | TP_LOOP(e) |
||
5 | if (n) { con_printf(" "); } |
||
6 | con_printf("%s",TP_CSTR(e)); |
||
7 | n += 1; |
||
8 | TP_END; |
||
9 | con_printf("\n"); |
||
10 | return tp_None; |
||
11 | } |
||
12 | |||
13 | tp_obj tp_bind(TP) { |
||
14 | tp_obj r = TP_OBJ(); |
||
15 | tp_obj self = TP_OBJ(); |
||
16 | return tp_fnc_new(tp,r.fnc.ftype|2,r.fnc.val,self,r.fnc.info->globals); |
||
17 | } |
||
18 | |||
19 | tp_obj tp_min(TP) { |
||
20 | tp_obj r = TP_OBJ(); |
||
21 | tp_obj e; |
||
22 | TP_LOOP(e) |
||
23 | if (tp_cmp(tp,r,e) > 0) { r = e; } |
||
24 | TP_END; |
||
25 | return r; |
||
26 | } |
||
27 | |||
28 | tp_obj tp_syscall(TP) { |
||
29 | tp_obj r = TP_OBJ(); |
||
30 | int result; |
||
31 | asm("int $0x40":"=a"(result): |
||
32 | "a"((int)tp_get(tp, r, tp_string_n("eax", 3)).number.val), |
||
33 | "b"((int)tp_get(tp, r, tp_string_n("ebx", 3)).number.val), |
||
34 | "c"((int)tp_get(tp, r, tp_string_n("ecx", 3)).number.val), |
||
35 | "d"((int)tp_get(tp, r, tp_string_n("edx", 3)).number.val)); |
||
36 | return tp_number(result); |
||
37 | } |
||
38 | |||
39 | tp_obj tp_max(TP) { |
||
40 | tp_obj r = TP_OBJ(); |
||
41 | tp_obj e; |
||
42 | TP_LOOP(e) |
||
43 | if (tp_cmp(tp,r,e) < 0) { r = e; } |
||
44 | TP_END; |
||
45 | return r; |
||
46 | } |
||
47 | |||
48 | tp_obj tp_copy(TP) { |
||
49 | tp_obj r = TP_OBJ(); |
||
50 | int type = r.type; |
||
51 | if (type == TP_LIST) { |
||
52 | return _tp_list_copy(tp,r); |
||
53 | } else if (type == TP_DICT) { |
||
54 | return _tp_dict_copy(tp,r); |
||
55 | } |
||
56 | tp_raise(tp_None,"tp_copy(%s)",TP_CSTR(r)); |
||
57 | } |
||
58 | |||
59 | |||
60 | tp_obj tp_len_(TP) { |
||
61 | tp_obj e = TP_OBJ(); |
||
62 | return tp_len(tp,e); |
||
63 | } |
||
64 | |||
65 | |||
66 | tp_obj tp_assert(TP) { |
||
67 | int a = TP_NUM(); |
||
68 | if (a) { return tp_None; } |
||
69 | tp_raise(tp_None,"%s","assert failed"); |
||
70 | } |
||
71 | |||
72 | tp_obj tp_range(TP) { |
||
73 | int a,b,c,i; |
||
74 | tp_obj r = tp_list(tp); |
||
75 | switch (tp->params.list.val->len) { |
||
76 | case 1: a = 0; b = TP_NUM(); c = 1; break; |
||
77 | case 2: |
||
78 | case 3: a = TP_NUM(); b = TP_NUM(); c = TP_DEFAULT(tp_number(1)).number.val; break; |
||
79 | default: return r; |
||
80 | } |
||
81 | if (c != 0) { |
||
82 | for (i=a; (c>0) ? ib; i+=c) { |
||
83 | _tp_list_append(tp,r.list.val,tp_number(i)); |
||
84 | } |
||
85 | } |
||
86 | return r; |
||
87 | } |
||
88 | |||
89 | |||
90 | tp_obj tp_system(TP) { |
||
91 | char const *s = TP_STR(); |
||
92 | int r = system(s); |
||
93 | return tp_number(r); |
||
94 | } |
||
95 | |||
96 | tp_obj tp_istype(TP) { |
||
97 | tp_obj v = TP_OBJ(); |
||
98 | char const *t = TP_STR(); |
||
99 | if (strcmp("string",t) == 0) { return tp_number(v.type == TP_STRING); } |
||
100 | if (strcmp("list",t) == 0) { return tp_number(v.type == TP_LIST); } |
||
101 | if (strcmp("dict",t) == 0) { return tp_number(v.type == TP_DICT); } |
||
102 | if (strcmp("number",t) == 0) { return tp_number(v.type == TP_NUMBER); } |
||
103 | tp_raise(tp_None,"is_type(%s,%s)",TP_CSTR(v),t); |
||
104 | } |
||
105 | |||
106 | |||
107 | tp_obj tp_float(TP) { |
||
108 | tp_obj v = TP_OBJ(); |
||
109 | int ord = TP_DEFAULT(tp_number(0)).number.val; |
||
110 | int type = v.type; |
||
111 | if (type == TP_NUMBER) { return v; } |
||
112 | if (type == TP_STRING) { |
||
113 | if (strchr(TP_CSTR(v),'.')) { return tp_number(atof(TP_CSTR(v))); } |
||
114 | return(tp_number(strtol(TP_CSTR(v),0,ord))); |
||
115 | } |
||
116 | tp_raise(tp_None,"tp_float(%s)",TP_CSTR(v)); |
||
117 | } |
||
118 | |||
119 | |||
120 | tp_obj tp_save(TP) { |
||
121 | char const *fname = TP_STR(); |
||
122 | tp_obj v = TP_OBJ(); |
||
123 | FILE *f; |
||
124 | f = fopen(fname,"wb"); |
||
125 | if (!f) { tp_raise(tp_None,"tp_save(%s,...)",fname); } |
||
126 | fwrite(v.string.val,v.string.len,1,f); |
||
127 | fclose(f); |
||
128 | return tp_None; |
||
129 | } |
||
130 | |||
131 | #define READ_BLK 1024 |
||
132 | tp_obj tp_load(TP) { |
||
133 | FILE *f; |
||
134 | long l = 0; |
||
135 | tp_obj r; |
||
136 | char *s; |
||
137 | char const *fname = TP_STR(); |
||
138 | long rd = 0; |
||
139 | long allocated = 32 * READ_BLK; |
||
140 | |||
141 | |||
142 | if (!(f = fopen(fname, "rb"))) { |
||
143 | tp_raise(tp_None,"tp_load(%s)",fname); |
||
144 | } |
||
145 | if (!(s = malloc(allocated))) |
||
146 | tp_raise(tp_None, "tp_load(%s): out of memory", fname); |
||
147 | rd = 0; |
||
148 | do { |
||
149 | if (l + READ_BLK < allocated) { |
||
150 | allocated += READ_BLK; |
||
151 | if (!(s = realloc(s, allocated))) { |
||
152 | tp_raise(tp_None, "tp_load(%s): out of memory", fname); |
||
153 | } |
||
154 | } |
||
155 | rd = fread(s + l, 1, READ_BLK, f); |
||
156 | l += rd; |
||
157 | } while (rd == READ_BLK); |
||
158 | r = tp_string_n(s, l); |
||
159 | fclose(f); |
||
160 | return tp_track(tp,r); |
||
161 | } |
||
162 | |||
163 | |||
164 | tp_obj tp_fpack(TP) { |
||
165 | tp_num v = TP_NUM(); |
||
166 | tp_obj r = tp_string_t(tp,sizeof(tp_num)); |
||
167 | *(tp_num*)r.string.val = v; |
||
168 | return tp_track(tp,r); |
||
169 | } |
||
170 | |||
171 | tp_obj tp_abs(TP) { |
||
172 | return tp_number(fabs(tp_float(tp).number.val)); |
||
173 | } |
||
174 | tp_obj tp_int(TP) { |
||
175 | return tp_number((long)tp_float(tp).number.val); |
||
176 | } |
||
177 | tp_num _roundf(tp_num v) { |
||
178 | tp_num av = fabs(v); tp_num iv = (long)av; |
||
179 | av = (av-iv < 0.5?iv:iv+1); |
||
180 | return (v<0?-av:av); |
||
181 | } |
||
182 | tp_obj tp_round(TP) { |
||
183 | return tp_number(_roundf(tp_float(tp).number.val)); |
||
184 | } |
||
185 | |||
186 | tp_obj tp_exists(TP) { |
||
187 | char const *s = TP_STR(); |
||
188 | struct stat stbuf; |
||
189 | return tp_number(!stat(s,&stbuf)); |
||
190 | } |
||
191 | tp_obj tp_mtime(TP) { |
||
192 | char const *s = TP_STR(); |
||
193 | struct stat stbuf; |
||
194 | if (!stat(s,&stbuf)) { return tp_number(stbuf.st_mtime); } |
||
195 | tp_raise(tp_None,"tp_mtime(%s)",s); |
||
196 | }0?-av:av); |