1 #ifndef ML_COMPILER2_H
2 #define ML_COMPILER2_H
3 
4 #include "ml_compiler.h"
5 #include "ml_bytecode.h"
6 
7 typedef enum ml_token_t {
8 	MLT_NONE,
9 	MLT_EOL,
10 	MLT_EOI,
11 	MLT_IF,
12 	MLT_THEN,
13 	MLT_ELSEIF,
14 	MLT_ELSE,
15 	MLT_END,
16 	MLT_LOOP,
17 	MLT_WHILE,
18 	MLT_UNTIL,
19 	MLT_EXIT,
20 	MLT_NEXT,
21 	MLT_FOR,
22 	MLT_EACH,
23 	MLT_TO,
24 	MLT_IN,
25 	MLT_IS,
26 	MLT_WHEN,
27 	MLT_SWITCH,
28 	MLT_CASE,
29 	MLT_FUN,
30 	MLT_MACRO,
31 	MLT_RET,
32 	MLT_SUSP,
33 	MLT_DEBUG,
34 	MLT_METH,
35 	MLT_WITH,
36 	MLT_DO,
37 	MLT_ON,
38 	MLT_NIL,
39 	MLT_AND,
40 	MLT_OR,
41 	MLT_NOT,
42 	MLT_OLD,
43 	MLT_DEF,
44 	MLT_LET,
45 	MLT_REF,
46 	MLT_VAR,
47 	MLT_IDENT,
48 	MLT_BLANK,
49 	MLT_LEFT_PAREN,
50 	MLT_RIGHT_PAREN,
51 	MLT_LEFT_SQUARE,
52 	MLT_RIGHT_SQUARE,
53 	MLT_LEFT_BRACE,
54 	MLT_RIGHT_BRACE,
55 	MLT_SEMICOLON,
56 	MLT_COLON,
57 	MLT_COMMA,
58 	MLT_ASSIGN,
59 	MLT_IMPORT,
60 	MLT_VALUE,
61 	MLT_EXPR,
62 	MLT_INLINE,
63 	MLT_EXPAND,
64 	MLT_OPERATOR,
65 	MLT_METHOD
66 } ml_token_t;
67 
68 typedef struct mlc_function_t mlc_function_t;
69 typedef struct mlc_frame_t mlc_frame_t;
70 typedef struct mlc_loop_t mlc_loop_t;
71 typedef struct mlc_block_t mlc_block_t;
72 typedef struct mlc_try_t mlc_try_t;
73 typedef struct mlc_upvalue_t mlc_upvalue_t;
74 typedef struct mlc_define_t mlc_define_t;
75 
76 struct mlc_expr_t {
77 	void (*compile)(mlc_function_t *, mlc_expr_t *, int);
78 	mlc_expr_t *Next;
79 	const char *Source;
80 	int StartLine, EndLine;
81 };
82 
83 struct mlc_function_t {
84 	ml_state_t Base;
85 	ml_compiler_t *Compiler;
86 	mlc_frame_t *Frame;
87 	void *Limit;
88 	const char *Source;
89 	mlc_function_t *Up;
90 	mlc_block_t *Block;
91 	ml_decl_t *Decls;
92 	mlc_define_t *Defines;
93 	mlc_loop_t *Loop;
94 	mlc_try_t *Try;
95 	mlc_upvalue_t *UpValues;
96 	ml_inst_t *Next, *Returns;
97 	int Top, Size, Self, Space;
98 };
99 
100 struct mlc_define_t {
101 	mlc_define_t *Next;
102 	const char *Ident;
103 	mlc_expr_t *Expr;
104 	long Hash;
105 	ml_source_t Source;
106 };
107 
108 typedef enum {
109 	ML_EXPR_REGISTER,
110 	ML_EXPR_BLANK,
111 	ML_EXPR_NIL,
112 	ML_EXPR_VALUE,
113 	ML_EXPR_IF,
114 	ML_EXPR_OR,
115 	ML_EXPR_AND,
116 	ML_EXPR_DEBUG,
117 	ML_EXPR_NOT,
118 	ML_EXPR_LOOP,
119 	ML_EXPR_NEXT,
120 	ML_EXPR_EXIT,
121 	ML_EXPR_RETURN,
122 	ML_EXPR_SUSPEND,
123 	ML_EXPR_WITH,
124 	ML_EXPR_FOR,
125 	ML_EXPR_EACH,
126 	ML_EXPR_VAR,
127 	ML_EXPR_VAR_TYPE,
128 	ML_EXPR_VAR_IN,
129 	ML_EXPR_VAR_UNPACK,
130 	ML_EXPR_LET,
131 	ML_EXPR_LET_IN,
132 	ML_EXPR_LET_UNPACK,
133 	ML_EXPR_DEF,
134 	ML_EXPR_DEF_IN,
135 	ML_EXPR_DEF_UNPACK,
136 	ML_EXPR_BLOCK,
137 	ML_EXPR_ASSIGN,
138 	ML_EXPR_OLD,
139 	ML_EXPR_TUPLE,
140 	ML_EXPR_LIST,
141 	ML_EXPR_MAP,
142 	ML_EXPR_CALL,
143 	ML_EXPR_CONST_CALL,
144 	ML_EXPR_RESOLVE,
145 	ML_EXPR_STRING,
146 	ML_EXPR_FUN,
147 	ML_EXPR_IDENT,
148 	ML_EXPR_DEFINE,
149 	ML_EXPR_INLINE
150 } ml_expr_type_t;
151 
152 ml_expr_type_t mlc_expr_type(mlc_expr_t *Expr);
153 
154 #define MLC_EXPR_FIELDS(name) \
155 	void (*compile)(mlc_function_t *, mlc_## name ## _expr_t *, int); \
156 	mlc_expr_t *Next; \
157 	const char *Source; \
158 	int StartLine, EndLine;
159 
160 typedef struct mlc_value_expr_t mlc_value_expr_t;
161 
162 struct mlc_value_expr_t {
163 	MLC_EXPR_FIELDS(value);
164 	ml_value_t *Value;
165 };
166 
167 typedef struct mlc_if_expr_t mlc_if_expr_t;
168 typedef struct mlc_if_case_t mlc_if_case_t;
169 
170 struct mlc_if_case_t {
171 	mlc_if_case_t *Next;
172 	mlc_expr_t *Condition;
173 	mlc_expr_t *Body;
174 	const char *Ident;
175 	int Line;
176 	ml_token_t Token;
177 };
178 
179 struct mlc_if_expr_t {
180 	MLC_EXPR_FIELDS(if);
181 	mlc_if_case_t *Cases;
182 	mlc_expr_t *Else;
183 };
184 
185 typedef struct mlc_parent_expr_t mlc_parent_expr_t;
186 
187 struct mlc_parent_expr_t {
188 	MLC_EXPR_FIELDS(parent);
189 	mlc_expr_t *Child;
190 };
191 
192 typedef struct mlc_local_expr_t mlc_local_expr_t;
193 typedef struct mlc_local_t mlc_local_t;
194 
195 struct mlc_local_t {
196 	mlc_local_t *Next;
197 	const char *Ident;
198 	int Line, Index;
199 };
200 
201 struct mlc_local_expr_t {
202 	MLC_EXPR_FIELDS(local);
203 	mlc_local_t *Local;
204 	mlc_expr_t *Child;
205 	int Count, Flags;
206 };
207 
208 typedef struct mlc_for_expr_t mlc_for_expr_t;
209 
210 struct mlc_for_expr_t {
211 	MLC_EXPR_FIELDS(for);
212 	const char *Key;
213 	mlc_local_t *Local;
214 	mlc_expr_t *Child;
215 	int Unpack;
216 };
217 
218 typedef struct mlc_block_expr_t mlc_block_expr_t;
219 typedef struct mlc_catch_expr_t mlc_catch_expr_t;
220 typedef struct mlc_catch_type_t mlc_catch_type_t;
221 
222 struct mlc_block_expr_t {
223 	MLC_EXPR_FIELDS(block);
224 	mlc_local_t *Vars, *Lets, *Defs;
225 	mlc_expr_t *Child;
226 	mlc_catch_expr_t *Catches;
227 	int NumVars, NumLets, NumDefs;
228 };
229 
230 struct mlc_catch_expr_t {
231 	mlc_catch_expr_t *Next;
232 	const char *Ident;
233 	mlc_catch_type_t *Types;
234 	mlc_expr_t *Body;
235 	int Line;
236 };
237 
238 struct mlc_catch_type_t {
239 	mlc_catch_type_t *Next;
240 	const char *Type;
241 };
242 
243 typedef struct mlc_parent_value_expr_t mlc_parent_value_expr_t;
244 
245 struct mlc_parent_value_expr_t {
246 	MLC_EXPR_FIELDS(parent_value);
247 	mlc_expr_t *Child;
248 	ml_value_t *Value;
249 };
250 
251 typedef struct mlc_string_expr_t mlc_string_expr_t;
252 typedef struct mlc_string_part_t mlc_string_part_t;
253 
254 struct mlc_string_expr_t {
255 	MLC_EXPR_FIELDS(string);
256 	mlc_string_part_t *Parts;
257 };
258 
259 struct mlc_string_part_t {
260 	mlc_string_part_t *Next;
261 	union {
262 		mlc_expr_t *Child;
263 		const char *Chars;
264 	};
265 	int Length, Line;
266 };
267 
268 typedef struct mlc_fun_expr_t mlc_fun_expr_t;
269 typedef struct mlc_param_t mlc_param_t;
270 
271 struct mlc_param_t {
272 	mlc_param_t *Next;
273 	const char *Ident;
274 	mlc_expr_t *Type;
275 	int Line, Flags;
276 };
277 
278 struct mlc_fun_expr_t {
279 	MLC_EXPR_FIELDS(fun);
280 	const char *Name;
281 	mlc_param_t *Params;
282 	mlc_expr_t *Body;
283 	mlc_expr_t *ReturnType;
284 };
285 
286 typedef struct mlc_ident_expr_t mlc_ident_expr_t;
287 
288 struct mlc_ident_expr_t {
289 	MLC_EXPR_FIELDS(ident);
290 	const char *Ident;
291 };
292 
293 long ml_ident_hash(const char *Ident);
294 
295 typedef struct ml_macro_t ml_macro_t;
296 
297 struct ml_macro_t {
298 	ml_type_t *Type;
299 	void (*apply)(mlc_function_t *, ml_macro_t *, mlc_expr_t *, mlc_expr_t *, int);
300 };
301 
302 extern ml_type_t MLMacroT[];
303 
304 #define MLCF_PUSH 1
305 #define MLCF_LOCAL 2
306 #define MLCF_CONSTANT 4
307 
308 typedef void (*mlc_frame_fn)(mlc_function_t *Function, ml_value_t *Value, void *Frame);
309 
310 struct mlc_frame_t {
311 	mlc_frame_t *Next;
312 	mlc_frame_fn run;
313 	int AllowErrors, Line;
314 	void *Data[];
315 };
316 
317 void *mlc_frame_alloc(mlc_function_t *Function, size_t Size, mlc_frame_fn run);
318 
319 void mlc_expr_error(mlc_function_t *Function, mlc_expr_t *Expr, ml_value_t *Error);
320 
321 #define MLC_EXPR_ERROR(EXPR, ERROR) return mlc_expr_error(Function, (mlc_expr_t *)(EXPR), ERROR)
322 
323 #define MLC_FRAME(TYPE, RUN) TYPE *Frame = (TYPE *)mlc_frame_alloc(Function, sizeof(TYPE), (mlc_frame_fn)RUN)
324 
325 #define MLC_XFRAME(TYPE, COUNT, TYPE2, RUN) TYPE *Frame = (TYPE *)mlc_frame_alloc(Function, sizeof(TYPE) + (COUNT) * sizeof(TYPE2), (mlc_frame_fn)RUN)
326 
327 void mlc_pop(mlc_function_t *Function);
328 #define MLC_POP() mlc_pop(Function)
329 
330 void mlc_return(mlc_function_t *Function, ml_value_t *Value);
331 #define MLC_RETURN(VALUE) return mlc_return(Function, VALUE)
332 
333 #define MLC_EMIT(LINE, OPCODE, N) ml_inst_alloc(Function, LINE, OPCODE, N)
334 #define MLC_LINK(START, TARGET) mlc_fix_links(START, TARGET)
335 
336 ml_inst_t *ml_inst_alloc(mlc_function_t *Function, int Line, ml_opcode_t Opcode, int N);
337 void mlc_fix_links(ml_inst_t *Start, ml_inst_t *Target);
338 
339 void mlc_inc_top(mlc_function_t *Function);
340 void mlc_compile(mlc_function_t *Function, mlc_expr_t *Expr, int Flags);
341 
342 #endif
343