Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1913 | jaeger | 1 | #ifndef TP_H |
2 | #define TP_H |
||
3 | |||
4 | #include |
||
5 | #include |
||
6 | #ifndef __USE_ISOC99 |
||
7 | #define __USE_ISOC99 |
||
8 | #endif |
||
9 | #include |
||
10 | #include |
||
11 | #include |
||
12 | #include |
||
13 | #include |
||
14 | |||
15 | #ifdef __GNUC__ |
||
16 | #define tp_inline __inline__ |
||
17 | #endif |
||
18 | |||
19 | #ifdef _MSC_VER |
||
20 | #define tp_inline __inline |
||
21 | #endif |
||
22 | |||
23 | #ifndef tp_inline |
||
24 | #error "Unsuported compiler" |
||
25 | #endif |
||
26 | |||
27 | #define tp_malloc(x) calloc((x),1) |
||
28 | #define tp_realloc(x,y) realloc(x,y) |
||
29 | #define tp_free(x) free(x) |
||
30 | |||
31 | /* #include |
||
32 | #define tp_malloc(x) GC_MALLOC(x) |
||
33 | #define tp_realloc(x,y) GC_REALLOC(x,y) |
||
34 | #define tp_free(x)*/ |
||
35 | |||
36 | enum { |
||
37 | TP_NONE,TP_NUMBER,TP_STRING,TP_DICT, |
||
38 | TP_LIST,TP_FNC,TP_DATA, |
||
39 | }; |
||
40 | |||
41 | typedef double tp_num; |
||
42 | |||
43 | typedef struct tp_number_ { |
||
44 | int type; |
||
45 | tp_num val; |
||
46 | } tp_number_; |
||
47 | typedef struct tp_string_ { |
||
48 | int type; |
||
49 | struct _tp_string *info; |
||
50 | char const *val; |
||
51 | int len; |
||
52 | } tp_string_; |
||
53 | typedef struct tp_list_ { |
||
54 | int type; |
||
55 | struct _tp_list *val; |
||
56 | } tp_list_; |
||
57 | typedef struct tp_dict_ { |
||
58 | int type; |
||
59 | struct _tp_dict *val; |
||
60 | } tp_dict_; |
||
61 | typedef struct tp_fnc_ { |
||
62 | int type; |
||
63 | struct _tp_fnc *info; |
||
64 | int ftype; |
||
65 | void *val; |
||
66 | } tp_fnc_; |
||
67 | typedef struct tp_data_ { |
||
68 | int type; |
||
69 | struct _tp_data *info; |
||
70 | void *val; |
||
71 | int magic; |
||
72 | } tp_data_; |
||
73 | |||
74 | typedef union tp_obj { |
||
75 | int type; |
||
76 | tp_number_ number; |
||
77 | struct { int type; int *data; } gci; |
||
78 | tp_string_ string; |
||
79 | tp_dict_ dict; |
||
80 | tp_list_ list; |
||
81 | tp_fnc_ fnc; |
||
82 | tp_data_ data; |
||
83 | } tp_obj; |
||
84 | |||
85 | typedef struct _tp_string { |
||
86 | int gci; |
||
87 | char s[1]; |
||
88 | } _tp_string; |
||
89 | typedef struct _tp_list { |
||
90 | int gci; |
||
91 | tp_obj *items; |
||
92 | int len; |
||
93 | int alloc; |
||
94 | } _tp_list; |
||
95 | typedef struct tp_item { |
||
96 | int used; |
||
97 | int hash; |
||
98 | tp_obj key; |
||
99 | tp_obj val; |
||
100 | } tp_item; |
||
101 | typedef struct _tp_dict { |
||
102 | int gci; |
||
103 | tp_item *items; |
||
104 | int len; |
||
105 | int alloc; |
||
106 | int cur; |
||
107 | int mask; |
||
108 | int used; |
||
109 | } _tp_dict; |
||
110 | typedef struct _tp_fnc { |
||
111 | int gci; |
||
112 | tp_obj self; |
||
113 | tp_obj globals; |
||
114 | } _tp_fnc; |
||
115 | |||
116 | |||
117 | typedef union tp_code { |
||
118 | unsigned char i; |
||
119 | struct { unsigned char i,a,b,c; } regs; |
||
120 | struct { char val[4]; } string; |
||
121 | struct { float val; } number; |
||
122 | } tp_code; |
||
123 | |||
124 | typedef struct tp_frame_ { |
||
125 | tp_code *codes; |
||
126 | tp_code *cur; |
||
127 | tp_code *jmp; |
||
128 | tp_obj *regs; |
||
129 | tp_obj *ret_dest; |
||
130 | tp_obj fname; |
||
131 | tp_obj name; |
||
132 | tp_obj line; |
||
133 | tp_obj globals; |
||
134 | int lineno; |
||
135 | int cregs; |
||
136 | } tp_frame_; |
||
137 | |||
138 | #define TP_GCMAX 4096 |
||
139 | #define TP_FRAMES 256 |
||
140 | /* #define TP_REGS_PER_FRAME 256*/ |
||
141 | #define TP_REGS 16384 |
||
142 | typedef struct tp_vm { |
||
143 | tp_obj builtins; |
||
144 | tp_obj modules; |
||
145 | tp_frame_ frames[TP_FRAMES]; |
||
146 | tp_obj _params; |
||
147 | tp_obj params; |
||
148 | tp_obj _regs; |
||
149 | tp_obj *regs; |
||
150 | tp_obj root; |
||
151 | jmp_buf buf; |
||
152 | int jmp; |
||
153 | tp_obj ex; |
||
154 | char chars[256][2]; |
||
155 | int cur; |
||
156 | /* gc*/ |
||
157 | _tp_list *white; |
||
158 | _tp_list *grey; |
||
159 | _tp_list *black; |
||
160 | _tp_dict *strings; |
||
161 | int steps; |
||
162 | } tp_vm; |
||
163 | |||
164 | #define TP tp_vm *tp |
||
165 | typedef struct _tp_data { |
||
166 | int gci; |
||
167 | void (*free)(TP,tp_obj); |
||
168 | } _tp_data; |
||
169 | |||
170 | /* NOTE: these are the few out of namespace items for convenience*/ |
||
171 | #define tp_True tp_number(1) |
||
172 | #define tp_False tp_number(0) |
||
173 | #define TP_CSTR(v) ((tp_str(tp,(v))).string.val) |
||
174 | |||
175 | extern tp_obj tp_None; |
||
176 | |||
177 | void tp_set(TP,tp_obj,tp_obj,tp_obj); |
||
178 | tp_obj tp_get(TP,tp_obj,tp_obj); |
||
179 | tp_obj tp_len(TP,tp_obj); |
||
180 | tp_obj tp_str(TP,tp_obj); |
||
181 | int tp_cmp(TP,tp_obj,tp_obj); |
||
182 | void _tp_raise(TP,tp_obj); |
||
183 | tp_obj tp_printf(TP,char const *fmt,...); |
||
184 | tp_obj tp_track(TP,tp_obj); |
||
185 | void tp_grey(TP,tp_obj); |
||
186 | |||
187 | /* __func__ __VA_ARGS__ __FILE__ __LINE__ */ |
||
188 | #define tp_raise(r,fmt,...) { \ |
||
189 | _tp_raise(tp,tp_printf(tp,fmt,__VA_ARGS__)); \ |
||
190 | return r; \ |
||
191 | } |
||
192 | #define TP_OBJ() (tp_get(tp,tp->params,tp_None)) |
||
193 | tp_inline static tp_obj tp_type(TP,int t,tp_obj v) { |
||
194 | if (v.type != t) { tp_raise(tp_None,"_tp_type(%d,%s)",t,TP_CSTR(v)); } |
||
195 | return v; |
||
196 | } |
||
197 | #define TP_TYPE(t) tp_type(tp,t,TP_OBJ()) |
||
198 | #define TP_NUM() (TP_TYPE(TP_NUMBER).number.val) |
||
199 | #define TP_STR() (TP_CSTR(TP_TYPE(TP_STRING))) |
||
200 | #define TP_DEFAULT(d) (tp->params.list.val->len?tp_get(tp,tp->params,tp_None):(d)) |
||
201 | #define TP_LOOP(e) \ |
||
202 | int __l = tp->params.list.val->len; \ |
||
203 | int __i; for (__i=0; __i<__l; __i++) { \ |
||
204 | (e) = _tp_list_get(tp,tp->params.list.val,__i,"TP_LOOP"); |
||
205 | #define TP_END \ |
||
206 | } |
||
207 | |||
208 | tp_inline static int _tp_min(int a, int b) { return (a |
||
209 | tp_inline static int _tp_max(int a, int b) { return (a>b?a:b); } |
||
210 | tp_inline static int _tp_sign(tp_num v) { return (v<0?-1:(v>0?1:0)); } |
||
211 | |||
212 | tp_inline static tp_obj tp_number(tp_num v) { |
||
213 | tp_obj val = {TP_NUMBER}; |
||
214 | val.number.val = v; |
||
215 | return val; |
||
216 | } |
||
217 | |||
218 | tp_inline static tp_obj tp_string(char const *v) { |
||
219 | tp_obj val; |
||
220 | tp_string_ s = {TP_STRING, 0, v, 0}; |
||
221 | s.len = strlen(v); |
||
222 | val.string = s; |
||
223 | return val; |
||
224 | } |
||
225 | |||
226 | tp_inline static tp_obj tp_string_n(char const *v,int n) { |
||
227 | tp_obj val; |
||
228 | tp_string_ s = {TP_STRING, 0,v,n}; |
||
229 | val.string = s; |
||
230 | return val; |
||
231 | } |
||
232 | |||
233 | #endif0?-1:(v> |