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