-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuiltins.h
330 lines (295 loc) · 8.3 KB
/
Builtins.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#if 1
inline tp_obj tp_print(tp_vm *tp) {
int n = 0;
tp_obj e;
TP_LOOP(e)
if (n) { printf(" "); }
tp_echo(tp, e);
n += 1;
TP_END;
printf("\n");
return tp_None;
}
inline tp_obj tp_bind(tp_vm *tp) {
tp_obj r = TP_TYPE(TP_FNC);
tp_obj self = TP_OBJ();
return tp_fnc_new(tp,
r.fnc.ftype | 2, r.fnc.cfnc, r.fnc.info->code,
self, r.fnc.info->globals);
}
inline tp_obj tp_min(tp_vm *tp) {
tp_obj r = TP_OBJ();
tp_obj e;
TP_LOOP(e)
if (tp_cmp(tp, r, e) > 0) { r = e; }
TP_END;
return r;
}
inline tp_obj tp_max(tp_vm *tp) {
tp_obj r = TP_OBJ();
tp_obj e;
TP_LOOP(e)
if (tp_cmp(tp, r, e) < 0) { r = e; }
TP_END;
return r;
}
inline tp_obj tp_copy(tp_vm *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_string("(tp_copy) TypeError: ?"));
}
inline tp_obj tp_len_(tp_vm *tp) {
tp_obj e = TP_OBJ();
return tp_len(tp, e);
}
inline tp_obj tp_assert(tp_vm *tp) {
int a = TP_NUM();
if (a) { return tp_None; }
tp_raise(tp_None, tp_string("(tp_assert) AssertionError"));
}
inline tp_obj tp_range(tp_vm *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;
}
/* Function: tp_system
*
* The system builtin. A grave security flaw. If your version of tinypy
* enables this, you better remove it before deploying your app :P
*/
inline tp_obj tp_system(tp_vm *tp)
{
char s[256];
tp_cstr(tp, TP_STR(), s, 256);
int r = system(s);
return tp_number(r);
}
inline tp_obj tp_istype(tp_vm *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: ?"));
}
inline tp_obj tp_float(tp_vm *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: ?"));
}
inline tp_obj tp_save(tp_vm *tp) {
char fname[256]; tp_cstr(tp, TP_STR(), fname, 256);
tp_obj v = TP_OBJ();
FILE *f;
f = fopen(fname, "wb");
if (!f) { tp_raise(tp_None, tp_string("(tp_save) IOError: ?")); }
fwrite(v.string.val, v.string.len, 1, f);
fclose(f);
return tp_None;
}
inline tp_obj tp_load(tp_vm *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);
}
inline tp_obj tp_fpack(tp_vm *tp) {
double v = TP_NUM();
tp_obj r = tp_string_t(tp, sizeof(double));
*(double*)r.string.val = v;
return tp_track(tp, r);
}
inline tp_obj tp_abs(tp_vm *tp) {
return tp_number(fabs(tp_float(tp).number.val));
}
inline tp_obj tp_int(tp_vm *tp) {
return tp_number((long)tp_float(tp).number.val);
}
inline double _roundf(double v) {
double av = fabs(v); double iv = (long)av;
av = (av - iv < 0.5 ? iv : iv + 1);
return (v < 0 ? -av : av);
}
inline tp_obj tp_round(tp_vm *tp) {
return tp_number(_roundf(tp_float(tp).number.val));
}
inline tp_obj tp_exists(tp_vm *tp) {
char fname[256];
tp_cstr(tp, TP_STR(), fname, 256);
struct stat stbuf;
return tp_number(!stat(fname, &stbuf));
}
inline tp_obj tp_mtime(tp_vm *tp) {
char fname[256];
tp_cstr(tp, TP_STR(), fname, 256);
struct stat stbuf;
if (!stat(fname, &stbuf)) { return tp_number(stbuf.st_mtime); }
tp_raise(tp_None, tp_string("(tp_mtime) IOError: ?"));
}
inline int _tp_lookup_(tp_vm *tp, tp_obj self, tp_obj k, tp_obj *meta, int depth) {
int n = _tp_dict_find(tp, self.dict.val, k);
if (n != -1) {
*meta = self.dict.val->items[n].val;
return 1;
}
depth--; if (!depth) { tp_raise(0, tp_string("(tp_lookup) RuntimeError: maximum lookup depth exceeded")); }
if (self.dict.dtype && self.dict.val->meta.type == TP_DICT && _tp_lookup_(tp, self.dict.val->meta, k, meta, depth)) {
if (self.dict.dtype == 2 && meta->type == TP_FNC) {
*meta = tp_fnc_new(tp, meta->fnc.ftype | 2,
meta->fnc.cfnc, meta->fnc.info->code,
self, meta->fnc.info->globals);
}
return 1;
}
return 0;
}
inline int _tp_lookup(tp_vm *tp, tp_obj self, tp_obj k, tp_obj *meta) {
return _tp_lookup_(tp, self, k, meta, 8);
}
#define TP_META_BEGIN(self,name) \
if (self.dict.dtype == 2) { \
tp_obj meta; if (_tp_lookup(tp,self,tp_string(name),&meta)) {
#define TP_META_END \
} \
}
/* Function: tp_setmeta
* Set a "dict's meta".
*
* This is a builtin function, so you need to use <tp_params> to provide the
* parameters.
*
* In tinypy, each dictionary can have a so-called "meta" dictionary attached
* to it. When dictionary attributes are accessed, but not present in the
* dictionary, they instead are looked up in the meta dictionary. To get the
* raw dictionary, you can use <tp_getraw>.
*
* This function is particulary useful for objects and classes, which are just
* special dictionaries created with <tp_object> and <tp_class>. There you can
* use tp_setmeta to change the class of the object or parent class of a class.
*
* Parameters:
* self - The dictionary for which to set a meta.
* meta - The meta dictionary.
*
* Returns:
* None
*/
inline tp_obj tp_setmeta(tp_vm *tp) {
tp_obj self = TP_TYPE(TP_DICT);
tp_obj meta = TP_TYPE(TP_DICT);
self.dict.val->meta = meta;
return tp_None;
}
inline tp_obj tp_getmeta(tp_vm *tp) {
tp_obj self = TP_TYPE(TP_DICT);
return self.dict.val->meta;
}
/* Function: tp_object
* Creates a new object.
*
* Returns:
* The newly created object. The object initially has no parent class, use
* <tp_setmeta> to set a class. Also see <tp_object_new>.
*/
inline tp_obj tp_object(tp_vm *tp) {
tp_obj self = tp_dict(tp);
self.dict.dtype = 2;
return self;
}
inline tp_obj tp_object_new(tp_vm *tp) {
tp_obj klass = TP_TYPE(TP_DICT);
tp_obj self = tp_object(tp);
self.dict.val->meta = klass;
TP_META_BEGIN(self, "__init__");
tp_call(tp, meta, tp->params);
TP_META_END;
return self;
}
inline tp_obj tp_object_call(tp_vm *tp) {
tp_obj self;
if (tp->params.list.val->len) {
self = TP_TYPE(TP_DICT);
self.dict.dtype = 2;
}
else {
self = tp_object(tp);
}
return self;
}
/* Function: tp_getraw
* Retrieve the raw dict of a dict.
*
* This builtin retrieves one dict parameter from tinypy, and returns its raw
* dict. This is very useful when implementing your own __get__ and __set__
* functions, as it allows you to directly access the attributes stored in the
* dict.
*/
inline tp_obj tp_getraw(tp_vm *tp) {
tp_obj self = TP_TYPE(TP_DICT);
self.dict.dtype = 0;
return self;
}
/* Function: tp_class
* Creates a new base class.
*
* Parameters:
* none
*
* Returns:
* A new, empty class (derived from tinypy's builtin "object" class).
*/
inline tp_obj tp_class(tp_vm *tp) {
tp_obj klass = tp_dict(tp);
klass.dict.val->meta = tp_get(tp, tp->builtins, tp_string("object"));
return klass;
}
/* Function: tp_builtins_bool
* Coerces any value to a boolean.
*/
inline tp_obj tp_builtins_bool(tp_vm *tp) {
tp_obj v = TP_OBJ();
return (tp_number(tp_bool(tp, v)));
}
#endif