-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.h
425 lines (356 loc) · 11 KB
/
types.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#ifndef __TYPES__
#define __TYPES__
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <setjmp.h>
#include <sys/types.h>
#ifdef CORU_USE
# include <coru.h>
#else
# include <pcl.h>
#endif /* CORU_USE */
#include <gmp.h>
#include "cell.h"
#include "hash.h"
#include "array.h"
#include "bulk.h"
#include "binbulk.h"
#include "t_gc.h"
#define BOOL 0 /* bool */
#define SYMBOL 1 /* symbol */
#define REF 2 /* reference (e.g. $XX) */
#define LIST 3 /* list (e.g. (...)) */
#define INTEGER 4 /* big integer */
#define REAL 5 /* 64bit real */
#define STRING 6 /* string (e.g. "...") */
#define SCRIPT 7 /* script is some statements */
#define STATEMENT 8 /* statement (e.g. ...;) */
#define BLOCK 9 /* block (e.g. {...} */
#define EVAL 10 /* eval (e.g. [...] */
#define NATIVE 11 /* C native code (e.g. <NATIVE>) */
#define OBJECT 12 /* a object */
#define EXCEPTION 13 /* throwable object */
#define CLOSURE 14 /* closure */
#define FUNC 15 /* function object */
#define CONTROL 16 /* control object (e.g. return, break, or continue) */
#define CONTAINER 17 /* C object container */
#define GETMACRO 18 /* GET Macro */
#define ALIAS 19 /* variable alias */
#define RQUOTE 20 /* reguler expresion string (e.g. '...') */
#define INITMACRO 21 /* INIT Macro */
#define BIND 22 /* bind var list (e.g. | ... |) */
#define DICT 23 /* dictionary (as primitive hash) */
#define VECTOR 24 /* vector (as primitive array) */
#define COROUTINE 25 /* coroutine */
#define BULK 26 /* bulk data store */
#define INTR 27 /* interrupt occurred */
#define __TYPE_LAST__ 28
#define FALSE 0 /* BOOL value, FALSE */
#define TRUE 1 /* BOOL value, TRUE */
#define S_NIL L"<nil>"
#define S_T L"<t>"
#define INTR_ITIMER 0 /* itimer interrupt */
#define TAG_MASK (0x000000ff)
#define GET_TAG(p) ((p==NULL) ? -1 : (TAG_MASK & (p->tag)))
#define IS_LIST_NULL(l) ((l==NULL) ? 1 : ((NULL==l->u.list.item)&&(NULL==l->u.list.nextp)))
#define IS_NIL(x) ((GET_TAG(x)==BOOL) ? (x->u.tbool.value==FALSE) : 0)
#define TAG_NAMED_MASK (0x00008000)
#define TAG_SWITCH_MASK (0x00004000)
#define IS_NAMED_SYM(p) ((p==NULL) ? 0 : (TAG_NAMED_MASK & (p->tag)))
#define IS_SWITCH_SYM(p) ((p==NULL) ? 0 : (TAG_SWITCH_MASK & (p->tag)))
#define SET_SCRIPT_ID(p,i) (p->tag|=((i<<16)&0xffff0000))
#define GET_SCRIPT_ID(p) (int)((p->tag&0xffff0000)>>16)
#define TAG_LAZY_MASK (0x00002000)
#define SET_LAZY(p) (p->tag|=TAG_LAZY_MASK)
#define CLEAR_LAZY(p) (p->tag&=(~TAG_LAZY_MASK))
#define IS_LAZY(p) (p->tag&TAG_LAZY_MASK)
#define TAG_NOPRINTABLE (0x00000200)
#define SET_NOPRINTABLE(p) (p->tag|=TAG_NOPRINTABLE)
#define IS_NOPRINTABLE(p) (p->tag&TAG_NOPRINTABLE)
#define TAG_PARAMNO_MASK (0x00001C00)
#define TAG_MAX_PARAMNO (7)
#define SET_PARAMNO(p,i) (p->tag|=((i<<10)&TAG_PARAMNO_MASK))
#define GET_PARAMNO(p) (int)((p->tag&TAG_PARAMNO_MASK)>>10)
#define CLEAR_PARAMNO(p) (p->tag&=(~TAG_PARAMNO_MASK))
#define IS_TOY_OBJECT(p) ((GET_TAG(p)>=BOOL)&&(GET_TAG(p)<__TYPE_LAST__))
#define SELF_HASH(i) (i->obj_stack[i->cur_obj_stack]->cur_object_slot)
#define SELF(i) (i->obj_stack[i->cur_obj_stack]->self)
#define SELF_OBJ(i) (i->obj_stack[i->cur_obj_stack]->cur_object)
#define GET_FUNC_ENV(p,i) ((((p->cur_func_stack)-i)<0) ? NULL : p->func_stack[(p->cur_func_stack)-i])
typedef struct _toy_func_trace_info {
int line;
struct _toy_type *object_name;
struct _toy_type *func_name_caller;
struct _toy_type *func_name;
struct _toy_type *statement;
} Toy_Func_Trace_Info;
typedef struct _toy_func_env {
Hash *localvar;
struct _toy_func_env *upstack;
struct _toy_type *tobe_bind_val;
struct _toy_func_trace_info *trace_info;
int script_id;
} Toy_Func_Env;
typedef struct _toy_obj_env {
struct _hash *cur_object_slot;
struct _toy_type *cur_object;
struct _toy_type *self;
} Toy_Obj_Env;
typedef struct _toy_env {
Toy_Func_Env *func_env;
Toy_Obj_Env *object_env;
struct _toy_type *tobe_bind_val;
} Toy_Env;
struct _toy_argspec {
struct _toy_type *list;
int posarg_len;
Array *posarg_array;
Hash *namedarg;
};
typedef struct _toy_interp {
/* interpriter name */
wchar_t *name;
/* stack size */
int stack_size;
/* some stacks */
int cur_func_stack;
Toy_Func_Env **func_stack;
int cur_obj_stack;
Toy_Obj_Env **obj_stack;
/* world */
struct _hash *funcs;
struct _hash *classes;
struct _hash *globals;
struct _hash *scripts;
/* running script id */
int script_id;
/* trace info for command function, this member only use in
command functions (in commands.c and methods.c). */
Toy_Func_Trace_Info *trace_info;
/* trace flag */
int trace;
int trace_fd;
/* debug flag */
int debug;
int debug_in;
/* coroutine */
int cstack_id;
void *cstack;
int cstack_size;
#ifdef CORU_USE
coru_t *coroid;
#else
coroutine_t coroid;
#endif /* CORU_USE */
struct _toy_interp *co_parent;
struct _toy_type *co_value;
int co_calling;
/* last command status */
struct _toy_type *last_status;
/* current function */
struct _toy_type *current_func;
/* itimer enable */
int itimer_enable;
/* signal mask
* if signal_mask_enable != 0 ... signal masked
* if signal_mask_enable == 0 ... signal no masked (enable signal)
*/
int signal_mask_enable;
} Toy_Interp;
typedef struct _toy_coroutine {
Toy_Interp *interp;
struct _toy_type *script;
int state;
#ifdef CORU_USE
coru_t *coro_id;
#else
coroutine_t coro_id;
#endif /* CORU_USE */
struct _toy_type *stacktrace;
} Toy_Coroutine;
/*
* TAG format:
* FEDCBA98 76543210
* +--------+--------+
* +0 | script id |
* +--------+--------+
* +2 |@#$***!-|%%%%%%%%|
* +--------+--------+
*
* %: type field
* @: named arg flag
* #: switch arg flag
* $: lazy flag
* ***:
* Number of position parameters (0-6),
* if less than 7 and only position parameters.
* !: no printable
* -: reserved
*/
typedef struct _toy_type {
u_int32_t tag;
union _u {
/* BOOL */
struct _tbool {
int value;
} tbool;
/* SYMBOL */
struct _symbol {
Cell *cell;
unsigned int hash_index;
} symbol;
/* REF */
struct _ref {
Cell *cell;
unsigned int hash_index;
} ref;
/* LIST */
struct _list {
struct _toy_type *item;
struct _toy_type *nextp;
} list;
/* INTEGER */
mpz_t biginteger;
/* REAL */
double real;
/* STRING */
Cell *string;
/* SCRIPT */
struct _toy_type *statement_list;
/* STATEMENT */
struct _statement {
struct _toy_type *item_list;
struct _toy_func_trace_info *trace_info;
} statement;
/* BLOCK */
struct _toy_type *block_body; /* point to _toy_type->script */
/* EVAL */
struct _toy_type *eval_body; /* point to _toy_type->script */
/* NATIVE */
struct _native {
struct _toy_type* (*cfunc)(
Toy_Interp* interp,
struct _toy_type* posargs, /* pint to _toy_type->list */
struct _hash* namedargs,
int arglen
);
struct _toy_type *argspec_list;
} native;
/* OBJECT */
struct _object {
struct _hash *slots;
struct _toy_type *delegate_list;
} object;
/* EXCEPTION */
struct _exception {
Cell *code;
struct _toy_type *msg_list;
} exception;
/* CLOSURE */
struct _closure {
struct _toy_type *block_body; /* point to _toy_type->script */
Toy_Env *env;
} closure;
/* FUNC */
struct _func {
struct _toy_argspec *argspec;
struct _toy_type *closure;
} func;
struct _control {
u_int32_t code;
struct _toy_type *ret_value;
} control;
/* CONTAINER */
struct _container {
void *data;
Cell *desc;
} container;
/* GET Macro */
struct _getmacro {
struct _toy_type *obj;
struct _toy_type *para;
} getmacro;
/* INIT Macro */
struct _initmacro {
struct _toy_type *class;
struct _toy_type *param;
} initmacro;
/* ALIAS */
struct _alias {
struct _hash *slot;
struct _toy_type *key;
} alias;
/* RQUOTE */
Cell *rquote;
/* BIND */
struct _toy_type *bind_var;
/* DICT */
struct _hash *dict;
/* VECTOR */
struct _array *vector;
/* COROUTINE */
Toy_Coroutine *coroutine;
/* BULK */
struct _binbulk *bulk;
/* INTERUPT */
int nintr;
} u;
} Toy_Type;
typedef struct _toy_script {
int id;
Toy_Type *path;
Bulk *src;
Toy_Type *script;
} Toy_Script;
Toy_Type* new_bool(int val);
Toy_Type* new_symbol(wchar_t *atom);
Toy_Type* new_ref(wchar_t *ref);
Toy_Type* new_list(Toy_Type *item);
Toy_Type* new_cons(Toy_Type *car, Toy_Type *cdr);
Toy_Type* list_append(Toy_Type *list, Toy_Type *item);
Toy_Type* list_next(Toy_Type *list);
Toy_Type* list_get_item(Toy_Type *list);
int list_length(Toy_Type *list);
Toy_Type* new_integer(mpz_t biginteger);
Toy_Type* new_integer_si(long int integer);
Toy_Type* new_integer_ullsi(unsigned long long integer);
Toy_Type* new_integer_d(double val);
wchar_t* integer_to_str(Toy_Type *val);
Toy_Type* new_real(double real);
Toy_Type* new_string_str(wchar_t *string);
Toy_Type* new_string_cell(Cell *string);
Toy_Type* new_script(Toy_Type *statement_list);
Toy_Type* new_statement(Toy_Type *item_list, int line);
Toy_Type* new_block(Toy_Type *block_body);
Toy_Type* new_eval(Toy_Type *eval_body);
Toy_Type* new_native(Toy_Type* (*cfunc)(Toy_Interp*, Toy_Type*, struct _hash*, int arglen),
Toy_Type *argspec_list);
Toy_Type* new_object(wchar_t* name, struct _hash* slots, Toy_Type *delegate_list);
Toy_Type* new_exception(wchar_t *code, wchar_t *desc, Toy_Interp* interp);
Toy_Type* new_closure(Toy_Type *block_body, Toy_Env* env, int script_id);
Toy_Type* new_func(Toy_Type *argspec_list, int posarglen, Array *posargarray, Hash *namedarg,
Toy_Type *closure);
Toy_Type* new_control(int code, Toy_Type *ret_value);
Toy_Type* new_container(void *container, wchar_t *desc);
Toy_Type* new_getmacro(Toy_Type *obj, Toy_Type *para);
Toy_Type* new_initmacro(Toy_Type *obj, Toy_Type *param);
Toy_Type* new_alias(struct _hash *slot, Toy_Type *key);
Toy_Type* new_rquote(wchar_t *string);
Toy_Type* new_bind(Toy_Type *bind_var);
Toy_Type* new_dict(struct _hash *dict);
Toy_Type* new_vector(struct _array *vector);
Toy_Type* new_binbulk_t(struct _binbulk *bulk);
Toy_Type* new_coroutine(Toy_Interp*, Toy_Type*);
Toy_Type* new_intr(int nintr);
Toy_Type* toy_clone(Toy_Type *obj);
wchar_t* toy_get_type_str(Toy_Type *obj);
wchar_t* to_string(Toy_Type *obj);
wchar_t* to_print(Toy_Type *obj);
#define list_next(l) (((l)==NULL) ? NULL : ((GET_TAG((l))==LIST) ? (l)->u.list.nextp : NULL))
#define list_get_item(l) (((l)==NULL) ? NULL : ((GET_TAG((l))==LIST) ? (l)->u.list.item : NULL))
#define list_set_car(l,v) (((l)==NULL) ? NULL : ((GET_TAG((l))==LIST) ? (l)->u.list.item=v : NULL))
#define list_set_cdr(l,v) (((l)==NULL) ? NULL : ((GET_TAG((l))==LIST) ? (l)->u.list.nextp=v : NULL))
#define wcisspace(c) ((c < 0x80) && isspace(c))
#define wcisprint(c) (isprint(c) || (c >= 0x80))
#endif /* __TYPES__ */