1 #include "minilang.h"
2 #include "ml_macros.h"
3 #include "ml_compiler2.h"
4 #include "stringmap.h"
5 #include <gc/gc.h>
6 #include "ml_runtime.h"
7 #include <string.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <limits.h>
11 #include "ml_compiler2.h"
12
13 struct mlc_upvalue_t {
14 mlc_upvalue_t *Next;
15 ml_decl_t *Decl;
16 int Index;
17 };
18
19 struct mlc_try_t {
20 mlc_try_t *Up;
21 ml_inst_t *Retries;
22 int Top;
23 };
24
25 #define ML_EXPR(EXPR, TYPE, COMP) \
26 mlc_ ## TYPE ## _expr_t *EXPR = new(mlc_ ## TYPE ## _expr_t); \
27 EXPR->compile = ml_ ## COMP ## _expr_compile; \
28 EXPR->Source = Parser->Source.Name; \
29 EXPR->StartLine = EXPR->EndLine = Parser->Source.Line
30
31 #define ML_EXPR_END(EXPR) (((mlc_expr_t *)EXPR)->EndLine = Parser->Source.Line, (mlc_expr_t *)EXPR)
32
33 struct ml_parser_t {
34 ml_type_t *Type;
35 const char *Next;
36 void *Data;
37 const char *(*Read)(void *);
38 union {
39 ml_value_t *Value;
40 mlc_expr_t *Expr;
41 const char *Ident;
42 };
43 ml_source_t Source;
44 int Line;
45 ml_token_t Token;
46 jmp_buf OnError;
47 };
48
49 struct ml_compiler_t {
50 ml_type_t *Type;
51 ml_getter_t GlobalGet;
52 void *Globals;
53 stringmap_t Vars[1];
54 };
55
56 #define STRINGIFY(x) #x
57 #define TOSTRING(x) STRINGIFY(x)
58
59 extern ml_value_t *IndexMethod;
60 extern ml_value_t *SymbolMethod;
61
ml_ident_hash(const char * Ident)62 inline long ml_ident_hash(const char *Ident) {
63 long Hash = 5381;
64 while (*Ident) Hash = ((Hash << 5) + Hash) + *Ident++;
65 return Hash;
66 }
67
68 ML_TYPE(MLMacroT, (), "macro");
69
mlc_function_run(mlc_function_t * Function,ml_value_t * Value)70 static void mlc_function_run(mlc_function_t *Function, ml_value_t *Value) {
71 if (ml_is_error(Value) && !Function->Frame->AllowErrors) {
72 ml_state_t *Caller = Function->Base.Caller;
73 ml_error_trace_add(Value, (ml_source_t){Function->Source, Function->Frame->Line});
74 ML_RETURN(Value);
75 }
76 Function->Frame->run(Function, Value, Function->Frame->Data);
77 }
78
79 ML_TYPE(MLCompilerFunctionT, (MLStateT), "compiler-function");
80
ML_TYPED_FN(ml_debugger_source,MLCompilerFunctionT,mlc_function_t * Function)81 static ml_source_t ML_TYPED_FN(ml_debugger_source, MLCompilerFunctionT, mlc_function_t *Function) {
82 return (ml_source_t){Function->Source, 0};
83 }
84
85 #define FRAME_BLOCK_SIZE 384
86
mlc_return(mlc_function_t * Function,ml_value_t * Value)87 inline void mlc_return(mlc_function_t *Function, ml_value_t *Value) {
88 mlc_frame_t *Frame = Function->Frame;
89 return Frame->run(Function, Value, Frame->Data);
90 }
91
mlc_pop(mlc_function_t * Function)92 inline void mlc_pop(mlc_function_t *Function) {
93 Function->Frame = Function->Frame->Next;
94 }
95
mlc_link_frame_run(mlc_function_t * Function,ml_value_t * Value,void ** Limit)96 static void mlc_link_frame_run(mlc_function_t *Function, ml_value_t *Value, void **Limit) {
97 Function->Limit = *Limit;
98 Function->Frame = Function->Frame->Next;
99 Function->Frame->run(Function, Value, Function->Frame->Data);
100 }
101
mlc_frame_alloc(mlc_function_t * Function,size_t Size,mlc_frame_fn run)102 void *mlc_frame_alloc(mlc_function_t *Function, size_t Size, mlc_frame_fn run) {
103 size_t FrameSize = sizeof(mlc_frame_t) + Size;
104 FrameSize = (FrameSize + 7) & ~7;
105 mlc_frame_t *Frame = (mlc_frame_t *)((void *)Function->Frame - FrameSize);
106 if (!Function->Limit || (void *)Frame < Function->Limit) {
107 size_t BlockSize = Size + sizeof(mlc_frame_t) + sizeof(void *);
108 if (BlockSize < FRAME_BLOCK_SIZE) BlockSize = FRAME_BLOCK_SIZE;
109 void *Limit = GC_malloc(BlockSize);
110 size_t LinkFrameSize = sizeof(mlc_frame_t) + sizeof(void *);
111 mlc_frame_t *LinkFrame = (mlc_frame_t *)((Limit + BlockSize) - LinkFrameSize);
112 LinkFrame->Next = Function->Frame;
113 LinkFrame->run = (mlc_frame_fn)mlc_link_frame_run;
114 LinkFrame->Data[0] = Function->Limit;
115 Function->Limit = Limit;
116 Frame = (mlc_frame_t *)((void *)LinkFrame - FrameSize);
117 Frame->Next = LinkFrame;
118 } else {
119 Frame->Next = Function->Frame;
120 }
121 Frame->AllowErrors = 0;
122 Frame->run = run;
123 Function->Frame = Frame;
124 return (void *)Frame->Data;
125 }
126
ml_inst_alloc(mlc_function_t * Function,int Line,ml_opcode_t Opcode,int N)127 ml_inst_t *ml_inst_alloc(mlc_function_t *Function, int Line, ml_opcode_t Opcode, int N) {
128 int Count = N + 1;
129 if (Function->Space < Count) {
130 ml_inst_t *GotoInst = Function->Next;
131 GotoInst->Opcode = MLI_LINK;
132 GotoInst->Line = Line;
133 GotoInst[1].Inst = Function->Next = anew(ml_inst_t, 128);
134 Function->Space = 126;
135 }
136 ml_inst_t *Inst = Function->Next;
137 Function->Next += Count;
138 Function->Space -= Count;
139 Inst->Opcode = Opcode;
140 Inst->Line = Line;
141 return Inst;
142 }
143
mlc_fix_links(ml_inst_t * Start,ml_inst_t * Target)144 inline void mlc_fix_links(ml_inst_t *Start, ml_inst_t *Target) {
145 while (Start) {
146 ml_inst_t *Next = Start->Inst;
147 Start->Inst = Target;
148 Start = Next;
149 }
150 }
151
mlc_inc_top(mlc_function_t * Function)152 inline void mlc_inc_top(mlc_function_t *Function) {
153 if (++Function->Top >= Function->Size) Function->Size = Function->Top + 1;
154 }
155
mlc_compile(mlc_function_t * Function,mlc_expr_t * Expr,int Flags)156 inline void mlc_compile(mlc_function_t *Function, mlc_expr_t *Expr, int Flags) {
157 Expr->compile(Function, Expr, Flags);
158 }
159
mlc_expr_error(mlc_function_t * Function,mlc_expr_t * Expr,ml_value_t * Error)160 inline void mlc_expr_error(mlc_function_t *Function, mlc_expr_t *Expr, ml_value_t *Error) {
161 ml_error_trace_add(Error, (ml_source_t){Function->Source, Expr->StartLine});
162 ML_CONTINUE(Function->Base.Caller, Error);
163 }
164
165 typedef struct {
166 mlc_expr_t *Expr;
167 ml_closure_info_t *Info;
168 } mlc_compile_frame_t;
169
mlc_expr_call2(mlc_function_t * Function,ml_value_t * Value,mlc_compile_frame_t * Frame)170 static void mlc_expr_call2(mlc_function_t *Function, ml_value_t *Value, mlc_compile_frame_t *Frame) {
171 ml_closure_info_t *Info = Frame->Info;
172 mlc_expr_t *Expr = Frame->Expr;
173 ml_state_t *Caller = Function->Base.Caller;
174 if (Function->UpValues) {
175 ml_value_t *Error = ml_error("EvalError", "Use of non-constant value %s in constant expression", Function->UpValues->Decl->Ident);
176 ml_error_trace_add(Error, (ml_source_t){Function->Source, Expr->EndLine});
177 ML_RETURN(Error);
178 }
179 Info->Return = MLC_EMIT(Expr->EndLine, MLI_RETURN, 0);
180 MLC_LINK(Function->Returns, Info->Return);
181 Info->Halt = Function->Next;
182 Info->Source = Function->Source;
183 Info->StartLine = Expr->StartLine;
184 Info->EndLine = Expr->EndLine;
185 asprintf((char **)&Info->Name, "<%s:%d>", Info->Source, Info->StartLine);
186 Info->FrameSize = Function->Size;
187 Info->NumParams = 0;
188 MLC_POP();
189 ml_call(Caller, ml_closure(Info), 0, NULL);
190 }
191
mlc_expr_call(mlc_function_t * Parent,mlc_expr_t * Expr)192 static void mlc_expr_call(mlc_function_t *Parent, mlc_expr_t *Expr) {
193 Parent->Frame->Line = Expr->EndLine;
194 mlc_function_t *Function = new(mlc_function_t);
195 Function->Base.Type = MLCompilerFunctionT;
196 Function->Base.Caller = (ml_state_t *)Parent;
197 Function->Base.Context = Parent->Base.Context;
198 Function->Base.run = (ml_state_fn)mlc_function_run;
199 Function->Compiler = Parent->Compiler;
200 Function->Source = Parent->Source;
201 Function->Up = Parent;
202 Function->Size = 1;
203 Function->Next = anew(ml_inst_t, 128);
204 Function->Space = 126;
205 Function->Returns = NULL;
206 MLC_FRAME(mlc_compile_frame_t, mlc_expr_call2);
207 Frame->Expr = Expr;
208 Frame->Info = new(ml_closure_info_t);
209 Frame->Info->Entry = Function->Next;
210 mlc_compile(Function, Expr, 0);
211 }
212
ml_register_expr_compile(mlc_function_t * Function,mlc_expr_t * Expr,int Flags)213 static void ml_register_expr_compile(mlc_function_t *Function, mlc_expr_t *Expr, int Flags) {
214 if (Flags & MLCF_PUSH) {
215 MLC_EMIT(Expr->StartLine, MLI_PUSH, 0);
216 mlc_inc_top(Function);
217 }
218 MLC_RETURN(NULL);
219 }
220
221 extern ml_value_t MLBlank[];
222
ml_blank_expr_compile(mlc_function_t * Function,mlc_expr_t * Expr,int Flags)223 static void ml_blank_expr_compile(mlc_function_t *Function, mlc_expr_t *Expr, int Flags) {
224 ml_inst_t *Inst = MLC_EMIT(Expr->StartLine, MLI_LOAD, 1);
225 Inst[1].Value = MLBlank;
226 if (Flags & MLCF_PUSH) {
227 Inst->Opcode = MLI_LOAD_PUSH;
228 mlc_inc_top(Function);
229 }
230 MLC_RETURN(NULL);
231 }
232
ml_nil_expr_compile(mlc_function_t * Function,mlc_expr_t * Expr,int Flags)233 static void ml_nil_expr_compile(mlc_function_t *Function, mlc_expr_t *Expr, int Flags) {
234 ml_inst_t *Inst = MLC_EMIT(Expr->StartLine, MLI_NIL, 0);
235 if (Flags & MLCF_PUSH) {
236 Inst->Opcode = MLI_NIL_PUSH;
237 mlc_inc_top(Function);
238 }
239 MLC_RETURN(NULL);
240 }
241
ml_value_expr_compile(mlc_function_t * Function,mlc_value_expr_t * Expr,int Flags)242 static void ml_value_expr_compile(mlc_function_t *Function, mlc_value_expr_t *Expr, int Flags) {
243 if (Flags & MLCF_CONSTANT) MLC_RETURN(Expr->Value);
244 ml_inst_t *Inst = MLC_EMIT(Expr->StartLine, MLI_LOAD, 1);
245 Inst[1].Value = Expr->Value;
246 if (Flags & MLCF_PUSH) {
247 Inst->Opcode = MLI_LOAD_PUSH;
248 mlc_inc_top(Function);
249 }
250 MLC_RETURN(NULL);
251 }
252
253 typedef struct {
254 mlc_if_expr_t *Expr;
255 mlc_if_case_t *Case;
256 ml_decl_t *Decls;
257 ml_inst_t *Exits, *IfInst;
258 int Flags;
259 } mlc_if_expr_frame_t;
260
261 static void ml_if_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_if_expr_frame_t *Frame);
262
ml_if_expr_compile4(mlc_function_t * Function,ml_value_t * Value,mlc_if_expr_frame_t * Frame)263 static void ml_if_expr_compile4(mlc_function_t *Function, ml_value_t *Value, mlc_if_expr_frame_t *Frame) {
264 mlc_if_expr_t *Expr = Frame->Expr;
265 MLC_LINK(Frame->Exits, Function->Next);
266 if (Frame->Flags & MLCF_PUSH) {
267 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
268 mlc_inc_top(Function);
269 }
270 MLC_POP();
271 MLC_RETURN(NULL);
272 }
273
ml_if_expr_compile3(mlc_function_t * Function,ml_value_t * Value,mlc_if_expr_frame_t * Frame)274 static void ml_if_expr_compile3(mlc_function_t *Function, ml_value_t *Value, mlc_if_expr_frame_t *Frame) {
275 mlc_if_case_t *Case = Frame->Case;
276 if (Case->Ident) {
277 Function->Decls = Frame->Decls;
278 --Function->Top;
279 ml_inst_t *ExitInst = MLC_EMIT(Case->Body->EndLine, MLI_EXIT, 2);
280 ExitInst[1].Count = 1;
281 ExitInst[2].Decls = Function->Decls;
282 }
283 mlc_if_expr_t *Expr = Frame->Expr;
284 if (Case->Next || Expr->Else) {
285 //if (!(Result & MLCF_RETURN)) {
286 ml_inst_t *GotoInst = MLC_EMIT(Case->Body->EndLine, MLI_GOTO, 1);
287 GotoInst[1].Inst = Frame->Exits;
288 Frame->Exits = GotoInst + 1;
289 //}
290 }
291 Frame->IfInst[1].Inst = Function->Next;
292 if (Case->Next) {
293 Frame->Case = Case = Case->Next;
294 Function->Frame->run = (mlc_frame_fn)ml_if_expr_compile2;
295 return mlc_compile(Function, Case->Condition, 0);
296 }
297 if (Expr->Else) {
298 Function->Frame->run = (mlc_frame_fn)ml_if_expr_compile4;
299 return mlc_compile(Function, Expr->Else, 0);
300 }
301 MLC_LINK(Frame->Exits, Function->Next);
302 if (Frame->Flags & MLCF_PUSH) {
303 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
304 mlc_inc_top(Function);
305 }
306 MLC_POP();
307 MLC_RETURN(NULL);
308 }
309
ml_if_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_if_expr_frame_t * Frame)310 static void ml_if_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_if_expr_frame_t *Frame) {
311 mlc_if_case_t *Case = Frame->Case;
312 int CaseLine = Case->Condition->EndLine;
313 Frame->IfInst = MLC_EMIT(CaseLine, MLI_AND, 1);
314 if (Case->Ident) {
315 ml_decl_t *Decl = new(ml_decl_t);
316 Decl->Source.Name = Function->Source;
317 Decl->Source.Line = Case->Line;
318 Decl->Ident = Case->Ident;
319 Decl->Hash = ml_ident_hash(Case->Ident);
320 Decl->Index = Function->Top;
321 mlc_inc_top(Function);
322 Decl->Next = Function->Decls;
323 Function->Decls = Decl;
324 ml_inst_t *WithInst = MLC_EMIT(CaseLine, Case->Token == MLT_VAR ? MLI_WITH_VAR : MLI_WITH, 1);
325 WithInst[1].Decls = Function->Decls;
326 }
327 Function->Frame->run = (mlc_frame_fn)ml_if_expr_compile3;
328 return mlc_compile(Function, Case->Body, 0);
329 }
330
ml_if_expr_compile(mlc_function_t * Function,mlc_if_expr_t * Expr,int Flags)331 static void ml_if_expr_compile(mlc_function_t *Function, mlc_if_expr_t *Expr, int Flags) {
332 MLC_FRAME(mlc_if_expr_frame_t, ml_if_expr_compile2);
333 Frame->Expr = Expr;
334 Frame->Decls = Function->Decls;
335 Frame->Flags = Flags;
336 Frame->Exits = NULL;
337 Frame->IfInst = NULL;
338 mlc_if_case_t *Case = Frame->Case = Expr->Cases;
339 return mlc_compile(Function, Case->Condition, 0);
340 }
341
342 typedef struct {
343 mlc_parent_expr_t *Expr;
344 mlc_expr_t *Child;
345 int Flags, Count;
346 } mlc_parent_expr_frame_t;
347
348
349 typedef struct {
350 mlc_parent_expr_t *Expr;
351 mlc_expr_t *Child;
352 ml_inst_t *Exits;
353 int Flags;
354 } mlc_link_expr_frame_t;
355
ml_or_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_link_expr_frame_t * Frame)356 static void ml_or_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_link_expr_frame_t *Frame) {
357 mlc_expr_t *Child = Frame->Child;
358 if (Child->Next) {
359 ml_inst_t *AndInst = MLC_EMIT(Child->EndLine, MLI_OR, 1);
360 Frame->Child = Child = Child->Next;
361 AndInst[1].Inst = Frame->Exits;
362 Frame->Exits = AndInst + 1;
363 return mlc_compile(Function, Child, 0);
364 }
365 MLC_LINK(Frame->Exits, Function->Next);
366 if (Frame->Flags & MLCF_PUSH) {
367 MLC_EMIT(Child->EndLine, MLI_PUSH, 0);
368 mlc_inc_top(Function);
369 }
370 MLC_POP();
371 MLC_RETURN(NULL);
372 }
373
ml_or_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)374 static void ml_or_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
375 MLC_FRAME(mlc_link_expr_frame_t, ml_or_expr_compile2);
376 Frame->Expr = Expr;
377 Frame->Flags = Flags;
378 Frame->Exits = NULL;
379 mlc_expr_t *Child = Frame->Child = Expr->Child;
380 return mlc_compile(Function, Child, 0);
381 }
382
ml_and_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_link_expr_frame_t * Frame)383 static void ml_and_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_link_expr_frame_t *Frame) {
384 mlc_expr_t *Child = Frame->Child;
385 if (Child->Next) {
386 ml_inst_t *AndInst = MLC_EMIT(Child->EndLine, MLI_AND, 1);
387 Frame->Child = Child = Child->Next;
388 AndInst[1].Inst = Frame->Exits;
389 Frame->Exits = AndInst + 1;
390 return mlc_compile(Function, Child, 0);
391 }
392 MLC_LINK(Frame->Exits, Function->Next);
393 if (Frame->Flags & MLCF_PUSH) {
394 MLC_EMIT(Child->EndLine, MLI_PUSH, 0);
395 mlc_inc_top(Function);
396 }
397 MLC_POP();
398 MLC_RETURN(NULL);
399 }
400
ml_and_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)401 static void ml_and_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
402 MLC_FRAME(mlc_link_expr_frame_t, ml_and_expr_compile2);
403 Frame->Expr = Expr;
404 Frame->Flags = Flags;
405 Frame->Exits = NULL;
406 mlc_expr_t *Child = Frame->Child = Expr->Child;
407 return mlc_compile(Function, Child, 0);
408 }
409
ml_debug_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_link_expr_frame_t * Frame)410 static void ml_debug_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_link_expr_frame_t *Frame) {
411 Frame->Exits[1].Inst = Function->Next;
412 if (Frame->Flags & MLCF_PUSH) {
413 MLC_EMIT(Frame->Expr->EndLine, MLI_NIL_PUSH, 0);
414 mlc_inc_top(Function);
415 }
416 MLC_POP();
417 MLC_RETURN(NULL);
418 }
419
ml_debug_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)420 static void ml_debug_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
421 MLC_FRAME(mlc_link_expr_frame_t, ml_debug_expr_compile2);
422 Frame->Expr = Expr;
423 Frame->Flags = Flags;
424 Frame->Exits = MLC_EMIT(Expr->StartLine, MLI_IF_DEBUG, 1);
425 return mlc_compile(Function, Expr->Child, 0);
426 }
427
ml_not_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)428 static void ml_not_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
429 mlc_parent_expr_t *Expr = Frame->Expr;
430 MLC_EMIT(Expr->EndLine, MLI_NOT, 0);
431 if (Frame->Flags & MLCF_PUSH) {
432 MLC_EMIT(Frame->Expr->EndLine, MLI_PUSH, 0);
433 mlc_inc_top(Function);
434 }
435 MLC_POP();
436 MLC_RETURN(NULL);
437 }
438
ml_not_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)439 static void ml_not_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
440 MLC_FRAME(mlc_parent_expr_frame_t, ml_not_expr_compile2);
441 Frame->Expr = Expr;
442 Frame->Flags = Flags;
443 return mlc_compile(Function, Expr->Child, 0);
444 }
445
446 typedef struct {
447 mlc_parent_expr_t *Expr;
448 mlc_expr_t *Child;
449 ml_inst_t *Exits;
450 ml_inst_t **Insts;
451 int Flags;
452 } mlc_switch_expr_frame_t;
453
ml_switch_expr_compile3(mlc_function_t * Function,ml_value_t * Value,mlc_switch_expr_frame_t * Frame)454 static void ml_switch_expr_compile3(mlc_function_t *Function, ml_value_t *Value, mlc_switch_expr_frame_t *Frame) {
455 mlc_expr_t *Child = Frame->Child;
456 if (Child->Next) {
457 ml_inst_t *GotoInst = MLC_EMIT(Child->EndLine, MLI_GOTO, 1);
458 Frame->Child = Child = Child->Next;
459 GotoInst[1].Inst = Frame->Exits;
460 Frame->Exits = GotoInst + 1;
461 *Frame->Insts++ = Function->Next;
462 return mlc_compile(Function, Child, 0);
463 }
464 MLC_LINK(Frame->Exits, Function->Next);
465 if (Frame->Flags & MLCF_PUSH) {
466 MLC_EMIT(Child->EndLine, MLI_PUSH, 0);
467 mlc_inc_top(Function);
468 }
469 MLC_POP();
470 MLC_RETURN(NULL);
471 }
472
ml_switch_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_switch_expr_frame_t * Frame)473 static void ml_switch_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_switch_expr_frame_t *Frame) {
474 mlc_expr_t *Child = Frame->Child;
475 int Count = 0;
476 for (mlc_expr_t *Next = Child->Next; Next; Next = Next->Next) ++Count;
477 ml_inst_t *SwitchInst = MLC_EMIT(Child->EndLine, MLI_SWITCH, 2);
478 SwitchInst[1].Count = Count;
479 Frame->Insts = SwitchInst[2].Insts = anew(ml_inst_t *, Count);
480 Frame->Child = Child = Child->Next;
481 Function->Frame->run = (mlc_frame_fn)ml_switch_expr_compile3;
482 *Frame->Insts++ = Function->Next;
483 return mlc_compile(Function, Child, 0);
484 }
485
ml_switch_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)486 static void ml_switch_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
487 MLC_FRAME(mlc_switch_expr_frame_t, ml_switch_expr_compile2);
488 Frame->Expr = Expr;
489 Frame->Flags = Flags;
490 Frame->Exits = NULL;
491 mlc_expr_t *Child = Frame->Child = Expr->Child;
492 return mlc_compile(Function, Child, 0);
493 }
494
495 struct mlc_loop_t {
496 mlc_loop_t *Up;
497 mlc_try_t *Try;
498 ml_decl_t *Decls;
499 ml_inst_t *Nexts, *Exits;
500 int NextTop, ExitTop;
501 };
502
503 typedef struct {
504 mlc_parent_expr_t *Expr;
505 ml_inst_t *Next;
506 mlc_loop_t Loop[1];
507 int Flags;
508 } mlc_loop_frame_t;
509
ml_loop_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_loop_frame_t * Frame)510 static void ml_loop_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_loop_frame_t *Frame) {
511 mlc_parent_expr_t *Expr = Frame->Expr;
512 MLC_LINK(Frame->Loop->Nexts, Frame->Next);
513 ml_inst_t *GotoInst = MLC_EMIT(Expr->EndLine, MLI_GOTO, 1);
514 GotoInst[1].Inst = Frame->Next;
515 MLC_LINK(Frame->Loop->Exits, Function->Next);
516 Function->Loop = Frame->Loop->Up;
517 if (Frame->Flags & MLCF_PUSH) {
518 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
519 mlc_inc_top(Function);
520 }
521 MLC_POP();
522 MLC_RETURN(NULL);
523 }
524
ml_loop_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)525 static void ml_loop_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
526 MLC_FRAME(mlc_loop_frame_t, ml_loop_expr_compile2);
527 Frame->Expr = Expr;
528 Frame->Flags = Flags;
529 Frame->Loop->Up = Function->Loop;
530 Frame->Loop->Try = Function->Try;
531 Frame->Loop->Decls = Function->Decls;
532 Frame->Loop->Exits = NULL;
533 Frame->Loop->Nexts = NULL;
534 Frame->Loop->ExitTop = Function->Top;
535 Frame->Loop->NextTop = Function->Top;
536 Function->Loop = Frame->Loop;
537 Frame->Next = Function->Next;
538 return mlc_compile(Function, Expr->Child, 0);
539 }
540
ml_next_expr_compile(mlc_function_t * Function,mlc_expr_t * Expr,int Flags)541 static void ml_next_expr_compile(mlc_function_t *Function, mlc_expr_t *Expr, int Flags) {
542 mlc_loop_t *Loop = Function->Loop;
543 if (!Loop) MLC_EXPR_ERROR(Expr, ml_error("CompilerError", "next not in loop"));
544 if (Function->Try != Loop->Try) {
545 ml_inst_t *TryInst = MLC_EMIT(Expr->StartLine, MLI_TRY, 1);
546 if (Loop->Try) {
547 TryInst[1].Inst = Loop->Try->Retries;
548 Loop->Try->Retries = TryInst + 1;
549 } else {
550 TryInst[1].Inst = Function->Returns;
551 Function->Returns = TryInst + 1;
552 }
553 }
554 if (Function->Top > Loop->NextTop) {
555 ml_inst_t *ExitInst = MLC_EMIT(Expr->EndLine, MLI_EXIT, 2);
556 ExitInst[1].Count = Function->Top - Loop->NextTop;
557 ExitInst[2].Decls = Loop->Decls;
558 }
559 ml_inst_t *GotoInst = MLC_EMIT(Expr->EndLine, MLI_GOTO, 1);
560 GotoInst[1].Inst = Loop->Nexts;
561 Loop->Nexts = GotoInst + 1;
562 MLC_RETURN(NULL);
563 }
564
565 typedef struct {
566 mlc_parent_expr_t *Expr;
567 mlc_loop_t *Loop;
568 mlc_try_t *Try;
569 int Flags;
570 } mlc_exit_expr_frame_t;
571
ml_exit_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_exit_expr_frame_t * Frame)572 static void ml_exit_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_exit_expr_frame_t *Frame) {
573 mlc_parent_expr_t *Expr = Frame->Expr;
574 mlc_loop_t *Loop = Frame->Loop;
575 Function->Loop = Loop;
576 Function->Try = Frame->Try;
577 if (Function->Top > Loop->ExitTop) {
578 ml_inst_t *ExitInst = MLC_EMIT(Expr->EndLine, MLI_EXIT, 2);
579 ExitInst[1].Count = Function->Top - Loop->ExitTop;
580 ExitInst[2].Decls = Loop->Decls;
581 }
582 ml_inst_t *GotoInst = MLC_EMIT(Expr->EndLine, MLI_GOTO, 1);
583 GotoInst[1].Inst = Loop->Exits;
584 Loop->Exits = GotoInst + 1;
585 if (Frame->Flags & MLCF_PUSH) {
586 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
587 mlc_inc_top(Function);
588 }
589 MLC_POP();
590 MLC_RETURN(NULL);
591 }
592
ml_exit_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)593 static void ml_exit_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
594 MLC_FRAME(mlc_exit_expr_frame_t, ml_exit_expr_compile2);
595 Frame->Expr = Expr;
596 Frame->Flags = Flags;
597 mlc_loop_t *Loop = Frame->Loop = Function->Loop;
598 if (!Loop) MLC_EXPR_ERROR(Expr, ml_error("CompilerError", "exit not in loop"));
599 if (Function->Try != Loop->Try) {
600 ml_inst_t *TryInst = MLC_EMIT(Expr->StartLine, MLI_TRY, 1);
601 if (Loop->Try) {
602 TryInst[1].Inst = Loop->Try->Retries;
603 Loop->Try->Retries = TryInst + 1;
604 } else {
605 TryInst[1].Inst = Function->Returns;
606 Function->Returns = TryInst + 1;
607 }
608 }
609 Frame->Try = Function->Try;
610 Function->Loop = Loop->Up;
611 Function->Try = Loop->Try;
612 if (Expr->Child) {
613 return mlc_compile(Function, Expr->Child, 0);
614 } else {
615 MLC_EMIT(Expr->StartLine, MLI_NIL, 0);
616 return ml_exit_expr_compile2(Function, NULL, Frame);
617 }
618 }
619
ml_return_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)620 static void ml_return_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
621 mlc_parent_expr_t *Expr = Frame->Expr;
622 MLC_EMIT(Expr->EndLine, MLI_RETURN, 0);
623 MLC_POP();
624 MLC_RETURN(NULL);
625 }
626
ml_return_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)627 static void ml_return_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
628 if (Expr->Child) {
629 MLC_FRAME(mlc_parent_expr_frame_t, ml_return_expr_compile2);
630 Frame->Expr = Expr;
631 Frame->Flags = Flags;
632 return mlc_compile(Function, Expr->Child, 0);
633 } else {
634 MLC_EMIT(Expr->StartLine, MLI_NIL, 0);
635 MLC_EMIT(Expr->EndLine, MLI_RETURN, 0);
636 MLC_RETURN(NULL);
637 }
638 }
639
ml_suspend_expr_compile3(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)640 static void ml_suspend_expr_compile3(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
641 mlc_parent_expr_t *Expr = Frame->Expr;
642 MLC_EMIT(Expr->EndLine, MLI_SUSPEND, 0);
643 MLC_EMIT(Expr->EndLine, MLI_RESUME, 0);
644 Function->Top -= 2;
645 if (Frame->Flags & MLCF_PUSH) {
646 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
647 mlc_inc_top(Function);
648 }
649 MLC_POP();
650 MLC_RETURN(NULL);
651 }
652
ml_suspend_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)653 static void ml_suspend_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
654 Function->Frame->run = (mlc_frame_fn)ml_suspend_expr_compile3;
655 mlc_compile(Function, Frame->Child, MLCF_PUSH);
656 }
657
ml_suspend_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)658 static void ml_suspend_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
659 MLC_FRAME(mlc_parent_expr_frame_t, ml_suspend_expr_compile2);
660 Frame->Expr = Expr;
661 Frame->Flags = Flags;
662 mlc_expr_t *Child = Expr->Child;
663 if (Child->Next) {
664 Frame->Child = Child->Next;
665 return mlc_compile(Function, Child, MLCF_PUSH);
666 } else {
667 Frame->Child = Child;
668 MLC_EMIT(Expr->StartLine, MLI_NIL_PUSH, 0);
669 mlc_inc_top(Function);
670 return ml_suspend_expr_compile2(Function, NULL, Frame);
671 }
672 }
673
674 typedef struct {
675 mlc_local_expr_t *Expr;
676 ml_decl_t *Decls;
677 mlc_local_t *Local;
678 mlc_expr_t *Child;
679 int Flags, Top;
680 } mlc_with_expr_frame_t;
681
ml_with_expr_compile3(mlc_function_t * Function,ml_value_t * Value,mlc_with_expr_frame_t * Frame)682 static void ml_with_expr_compile3(mlc_function_t *Function, ml_value_t *Value, mlc_with_expr_frame_t *Frame) {
683 mlc_local_expr_t *Expr = Frame->Expr;
684 ml_inst_t *ExitInst = MLC_EMIT(Expr->EndLine, MLI_EXIT, 2);
685 ExitInst[1].Count = Function->Top - Frame->Top;
686 ExitInst[2].Decls = Frame->Decls;
687 Function->Decls = Frame->Decls;
688 Function->Top = Frame->Top;
689 if (Frame->Flags & MLCF_PUSH) {
690 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
691 mlc_inc_top(Function);
692 }
693 MLC_POP();
694 MLC_RETURN(NULL);
695 }
696
ml_with_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_with_expr_frame_t * Frame)697 static void ml_with_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_with_expr_frame_t *Frame) {
698 mlc_expr_t *Child = Frame->Child;
699 mlc_local_t *Local = Frame->Local;
700 int Count = Local->Index;
701 if (Count) {
702 int I = Count;
703 do {
704 ml_decl_t *Decl = new(ml_decl_t);
705 Decl->Source.Name = Function->Source;
706 Decl->Source.Line = Local->Line;
707 Decl->Ident = Local->Ident;
708 Decl->Hash = ml_ident_hash(Local->Ident);
709 Decl->Index = Function->Top;
710 mlc_inc_top(Function);
711 Decl->Next = Function->Decls;
712 Function->Decls = Decl;
713 Local = Local->Next;
714 } while (--I > 0);
715 ml_inst_t *PushInst = MLC_EMIT(Child->EndLine, MLI_WITHX, 2);
716 PushInst[1].Count = Count;
717 PushInst[2].Decls = Function->Decls;
718 } else {
719 ml_decl_t *Decl = new(ml_decl_t);
720 Decl->Source.Name = Function->Source;
721 Decl->Source.Line = Local->Line;
722 Decl->Ident = Local->Ident;
723 Decl->Hash = ml_ident_hash(Local->Ident);
724 Decl->Index = Function->Top;
725 mlc_inc_top(Function);
726 Decl->Next = Function->Decls;
727 Function->Decls = Decl;
728 Local = Local->Next;
729 ml_inst_t *PushInst = MLC_EMIT(Child->EndLine, MLI_WITH, 1);
730 PushInst[1].Decls = Function->Decls;
731 }
732 Child = Frame->Child = Child->Next;
733 if (Local) {
734 Frame->Local = Local;
735 return mlc_compile(Function, Child, 0);
736 }
737 Function->Frame->run = (mlc_frame_fn)ml_with_expr_compile3;
738 return mlc_compile(Function, Child, 0);
739 }
740
ml_with_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)741 static void ml_with_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
742 MLC_FRAME(mlc_with_expr_frame_t, ml_with_expr_compile2);
743 Frame->Expr = Expr;
744 Frame->Flags = Flags;
745 Frame->Top = Function->Top;
746 Frame->Decls = Function->Decls;
747 mlc_expr_t *Child = Frame->Child = Expr->Child;
748 Frame->Local = Expr->Local;
749 return mlc_compile(Function, Child, 0);
750 }
751
752 typedef struct {
753 mlc_for_expr_t *Expr;
754 ml_inst_t *IterInst;
755 mlc_loop_t Loop[1];
756 int Flags;
757 } mlc_for_expr_frame_t;
758
ml_for_expr_compile4(mlc_function_t * Function,ml_value_t * Value,mlc_for_expr_frame_t * Frame)759 static void ml_for_expr_compile4(mlc_function_t *Function, ml_value_t *Value, mlc_for_expr_frame_t *Frame) {
760 mlc_for_expr_t *Expr = Frame->Expr;
761 MLC_LINK(Frame->Loop->Exits, Function->Next);
762 if (Frame->Flags & MLCF_PUSH) {
763 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
764 mlc_inc_top(Function);
765 }
766 MLC_POP();
767 MLC_RETURN(NULL);
768 }
769
ml_for_expr_compile3(mlc_function_t * Function,ml_value_t * Value,mlc_for_expr_frame_t * Frame)770 static void ml_for_expr_compile3(mlc_function_t *Function, ml_value_t *Value, mlc_for_expr_frame_t *Frame) {
771 mlc_for_expr_t *Expr = Frame->Expr;
772 ml_inst_t *ExitInst = MLC_EMIT(Expr->EndLine, MLI_EXIT, 2);
773 if (Expr->Unpack) {
774 ExitInst[1].Count = Expr->Unpack + !!Expr->Key;
775 } else {
776 ExitInst[1].Count = 1 + !!Expr->Key;
777 }
778 ExitInst[2].Decls = Frame->Loop->Decls;
779 ml_inst_t *NextInst = MLC_EMIT(Expr->StartLine, MLI_NEXT, 1);
780 NextInst[1].Inst = Frame->IterInst;
781 MLC_LINK(Frame->Loop->Nexts, NextInst);
782 Frame->IterInst[1].Inst = Function->Next;
783 Function->Loop = Frame->Loop->Up;
784 Function->Top = Frame->Loop->ExitTop;
785 Function->Decls = Frame->Loop->Decls;
786 if (Expr->Child->Next->Next) {
787 Function->Frame->run = (mlc_frame_fn)ml_for_expr_compile4;
788 return mlc_compile(Function, Expr->Child->Next->Next, 0);
789 }
790 MLC_LINK(Frame->Loop->Exits, Function->Next);
791 if (Frame->Flags & MLCF_PUSH) {
792 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
793 mlc_inc_top(Function);
794 }
795 MLC_POP();
796 MLC_RETURN(NULL);
797 }
798
ml_for_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_for_expr_frame_t * Frame)799 static void ml_for_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_for_expr_frame_t *Frame) {
800 mlc_for_expr_t *Expr = Frame->Expr;
801 mlc_expr_t *Child = Expr->Child;
802 MLC_EMIT(Child->EndLine, MLI_FOR, 0);
803 Frame->IterInst = MLC_EMIT(Child->EndLine, MLI_ITER, 1);
804 mlc_inc_top(Function);
805 if (Expr->Key) {
806 ml_decl_t *Decl = new(ml_decl_t);
807 Decl->Source.Name = Function->Source;
808 Decl->Source.Line = Expr->StartLine;
809 Decl->Ident = Expr->Key;
810 Decl->Hash = ml_ident_hash(Decl->Ident);
811 Decl->Index = Function->Top++;
812 Decl->Next = Function->Decls;
813 Function->Decls = Decl;
814 ml_inst_t *KeyInst = MLC_EMIT(Child->EndLine, MLI_KEY, 1);
815 KeyInst[1].Index = -1;
816 ml_inst_t *WithInst = MLC_EMIT(Child->EndLine, MLI_WITH, 1);
817 WithInst[1].Decls = Decl;
818 }
819 for (mlc_local_t *Local = Expr->Local; Local; Local = Local->Next) {
820 ml_decl_t *Decl = new(ml_decl_t);
821 Decl->Source.Name = Function->Source;
822 Decl->Source.Line = Local->Line;
823 Decl->Ident = Local->Ident;
824 Decl->Hash = ml_ident_hash(Local->Ident);
825 Decl->Index = Function->Top++;
826 Decl->Next = Function->Decls;
827 Function->Decls = Decl;
828 }
829 ml_inst_t *ValueInst = MLC_EMIT(Child->EndLine, MLI_VALUE, 1);
830 ValueInst[1].Index = Expr->Key ? -2 : -1;
831 if (Expr->Unpack) {
832 ml_inst_t *WithInst = MLC_EMIT(Child->EndLine, MLI_WITHX, 2);
833 WithInst[1].Count = Expr->Unpack;
834 WithInst[2].Decls = Function->Decls;
835 } else {
836 ml_inst_t *WithInst = MLC_EMIT(Child->EndLine, MLI_WITH, 1);
837 WithInst[1].Decls = Function->Decls;
838 }
839 Frame->Loop->Up = Function->Loop;
840 Frame->Loop->Try = Function->Try;
841 Frame->Loop->Exits = NULL;
842 Frame->Loop->Nexts = NULL;
843 if (Function->Top >= Function->Size) Function->Size = Function->Top + 1;
844 Function->Loop = Frame->Loop;
845 Function->Frame->run = (mlc_frame_fn)ml_for_expr_compile3;
846 return mlc_compile(Function, Child->Next, 0);
847 }
848
ml_for_expr_compile(mlc_function_t * Function,mlc_for_expr_t * Expr,int Flags)849 static void ml_for_expr_compile(mlc_function_t *Function, mlc_for_expr_t *Expr, int Flags) {
850 MLC_FRAME(mlc_for_expr_frame_t, ml_for_expr_compile2);
851 Frame->Expr = Expr;
852 Frame->Flags = Flags;
853 Frame->Loop->ExitTop = Function->Top;
854 Frame->Loop->NextTop = Function->Top + 1;
855 Frame->Loop->Decls = Function->Decls;
856 return mlc_compile(Function, Expr->Child, 0);
857 }
858
ml_each_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)859 static void ml_each_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
860 mlc_parent_expr_t *Expr = Frame->Expr;
861 mlc_expr_t *Child = Expr->Child;
862 MLC_EMIT(Child->EndLine, MLI_FOR, 0);
863 ml_inst_t *AndInst = MLC_EMIT(Child->EndLine, MLI_ITER, 1);
864 mlc_inc_top(Function);
865 ml_inst_t *ValueInst = MLC_EMIT(Child->EndLine, MLI_VALUE, 1);
866 ValueInst[1].Index = -1;
867 ml_inst_t *NextInst = MLC_EMIT(Expr->StartLine, MLI_NEXT, 1);
868 NextInst[1].Inst = AndInst;
869 AndInst[1].Inst = Function->Next;
870 if (Frame->Flags & MLCF_PUSH) {
871 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
872 mlc_inc_top(Function);
873 }
874 MLC_POP();
875 MLC_RETURN(NULL);
876 }
877
ml_each_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)878 static void ml_each_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
879 MLC_FRAME(mlc_parent_expr_frame_t, ml_each_expr_compile2);
880 Frame->Expr = Expr;
881 Frame->Flags = Flags;
882 return mlc_compile(Function, Expr->Child, 0);
883 }
884
885 struct mlc_block_t {
886 mlc_block_t *Up;
887 ml_decl_t *OldDecls;
888 mlc_block_expr_t *Expr;
889 mlc_expr_t *Child;
890 mlc_catch_expr_t *CatchExpr;
891 ml_inst_t *TryInst, *CatchInst, *Exits;
892 inthash_t DeclHashes;
893 mlc_try_t Try;
894 int Flags, Size, Top;
895 ml_decl_t *Decls[];
896 };
897
898 typedef struct {
899 mlc_local_expr_t *Expr;
900 int Flags;
901 } mlc_local_expr_frame_t;
902
ml_var_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_local_expr_frame_t * Frame)903 static void ml_var_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_local_expr_frame_t *Frame) {
904 mlc_local_expr_t *Expr = Frame->Expr;
905 mlc_local_t *Local = Expr->Local;
906 ml_decl_t *Decl = Function->Block->Decls[Local->Index];
907 ml_inst_t *VarInst = MLC_EMIT(Expr->EndLine, MLI_VAR, 1);
908 VarInst[1].Index = Function->Block->Top + Local->Index - Function->Top;
909 Decl->Flags = 0;
910 if (Frame->Flags & MLCF_PUSH) {
911 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
912 mlc_inc_top(Function);
913 }
914 MLC_POP();
915 MLC_RETURN(NULL);
916 }
917
ml_var_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)918 static void ml_var_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
919 MLC_FRAME(mlc_local_expr_frame_t, ml_var_expr_compile2);
920 Frame->Expr = Expr;
921 Frame->Flags = Flags;
922 return mlc_compile(Function, Expr->Child, 0);
923 }
924
ml_var_type_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_local_expr_frame_t * Frame)925 static void ml_var_type_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_local_expr_frame_t *Frame) {
926 mlc_local_expr_t *Expr = Frame->Expr;
927 mlc_local_t *Local = Expr->Local;
928 ml_inst_t *TypeInst = MLC_EMIT(Expr->EndLine, MLI_VAR_TYPE, 1);
929 TypeInst[1].Index = Function->Block->Top + Local->Index - Function->Top;
930 if (Frame->Flags & MLCF_PUSH) {
931 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
932 mlc_inc_top(Function);
933 }
934 MLC_POP();
935 MLC_RETURN(NULL);
936 }
937
ml_var_type_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)938 static void ml_var_type_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
939 MLC_FRAME(mlc_local_expr_frame_t, ml_var_type_expr_compile2);
940 Frame->Expr = Expr;
941 Frame->Flags = Flags;
942 return mlc_compile(Function, Expr->Child, 0);
943 }
944
ml_var_in_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_local_expr_frame_t * Frame)945 static void ml_var_in_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_local_expr_frame_t *Frame) {
946 mlc_local_expr_t *Expr = Frame->Expr;
947 mlc_local_t *Local = Expr->Local;
948 ml_decl_t **Decls = Function->Block->Decls + Local->Index;
949 for (int I = 0; I < Expr->Count; ++I, Local = Local->Next) {
950 ml_inst_t *PushInst = MLC_EMIT(Expr->EndLine, MLI_LOCAL_PUSH, 1);
951 PushInst[1].Index = Function->Top - 1;
952 mlc_inc_top(Function);
953 ml_inst_t *ValueInst = MLC_EMIT(Expr->EndLine, MLI_LOAD_PUSH, 1);
954 ValueInst[1].Value = ml_cstring(Local->Ident);
955 mlc_inc_top(Function);
956 ml_inst_t *CallInst = MLC_EMIT(Expr->EndLine, MLI_CONST_CALL, 2);
957 CallInst[1].Count = 2;
958 CallInst[2].Value = SymbolMethod;
959 Function->Top -= 2;
960 ml_decl_t *Decl = Decls[I];
961 ml_inst_t *VarInst = MLC_EMIT(Expr->EndLine, MLI_VAR, 1);
962 VarInst[1].Index = Function->Block->Top + Local->Index - Function->Top;
963 Decl->Flags = 0;
964 }
965 if (!(Frame->Flags & MLCF_PUSH)) {
966 MLC_EMIT(Expr->EndLine, MLI_POP, 0);
967 --Function->Top;
968 }
969 MLC_POP();
970 MLC_RETURN(NULL);
971 }
972
ml_var_in_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)973 static void ml_var_in_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
974 MLC_FRAME(mlc_local_expr_frame_t, ml_var_in_expr_compile2);
975 Frame->Expr = Expr;
976 Frame->Flags = Flags;
977 return mlc_compile(Function, Expr->Child, MLCF_PUSH);
978 }
979
ml_var_unpack_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_local_expr_frame_t * Frame)980 static void ml_var_unpack_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_local_expr_frame_t *Frame) {
981 mlc_local_expr_t *Expr = Frame->Expr;
982 mlc_local_t *Local = Expr->Local;
983 ml_inst_t *VarInst = MLC_EMIT(Expr->EndLine, MLI_VARX, 2);
984 VarInst[1].Index = Function->Block->Top + Local->Index - Function->Top;
985 VarInst[2].Count = Expr->Count;
986 if (Frame->Flags & MLCF_PUSH) {
987 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
988 mlc_inc_top(Function);
989 }
990 MLC_POP();
991 MLC_RETURN(NULL);
992 }
993
ml_var_unpack_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)994 static void ml_var_unpack_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
995 MLC_FRAME(mlc_local_expr_frame_t, ml_var_unpack_expr_compile2);
996 Frame->Expr = Expr;
997 Frame->Flags = Flags;
998 return mlc_compile(Function, Expr->Child, 0);
999 }
1000
ml_let_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_local_expr_frame_t * Frame)1001 static void ml_let_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_local_expr_frame_t *Frame) {
1002 mlc_local_expr_t *Expr = Frame->Expr;
1003 mlc_local_t *Local = Expr->Local;
1004 ml_decl_t *Decl = Function->Block->Decls[Local->Index];
1005 ml_inst_t *LetInst;
1006 if (Expr->Flags == MLT_REF) {
1007 if (Decl->Flags & MLC_DECL_BACKFILL) {
1008 LetInst = MLC_EMIT(Expr->EndLine, MLI_REFI, 1);
1009 } else {
1010 LetInst = MLC_EMIT(Expr->EndLine, MLI_REF, 1);
1011 }
1012 } else {
1013 if (Decl->Flags & MLC_DECL_BACKFILL) {
1014 LetInst = MLC_EMIT(Expr->EndLine, MLI_LETI, 1);
1015 } else {
1016 LetInst = MLC_EMIT(Expr->EndLine, MLI_LET, 1);
1017 }
1018 }
1019 LetInst[1].Index = Function->Block->Top + Local->Index - Function->Top;
1020 Decl->Flags = 0;
1021 if (Frame->Flags & MLCF_PUSH) {
1022 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1023 mlc_inc_top(Function);
1024 }
1025 MLC_POP();
1026 MLC_RETURN(NULL);
1027 }
1028
ml_let_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)1029 static void ml_let_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
1030 MLC_FRAME(mlc_local_expr_frame_t, ml_let_expr_compile2);
1031 Frame->Expr = Expr;
1032 Frame->Flags = Flags;
1033 return mlc_compile(Function, Expr->Child, 0);
1034 }
1035
ml_let_in_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_local_expr_frame_t * Frame)1036 static void ml_let_in_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_local_expr_frame_t *Frame) {
1037 mlc_local_expr_t *Expr = Frame->Expr;
1038 mlc_local_t *Local = Expr->Local;
1039 ml_decl_t **Decls = Function->Block->Decls + Local->Index;
1040 for (int I = 0; I < Expr->Count; ++I, Local = Local->Next) {
1041 ml_inst_t *PushInst = MLC_EMIT(Expr->EndLine, MLI_LOCAL_PUSH, 1);
1042 PushInst[1].Index = Function->Top - 1;
1043 mlc_inc_top(Function);
1044 ml_inst_t *ValueInst = MLC_EMIT(Expr->EndLine, MLI_LOAD_PUSH, 1);
1045 ValueInst[1].Value = ml_cstring(Local->Ident);
1046 mlc_inc_top(Function);
1047 ml_inst_t *CallInst = MLC_EMIT(Expr->EndLine, MLI_CONST_CALL, 2);
1048 CallInst[1].Count = 2;
1049 CallInst[2].Value = SymbolMethod;
1050 Function->Top -= 2;
1051 ml_decl_t *Decl = Decls[I];
1052 ml_inst_t *LetInst;
1053 if (Expr->Flags == MLT_REF) {
1054 if (Decl->Flags & MLC_DECL_BACKFILL) {
1055 LetInst = MLC_EMIT(Expr->EndLine, MLI_REFI, 1);
1056 } else {
1057 LetInst = MLC_EMIT(Expr->EndLine, MLI_REF, 1);
1058 }
1059 } else {
1060 if (Decl->Flags & MLC_DECL_BACKFILL) {
1061 LetInst = MLC_EMIT(Expr->EndLine, MLI_LETI, 1);
1062 } else {
1063 LetInst = MLC_EMIT(Expr->EndLine, MLI_LET, 1);
1064 }
1065 }
1066 LetInst[1].Index = Function->Block->Top + Local->Index - Function->Top;
1067 Decl->Flags = 0;
1068 }
1069 if (!(Frame->Flags & MLCF_PUSH)) {
1070 MLC_EMIT(Expr->EndLine, MLI_POP, 0);
1071 --Function->Top;
1072 }
1073 MLC_POP();
1074 MLC_RETURN(NULL);
1075 }
1076
ml_let_in_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)1077 static void ml_let_in_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
1078 MLC_FRAME(mlc_local_expr_frame_t, ml_let_in_expr_compile2);
1079 Frame->Expr = Expr;
1080 Frame->Flags = Flags;
1081 return mlc_compile(Function, Expr->Child, MLCF_PUSH);
1082 }
1083
ml_let_unpack_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_local_expr_frame_t * Frame)1084 static void ml_let_unpack_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_local_expr_frame_t *Frame) {
1085 mlc_local_expr_t *Expr = Frame->Expr;
1086 mlc_local_t *Local = Expr->Local;
1087 ml_decl_t **Decls = Function->Block->Decls + Local->Index;
1088 ml_inst_t *LetInst = MLC_EMIT(Expr->EndLine, Expr->Flags == MLT_REF ? MLI_REFX : MLI_LETX, 2);
1089 LetInst[1].Index = Function->Block->Top + Local->Index - Function->Top;
1090 LetInst[2].Count = Expr->Count;
1091 for (int I = 0; I < Expr->Count; ++I) Decls[I]->Flags = 0;
1092 if (Frame->Flags & MLCF_PUSH) {
1093 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1094 mlc_inc_top(Function);
1095 }
1096 MLC_POP();
1097 MLC_RETURN(NULL);
1098 }
1099
ml_let_unpack_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)1100 static void ml_let_unpack_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
1101 MLC_FRAME(mlc_local_expr_frame_t, ml_let_unpack_expr_compile2);
1102 Frame->Expr = Expr;
1103 Frame->Flags = Flags;
1104 return mlc_compile(Function, Expr->Child, 0);
1105 }
1106
ml_def_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_local_expr_frame_t * Frame)1107 static void ml_def_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_local_expr_frame_t *Frame) {
1108 if (ml_is_error(Value)) {
1109 ml_state_t *Caller = Function->Base.Caller;
1110 ML_RETURN(Value);
1111 }
1112 mlc_local_expr_t *Expr = Frame->Expr;
1113 mlc_local_t *Local = Expr->Local;
1114 ml_decl_t *Decl = Function->Block->Decls[Local->Index];
1115 if (Decl->Value) ml_uninitialized_set(Decl->Value, Value);
1116 Decl->Value = Value;
1117 ml_value_set_name(Value, Local->Ident);
1118 if (Frame->Flags & MLCF_PUSH) {
1119 MLC_EMIT(Expr->EndLine, MLI_NIL_PUSH, 0);
1120 mlc_inc_top(Function);
1121 }
1122 MLC_POP();
1123 MLC_RETURN(NULL);
1124 }
1125
ml_def_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)1126 static void ml_def_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
1127 MLC_FRAME(mlc_local_expr_frame_t, ml_def_expr_compile2);
1128 Frame->Expr = Expr;
1129 Frame->Flags = Flags;
1130 return mlc_expr_call(Function, Expr->Child);
1131 }
1132
1133 typedef struct {
1134 mlc_local_expr_t *Expr;
1135 mlc_local_t *Local;
1136 ml_value_t *Args[2];
1137 ml_decl_t **Decls;
1138 int Flags, Index;
1139 } mlc_def_in_expr_frame_t;
1140
ml_def_in_expr_compile3(mlc_function_t * Function,ml_value_t * Value,mlc_def_in_expr_frame_t * Frame)1141 static void ml_def_in_expr_compile3(mlc_function_t *Function, ml_value_t *Value, mlc_def_in_expr_frame_t *Frame) {
1142 if (ml_is_error(Value)) {
1143 ml_state_t *Caller = Function->Base.Caller;
1144 ML_RETURN(Value);
1145 }
1146 mlc_local_expr_t *Expr = Frame->Expr;
1147 int Index = Frame->Index;
1148 mlc_local_t *Local = Frame->Local;
1149 ml_decl_t *Decl = Frame->Decls[Index];
1150 if (Decl->Value) ml_uninitialized_set(Decl->Value, Value);
1151 Decl->Value = Value;
1152 if (++Index < Expr->Count) {
1153 Frame->Index = Index;
1154 Local = Frame->Local = Local->Next;
1155 Frame->Args[1] = ml_cstring(Local->Ident);
1156 return ml_call(Function, SymbolMethod, 2, Frame->Args);
1157 }
1158 if (Frame->Flags & MLCF_PUSH) {
1159 MLC_EMIT(Expr->EndLine, MLI_NIL_PUSH, 0);
1160 mlc_inc_top(Function);
1161 }
1162 MLC_POP();
1163 MLC_RETURN(NULL);
1164 }
1165
ml_def_in_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_def_in_expr_frame_t * Frame)1166 static void ml_def_in_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_def_in_expr_frame_t *Frame) {
1167 if (ml_is_error(Value)) {
1168 ml_state_t *Caller = Function->Base.Caller;
1169 ML_RETURN(Value);
1170 }
1171 mlc_local_expr_t *Expr = Frame->Expr;
1172 Frame->Args[0] = Value;
1173 mlc_local_t *Local = Frame->Local = Expr->Local;
1174 Frame->Index = 0;
1175 Frame->Decls = Function->Block->Decls + Local->Index;
1176 Frame->Args[1] = ml_cstring(Local->Ident);
1177 Function->Frame->run = (mlc_frame_fn)ml_def_in_expr_compile3;
1178 return ml_call(Function, SymbolMethod, 2, Frame->Args);
1179 }
1180
ml_def_in_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)1181 static void ml_def_in_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
1182 MLC_FRAME(mlc_def_in_expr_frame_t, ml_def_in_expr_compile2);
1183 Frame->Expr = Expr;
1184 Frame->Flags = Flags;
1185 return mlc_expr_call(Function, Expr->Child);
1186 }
1187
ml_def_unpack_expr_compile2(mlc_function_t * Function,ml_value_t * Packed,mlc_local_expr_frame_t * Frame)1188 static void ml_def_unpack_expr_compile2(mlc_function_t *Function, ml_value_t *Packed, mlc_local_expr_frame_t *Frame) {
1189 if (ml_is_error(Packed)) {
1190 ml_state_t *Caller = Function->Base.Caller;
1191 ML_RETURN(Packed);
1192 }
1193 mlc_local_expr_t *Expr = Frame->Expr;
1194 mlc_local_t *Local = Expr->Local;
1195 ml_decl_t **Decls = Function->Block->Decls + Local->Index;
1196 for (int I = 0; I < Expr->Count; ++I) {
1197 ml_value_t *Value = ml_unpack(Packed, I + 1);
1198 ml_decl_t *Decl = Decls[I];
1199 if (Decl->Value) ml_uninitialized_set(Decl->Value, Value);
1200 Decl->Value = Value;
1201 }
1202 if (Frame->Flags & MLCF_PUSH) {
1203 MLC_EMIT(Expr->EndLine, MLI_NIL_PUSH, 0);
1204 mlc_inc_top(Function);
1205 }
1206 MLC_POP();
1207 MLC_RETURN(NULL);
1208 }
1209
ml_def_unpack_expr_compile(mlc_function_t * Function,mlc_local_expr_t * Expr,int Flags)1210 static void ml_def_unpack_expr_compile(mlc_function_t *Function, mlc_local_expr_t *Expr, int Flags) {
1211 MLC_FRAME(mlc_local_expr_frame_t, ml_def_unpack_expr_compile2);
1212 Frame->Expr = Expr;
1213 Frame->Flags = Flags;
1214 return mlc_expr_call(Function, Expr->Child);
1215 }
1216
ml_block_expr_compile3(mlc_function_t * Function,ml_value_t * Value,mlc_block_t * Frame)1217 static void ml_block_expr_compile3(mlc_function_t *Function, ml_value_t *Value, mlc_block_t *Frame) {
1218 mlc_catch_expr_t *CatchExpr = Frame->CatchExpr;
1219 ml_inst_t *ExitInst = MLC_EMIT(CatchExpr->Body->EndLine, MLI_EXIT, 2);
1220 ExitInst[1].Count = 1;
1221 ExitInst[2].Decls = Frame->OldDecls;
1222 Function->Decls = Frame->OldDecls;
1223 Function->Top = Frame->Top;
1224 ml_inst_t *GotoInst = MLC_EMIT(CatchExpr->Body->EndLine, MLI_GOTO, 1);
1225 GotoInst[1].Inst = Frame->Exits;
1226 Frame->Exits = GotoInst + 1;
1227 if ((CatchExpr = CatchExpr->Next)) {
1228 if (Frame->CatchInst) {
1229 Frame->CatchInst[1].Inst = Function->Next;
1230 Frame->CatchInst = NULL;
1231 }
1232 if (CatchExpr->Types) {
1233 int NumTypes = 0;
1234 for (mlc_catch_type_t *Type = CatchExpr->Types; Type; Type = Type->Next) ++NumTypes;
1235 Frame->CatchInst = MLC_EMIT(CatchExpr->Line, MLI_CATCH_TYPE, 2);
1236 const char **Ptrs = Frame->CatchInst[2].Ptrs = anew(const char *, NumTypes + 1);
1237 for (mlc_catch_type_t *Type = CatchExpr->Types; Type; Type = Type->Next) *Ptrs++ = Type->Type;
1238 }
1239 ml_decl_t *Decl = new(ml_decl_t);
1240 Decl->Source.Name = Function->Source;
1241 Decl->Source.Line = CatchExpr->Line;
1242 Decl->Ident = CatchExpr->Ident;
1243 Decl->Hash = ml_ident_hash(CatchExpr->Ident);
1244 Decl->Index = Function->Top;
1245 Decl->Next = Function->Decls;
1246 Function->Decls = Decl;
1247 mlc_inc_top(Function);
1248 ml_inst_t *CatchInst = MLC_EMIT(CatchExpr->Line, MLI_CATCH, 2);
1249 CatchInst[1].Index = Frame->Top;
1250 CatchInst[2].Decls = Function->Decls;
1251 return mlc_compile(Function, CatchExpr->Body, 0);
1252 }
1253 mlc_block_expr_t *Expr = Frame->Expr;
1254 if (Frame->CatchInst) {
1255 Frame->CatchInst[1].Inst = Function->Next;
1256 MLC_EMIT(Expr->EndLine, MLI_RETRY, 0);
1257 }
1258 MLC_LINK(Frame->Exits, Function->Next);
1259 if (Frame->Flags & MLCF_PUSH) {
1260 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1261 mlc_inc_top(Function);
1262 }
1263 MLC_POP();
1264 MLC_RETURN(NULL);
1265 }
1266
ml_block_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_block_t * Frame)1267 static void ml_block_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_block_t *Frame) {
1268 mlc_expr_t *Child = Frame->Child;
1269 if (Child) {
1270 Frame->Child = Child->Next;
1271 return mlc_compile(Function, Child, 0);
1272 }
1273 mlc_block_expr_t *Expr = Frame->Expr;
1274 if (Expr->NumVars + Expr->NumLets) {
1275 ml_inst_t *ExitInst = MLC_EMIT(Expr->EndLine, MLI_EXIT, 2);
1276 ExitInst[1].Count = Expr->NumVars + Expr->NumLets;
1277 ExitInst[2].Decls = Frame->OldDecls;
1278 }
1279 Function->Decls = Frame->OldDecls;
1280 Function->Block = Frame->Up;
1281 Function->Top = Frame->Top;
1282 if (Expr->Catches) {
1283 Frame->Exits = NULL;
1284 Function->Try = Function->Try->Up;
1285 ml_inst_t *TryInst = MLC_EMIT(Expr->EndLine, MLI_TRY, 1);
1286 if (Function->Try) {
1287 TryInst[1].Inst = Function->Try->Retries;
1288 Function->Try->Retries = TryInst + 1;
1289 } else {
1290 TryInst[1].Inst = Function->Returns;
1291 Function->Returns = TryInst + 1;
1292 }
1293 ml_inst_t *GotoInst = MLC_EMIT(Expr->EndLine, MLI_GOTO, 1);
1294 GotoInst[1].Inst = Frame->Exits;
1295 Frame->Exits = GotoInst + 1;
1296 TryInst = MLC_EMIT(Expr->Catches->Line, MLI_TRY, 1);
1297 MLC_LINK(Frame->Try.Retries, TryInst);
1298 if (Function->Try) {
1299 TryInst[1].Inst = Function->Try->Retries;
1300 Function->Try->Retries = TryInst + 1;
1301 } else {
1302 TryInst[1].Inst = Function->Returns;
1303 Function->Returns = TryInst + 1;
1304 }
1305 Frame->TryInst[1].Inst = TryInst;
1306 mlc_catch_expr_t *CatchExpr = Frame->CatchExpr = Expr->Catches;
1307 Function->Frame->run = (mlc_frame_fn)ml_block_expr_compile3;
1308 if (CatchExpr->Types) {
1309 int NumTypes = 0;
1310 for (mlc_catch_type_t *Type = CatchExpr->Types; Type; Type = Type->Next) ++NumTypes;
1311 Frame->CatchInst = MLC_EMIT(CatchExpr->Line, MLI_CATCH_TYPE, 2);
1312 const char **Ptrs = Frame->CatchInst[2].Ptrs = anew(const char *, NumTypes + 1);
1313 for (mlc_catch_type_t *Type = CatchExpr->Types; Type; Type = Type->Next) *Ptrs++ = Type->Type;
1314 }
1315 ml_decl_t *Decl = new(ml_decl_t);
1316 Decl->Source.Name = Function->Source;
1317 Decl->Source.Line = CatchExpr->Line;
1318 Decl->Ident = CatchExpr->Ident;
1319 Decl->Hash = ml_ident_hash(CatchExpr->Ident);
1320 Decl->Index = Function->Top;
1321 Decl->Next = Function->Decls;
1322 Function->Decls = Decl;
1323 mlc_inc_top(Function);
1324 ml_inst_t *CatchInst = MLC_EMIT(CatchExpr->Line, MLI_CATCH, 2);
1325 CatchInst[1].Index = Frame->Top;
1326 CatchInst[2].Decls = Function->Decls;
1327 return mlc_compile(Function, CatchExpr->Body, 0);
1328 }
1329 if (Frame->Flags & MLCF_PUSH) {
1330 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1331 mlc_inc_top(Function);
1332 }
1333 MLC_POP();
1334 MLC_RETURN(NULL);
1335 }
1336
ml_block_expr_compile(mlc_function_t * Function,mlc_block_expr_t * Expr,int Flags)1337 static void ml_block_expr_compile(mlc_function_t *Function, mlc_block_expr_t *Expr, int Flags) {
1338 int NumDecls = Expr->NumVars + Expr->NumLets + Expr->NumDefs;
1339 MLC_XFRAME(mlc_block_t, NumDecls, ml_decl_t *, ml_block_expr_compile2);
1340 Frame->Expr = Expr;
1341 Frame->Flags = Flags;
1342 Frame->Top = Function->Top;
1343 Frame->OldDecls = Function->Decls;
1344 Frame->CatchInst = NULL;
1345 if (Expr->Catches) {
1346 Frame->TryInst = MLC_EMIT(Expr->StartLine, MLI_TRY, 1);
1347 Frame->Try.Up = Function->Try;
1348 Frame->Try.Retries = NULL;
1349 Frame->Try.Top = Function->Top;
1350 Function->Try = &Frame->Try;
1351 }
1352 int Top = Function->Top;
1353 ml_decl_t *Last = Function->Decls, *Decls = Last;
1354 Frame->DeclHashes = (inthash_t)INTHASH_INIT;
1355 inthash_t *DeclHashes = &Frame->DeclHashes;
1356 Frame->Up = Function->Block;
1357 Function->Block = Frame;
1358 for (mlc_local_t *Local = Expr->Vars; Local; Local = Local->Next) {
1359 ml_decl_t *Decl = new(ml_decl_t);
1360 Decl->Source.Name = Function->Source;
1361 Decl->Source.Line = Local->Line;
1362 Decl->Ident = Local->Ident;
1363 Decl->Hash = ml_ident_hash(Local->Ident);
1364 Decl->Index = Top++;
1365 Frame->Decls[Local->Index] = Decl;
1366 if (Local->Ident[0] && inthash_insert(DeclHashes, (uintptr_t)Decl->Hash, Decl)) {
1367 for (ml_decl_t *Prev = Decls; Prev != Last; Prev = Prev->Next) {
1368 if (!strcmp(Prev->Ident, Decl->Ident)) {
1369 MLC_EXPR_ERROR(Expr, ml_error("NameError", "Identifier %s redefined in line %d, previously declared on line %d", Decl->Ident, Decl->Source.Line, Prev->Source.Line));
1370 }
1371 }
1372 }
1373 Decl->Next = Decls;
1374 Decls = Decl;
1375 }
1376 for (mlc_local_t *Local = Expr->Lets; Local; Local = Local->Next) {
1377 ml_decl_t *Decl = new(ml_decl_t);
1378 Decl->Source.Name = Function->Source;
1379 Decl->Source.Line = Local->Line;
1380 Decl->Ident = Local->Ident;
1381 Decl->Hash = ml_ident_hash(Local->Ident);
1382 Decl->Index = Top++;
1383 Decl->Flags = MLC_DECL_FORWARD;
1384 Frame->Decls[Local->Index] = Decl;
1385 if (Local->Ident[0] && inthash_insert(DeclHashes, (uintptr_t)Decl->Hash, Decl)) {
1386 for (ml_decl_t *Prev = Decls; Prev != Last; Prev = Prev->Next) {
1387 if (!strcmp(Prev->Ident, Decl->Ident)) {
1388 MLC_EXPR_ERROR(Expr, ml_error("NameError", "Identifier %s redefined in line %d, previously declared on line %d", Decl->Ident, Decl->Source.Line, Prev->Source.Line));
1389 }
1390 }
1391 }
1392 Decl->Next = Decls;
1393 Decls = Decl;
1394 }
1395 for (mlc_local_t *Local = Expr->Defs; Local; Local = Local->Next) {
1396 ml_decl_t *Decl = new(ml_decl_t);
1397 Decl->Source.Name = Function->Source;
1398 Decl->Source.Line = Local->Line;
1399 Decl->Ident = Local->Ident;
1400 Decl->Hash = ml_ident_hash(Local->Ident);
1401 Decl->Flags = MLC_DECL_CONSTANT;
1402 Frame->Decls[Local->Index] = Decl;
1403 if (Local->Ident[0] && inthash_insert(DeclHashes, (uintptr_t)Decl->Hash, Decl)) {
1404 for (ml_decl_t *Prev = Decls; Prev != Last; Prev = Prev->Next) {
1405 if (!strcmp(Prev->Ident, Decl->Ident)) {
1406 MLC_EXPR_ERROR(Expr, ml_error("NameError", "Identifier %s redefined in line %d, previously declared on line %d", Decl->Ident, Decl->Source.Line, Prev->Source.Line));
1407 }
1408 }
1409 }
1410 Decl->Next = Decls;
1411 Decls = Decl;
1412 }
1413 if (Top >= Function->Size) Function->Size = Top + 1;
1414 Function->Top = Top;
1415 Function->Decls = Decls;
1416 if (Expr->NumVars + Expr->NumLets) {
1417 ml_inst_t *EnterInst = MLC_EMIT(Expr->StartLine, MLI_ENTER, 3);
1418 EnterInst[1].Count = Expr->NumVars;
1419 EnterInst[2].Count = Expr->NumLets;
1420 EnterInst[3].Decls = Function->Decls;
1421 }
1422 mlc_expr_t *Child = Expr->Child;
1423 if (Child) {
1424 Frame->Child = Child->Next;
1425 return mlc_compile(Function, Child, 0);
1426 } else {
1427 Frame->Child = NULL;
1428 MLC_EMIT(Expr->StartLine, MLI_NIL, 0);
1429 return ml_block_expr_compile2(Function, NULL, Frame);
1430 }
1431 }
1432
ml_assign_expr_compile4(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)1433 static void ml_assign_expr_compile4(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
1434 mlc_parent_expr_t *Expr = Frame->Expr;
1435 MLC_EMIT(Expr->EndLine, MLI_ASSIGN, 0);
1436 --Function->Top;
1437 if (Frame->Flags & MLCF_PUSH) {
1438 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1439 mlc_inc_top(Function);
1440 }
1441 Function->Self = Frame->Count;
1442 MLC_POP();
1443 MLC_RETURN(NULL);
1444 }
1445
ml_assign_expr_compile3(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)1446 static void ml_assign_expr_compile3(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
1447 mlc_parent_expr_t *Expr = Frame->Expr;
1448 ml_inst_t *AssignInst = MLC_EMIT(Expr->EndLine, MLI_ASSIGN_LOCAL, 1);
1449 AssignInst[1].Index = Function->Self;
1450 if (Frame->Flags & MLCF_PUSH) {
1451 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1452 mlc_inc_top(Function);
1453 }
1454 Function->Self = Frame->Count;
1455 MLC_POP();
1456 MLC_RETURN(NULL);
1457 }
1458
ml_assign_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)1459 static void ml_assign_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
1460 Frame->Count = Function->Self;
1461 if (Value) {
1462 Function->Self = ml_integer_value_fast(Value);
1463 Function->Frame->run = (mlc_frame_fn)ml_assign_expr_compile3;
1464 } else {
1465 Function->Self = Function->Top - 1;
1466 Function->Frame->run = (mlc_frame_fn)ml_assign_expr_compile4;
1467 }
1468 return mlc_compile(Function, Frame->Child, 0);
1469 }
1470
ml_assign_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)1471 static void ml_assign_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
1472 MLC_FRAME(mlc_parent_expr_frame_t, ml_assign_expr_compile2);
1473 Frame->Expr = Expr;
1474 Frame->Flags = Flags;
1475 mlc_expr_t *Child = Expr->Child;
1476 Frame->Child = Child->Next;
1477 return mlc_compile(Function, Child, MLCF_LOCAL | MLCF_PUSH);
1478 }
1479
ml_old_expr_compile(mlc_function_t * Function,mlc_expr_t * Expr,int Flags)1480 static void ml_old_expr_compile(mlc_function_t *Function, mlc_expr_t *Expr, int Flags) {
1481 if (Flags & MLCF_PUSH) {
1482 ml_inst_t *OldInst = MLC_EMIT(Expr->StartLine, MLI_LOCAL_PUSH, 1);
1483 OldInst[1].Index = Function->Self;
1484 mlc_inc_top(Function);
1485 } else {
1486 ml_inst_t *OldInst = MLC_EMIT(Expr->StartLine, MLI_LOCAL, 1);
1487 OldInst[1].Index = Function->Self;
1488 }
1489 MLC_RETURN(NULL);
1490 }
1491
ml_tuple_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)1492 static void ml_tuple_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
1493 mlc_expr_t *Child = Frame->Child;
1494 if ((Child = Child->Next)) {
1495 Frame->Child = Child;
1496 ++Frame->Count;
1497 return mlc_compile(Function, Child, MLCF_PUSH);
1498 }
1499 mlc_parent_expr_t *Expr = Frame->Expr;
1500 ml_inst_t *TupleInst = MLC_EMIT(Expr->StartLine, MLI_TUPLE_NEW, 1);
1501 TupleInst[1].Count = Frame->Count;
1502 Function->Top -= Frame->Count;
1503 if (Frame->Flags & MLCF_PUSH) {
1504 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1505 mlc_inc_top(Function);
1506 }
1507 MLC_POP();
1508 MLC_RETURN(NULL);
1509 }
1510
ml_tuple_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)1511 static void ml_tuple_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
1512 mlc_expr_t *Child = Expr->Child;
1513 if (Child) {
1514 MLC_FRAME(mlc_parent_expr_frame_t, ml_tuple_expr_compile2);
1515 Frame->Expr = Expr;
1516 Frame->Child = Child;
1517 Frame->Flags = Flags;
1518 Frame->Count = 1;
1519 return mlc_compile(Function, Child, MLCF_PUSH);
1520 }
1521 ml_inst_t *TupleInst = MLC_EMIT(Expr->StartLine, MLI_TUPLE_NEW, 1);
1522 TupleInst[1].Count = 0;
1523 if (Flags & MLCF_PUSH) {
1524 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1525 mlc_inc_top(Function);
1526 }
1527 MLC_RETURN(NULL);
1528 }
1529
ml_list_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)1530 static void ml_list_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
1531 mlc_expr_t *Child = Frame->Child;
1532 MLC_EMIT(Child->EndLine, MLI_LIST_APPEND, 0);
1533 if ((Child = Child->Next)) {
1534 Frame->Child = Child;
1535 return mlc_compile(Function, Child, 0);
1536 }
1537 mlc_parent_expr_t *Expr = Frame->Expr;
1538 if (!(Frame->Flags & MLCF_PUSH)) {
1539 MLC_EMIT(Expr->EndLine, MLI_POP, 0);
1540 --Function->Top;
1541 }
1542 MLC_POP();
1543 MLC_RETURN(NULL);
1544 }
1545
ml_list_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)1546 static void ml_list_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
1547 MLC_EMIT(Expr->StartLine, MLI_LIST_NEW, 0);
1548 mlc_inc_top(Function);
1549 mlc_expr_t *Child = Expr->Child;
1550 if (Child) {
1551 MLC_FRAME(mlc_parent_expr_frame_t, ml_list_expr_compile2);
1552 Frame->Expr = Expr;
1553 Frame->Child = Child;
1554 Frame->Flags = Flags;
1555 return mlc_compile(Function, Child, 0);
1556 }
1557 if (!(Flags & MLCF_PUSH)) {
1558 MLC_EMIT(Expr->EndLine, MLI_POP, 0);
1559 --Function->Top;
1560 }
1561 MLC_RETURN(NULL);
1562 }
1563
1564 static void ml_map_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame);
1565
ml_map_expr_compile3(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)1566 static void ml_map_expr_compile3(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
1567 mlc_expr_t *Child = Frame->Child;
1568 MLC_EMIT(Child->EndLine, MLI_MAP_INSERT, 0);
1569 --Function->Top;
1570 if ((Child = Child->Next)) {
1571 Function->Frame->run = (mlc_frame_fn)ml_map_expr_compile2;
1572 Frame->Child = Child;
1573 return mlc_compile(Function, Child, MLCF_PUSH);
1574 }
1575 mlc_parent_expr_t *Expr = Frame->Expr;
1576 if (!(Frame->Flags & MLCF_PUSH)) {
1577 MLC_EMIT(Expr->EndLine, MLI_POP, 0);
1578 --Function->Top;
1579 }
1580 MLC_POP();
1581 MLC_RETURN(NULL);
1582 }
1583
ml_map_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_parent_expr_frame_t * Frame)1584 static void ml_map_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_parent_expr_frame_t *Frame) {
1585 Function->Frame->run = (mlc_frame_fn)ml_map_expr_compile3;
1586 mlc_expr_t *Child = Frame->Child = Frame->Child->Next;
1587 mlc_compile(Function, Child, 0);
1588 }
1589
ml_map_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)1590 static void ml_map_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
1591 MLC_EMIT(Expr->StartLine, MLI_MAP_NEW, 0);
1592 mlc_inc_top(Function);
1593 mlc_expr_t *Child = Expr->Child;
1594 if (Child) {
1595 MLC_FRAME(mlc_parent_expr_frame_t, ml_map_expr_compile2);
1596 Frame->Expr = Expr;
1597 Frame->Child = Child;
1598 Frame->Flags = Flags;
1599 return mlc_compile(Function, Child, MLCF_PUSH);
1600 }
1601 if (!(Flags & MLCF_PUSH)) {
1602 MLC_EMIT(Expr->EndLine, MLI_POP, 0);
1603 --Function->Top;
1604 }
1605 MLC_RETURN(NULL);
1606 }
1607
1608 typedef struct {
1609 ml_macro_t Base;
1610 ml_decl_t *Params;
1611 mlc_expr_t *Expr;
1612 } ml_template_macro_t;
1613
ml_template_macro_apply2(mlc_function_t * Function,ml_value_t * Value,mlc_define_t ** Frame)1614 static void ml_template_macro_apply2(mlc_function_t *Function, ml_value_t *Value, mlc_define_t **Frame) {
1615 Function->Defines = Frame[0];
1616 MLC_POP();
1617 MLC_RETURN(NULL);
1618 }
1619
ml_template_macro_apply(mlc_function_t * Function,ml_template_macro_t * Macro,mlc_expr_t * Expr,mlc_expr_t * Child,int Flags)1620 static void ml_template_macro_apply(mlc_function_t *Function, ml_template_macro_t *Macro, mlc_expr_t *Expr, mlc_expr_t *Child, int Flags) {
1621 MLC_FRAME(mlc_define_t *, ml_template_macro_apply2);
1622 Frame[0] = Function->Defines;
1623 for (ml_decl_t *Param = Macro->Params; Param; Param = Param->Next) {
1624 if (!Child) MLC_EXPR_ERROR(Expr, ml_error("MacroError", "Insufficient arguments to macro"));
1625 mlc_define_t *Define = new(mlc_define_t);
1626 Define->Ident = Param->Ident;
1627 Define->Hash = Param->Hash;
1628 Define->Expr = Child;
1629 Define->Next = Function->Defines;
1630 Function->Defines = Define;
1631 Child = Child->Next;
1632 }
1633 return mlc_compile(Function, Macro->Expr, Flags);
1634 }
1635
1636 typedef struct ml_scope_macro_t ml_scope_macro_t;
1637
1638 struct ml_scope_macro_t {
1639 ml_macro_t Base;
1640 stringmap_t Names[1];
1641 };
1642
ml_scope_macro_fn(const char * Name,ml_value_t * Value,mlc_function_t * Function)1643 static int ml_scope_macro_fn(const char *Name, ml_value_t *Value, mlc_function_t *Function) {
1644 ml_decl_t *Decl = new(ml_decl_t);
1645 Decl->Ident = Name;
1646 Decl->Hash = ml_ident_hash(Name);
1647 Decl->Value = Value;
1648 Decl->Next = Function->Decls;
1649 Function->Decls = Decl;
1650 return 0;
1651 }
1652
ml_scope_macro_apply2(mlc_function_t * Function,ml_value_t * Value,ml_decl_t ** Frame)1653 static void ml_scope_macro_apply2(mlc_function_t *Function, ml_value_t *Value, ml_decl_t **Frame) {
1654 Function->Decls = Frame[0];
1655 MLC_POP();
1656 MLC_RETURN(NULL);
1657 }
1658
ml_scope_macro_apply(mlc_function_t * Function,ml_scope_macro_t * Macro,mlc_expr_t * Expr,mlc_expr_t * Child,int Flags)1659 static void ml_scope_macro_apply(mlc_function_t *Function, ml_scope_macro_t *Macro, mlc_expr_t *Expr, mlc_expr_t *Child, int Flags) {
1660 if (!Child) MLC_EXPR_ERROR(Expr, ml_error("MacroError", "Insufficient arguments to macro"));
1661 MLC_FRAME(ml_decl_t *, ml_scope_macro_apply2);
1662 Frame[0] = Function->Decls;
1663 stringmap_foreach(Macro->Names, Function, (void *)ml_scope_macro_fn);
1664 return mlc_compile(Function, Child, Flags);
1665 }
1666
ml_scope_macro_new()1667 ml_scope_macro_t *ml_scope_macro_new() {
1668 ml_scope_macro_t *Macro = new(ml_scope_macro_t);
1669 Macro->Base.Type = MLMacroT;
1670 Macro->Base.apply = (void *)ml_scope_macro_apply;
1671 return Macro;
1672 }
1673
ml_scope_macro_define(ml_scope_macro_t * Macro,const char * Name,ml_value_t * Value)1674 void ml_scope_macro_define(ml_scope_macro_t *Macro, const char *Name, ml_value_t *Value) {
1675 stringmap_insert(Macro->Names, Name, Value);
1676 }
1677
ml_stringify_macro_apply(mlc_function_t * Function,ml_macro_t * Macro,mlc_expr_t * Expr,mlc_expr_t * Child,int Flags)1678 static void ml_stringify_macro_apply(mlc_function_t *Function, ml_macro_t *Macro, mlc_expr_t *Expr, mlc_expr_t *Child, int Flags) {
1679 }
1680
1681 typedef struct {
1682 mlc_expr_t *Expr;
1683 mlc_expr_t *Child;
1684 ml_value_t *Value;
1685 int Count, Index, Flags;
1686 } ml_call_expr_frame_t;
1687
ml_call_expr_compile3(mlc_function_t * Function,ml_value_t * Value,ml_call_expr_frame_t * Frame)1688 static void ml_call_expr_compile3(mlc_function_t *Function, ml_value_t *Value, ml_call_expr_frame_t *Frame) {
1689 mlc_expr_t *Child = Frame->Child;
1690 ml_inst_t *SetInst = MLC_EMIT(Child->EndLine, MLI_PARTIAL_SET, 1);
1691 int Index = SetInst[1].Index = Frame->Index;
1692 while ((Child = Child->Next)) {
1693 ++Index;
1694 if (Child->compile != (void *)ml_blank_expr_compile) {
1695 Frame->Index = Index;
1696 Frame->Child = Child;
1697 return mlc_compile(Function, Child, 0);
1698 }
1699 }
1700 mlc_expr_t *Expr = Frame->Expr;
1701 if (!(Frame->Flags & MLCF_PUSH)) {
1702 MLC_EMIT(Expr->EndLine, MLI_POP, 0);
1703 --Function->Top;
1704 }
1705 MLC_POP();
1706 MLC_RETURN(NULL);
1707 }
1708
ml_call_expr_compile2(mlc_function_t * Function,ml_value_t * Value,ml_call_expr_frame_t * Frame)1709 static void ml_call_expr_compile2(mlc_function_t *Function, ml_value_t *Value, ml_call_expr_frame_t *Frame) {
1710 mlc_expr_t *Expr = Frame->Expr;
1711 ml_inst_t *PartialInst = MLC_EMIT(Expr->StartLine, MLI_PARTIAL_NEW, 1);
1712 PartialInst[1].Count = Frame->Count;
1713 mlc_inc_top(Function);
1714 int Index = 0;
1715 for (mlc_expr_t *Child = Frame->Child; Child; Child = Child->Next) {
1716 if (Child->compile != (void *)ml_blank_expr_compile) {
1717 Function->Frame->run = (mlc_frame_fn)ml_call_expr_compile3;
1718 Frame->Index = Index;
1719 Frame->Child = Child;
1720 return mlc_compile(Function, Child, 0);
1721 }
1722 ++Index;
1723 }
1724 PartialInst[1].Count = Frame->Count;
1725 if (!(Frame->Flags & MLCF_PUSH)) {
1726 MLC_EMIT(Expr->EndLine, MLI_POP, 0);
1727 --Function->Top;
1728 }
1729 MLC_POP();
1730 MLC_RETURN(NULL);
1731 }
1732
ml_call_expr_compile5(mlc_function_t * Function,ml_value_t * Value,ml_call_expr_frame_t * Frame)1733 static void ml_call_expr_compile5(mlc_function_t *Function, ml_value_t *Value, ml_call_expr_frame_t *Frame) {
1734 mlc_expr_t *Child = Frame->Child;
1735 if (Child) {
1736 Frame->Child = Child->Next;
1737 return mlc_compile(Function, Child, MLCF_PUSH);
1738 }
1739 mlc_expr_t *Expr = Frame->Expr;
1740 if (Frame->Value) {
1741 ml_inst_t *CallInst = MLC_EMIT(Expr->EndLine, MLI_CONST_CALL, 2);
1742 CallInst[1].Count = Frame->Count;
1743 ml_value_t *Value = Frame->Value;
1744 CallInst[2].Value = Value;
1745 if (ml_typeof(Value) == MLUninitializedT) ml_uninitialized_use(Value, &CallInst[2].Value);
1746 Function->Top -= Frame->Count;
1747 } else {
1748 ml_inst_t *CallInst = MLC_EMIT(Expr->EndLine, MLI_CALL, 1);
1749 CallInst[1].Count = Frame->Count;
1750 Function->Top -= Frame->Count + 1;
1751 }
1752 if (Frame->Flags & MLCF_PUSH) {
1753 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1754 mlc_inc_top(Function);
1755 }
1756 MLC_POP();
1757 MLC_RETURN(NULL);
1758 }
1759
ml_call_expr_compile4(mlc_function_t * Function,ml_value_t * Value,ml_call_expr_frame_t * Frame)1760 static void ml_call_expr_compile4(mlc_function_t *Function, ml_value_t *Value, ml_call_expr_frame_t *Frame) {
1761 mlc_expr_t *Expr = Frame->Expr;
1762 if (Value) {
1763 ml_value_t *Deref = ml_deref(Value);
1764 if (ml_typeof(Deref) == MLMacroT) {
1765 MLC_POP();
1766 ml_macro_t *Macro = (ml_macro_t *)Deref;
1767 return Macro->apply(Function, Macro, Expr, Frame->Child, Frame->Flags);
1768 }
1769 }
1770 mlc_expr_t *Child = Frame->Child;
1771 Function->Frame->run = (mlc_frame_fn)ml_call_expr_compile5;
1772 Frame->Value = Value;
1773 if (Child) {
1774 Frame->Child = Child->Next;
1775 return mlc_compile(Function, Child, MLCF_PUSH);
1776 } else {
1777 Frame->Child = NULL;
1778 return ml_call_expr_compile5(Function, NULL, Frame);
1779 }
1780 }
1781
ml_call_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)1782 static void ml_call_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
1783 MLC_FRAME(ml_call_expr_frame_t, ml_call_expr_compile4);
1784 Frame->Expr = (mlc_expr_t *)Expr;
1785 Frame->Child = Expr->Child->Next;
1786 int Count = 0;
1787 for (mlc_expr_t *Child = Expr->Child->Next; Child; Child = Child->Next) ++Count;
1788 Frame->Count = Count;
1789 Frame->Flags = Flags;
1790 for (mlc_expr_t *Child = Expr->Child->Next; Child; Child = Child->Next) {
1791 if (Child->compile == (void *)ml_blank_expr_compile) {
1792 Function->Frame->run = (mlc_frame_fn)ml_call_expr_compile2;
1793 return mlc_compile(Function, Expr->Child, 0);
1794 }
1795 }
1796 return mlc_compile(Function, Expr->Child, MLCF_CONSTANT | MLCF_PUSH);
1797 }
1798
ml_const_call_expr_compile(mlc_function_t * Function,mlc_parent_value_expr_t * Expr,int Flags)1799 static void ml_const_call_expr_compile(mlc_function_t *Function, mlc_parent_value_expr_t *Expr, int Flags) {
1800 MLC_FRAME(ml_call_expr_frame_t, ml_call_expr_compile4);
1801 Frame->Expr = (mlc_expr_t *)Expr;
1802 Frame->Child = Expr->Child;
1803 int Count = 0;
1804 for (mlc_expr_t *Child = Expr->Child; Child; Child = Child->Next) ++Count;
1805 Frame->Count = Count;
1806 Frame->Flags = Flags;
1807 for (mlc_expr_t *Child = Expr->Child; Child; Child = Child->Next) {
1808 if (Child->compile == (void *)ml_blank_expr_compile) {
1809 ml_inst_t *LoadInst = MLC_EMIT(Expr->StartLine, MLI_LOAD, 1);
1810 LoadInst[1].Value = Expr->Value;
1811 return ml_call_expr_compile2(Function, Expr->Value, Frame);
1812 }
1813 }
1814 return ml_call_expr_compile4(Function, Expr->Value, Frame);
1815 }
1816
1817 typedef struct {
1818 mlc_parent_value_expr_t *Expr;
1819 ml_value_t *Args[2];
1820 int Flags;
1821 } ml_resolve_expr_frame_t;
1822
ml_resolve_expr_compile3(mlc_function_t * Function,ml_value_t * Value,ml_resolve_expr_frame_t * Frame)1823 static void ml_resolve_expr_compile3(mlc_function_t *Function, ml_value_t *Value, ml_resolve_expr_frame_t *Frame) {
1824 mlc_parent_value_expr_t *Expr = Frame->Expr;
1825 if (ml_is_error(Value)) {
1826 ml_inst_t *LoadInst = MLC_EMIT(Expr->EndLine, MLI_LOAD, 1);
1827 LoadInst[1].Value = Frame->Args[0];
1828 ml_inst_t *ResolveInst = MLC_EMIT(Expr->EndLine, MLI_RESOLVE, 1);
1829 ResolveInst[1].Value = Expr->Value;
1830 if (Frame->Flags & MLCF_PUSH) {
1831 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1832 mlc_inc_top(Function);
1833 }
1834 MLC_POP();
1835 MLC_RETURN(NULL);
1836 }
1837 int Flags = Frame->Flags;
1838 if (Flags & MLCF_CONSTANT) {
1839 MLC_POP();
1840 MLC_RETURN(Value);
1841 } else if (Flags & MLCF_PUSH) {
1842 ml_inst_t *ValueInst = MLC_EMIT(Expr->EndLine, MLI_LOAD_PUSH, 1);
1843 ValueInst[1].Value = Value;
1844 mlc_inc_top(Function);
1845 MLC_POP();
1846 MLC_RETURN(NULL);
1847 } else {
1848 ml_inst_t *ValueInst = MLC_EMIT(Expr->EndLine, MLI_LOAD, 1);
1849 ValueInst[1].Value = Value;
1850 MLC_POP();
1851 MLC_RETURN(NULL);
1852 }
1853 }
1854
ml_resolve_expr_compile2(mlc_function_t * Function,ml_value_t * Value,ml_resolve_expr_frame_t * Frame)1855 static void ml_resolve_expr_compile2(mlc_function_t *Function, ml_value_t *Value, ml_resolve_expr_frame_t *Frame) {
1856 mlc_parent_value_expr_t *Expr = Frame->Expr;
1857 if (Value) {
1858 Frame->Args[0] = Value;
1859 Frame->Args[1] = Expr->Value;
1860 Function->Frame->AllowErrors = 1;
1861 Function->Frame->run = (mlc_frame_fn)ml_resolve_expr_compile3;
1862 return ml_call(Function, SymbolMethod, 2, Frame->Args);
1863 } else {
1864 ml_inst_t *ResolveInst = MLC_EMIT(Expr->EndLine, MLI_RESOLVE, 1);
1865 ResolveInst[1].Value = Expr->Value;
1866 if (Frame->Flags & MLCF_PUSH) {
1867 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1868 mlc_inc_top(Function);
1869 }
1870 MLC_POP();
1871 MLC_RETURN(NULL);
1872 }
1873 }
1874
ml_resolve_expr_compile(mlc_function_t * Function,mlc_parent_value_expr_t * Expr,int Flags)1875 static void ml_resolve_expr_compile(mlc_function_t *Function, mlc_parent_value_expr_t *Expr, int Flags) {
1876 MLC_FRAME(ml_resolve_expr_frame_t, ml_resolve_expr_compile2);
1877 Frame->Expr = Expr;
1878 Frame->Flags = Flags;
1879 mlc_compile(Function, Expr->Child, MLCF_CONSTANT);
1880 }
1881
1882 typedef struct {
1883 mlc_string_expr_t *Expr;
1884 mlc_string_part_t *Part;
1885 mlc_expr_t *Child;
1886 int NumArgs, Flags;
1887 } ml_string_expr_frame_t;
1888
ml_string_expr_compile2(mlc_function_t * Function,ml_value_t * Value,ml_string_expr_frame_t * Frame)1889 static void ml_string_expr_compile2(mlc_function_t *Function, ml_value_t *Value, ml_string_expr_frame_t *Frame) {
1890 mlc_expr_t *Child = Frame->Child;
1891 if (Child) {
1892 ++Frame->NumArgs;
1893 Frame->Child = Child->Next;
1894 return mlc_compile(Function, Child, MLCF_PUSH);
1895 }
1896 mlc_string_part_t *Part = Frame->Part;
1897 ml_inst_t *AddInst = MLC_EMIT(Part->Line, MLI_STRING_ADD, 1);
1898 AddInst[1].Count = Frame->NumArgs;
1899 Function->Top -= Frame->NumArgs;
1900 while ((Part = Part->Next)) {
1901 if (Part->Length) {
1902 ml_inst_t *AddInst = MLC_EMIT(Part->Line, MLI_STRING_ADDS, 2);
1903 AddInst[1].Count = Part->Length;
1904 AddInst[2].Chars = Part->Chars;
1905 } else {
1906 Frame->Part = Part;
1907 mlc_expr_t *Child = Part->Child;
1908 Frame->Child = Child->Next;
1909 Frame->NumArgs = 1;
1910 return mlc_compile(Function, Child, MLCF_PUSH);
1911 }
1912 }
1913 mlc_string_expr_t *Expr = Frame->Expr;
1914 MLC_EMIT(Expr->StartLine, MLI_STRING_END, 0);
1915 if (Frame->Flags & MLCF_PUSH) {
1916 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1917 } else {
1918 --Function->Top;
1919 }
1920 MLC_POP();
1921 MLC_RETURN(NULL);
1922 }
1923
ml_string_expr_compile(mlc_function_t * Function,mlc_string_expr_t * Expr,int Flags)1924 static void ml_string_expr_compile(mlc_function_t *Function, mlc_string_expr_t *Expr, int Flags) {
1925 MLC_EMIT(Expr->StartLine, MLI_STRING_NEW, 0);
1926 mlc_inc_top(Function);
1927 for (mlc_string_part_t *Part = Expr->Parts; Part; Part = Part->Next) {
1928 if (Part->Length) {
1929 ml_inst_t *AddInst = MLC_EMIT(Part->Line, MLI_STRING_ADDS, 2);
1930 AddInst[1].Count = Part->Length;
1931 AddInst[2].Chars = Part->Chars;
1932 } else {
1933 MLC_FRAME(ml_string_expr_frame_t, ml_string_expr_compile2);
1934 Frame->Expr = Expr;
1935 Frame->Part = Part;
1936 mlc_expr_t *Child = Part->Child;
1937 Frame->Child = Child->Next;
1938 Frame->NumArgs = 1;
1939 Frame->Flags = Flags;
1940 return mlc_compile(Function, Child, MLCF_PUSH);
1941 }
1942 }
1943 MLC_EMIT(Expr->EndLine, MLI_STRING_END, 0);
1944 if (Flags & MLCF_PUSH) {
1945 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
1946 } else {
1947 --Function->Top;
1948 }
1949 MLC_RETURN(NULL);
1950 }
1951
1952 #define ML_PARAM_EXTRA 1
1953 #define ML_PARAM_NAMED 2
1954 #define ML_PARAM_BYREF 3
1955
1956 typedef struct {
1957 mlc_fun_expr_t *Expr;
1958 ml_closure_info_t *Info;
1959 mlc_function_t *SubFunction;
1960 mlc_param_t *Param;
1961 int HasParamTypes, Index, Flags;
1962 ml_opcode_t OpCode;
1963 } mlc_fun_expr_frame_t;
1964
ml_fun_expr_compile4(mlc_function_t * Function,ml_value_t * Value,mlc_fun_expr_frame_t * Frame)1965 static void ml_fun_expr_compile4(mlc_function_t *Function, ml_value_t *Value, mlc_fun_expr_frame_t *Frame) {
1966 mlc_fun_expr_t *Expr = Frame->Expr;
1967 mlc_param_t *Param = Frame->Param;
1968 int Index = Frame->Index;
1969 ml_inst_t *TypeInst = MLC_EMIT(Param->Line, MLI_PARAM_TYPE, 1);
1970 TypeInst[1].Index = Index;
1971 while ((Param = Param->Next)) {
1972 ++Index;
1973 if (Param->Type) {
1974 Frame->Param = Param;
1975 Frame->Index = Index;
1976 return mlc_compile(Function, Param->Type, 0);
1977 }
1978 }
1979 if (!(Frame->Flags & MLCF_PUSH)) {
1980 MLC_EMIT(Expr->EndLine, MLI_POP, 0);
1981 --Function->Top;
1982 }
1983 MLC_POP();
1984 MLC_RETURN(NULL);
1985 }
1986
ml_fun_expr_compile3(mlc_function_t * Function,ml_value_t * Value,mlc_fun_expr_frame_t * Frame)1987 static void ml_fun_expr_compile3(mlc_function_t *Function, ml_value_t *Value, mlc_fun_expr_frame_t *Frame) {
1988 mlc_fun_expr_t *Expr = Frame->Expr;
1989 ml_closure_info_t *Info = Frame->Info;
1990 mlc_function_t *SubFunction = Frame->SubFunction;
1991 int NumUpValues = 0;
1992 for (mlc_upvalue_t *UpValue = SubFunction->UpValues; UpValue; UpValue = UpValue->Next) ++NumUpValues;
1993 ml_inst_t *ClosureInst = MLC_EMIT(Expr->StartLine, Frame->OpCode, NumUpValues + 1);
1994 Info->NumUpValues = NumUpValues;
1995 ClosureInst[1].ClosureInfo = Info;
1996 int Index = 1;
1997 for (mlc_upvalue_t *UpValue = SubFunction->UpValues; UpValue; UpValue = UpValue->Next) ClosureInst[++Index].Index = UpValue->Index;
1998 if (Frame->HasParamTypes) {
1999 MLC_EMIT(Expr->StartLine, MLI_PUSH, 0);
2000 mlc_inc_top(Function);
2001 mlc_param_t *Param = Expr->Params;
2002 int Index = 0;
2003 while (!Param->Type) {
2004 Param = Param->Next;
2005 ++Index;
2006 }
2007 Frame->Param = Param;
2008 Frame->Index = Index;
2009 Function->Frame->run = (mlc_frame_fn)ml_fun_expr_compile4;
2010 return mlc_compile(Function, Param->Type, 0);
2011 }
2012 if (Frame->Flags & MLCF_PUSH) {
2013 MLC_EMIT(Expr->EndLine, MLI_PUSH, 0);
2014 mlc_inc_top(Function);
2015 }
2016 MLC_POP();
2017 MLC_RETURN(NULL);
2018 }
2019
ml_fun_expr_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_fun_expr_frame_t * Frame)2020 static void ml_fun_expr_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_fun_expr_frame_t *Frame) {
2021 mlc_fun_expr_t *Expr = Frame->Expr;
2022 ml_closure_info_t *Info = Frame->Info;
2023 mlc_function_t *SubFunction = Frame->SubFunction;
2024 Info->Return = ml_inst_alloc(SubFunction, Expr->EndLine, MLI_RETURN, 0);
2025 MLC_LINK(SubFunction->Returns, Info->Return);
2026 Info->Halt = SubFunction->Next;
2027 ml_decl_t **UpValueSlot = &SubFunction->Decls;
2028 while (UpValueSlot[0]) UpValueSlot = &UpValueSlot[0]->Next;
2029 int Index = 0;
2030 for (mlc_upvalue_t *UpValue = SubFunction->UpValues; UpValue; UpValue = UpValue->Next, ++Index) {
2031 ml_decl_t *Decl = new(ml_decl_t);
2032 Decl->Source.Name = Function->Source;
2033 Decl->Source.Line = Expr->StartLine;
2034 Decl->Ident = UpValue->Decl->Ident;
2035 Decl->Hash = UpValue->Decl->Hash;
2036 Decl->Value = UpValue->Decl->Value;
2037 Decl->Index = ~Index;
2038 UpValueSlot[0] = Decl;
2039 UpValueSlot = &Decl->Next;
2040 }
2041 Info->FrameSize = SubFunction->Size;
2042 if (SubFunction->UpValues || Frame->HasParamTypes || Expr->ReturnType) {
2043 #ifdef ML_GENERICS
2044 if (Expr->ReturnType) {
2045 Frame->OpCode = MLI_CLOSURE_TYPED;
2046 Function->Frame->run = (mlc_frame_fn)ml_fun_expr_compile3;
2047 return mlc_compile(Function, Expr->ReturnType, 0);
2048 } else {
2049 #endif
2050 Frame->OpCode = MLI_CLOSURE;
2051 return ml_fun_expr_compile3(Function, NULL, Frame);
2052 #ifdef ML_GENERICS
2053 }
2054 #endif
2055 } else {
2056 Info->NumUpValues = 0;
2057 ml_value_t *Closure = ml_closure(Info);
2058 if (Frame->Flags & MLCF_PUSH) {
2059 ml_inst_t *LoadInst = MLC_EMIT(Expr->StartLine, MLI_LOAD_PUSH, 1);
2060 LoadInst[1].Value = Closure;
2061 mlc_inc_top(Function);
2062 } else {
2063 ml_inst_t *LoadInst = MLC_EMIT(Expr->StartLine, MLI_LOAD, 1);
2064 LoadInst[1].Value = Closure;
2065 }
2066 MLC_POP();
2067 MLC_RETURN(NULL);
2068 }
2069 }
2070
ml_subfunction_run(mlc_function_t * SubFunction,ml_value_t * Value,void * Frame)2071 static void ml_subfunction_run(mlc_function_t *SubFunction, ml_value_t *Value, void *Frame) {
2072 mlc_function_t *Function = SubFunction->Up;
2073 MLC_RETURN(NULL);
2074 }
2075
ml_fun_expr_compile(mlc_function_t * Function,mlc_fun_expr_t * Expr,int Flags)2076 static void ml_fun_expr_compile(mlc_function_t *Function, mlc_fun_expr_t *Expr, int Flags) {
2077 // closure <entry> <frame_size> <num_params> <num_upvalues> <upvalue_1> ...
2078 mlc_function_t *SubFunction = new(mlc_function_t);
2079 SubFunction->Base.Type = MLCompilerFunctionT;
2080 SubFunction->Base.Caller = (ml_state_t *)Function;
2081 SubFunction->Base.Context = Function->Base.Context;
2082 SubFunction->Base.run = (ml_state_fn)mlc_function_run;
2083 SubFunction->Compiler = Function->Compiler;
2084 SubFunction->Up = Function;
2085 SubFunction->Source = Expr->Source;
2086 ml_closure_info_t *Info = new(ml_closure_info_t);
2087 Info->Source = Expr->Source;
2088 Info->StartLine = Expr->StartLine;
2089 Info->EndLine = Expr->EndLine;
2090 if (Expr->Name) {
2091 Info->Name = Expr->Name;
2092 } else {
2093 asprintf((char **)&Info->Name, "<%s:%d>", Info->Source, Info->StartLine);
2094 }
2095 int NumParams = 0, HasParamTypes = 0;
2096 ml_decl_t **DeclSlot = &SubFunction->Decls;
2097 for (mlc_param_t *Param = Expr->Params; Param; Param = Param->Next) {
2098 ml_decl_t *Decl = DeclSlot[0] = new(ml_decl_t);
2099 Decl->Source.Name = Function->Source;
2100 Decl->Source.Line = Param->Line;
2101 Decl->Ident = Param->Ident;
2102 Decl->Hash = ml_ident_hash(Param->Ident);
2103 Decl->Index = NumParams++;
2104 switch (Param->Flags) {
2105 case ML_PARAM_EXTRA:
2106 Info->ExtraArgs = 1;
2107 break;
2108 case ML_PARAM_NAMED:
2109 Info->NamedArgs = 1;
2110 break;
2111 case ML_PARAM_BYREF:
2112 Decl->Flags |= MLC_DECL_BYREF;
2113 /* no break */
2114 default:
2115 stringmap_insert(Info->Params, Param->Ident, (void *)(intptr_t)NumParams);
2116 break;
2117 }
2118 if (Param->Type) HasParamTypes = 1;
2119 DeclSlot = &Decl->Next;
2120 }
2121 Info->NumParams = NumParams;
2122 SubFunction->Top = SubFunction->Size = NumParams;
2123 SubFunction->Next = anew(ml_inst_t, 128);
2124 SubFunction->Space = 126;
2125 SubFunction->Returns = NULL;
2126 mlc_frame_alloc(SubFunction, 0, ml_subfunction_run);
2127 Info->Decls = SubFunction->Decls;
2128 Info->Entry = SubFunction->Next;
2129 MLC_FRAME(mlc_fun_expr_frame_t, ml_fun_expr_compile2);
2130 Frame->Expr = Expr;
2131 Frame->Info = Info;
2132 Frame->HasParamTypes = HasParamTypes;
2133 Frame->SubFunction = SubFunction;
2134 Frame->Flags = Flags;
2135 mlc_compile(SubFunction, Expr->Body, 0);
2136 }
2137
ml_upvalue_find(mlc_function_t * Function,ml_decl_t * Decl,mlc_function_t * Origin)2138 static int ml_upvalue_find(mlc_function_t *Function, ml_decl_t *Decl, mlc_function_t *Origin) {
2139 if (Function == Origin) return Decl->Index;
2140 mlc_upvalue_t **UpValueSlot = &Function->UpValues;
2141 int Index = 0;
2142 while (UpValueSlot[0]) {
2143 if (UpValueSlot[0]->Decl == Decl) return ~Index;
2144 UpValueSlot = &UpValueSlot[0]->Next;
2145 ++Index;
2146 }
2147 mlc_upvalue_t *UpValue = new(mlc_upvalue_t);
2148 UpValue->Decl = Decl;
2149 UpValue->Index = ml_upvalue_find(Function->Up, Decl, Origin);
2150 UpValueSlot[0] = UpValue;
2151 return ~Index;
2152 }
2153
ml_ident_expr_finish(mlc_function_t * Function,mlc_ident_expr_t * Expr,ml_value_t * Value,int Flags)2154 static void ml_ident_expr_finish(mlc_function_t *Function, mlc_ident_expr_t *Expr, ml_value_t *Value, int Flags) {
2155 if (Flags & MLCF_CONSTANT) MLC_RETURN(Value);
2156 ml_inst_t *ValueInst = MLC_EMIT(Expr->StartLine, MLI_LOAD, 1);
2157 if (ml_typeof(Value) == MLUninitializedT) {
2158 ml_uninitialized_use(Value, &ValueInst[1].Value);
2159 }
2160 ValueInst[1].Value = Value;
2161 if (Flags & MLCF_PUSH) {
2162 ValueInst->Opcode = MLI_LOAD_PUSH;
2163 mlc_inc_top(Function);
2164 }
2165 MLC_RETURN(NULL);
2166 }
2167
ml_ident_expr_compile(mlc_function_t * Function,mlc_ident_expr_t * Expr,int Flags)2168 static void ml_ident_expr_compile(mlc_function_t *Function, mlc_ident_expr_t *Expr, int Flags) {
2169 long Hash = ml_ident_hash(Expr->Ident);
2170 //printf("#<%s> -> %ld\n", Expr->Ident, Hash);
2171 for (mlc_function_t *UpFunction = Function; UpFunction; UpFunction = UpFunction->Up) {
2172 for (ml_decl_t *Decl = UpFunction->Decls; Decl; Decl = Decl->Next) {
2173 if (Hash == Decl->Hash) {
2174 //printf("\tTesting <%s>\n", Decl->Ident);
2175 if (!strcmp(Decl->Ident, Expr->Ident)) {
2176 if (Decl->Flags == MLC_DECL_CONSTANT) {
2177 if (!Decl->Value) Decl->Value = ml_uninitialized(Decl->Ident);
2178 return ml_ident_expr_finish(Function, Expr, Decl->Value, Flags);
2179 } else {
2180 int Index = ml_upvalue_find(Function, Decl, UpFunction);
2181 if (Decl->Flags & MLC_DECL_FORWARD) Decl->Flags |= MLC_DECL_BACKFILL;
2182 if ((Index >= 0) && (Decl->Flags & MLC_DECL_FORWARD)) {
2183 ml_inst_t *LocalInst = MLC_EMIT(Expr->StartLine, MLI_LOCALX, 2);
2184 LocalInst[1].Index = Index;
2185 LocalInst[2].Chars = Decl->Ident;
2186 } else if (Index >= 0) {
2187 if (Flags & MLCF_LOCAL) {
2188 MLC_RETURN(ml_integer(Index));
2189 } else if (Flags & MLCF_PUSH) {
2190 ml_inst_t *LocalInst = MLC_EMIT(Expr->StartLine, MLI_LOCAL_PUSH, 1);
2191 LocalInst[1].Index = Index;
2192 mlc_inc_top(Function);
2193 MLC_RETURN(NULL);
2194 } else {
2195 ml_inst_t *LocalInst = MLC_EMIT(Expr->StartLine, MLI_LOCAL, 1);
2196 LocalInst[1].Index = Index;
2197 }
2198 } else {
2199 ml_inst_t *LocalInst = MLC_EMIT(Expr->StartLine, MLI_UPVALUE, 1);
2200 LocalInst[1].Index = ~Index;
2201 }
2202 if (Flags & MLCF_PUSH) {
2203 MLC_EMIT(Expr->StartLine, MLI_PUSH, 0);
2204 mlc_inc_top(Function);
2205 }
2206 MLC_RETURN(NULL);
2207 }
2208 }
2209 }
2210 }
2211 }
2212 ml_value_t *Value = (ml_value_t *)stringmap_search(Function->Compiler->Vars, Expr->Ident);
2213 if (!Value) Value = Function->Compiler->GlobalGet(Function->Compiler->Globals, Expr->Ident);
2214 if (!Value) {
2215 MLC_EXPR_ERROR(Expr, ml_error("CompilerError", "identifier %s not declared", Expr->Ident));
2216 }
2217 if (ml_is_error(Value)) MLC_EXPR_ERROR(Expr, Value);
2218 return ml_ident_expr_finish(Function, Expr, Value, Flags);
2219 }
2220
ml_define_expr_compile(mlc_function_t * Function,mlc_ident_expr_t * Expr,int Flags)2221 static void ml_define_expr_compile(mlc_function_t *Function, mlc_ident_expr_t *Expr, int Flags) {
2222 long Hash = ml_ident_hash(Expr->Ident);
2223 for (mlc_function_t *UpFunction = Function; UpFunction; UpFunction = UpFunction->Up) {
2224 for (mlc_define_t *Define = UpFunction->Defines; Define; Define = Define->Next) {
2225 if (Hash == Define->Hash) {
2226 //printf("\tTesting <%s>\n", Decl->Ident);
2227 if (!strcmp(Define->Ident, Expr->Ident)) {
2228 return mlc_compile(Function, Define->Expr, Flags);
2229 }
2230 }
2231 }
2232 }
2233 MLC_EXPR_ERROR(Expr, ml_error("CompilerError", "identifier %s not defined", Expr->Ident));
2234 }
2235
2236 typedef struct {
2237 mlc_parent_expr_t *Expr;
2238 int Flags;
2239 } ml_inline_expr_frame_t;
2240
ml_inline_expr_compile2(mlc_function_t * Function,ml_value_t * Value,ml_inline_expr_frame_t * Frame)2241 static void ml_inline_expr_compile2(mlc_function_t *Function, ml_value_t *Value, ml_inline_expr_frame_t *Frame) {
2242 if (ml_is_error(Value)) {
2243 ml_state_t *Caller = Function->Base.Caller;
2244 ML_RETURN(Value);
2245 }
2246 mlc_parent_expr_t *Expr = Frame->Expr;
2247 int Flags = Frame->Flags;
2248 if (Flags & MLCF_CONSTANT) {
2249 MLC_POP();
2250 MLC_RETURN(Value);
2251 }
2252 ml_inst_t *ValueInst = MLC_EMIT(Expr->StartLine, MLI_LOAD, 1);
2253 if (ml_typeof(Value) == MLUninitializedT) {
2254 ml_uninitialized_use(Value, &ValueInst[1].Value);
2255 }
2256 ValueInst[1].Value = Value;
2257 if (Flags & MLCF_PUSH) {
2258 ValueInst->Opcode = MLI_LOAD_PUSH;
2259 mlc_inc_top(Function);
2260 }
2261 MLC_POP();
2262 MLC_RETURN(NULL);
2263 }
2264
ml_inline_expr_compile(mlc_function_t * Function,mlc_parent_expr_t * Expr,int Flags)2265 static void ml_inline_expr_compile(mlc_function_t *Function, mlc_parent_expr_t *Expr, int Flags) {
2266 MLC_FRAME(ml_inline_expr_frame_t, ml_inline_expr_compile2);
2267 Frame->Expr = Expr;
2268 Frame->Flags = Flags;
2269 mlc_expr_call(Function, Expr->Child);
2270 }
2271
mlc_expr_type(mlc_expr_t * Expr)2272 ml_expr_type_t mlc_expr_type(mlc_expr_t *Expr) {
2273 if (Expr->compile == (void *)ml_register_expr_compile) {
2274 return ML_EXPR_REGISTER;
2275 } else if (Expr->compile == (void *)ml_blank_expr_compile) {
2276 return ML_EXPR_BLANK;
2277 } else if (Expr->compile == (void *)ml_nil_expr_compile) {
2278 return ML_EXPR_NIL;
2279 } else if (Expr->compile == (void *)ml_value_expr_compile) {
2280 return ML_EXPR_VALUE;
2281 } else if (Expr->compile == (void *)ml_if_expr_compile) {
2282 return ML_EXPR_IF;
2283 } else if (Expr->compile == (void *)ml_or_expr_compile) {
2284 return ML_EXPR_OR;
2285 } else if (Expr->compile == (void *)ml_and_expr_compile) {
2286 return ML_EXPR_AND;
2287 } else if (Expr->compile == (void *)ml_debug_expr_compile) {
2288 return ML_EXPR_DEBUG;
2289 } else if (Expr->compile == (void *)ml_not_expr_compile) {
2290 return ML_EXPR_NOT;
2291 } else if (Expr->compile == (void *)ml_loop_expr_compile) {
2292 return ML_EXPR_LOOP;
2293 } else if (Expr->compile == (void *)ml_next_expr_compile) {
2294 return ML_EXPR_NEXT;
2295 } else if (Expr->compile == (void *)ml_exit_expr_compile) {
2296 return ML_EXPR_EXIT;
2297 } else if (Expr->compile == (void *)ml_return_expr_compile) {
2298 return ML_EXPR_RETURN;
2299 } else if (Expr->compile == (void *)ml_suspend_expr_compile) {
2300 return ML_EXPR_SUSPEND;
2301 } else if (Expr->compile == (void *)ml_with_expr_compile) {
2302 return ML_EXPR_WITH;
2303 } else if (Expr->compile == (void *)ml_for_expr_compile) {
2304 return ML_EXPR_FOR;
2305 } else if (Expr->compile == (void *)ml_each_expr_compile) {
2306 return ML_EXPR_EACH;
2307 } else if (Expr->compile == (void *)ml_var_expr_compile) {
2308 return ML_EXPR_VAR;
2309 } else if (Expr->compile == (void *)ml_var_type_expr_compile) {
2310 return ML_EXPR_VAR_TYPE;
2311 } else if (Expr->compile == (void *)ml_var_in_expr_compile) {
2312 return ML_EXPR_VAR_IN;
2313 } else if (Expr->compile == (void *)ml_var_unpack_expr_compile) {
2314 return ML_EXPR_VAR_UNPACK;
2315 } else if (Expr->compile == (void *)ml_let_expr_compile) {
2316 return ML_EXPR_LET;
2317 } else if (Expr->compile == (void *)ml_let_in_expr_compile) {
2318 return ML_EXPR_LET_IN;
2319 } else if (Expr->compile == (void *)ml_let_unpack_expr_compile) {
2320 return ML_EXPR_LET_UNPACK;
2321 } else if (Expr->compile == (void *)ml_def_expr_compile) {
2322 return ML_EXPR_DEF;
2323 } else if (Expr->compile == (void *)ml_def_in_expr_compile) {
2324 return ML_EXPR_DEF_IN;
2325 } else if (Expr->compile == (void *)ml_def_unpack_expr_compile) {
2326 return ML_EXPR_DEF_UNPACK;
2327 } else if (Expr->compile == (void *)ml_block_expr_compile) {
2328 return ML_EXPR_BLOCK;
2329 } else if (Expr->compile == (void *)ml_assign_expr_compile) {
2330 return ML_EXPR_ASSIGN;
2331 } else if (Expr->compile == (void *)ml_old_expr_compile) {
2332 return ML_EXPR_OLD;
2333 } else if (Expr->compile == (void *)ml_tuple_expr_compile) {
2334 return ML_EXPR_TUPLE;
2335 } else if (Expr->compile == (void *)ml_list_expr_compile) {
2336 return ML_EXPR_LIST;
2337 } else if (Expr->compile == (void *)ml_map_expr_compile) {
2338 return ML_EXPR_MAP;
2339 } else if (Expr->compile == (void *)ml_call_expr_compile) {
2340 return ML_EXPR_CALL;
2341 } else if (Expr->compile == (void *)ml_const_call_expr_compile) {
2342 return ML_EXPR_CONST_CALL;
2343 } else if (Expr->compile == (void *)ml_resolve_expr_compile) {
2344 return ML_EXPR_RESOLVE;
2345 } else if (Expr->compile == (void *)ml_string_expr_compile) {
2346 return ML_EXPR_STRING;
2347 } else if (Expr->compile == (void *)ml_fun_expr_compile) {
2348 return ML_EXPR_FUN;
2349 } else if (Expr->compile == (void *)ml_ident_expr_compile) {
2350 return ML_EXPR_IDENT;
2351 } else if (Expr->compile == (void *)ml_define_expr_compile) {
2352 return ML_EXPR_DEFINE;
2353 } else if (Expr->compile == (void *)ml_inline_expr_compile) {
2354 return ML_EXPR_INLINE;
2355 } else {
2356 return 0;
2357 }
2358 }
2359
2360 #define MLT_DELIM_FIRST MLT_LEFT_PAREN
2361 #define MLT_DELIM_LAST MLT_COMMA
2362
2363 const char *MLTokens[] = {
2364 "", // MLT_NONE,
2365 "<end of line>", // MLT_EOL,
2366 "<end of input>", // MLT_EOI,
2367 "if", // MLT_IF,
2368 "then", // MLT_THEN,
2369 "elseif", // MLT_ELSEIF,
2370 "else", // MLT_ELSE,
2371 "end", // MLT_END,
2372 "loop", // MLT_LOOP,
2373 "while", // MLT_WHILE,
2374 "until", // MLT_UNTIL,
2375 "exit", // MLT_EXIT,
2376 "next", // MLT_NEXT,
2377 "for", // MLT_FOR,
2378 "each", // MLT_EACH,
2379 "to", // MLT_TO,
2380 "in", // MLT_IN,
2381 "is", // MLT_IS,
2382 "when", // MLT_WHEN,
2383 "switch", // MLT_SWITCH,
2384 "case", // MLT_CASE,
2385 "fun", // MLT_FUN,
2386 "macro", // MLT_MACRO,
2387 "ret", // MLT_RET,
2388 "susp", // MLT_SUSP,
2389 "debug", // MLT_DEBUG,
2390 "meth", // MLT_METH,
2391 "with", // MLT_WITH,
2392 "do", // MLT_DO,
2393 "on", // MLT_ON,
2394 "nil", // MLT_NIL,
2395 "and", // MLT_AND,
2396 "or", // MLT_OR,
2397 "not", // MLT_NOT,
2398 "old", // MLT_OLD,
2399 "def", // MLT_DEF,
2400 "let", // MLT_LET,
2401 "ref", // MLT_REF,
2402 "var", // MLT_VAR,
2403 "<identifier>", // MLT_IDENT,
2404 "_", // MLT_BLANK,
2405 "(", // MLT_LEFT_PAREN,
2406 ")", // MLT_RIGHT_PAREN,
2407 "[", // MLT_LEFT_SQUARE,
2408 "]", // MLT_RIGHT_SQUARE,
2409 "{", // MLT_LEFT_BRACE,
2410 "}", // MLT_RIGHT_BRACE,
2411 ";", // MLT_SEMICOLON,
2412 ":", // MLT_COLON,
2413 ",", // MLT_COMMA,
2414 ":=", // MLT_ASSIGN,
2415 "::", // MLT_SYMBOL,
2416 "<value>", // MLT_VALUE,
2417 "<expr>", // MLT_EXPR,
2418 "<inline>", // MLT_INLINE,
2419 "<expand>", // MLT_EXPAND,
2420 "<operator>", // MLT_OPERATOR
2421 "<method>" // MLT_METHOD
2422 };
2423
ml_compiler_call(ml_state_t * Caller,ml_compiler_t * Compiler,int Count,ml_value_t ** Args)2424 static void ml_compiler_call(ml_state_t *Caller, ml_compiler_t *Compiler, int Count, ml_value_t **Args) {
2425 ML_RETURN(MLNil);
2426 }
2427
ml_function_global_get(ml_value_t * Function,const char * Name)2428 static ml_value_t *ml_function_global_get(ml_value_t *Function, const char *Name) {
2429 ml_value_t *Value = ml_simple_inline(Function, 1, ml_cstring(Name));
2430 return (Value != MLNotFound) ? Value : NULL;
2431 }
2432
ml_map_global_get(ml_value_t * Map,const char * Name)2433 static ml_value_t *ml_map_global_get(ml_value_t *Map, const char *Name) {
2434 return ml_map_search0(Map, ml_cstring(Name));
2435 }
2436
ML_FUNCTION(MLCompiler)2437 ML_FUNCTION(MLCompiler) {
2438 //@compiler
2439 //<Global:function|map
2440 //<?Read:function
2441 //>compiler
2442 ML_CHECK_ARG_COUNT(1);
2443 ml_getter_t GlobalGet = (ml_getter_t)ml_function_global_get;
2444 if (ml_is(Args[0], MLMapT)) GlobalGet = (ml_getter_t)ml_map_global_get;
2445 return (ml_value_t *)ml_compiler(GlobalGet, Args[0]);
2446 }
2447
2448 ML_TYPE(MLCompilerT, (MLStateT), "compiler",
2449 .call = (void *)ml_compiler_call,
2450 .Constructor = (ml_value_t *)MLCompiler
2451 );
2452
ml_compiler(ml_getter_t GlobalGet,void * Globals)2453 ml_compiler_t *ml_compiler(ml_getter_t GlobalGet, void *Globals) {
2454 ml_compiler_t *Compiler = new(ml_compiler_t);
2455 Compiler->Type = MLCompilerT;
2456 Compiler->GlobalGet = GlobalGet;
2457 Compiler->Globals = Globals;
2458 return Compiler;
2459 }
2460
ml_compiler_define(ml_compiler_t * Compiler,const char * Name,ml_value_t * Value)2461 void ml_compiler_define(ml_compiler_t *Compiler, const char *Name, ml_value_t *Value) {
2462 stringmap_insert(Compiler->Vars, Name, Value);
2463 }
2464
ml_compiler_lookup(ml_compiler_t * Compiler,const char * Name)2465 ml_value_t *ml_compiler_lookup(ml_compiler_t *Compiler, const char *Name) {
2466 ml_value_t *Value = (ml_value_t *)stringmap_search(Compiler->Vars, Name);
2467 if (!Value) Value = Compiler->GlobalGet(Compiler->Globals, Name);
2468 return Value;
2469 }
2470
ml_parser_no_input(void * Data)2471 static const char *ml_parser_no_input(void *Data) {
2472 return NULL;
2473 }
2474
ml_function_read(ml_value_t * Function)2475 static const char *ml_function_read(ml_value_t *Function) {
2476 ml_value_t *Result = ml_simple_call(Function, 0, NULL);
2477 if (!ml_is(Result, MLStringT)) return NULL;
2478 return ml_string_value(Result);
2479 }
2480
ML_FUNCTION(MLParser)2481 ML_FUNCTION(MLParser) {
2482 //@compiler
2483 void *Input = NULL;
2484 ml_reader_t Reader = ml_parser_no_input;
2485 if (Count > 0) {
2486 Input = Args[0];
2487 Reader = (ml_reader_t)ml_function_read;
2488 }
2489 return (ml_value_t *)ml_parser(Reader, Input);
2490 }
2491
2492 ML_TYPE(MLParserT, (), "parser",
2493 .Constructor = (ml_value_t *)MLParser
2494 );
2495
ml_parser(ml_reader_t Read,void * Data)2496 ml_parser_t *ml_parser(ml_reader_t Read, void *Data) {
2497 ml_parser_t *Parser = new(ml_parser_t);
2498 Parser->Type = MLParserT;
2499 Parser->Token = MLT_NONE;
2500 Parser->Next = "";
2501 Parser->Source.Name = "";
2502 Parser->Source.Line = 0;
2503 Parser->Line = 0;
2504 Parser->Data = Data;
2505 Parser->Read = Read ?: ml_parser_no_input;
2506 return Parser;
2507 }
2508
2509 static mlc_expr_t *ml_accept_block(ml_parser_t *Parser);
2510 static void ml_accept_eoi(ml_parser_t *Parser);
2511
ml_parser_name(ml_parser_t * Parser)2512 const char *ml_parser_name(ml_parser_t *Parser) {
2513 return Parser->Source.Name;
2514 }
2515
ml_parser_source(ml_parser_t * Parser,ml_source_t Source)2516 ml_source_t ml_parser_source(ml_parser_t *Parser, ml_source_t Source) {
2517 ml_source_t OldSource = Parser->Source;
2518 Parser->Source = Source;
2519 Parser->Line = Source.Line;
2520 return OldSource;
2521 }
2522
ml_parser_value(ml_parser_t * Parser)2523 ml_value_t *ml_parser_value(ml_parser_t *Parser) {
2524 return Parser->Value;
2525 }
2526
ml_parser_reset(ml_parser_t * Parser)2527 void ml_parser_reset(ml_parser_t *Parser) {
2528 Parser->Token = MLT_NONE;
2529 Parser->Next = "";
2530 }
2531
ml_parser_input(ml_parser_t * Parser,const char * Text)2532 void ml_parser_input(ml_parser_t *Parser, const char *Text) {
2533 Parser->Next = Text;
2534 ++Parser->Line;
2535 }
2536
ml_parser_clear(ml_parser_t * Parser)2537 const char *ml_parser_clear(ml_parser_t *Parser) {
2538 const char *Next = Parser->Next;
2539 Parser->Next = "";
2540 return Next;
2541 }
2542
ml_parse_error(ml_parser_t * Parser,const char * Error,const char * Format,...)2543 void ml_parse_error(ml_parser_t *Parser, const char *Error, const char *Format, ...) {
2544 va_list Args;
2545 va_start(Args, Format);
2546 ml_value_t *Value = ml_errorv(Error, Format, Args);
2547 va_end(Args);
2548 ml_error_trace_add(Value, Parser->Source);
2549 Parser->Value = Value;
2550 longjmp(Parser->OnError, 1);
2551 }
2552
2553 typedef enum {
2554 EXPR_SIMPLE,
2555 EXPR_AND,
2556 EXPR_OR,
2557 EXPR_FOR,
2558 EXPR_DEFAULT
2559 } ml_expr_level_t;
2560
2561 static int ml_parse(ml_parser_t *Parser, ml_token_t Token);
2562 static void ml_accept(ml_parser_t *Parser, ml_token_t Token);
2563 static mlc_expr_t *ml_parse_expression(ml_parser_t *Parser, ml_expr_level_t Level);
2564 static mlc_expr_t *ml_accept_term(ml_parser_t *Parser);
2565 static mlc_expr_t *ml_accept_expression(ml_parser_t *Parser, ml_expr_level_t Level);
2566 static void ml_accept_arguments(ml_parser_t *Parser, ml_token_t EndToken, mlc_expr_t **ArgsSlot);
2567
ml_accept_string(ml_parser_t * Parser)2568 static ml_token_t ml_accept_string(ml_parser_t *Parser) {
2569 mlc_string_part_t *Parts = NULL, **Slot = &Parts;
2570 ml_stringbuffer_t Buffer[1] = {ML_STRINGBUFFER_INIT};
2571 const char *End = Parser->Next;
2572 for (;;) {
2573 char C = *End++;
2574 if (!C) {
2575 End = Parser->Read(Parser->Data);
2576 if (!End) {
2577 ml_parse_error(Parser, "ParseError", "end of input while parsing string");
2578 }
2579 ++Parser->Line;
2580 } else if (C == '\'') {
2581 Parser->Next = End;
2582 break;
2583 } else if (C == '{') {
2584 if (Buffer->Length) {
2585 mlc_string_part_t *Part = new(mlc_string_part_t);
2586 Part->Length = Buffer->Length;
2587 Part->Chars = ml_stringbuffer_get(Buffer);
2588 Part->Line = Parser->Source.Line;
2589 Slot[0] = Part;
2590 Slot = &Part->Next;
2591 }
2592 Parser->Next = End;
2593 mlc_string_part_t *Part = new(mlc_string_part_t);
2594 ml_accept_arguments(Parser, MLT_RIGHT_BRACE, &Part->Child);
2595 Part->Line = Parser->Source.Line;
2596 End = Parser->Next;
2597 Slot[0] = Part;
2598 Slot = &Part->Next;
2599 } else if (C == '\\') {
2600 C = *End++;
2601 switch (C) {
2602 case 'r': ml_stringbuffer_add(Buffer, "\r", 1); break;
2603 case 'n': ml_stringbuffer_add(Buffer, "\n", 1); break;
2604 case 't': ml_stringbuffer_add(Buffer, "\t", 1); break;
2605 case 'e': ml_stringbuffer_add(Buffer, "\e", 1); break;
2606 case '\'': ml_stringbuffer_add(Buffer, "\'", 1); break;
2607 case '\"': ml_stringbuffer_add(Buffer, "\"", 1); break;
2608 case '\\': ml_stringbuffer_add(Buffer, "\\", 1); break;
2609 case '{': ml_stringbuffer_add(Buffer, "{", 1); break;
2610 case '\n': break;
2611 case 0: ml_parse_error(Parser, "ParseError", "end of line while parsing string");
2612 }
2613 } else {
2614 ml_stringbuffer_add(Buffer, End - 1, 1);
2615 }
2616 }
2617 if (!Parts) {
2618 Parser->Value = ml_stringbuffer_value(Buffer);
2619 return (Parser->Token = MLT_VALUE);
2620 } else {
2621 if (Buffer->Length) {
2622 mlc_string_part_t *Part = new(mlc_string_part_t);
2623 Part->Length = Buffer->Length;
2624 Part->Chars = ml_stringbuffer_get(Buffer);
2625 Part->Line = Parser->Source.Line;
2626 Slot[0] = Part;
2627 }
2628 ML_EXPR(Expr, string, string);
2629 Expr->Parts = Parts;
2630 Parser->Expr = ML_EXPR_END(Expr);
2631 return (Parser->Token = MLT_EXPR);
2632 }
2633 }
2634
2635 typedef enum {
2636 ML_CHAR_OTHER,
2637 ML_CHAR_EOI,
2638 ML_CHAR_SPACE,
2639 ML_CHAR_LINE,
2640 ML_CHAR_ALPHA,
2641 ML_CHAR_DIGIT,
2642 ML_CHAR_OPER,
2643 ML_CHAR_DELIM,
2644 ML_CHAR_COLON,
2645 ML_CHAR_SQUOTE,
2646 ML_CHAR_DQUOTE
2647 } ml_char_type_t;
2648
2649 static const ml_char_type_t CharTypes[256] = {
2650 ML_CHAR_OTHER,
2651 [0] = ML_CHAR_EOI,
2652 [1 ... ' '] = ML_CHAR_SPACE,
2653 ['\n'] = ML_CHAR_LINE,
2654 ['0' ... '9'] = ML_CHAR_DIGIT,
2655 ['_'] = ML_CHAR_ALPHA,
2656 ['A' ... 'Z'] = ML_CHAR_ALPHA,
2657 ['a' ... 'z'] = ML_CHAR_ALPHA,
2658 ['!'] = ML_CHAR_OPER,
2659 ['@'] = ML_CHAR_OPER,
2660 ['#'] = ML_CHAR_OPER,
2661 ['$'] = ML_CHAR_OPER,
2662 ['%'] = ML_CHAR_OPER,
2663 ['^'] = ML_CHAR_OPER,
2664 ['&'] = ML_CHAR_OPER,
2665 ['*'] = ML_CHAR_OPER,
2666 ['-'] = ML_CHAR_OPER,
2667 ['+'] = ML_CHAR_OPER,
2668 ['='] = ML_CHAR_OPER,
2669 ['|'] = ML_CHAR_OPER,
2670 ['\\'] = ML_CHAR_OPER,
2671 ['~'] = ML_CHAR_OPER,
2672 ['`'] = ML_CHAR_OPER,
2673 ['/'] = ML_CHAR_OPER,
2674 ['?'] = ML_CHAR_OPER,
2675 ['<'] = ML_CHAR_OPER,
2676 ['>'] = ML_CHAR_OPER,
2677 ['.'] = ML_CHAR_OPER,
2678 [':'] = ML_CHAR_COLON,
2679 ['('] = ML_CHAR_DELIM,
2680 [')'] = ML_CHAR_DELIM,
2681 ['['] = ML_CHAR_DELIM,
2682 [']'] = ML_CHAR_DELIM,
2683 ['{'] = ML_CHAR_DELIM,
2684 ['}'] = ML_CHAR_DELIM,
2685 [';'] = ML_CHAR_DELIM,
2686 [','] = ML_CHAR_DELIM,
2687 ['\''] = ML_CHAR_SQUOTE,
2688 ['\"'] = ML_CHAR_DQUOTE,
2689 [128 ... 255] = ML_CHAR_ALPHA
2690 };
2691
2692 static const ml_token_t CharTokens[256] = {
2693 0,
2694 ['('] = MLT_LEFT_PAREN,
2695 [')'] = MLT_RIGHT_PAREN,
2696 ['['] = MLT_LEFT_SQUARE,
2697 [']'] = MLT_RIGHT_SQUARE,
2698 ['{'] = MLT_LEFT_BRACE,
2699 ['}'] = MLT_RIGHT_BRACE,
2700 [';'] = MLT_SEMICOLON,
2701 [':'] = MLT_COLON,
2702 [','] = MLT_COMMA
2703 };
2704
ml_isidstart(char C)2705 static inline int ml_isidstart(char C) {
2706 return CharTypes[(unsigned char)C] == ML_CHAR_ALPHA;
2707 }
2708
ml_isidchar(char C)2709 static inline int ml_isidchar(char C) {
2710 return CharTypes[(unsigned char)C] == ML_CHAR_ALPHA || CharTypes[(unsigned char)C] == ML_CHAR_DIGIT;
2711 }
2712
ml_isoperator(char C)2713 static inline int ml_isoperator(char C) {
2714 return CharTypes[(unsigned char)C] == ML_CHAR_OPER;
2715 }
2716
ml_isdigit(char C)2717 static inline int ml_isdigit(char C) {
2718 return CharTypes[(unsigned char)C] == ML_CHAR_DIGIT;
2719 }
2720
2721 #include "keywords.c"
2722
2723 static stringmap_t StringFns[1] = {STRINGMAP_INIT};
2724
ml_string_fn_register(const char * Prefix,string_fn_t Fn)2725 void ml_string_fn_register(const char *Prefix, string_fn_t Fn) {
2726 stringmap_insert(StringFns, Prefix, Fn);
2727 }
2728
ml_scan_string(ml_parser_t * Parser)2729 static int ml_scan_string(ml_parser_t *Parser) {
2730 const char *End = Parser->Next;
2731 while (End[0] != '\"') {
2732 if (!End[0]) {
2733 ml_parse_error(Parser, "ParseError", "End of input while parsing string");
2734 }
2735 if (End[0] == '\\') ++End;
2736 ++End;
2737 }
2738 int Length = End - Parser->Next;
2739 char *Quoted = snew(Length + 1), *D = Quoted;
2740 for (const char *S = Parser->Next; S < End; ++S) {
2741 if (*S == '\\') {
2742 ++S;
2743 switch (*S) {
2744 case 'r': *D++ = '\r'; break;
2745 case 'n': *D++ = '\n'; break;
2746 case 't': *D++ = '\t'; break;
2747 case 'e': *D++ = '\e'; break;
2748 case '\'': *D++ = '\''; break;
2749 case '\"': *D++ = '\"'; break;
2750 case '\\': *D++ = '\\'; break;
2751 case '0': *D++ = '\0'; break;
2752 default: *D++ = '\\'; *D++ = *S; break;
2753 }
2754 } else {
2755 *D++ = *S;
2756 }
2757 }
2758 *D = 0;
2759 Parser->Ident = Quoted;
2760 Parser->Next = End + 1;
2761 return D - Quoted;
2762 }
2763
ml_scan_raw_string(ml_parser_t * Parser)2764 static int ml_scan_raw_string(ml_parser_t *Parser) {
2765 const char *End = Parser->Next;
2766 while (End[0] != '\"') {
2767 if (!End[0]) {
2768 ml_parse_error(Parser, "ParseError", "End of input while parsing string");
2769 }
2770 if (End[0] == '\\') ++End;
2771 ++End;
2772 }
2773 int Length = End - Parser->Next;
2774 char *Raw = snew(Length + 1);
2775 memcpy(Raw, Parser->Next, Length);
2776 Raw[Length] = 0;
2777 Parser->Ident = Raw;
2778 Parser->Next = End + 1;
2779 return Length;
2780 }
2781
ml_scan(ml_parser_t * Parser)2782 static ml_token_t ml_scan(ml_parser_t *Parser) {
2783 const char *Next = Parser->Next;
2784 for (;;) {
2785 char Char = Next[0];
2786 static const void *Labels[] = {
2787 &&DO_CHAR_OTHER,
2788 &&DO_CHAR_EOI,
2789 &&DO_CHAR_SPACE,
2790 &&DO_CHAR_LINE,
2791 &&DO_CHAR_ALPHA,
2792 &&DO_CHAR_DIGIT,
2793 &&DO_CHAR_OPER,
2794 &&DO_CHAR_DELIM,
2795 &&DO_CHAR_COLON,
2796 &&DO_CHAR_SQUOTE,
2797 &&DO_CHAR_DQUOTE
2798 };
2799 goto *Labels[CharTypes[(unsigned char)Char]];
2800 DO_CHAR_EOI:
2801 Next = Parser->Read(Parser->Data);
2802 if (Next) continue;
2803 Parser->Next = "";
2804 Parser->Token = MLT_EOI;
2805 return Parser->Token;
2806 DO_CHAR_LINE:
2807 Parser->Next = Next + 1;
2808 ++Parser->Line;
2809 Parser->Token = MLT_EOL;
2810 return Parser->Token;
2811 DO_CHAR_SPACE:
2812 ++Next;
2813 continue;
2814 DO_CHAR_ALPHA: {
2815 const char *End = Next + 1;
2816 while (ml_isidchar(*End)) ++End;
2817 int Length = End - Next;
2818 const struct keyword_t *Keyword = lookup(Next, Length);
2819 if (Keyword) {
2820 Parser->Token = Keyword->Token;
2821 Parser->Ident = MLTokens[Parser->Token];
2822 Parser->Next = End;
2823 return Parser->Token;
2824 }
2825 char *Ident = snew(Length + 1);
2826 memcpy(Ident, Next, Length);
2827 Ident[Length] = 0;
2828 if (End[0] == '\"') {
2829 string_fn_t StringFn = stringmap_search(StringFns, Ident);
2830 if (!StringFn) ml_parse_error(Parser, "ParseError", "Unknown string prefix: %s", Ident);
2831 Parser->Next = End + 1;
2832 int Length = ml_scan_raw_string(Parser);
2833 ml_value_t *Value = StringFn(Parser->Ident, Length);
2834 if (ml_is_error(Value)) {
2835 ml_error_trace_add(Value, Parser->Source);
2836 Parser->Value = Value;
2837 longjmp(Parser->OnError, 1);
2838 }
2839 Parser->Value = Value;
2840 Parser->Token = MLT_VALUE;
2841 return Parser->Token;
2842 }
2843 Parser->Next = End;
2844 Parser->Ident = Ident;
2845 Parser->Token = MLT_IDENT;
2846 return Parser->Token;
2847 }
2848 DO_CHAR_DIGIT: {
2849 char *End;
2850 double Double = strtod(Next, (char **)&End);
2851 #ifdef ML_COMPLEX
2852 if (*End == 'i') {
2853 Parser->Value = ml_complex(Double * 1i);
2854 Parser->Token = MLT_VALUE;
2855 Parser->Next = End + 1;
2856 return Parser->Token;
2857 }
2858 #endif
2859 for (const char *P = Next; P < End; ++P) {
2860 if (P[0] == '.' || P[0] == 'e' || P[0] == 'E') {
2861 Parser->Value = ml_real(Double);
2862 Parser->Token = MLT_VALUE;
2863 Parser->Next = End;
2864 return Parser->Token;
2865 }
2866 }
2867 long Integer = strtol(Next, (char **)&End, 10);
2868 Parser->Value = ml_integer(Integer);
2869 Parser->Token = MLT_VALUE;
2870 Parser->Next = End;
2871 return Parser->Token;
2872 }
2873 DO_CHAR_SQUOTE:
2874 Parser->Next = Next + 1;
2875 return ml_accept_string(Parser);
2876 DO_CHAR_DQUOTE:
2877 Parser->Next = Next + 1;
2878 int Length = ml_scan_string(Parser);;
2879 Parser->Value = ml_string(Parser->Ident, Length);
2880 Parser->Token = MLT_VALUE;
2881 return Parser->Token;
2882 DO_CHAR_COLON: {
2883 Char = *++Next;
2884 if (Char == '=') {
2885 Parser->Token = MLT_ASSIGN;
2886 Parser->Next = Next + 1;
2887 return Parser->Token;
2888 } else if (Char == ':') {
2889 Parser->Token = MLT_IMPORT;
2890 Char = *++Next;
2891 if (ml_isidchar(Char)) {
2892 const char *End = Next + 1;
2893 while (ml_isidchar(*End)) ++End;
2894 int Length = End - Next;
2895 char *Ident = snew(Length + 1);
2896 memcpy(Ident, Next, Length);
2897 Ident[Length] = 0;
2898 Parser->Ident = Ident;
2899 Parser->Next = End;
2900 } else if (Char == '\"') {
2901 Parser->Next = Next + 1;
2902 ml_scan_string(Parser);
2903 } else if (ml_isoperator(Char)) {
2904 const char *End = Next + 1;
2905 while (ml_isoperator(*End)) ++End;
2906 int Length = End - Next;
2907 char *Operator = snew(Length + 1);
2908 memcpy(Operator, Next, Length);
2909 Operator[Length] = 0;
2910 Parser->Ident = Operator;
2911 Parser->Next = End;
2912 } else {
2913 Parser->Next = Next;
2914 Parser->Ident = "::";
2915 Parser->Token = MLT_OPERATOR;
2916 }
2917 return Parser->Token;
2918 } else if (ml_isidchar(Char)) {
2919 const char *End = Next + 1;
2920 while (ml_isidchar(*End)) ++End;
2921 int Length = End - Next;
2922 char *Ident = snew(Length + 1);
2923 memcpy(Ident, Next, Length);
2924 Ident[Length] = 0;
2925 Parser->Ident = Ident;
2926 Parser->Token = MLT_METHOD;
2927 Parser->Next = End;
2928 return Parser->Token;
2929 } else if (Char == '\"') {
2930 Parser->Next = Next + 1;
2931 ml_scan_string(Parser);
2932 Parser->Token = MLT_METHOD;
2933 return Parser->Token;
2934 } else if (Char == '>') {
2935 const char *End = Next + 1;
2936 while (End[0] && End[0] != '\n') ++End;
2937 Next = End;
2938 continue;
2939 } else if (Char == '<') {
2940 ++Next;
2941 int Level = 1;
2942 do {
2943 switch (*Next++) {
2944 case '\n':
2945 ++Parser->Line;
2946 break;
2947 case 0:
2948 Next = Parser->Read(Parser->Data);
2949 if (!Next) {
2950 Parser->Next = "";
2951 ml_parse_error(Parser, "ParseError", "End of input in comment");
2952 }
2953 break;
2954 case '>':
2955 if (Next[0] == ':') {
2956 ++Next;
2957 --Level;
2958 }
2959 break;
2960 case ':':
2961 if (Next[0] == '<') {
2962 ++Next;
2963 ++Level;
2964 }
2965 break;
2966 }
2967 } while (Level);
2968 continue;
2969 } else if (Char == '(') {
2970 Parser->Token = MLT_INLINE;
2971 Parser->Next = Next + 1;
2972 return Parser->Token;
2973 } else if (Char == '$') {
2974 Parser->Token = MLT_EXPAND;
2975 Parser->Next = Next + 1;
2976 return Parser->Token;
2977 } else {
2978 Parser->Token = MLT_COLON;
2979 Parser->Next = Next;
2980 return Parser->Token;
2981 }
2982 }
2983 DO_CHAR_DELIM:
2984 Parser->Next = Next + 1;
2985 Parser->Token = CharTokens[(unsigned char)Char];
2986 return Parser->Token;
2987 DO_CHAR_OPER: {
2988 if (Char == '-' || Char == '.') {
2989 if (ml_isdigit(Next[1])) goto DO_CHAR_DIGIT;
2990 }
2991 const char *End = Next + 1;
2992 while (ml_isoperator(*End)) ++End;
2993 int Length = End - Next;
2994 char *Operator = snew(Length + 1);
2995 memcpy(Operator, Next, Length);
2996 Operator[Length] = 0;
2997 Parser->Ident = Operator;
2998 Parser->Token = MLT_OPERATOR;
2999 Parser->Next = End;
3000 return Parser->Token;
3001 }
3002 DO_CHAR_OTHER:
3003 ml_parse_error(Parser, "ParseError", "unexpected character <%c>", Char);
3004 }
3005 return Parser->Token;
3006 }
3007
ml_current(ml_parser_t * Parser)3008 static inline ml_token_t ml_current(ml_parser_t *Parser) {
3009 if (Parser->Token == MLT_NONE) ml_scan(Parser);
3010 return Parser->Token;
3011 }
3012
ml_next(ml_parser_t * Parser)3013 static inline void ml_next(ml_parser_t *Parser) {
3014 Parser->Token = MLT_NONE;
3015 Parser->Source.Line = Parser->Line;
3016 }
3017
ml_parse(ml_parser_t * Parser,ml_token_t Token)3018 static inline int ml_parse(ml_parser_t *Parser, ml_token_t Token) {
3019 if (Parser->Token == MLT_NONE) ml_scan(Parser);
3020 if (Parser->Token == Token) {
3021 Parser->Token = MLT_NONE;
3022 Parser->Source.Line = Parser->Line;
3023 return 1;
3024 } else {
3025 return 0;
3026 }
3027 }
3028
ml_skip_eol(ml_parser_t * Parser)3029 static inline void ml_skip_eol(ml_parser_t *Parser) {
3030 if (Parser->Token == MLT_NONE) ml_scan(Parser);
3031 while (Parser->Token == MLT_EOL) ml_scan(Parser);
3032 }
3033
ml_parse2(ml_parser_t * Parser,ml_token_t Token)3034 static inline int ml_parse2(ml_parser_t *Parser, ml_token_t Token) {
3035 if (Parser->Token == MLT_NONE) ml_scan(Parser);
3036 while (Parser->Token == MLT_EOL) ml_scan(Parser);
3037 if (Parser->Token == Token) {
3038 Parser->Token = MLT_NONE;
3039 Parser->Source.Line = Parser->Line;
3040 return 1;
3041 } else {
3042 return 0;
3043 }
3044 }
3045
ml_accept(ml_parser_t * Parser,ml_token_t Token)3046 static void ml_accept(ml_parser_t *Parser, ml_token_t Token) {
3047 if (ml_parse2(Parser, Token)) return;
3048 if (Parser->Token == MLT_IDENT) {
3049 ml_parse_error(Parser, "ParseError", "expected %s not %s (%s)", MLTokens[Token], MLTokens[Parser->Token], Parser->Ident);
3050 } else {
3051 ml_parse_error(Parser, "ParseError", "expected %s not %s", MLTokens[Token], MLTokens[Parser->Token]);
3052 }
3053 }
3054
ml_accept_eoi(ml_parser_t * Parser)3055 static void ml_accept_eoi(ml_parser_t *Parser) {
3056 ml_accept(Parser, MLT_EOI);
3057 }
3058
3059 static mlc_expr_t *ml_parse_factor(ml_parser_t *Parser, int MethDecl);
3060 static mlc_expr_t *ml_parse_term(ml_parser_t *Parser, int MethDecl);
3061 static mlc_expr_t *ml_accept_block(ml_parser_t *Parser);
3062
ml_accept_fun_expr(ml_parser_t * Parser,const char * Name,ml_token_t EndToken)3063 static mlc_expr_t *ml_accept_fun_expr(ml_parser_t *Parser, const char *Name, ml_token_t EndToken) {
3064 ML_EXPR(FunExpr, fun, fun);
3065 FunExpr->Name = Name;
3066 FunExpr->Source = Parser->Source.Name;
3067 if (!ml_parse2(Parser, EndToken)) {
3068 mlc_param_t **ParamSlot = &FunExpr->Params;
3069 do {
3070 mlc_param_t *Param = ParamSlot[0] = new(mlc_param_t);
3071 Param->Line = Parser->Source.Line;
3072 ParamSlot = &Param->Next;
3073 if (ml_parse2(Parser, MLT_LEFT_SQUARE)) {
3074 ml_accept(Parser, MLT_IDENT);
3075 Param->Ident = Parser->Ident;
3076 Param->Flags = ML_PARAM_EXTRA;
3077 ml_accept(Parser, MLT_RIGHT_SQUARE);
3078 if (ml_parse2(Parser, MLT_COMMA)) {
3079 ml_accept(Parser, MLT_LEFT_BRACE);
3080 mlc_param_t *Param = ParamSlot[0] = new(mlc_param_t);
3081 Param->Line = Parser->Source.Line;
3082 ml_accept(Parser, MLT_IDENT);
3083 Param->Ident = Parser->Ident;
3084 Param->Flags = ML_PARAM_NAMED;
3085 ml_accept(Parser, MLT_RIGHT_BRACE);
3086 }
3087 break;
3088 } else if (ml_parse2(Parser, MLT_LEFT_BRACE)) {
3089 ml_accept(Parser, MLT_IDENT);
3090 Param->Ident = Parser->Ident;
3091 Param->Flags = ML_PARAM_NAMED;
3092 ml_accept(Parser, MLT_RIGHT_BRACE);
3093 break;
3094 } else {
3095 if (ml_parse2(Parser, MLT_BLANK)) {
3096 Param->Ident = "_";
3097 } else {
3098 if (ml_parse2(Parser, MLT_REF)) Param->Flags = ML_PARAM_BYREF;
3099 ml_accept(Parser, MLT_IDENT);
3100 Param->Ident = Parser->Ident;
3101 }
3102 if (ml_parse2(Parser, MLT_COLON)) {
3103 Param->Type = ml_accept_term(Parser);
3104 }
3105 }
3106 } while (ml_parse2(Parser, MLT_COMMA));
3107 ml_accept(Parser, EndToken);
3108 }
3109 if (ml_parse2(Parser, MLT_COLON)) {
3110 FunExpr->ReturnType = ml_parse_term(Parser, 0);
3111 }
3112 FunExpr->Body = ml_accept_expression(Parser, EXPR_DEFAULT);
3113 FunExpr->StartLine = FunExpr->Body->StartLine;
3114 return ML_EXPR_END(FunExpr);
3115 }
3116
ml_accept_macro_expr(ml_parser_t * Parser)3117 static mlc_expr_t *ml_accept_macro_expr(ml_parser_t *Parser) {
3118 ML_EXPR(MacroExpr, value, value);
3119 MacroExpr->StartLine = Parser->Source.Line;
3120 ml_accept(Parser, MLT_LEFT_PAREN);
3121 ml_template_macro_t *Macro = new(ml_template_macro_t);
3122 Macro->Base.Type = MLMacroT;
3123 Macro->Base.apply = (void *)ml_template_macro_apply;
3124 ml_decl_t **ParamSlot = &Macro->Params;
3125 if (!ml_parse2(Parser, MLT_RIGHT_PAREN)) {
3126 do {
3127 ml_accept(Parser, MLT_IDENT);
3128 ml_decl_t *Param = ParamSlot[0] = new(ml_decl_t);
3129 Param->Ident = Parser->Ident;
3130 Param->Hash = ml_ident_hash(Parser->Ident);
3131 ParamSlot = &Param->Next;
3132 } while (ml_parse2(Parser, MLT_COMMA));
3133 ml_accept(Parser, MLT_RIGHT_PAREN);
3134 }
3135 Macro->Expr = ml_accept_expression(Parser, EXPR_DEFAULT);
3136 MacroExpr->Value = (ml_value_t *)Macro;
3137 return ML_EXPR_END(MacroExpr);
3138 }
3139
3140 extern ml_cfunctionx_t MLMethodSet[];
3141
ml_accept_meth_expr(ml_parser_t * Parser)3142 static mlc_expr_t *ml_accept_meth_expr(ml_parser_t *Parser) {
3143 ML_EXPR(MethodExpr, parent_value, const_call);
3144 MethodExpr->Value = (ml_value_t *)MLMethodSet;
3145 mlc_expr_t *Method = ml_parse_term(Parser, 1);
3146 if (!Method) ml_parse_error(Parser, "ParseError", "expected <factor> not <%s>", MLTokens[Parser->Token]);
3147 MethodExpr->Child = Method;
3148 mlc_expr_t **ArgsSlot = &Method->Next;
3149 ml_accept(Parser, MLT_LEFT_PAREN);
3150 ML_EXPR(FunExpr, fun, fun);
3151 FunExpr->Source = Parser->Source.Name;
3152 if (!ml_parse2(Parser, MLT_RIGHT_PAREN)) {
3153 mlc_param_t **ParamSlot = &FunExpr->Params;
3154 do {
3155 if (ml_parse2(Parser, MLT_OPERATOR)) {
3156 if (!strcmp(Parser->Ident, "..")) {
3157 ML_EXPR(ValueExpr, value, value);
3158 ValueExpr->Value = ml_method("..");
3159 mlc_expr_t *Arg = ArgsSlot[0] = ML_EXPR_END(ValueExpr);
3160 ArgsSlot = &Arg->Next;
3161 break;
3162 } else {
3163 ml_parse_error(Parser, "ParseError", "expected <identfier> not %s (%s)", MLTokens[Parser->Token], Parser->Ident);
3164 }
3165 }
3166 mlc_param_t *Param = ParamSlot[0] = new(mlc_param_t);
3167 Param->Line = Parser->Source.Line;
3168 ParamSlot = &Param->Next;
3169 if (ml_parse2(Parser, MLT_LEFT_SQUARE)) {
3170 ml_accept(Parser, MLT_IDENT);
3171 Param->Ident = Parser->Ident;
3172 Param->Flags = ML_PARAM_EXTRA;
3173 ml_accept(Parser, MLT_RIGHT_SQUARE);
3174 if (ml_parse2(Parser, MLT_COMMA)) {
3175 ml_accept(Parser, MLT_LEFT_BRACE);
3176 mlc_param_t *Param = ParamSlot[0] = new(mlc_param_t);
3177 Param->Line = Parser->Source.Line;
3178 ml_accept(Parser, MLT_IDENT);
3179 Param->Ident = Parser->Ident;
3180 Param->Flags = ML_PARAM_NAMED;
3181 ml_accept(Parser, MLT_RIGHT_BRACE);
3182 }
3183 ML_EXPR(ValueExpr, value, value);
3184 ValueExpr->Value = ml_method("..");
3185 mlc_expr_t *Arg = ArgsSlot[0] = ML_EXPR_END(ValueExpr);
3186 ArgsSlot = &Arg->Next;
3187 break;
3188 } else if (ml_parse2(Parser, MLT_LEFT_BRACE)) {
3189 ml_accept(Parser, MLT_IDENT);
3190 Param->Ident = Parser->Ident;
3191 Param->Flags = ML_PARAM_NAMED;
3192 ml_accept(Parser, MLT_RIGHT_BRACE);
3193 ML_EXPR(ValueExpr, value, value);
3194 ValueExpr->Value = ml_method("..");
3195 mlc_expr_t *Arg = ArgsSlot[0] = ML_EXPR_END(ValueExpr);
3196 ArgsSlot = &Arg->Next;
3197 break;
3198 } else {
3199
3200 if (ml_parse2(Parser, MLT_BLANK)) {
3201 Param->Ident = "_";
3202 } else {
3203 if (ml_parse2(Parser, MLT_REF)) Param->Flags = ML_PARAM_BYREF;
3204 ml_accept(Parser, MLT_IDENT);
3205 Param->Ident = Parser->Ident;
3206 }
3207 ml_accept(Parser, MLT_COLON);
3208 mlc_expr_t *Arg = ArgsSlot[0] = ml_accept_expression(Parser, EXPR_DEFAULT);
3209 ArgsSlot = &Arg->Next;
3210 }
3211 } while (ml_parse2(Parser, MLT_COMMA));
3212 ml_accept(Parser, MLT_RIGHT_PAREN);
3213 }
3214 if (ml_parse2(Parser, MLT_ASSIGN)) {
3215 ArgsSlot[0] = ml_accept_expression(Parser, EXPR_DEFAULT);
3216 } else {
3217 FunExpr->Body = ml_accept_expression(Parser, EXPR_DEFAULT);
3218 ArgsSlot[0] = ML_EXPR_END(FunExpr);
3219 }
3220 return ML_EXPR_END(MethodExpr);
3221 }
3222
ml_accept_named_arguments(ml_parser_t * Parser,ml_token_t EndToken,mlc_expr_t ** ArgsSlot,ml_value_t * Names)3223 static void ml_accept_named_arguments(ml_parser_t *Parser, ml_token_t EndToken, mlc_expr_t **ArgsSlot, ml_value_t *Names) {
3224 mlc_expr_t **NamesSlot = ArgsSlot;
3225 mlc_expr_t *Arg = ArgsSlot[0];
3226 ArgsSlot = &Arg->Next;
3227 if (ml_parse2(Parser, MLT_SEMICOLON)) {
3228 ArgsSlot[0] = ml_accept_fun_expr(Parser, NULL, EndToken);
3229 return;
3230 }
3231 Arg = ArgsSlot[0] = ml_accept_expression(Parser, EXPR_DEFAULT);
3232 ArgsSlot = &Arg->Next;
3233 while (ml_parse2(Parser, MLT_COMMA)) {
3234 if (ml_parse2(Parser, MLT_IDENT)) {
3235 ml_names_add(Names, ml_cstring(Parser->Ident));
3236 } else if (ml_parse2(Parser, MLT_VALUE)) {
3237 if (ml_typeof(Parser->Value) != MLStringT) {
3238 ml_parse_error(Parser, "ParseError", "Argument names must be identifiers or string");
3239 }
3240 ml_names_add(Names, Parser->Value);
3241 } else {
3242 ml_parse_error(Parser, "ParseError", "Argument names must be identifiers or string");
3243 }
3244 ml_accept(Parser, MLT_IS);
3245 if (ml_parse2(Parser, MLT_SEMICOLON)) {
3246 ArgsSlot[0] = ml_accept_fun_expr(Parser, NULL, EndToken);
3247 return;
3248 }
3249 Arg = ArgsSlot[0] = ml_accept_expression(Parser, EXPR_DEFAULT);
3250 ArgsSlot = &Arg->Next;
3251 }
3252 if (ml_parse2(Parser, MLT_SEMICOLON)) {
3253 mlc_expr_t *FunExpr = ml_accept_fun_expr(Parser, NULL, EndToken);
3254 FunExpr->Next = NamesSlot[0];
3255 NamesSlot[0] = FunExpr;
3256 } else {
3257 ml_accept(Parser, EndToken);
3258 }
3259 }
3260
ml_accept_arguments(ml_parser_t * Parser,ml_token_t EndToken,mlc_expr_t ** ArgsSlot)3261 static void ml_accept_arguments(ml_parser_t *Parser, ml_token_t EndToken, mlc_expr_t **ArgsSlot) {
3262 if (ml_parse2(Parser, MLT_SEMICOLON)) {
3263 ArgsSlot[0] = ml_accept_fun_expr(Parser, NULL, EndToken);
3264 } else if (!ml_parse2(Parser, EndToken)) {
3265 do {
3266 mlc_expr_t *Arg = ml_accept_expression(Parser, EXPR_DEFAULT);
3267 if (ml_parse2(Parser, MLT_IS)) {
3268 ml_value_t *Names = ml_names();
3269 if (Arg->compile == (void *)ml_ident_expr_compile) {
3270 ml_names_add(Names, ml_cstring(((mlc_ident_expr_t *)Arg)->Ident));
3271 } else if (Arg->compile == (void *)ml_value_expr_compile) {
3272 ml_value_t *Name = ((mlc_value_expr_t *)Arg)->Value;
3273 if (ml_typeof(Name) != MLStringT) {
3274 ml_parse_error(Parser, "ParseError", "Argument names must be identifiers or strings");
3275 }
3276 ml_names_add(Names, Name);
3277 } else {
3278 ml_parse_error(Parser, "ParseError", "Argument names must be identifiers or strings");
3279 }
3280 ML_EXPR(NamesArg, value, value);
3281 NamesArg->Value = Names;
3282 ArgsSlot[0] = ML_EXPR_END(NamesArg);
3283 return ml_accept_named_arguments(Parser, EndToken, ArgsSlot, Names);
3284 } else {
3285 ArgsSlot[0] = Arg;
3286 ArgsSlot = &Arg->Next;
3287 }
3288 } while (ml_parse2(Parser, MLT_COMMA));
3289 if (ml_parse2(Parser, MLT_SEMICOLON)) {
3290 ArgsSlot[0] = ml_accept_fun_expr(Parser, NULL, EndToken);
3291 } else {
3292 ml_accept(Parser, EndToken);
3293 }
3294 return;
3295 }
3296 }
3297
ml_accept_with_expr(ml_parser_t * Parser,mlc_expr_t * Child)3298 static mlc_expr_t *ml_accept_with_expr(ml_parser_t *Parser, mlc_expr_t *Child) {
3299 ML_EXPR(WithExpr, local, with);
3300 mlc_local_t **LocalSlot = &WithExpr->Local;
3301 mlc_expr_t **ExprSlot = &WithExpr->Child;
3302 do {
3303 if (ml_parse2(Parser, MLT_LEFT_PAREN)) {
3304 int Count = 0;
3305 mlc_local_t **First = LocalSlot;
3306 do {
3307 if (!ml_parse2(Parser, MLT_BLANK)) ml_accept(Parser, MLT_IDENT);
3308 ++Count;
3309 mlc_local_t *Local = LocalSlot[0] = new(mlc_local_t);
3310 Local->Line = Parser->Source.Line;
3311 Local->Ident = Parser->Ident;
3312 LocalSlot = &Local->Next;
3313 } while (ml_parse2(Parser, MLT_COMMA));
3314 ml_accept(Parser, MLT_RIGHT_PAREN);
3315 First[0]->Index = Count;
3316 } else {
3317 ml_accept(Parser, MLT_IDENT);
3318 mlc_local_t *Local = LocalSlot[0] = new(mlc_local_t);
3319 Local->Line = Parser->Source.Line;
3320 LocalSlot = &Local->Next;
3321 Local->Ident = Parser->Ident;
3322 Local->Index = 0;
3323 }
3324 ml_accept(Parser, MLT_ASSIGN);
3325 mlc_expr_t *Expr = ExprSlot[0] = ml_accept_expression(Parser, EXPR_DEFAULT);
3326 ExprSlot = &Expr->Next;
3327 } while (ml_parse2(Parser, MLT_COMMA));
3328 if (Child) {
3329 ExprSlot[0] = Child;
3330 } else {
3331 ml_accept(Parser, MLT_DO);
3332 ExprSlot[0] = ml_accept_block(Parser);
3333 ml_accept(Parser, MLT_END);
3334 }
3335 return ML_EXPR_END(WithExpr);
3336 }
3337
ml_accept_for_decl(ml_parser_t * Parser,mlc_for_expr_t * Expr)3338 static void ml_accept_for_decl(ml_parser_t *Parser, mlc_for_expr_t *Expr) {
3339 if (ml_parse2(Parser, MLT_IDENT)) {
3340 const char *Ident = Parser->Ident;
3341 if (ml_parse2(Parser, MLT_COMMA)) {
3342 Expr->Key = Ident;
3343 } else {
3344 mlc_local_t *Local = Expr->Local = new(mlc_local_t);
3345 Local->Line = Parser->Source.Line;
3346 Local->Ident = Ident;
3347 return;
3348 }
3349 }
3350 if (ml_parse2(Parser, MLT_LEFT_PAREN)) {
3351 int Count = 0;
3352 mlc_local_t **Slot = &Expr->Local;
3353 do {
3354 ml_accept(Parser, MLT_IDENT);
3355 ++Count;
3356 mlc_local_t *Local = Slot[0] = new(mlc_local_t);
3357 Local->Line = Parser->Source.Line;
3358 Local->Ident = Parser->Ident;
3359 Slot = &Local->Next;
3360 } while (ml_parse2(Parser, MLT_COMMA));
3361 ml_accept(Parser, MLT_RIGHT_PAREN);
3362 Expr->Unpack = Count;
3363 } else {
3364 ml_accept(Parser, MLT_IDENT);
3365 mlc_local_t *Local = Expr->Local = new(mlc_local_t);
3366 Local->Line = Parser->Source.Line;
3367 Local->Ident = Parser->Ident;
3368 }
3369 }
3370
3371 static ML_METHOD_DECL(MLInMethod, "in");
3372 static ML_METHOD_DECL(MLIsMethod, "=");
3373
ml_parse_factor(ml_parser_t * Parser,int MethDecl)3374 static mlc_expr_t *ml_parse_factor(ml_parser_t *Parser, int MethDecl) {
3375 static void *CompileFns[] = {
3376 [MLT_EACH] = ml_each_expr_compile,
3377 [MLT_NOT] = ml_not_expr_compile,
3378 [MLT_WHILE] = ml_or_expr_compile,
3379 [MLT_UNTIL] = ml_and_expr_compile,
3380 [MLT_EXIT] = ml_exit_expr_compile,
3381 [MLT_RET] = ml_return_expr_compile,
3382 [MLT_NEXT] = ml_next_expr_compile,
3383 [MLT_NIL] = ml_nil_expr_compile,
3384 [MLT_BLANK] = ml_blank_expr_compile,
3385 [MLT_OLD] = ml_old_expr_compile,
3386 [MLT_DEBUG] = ml_debug_expr_compile
3387 };
3388 switch (ml_current(Parser)) {
3389 case MLT_EACH:
3390 case MLT_NOT:
3391 case MLT_DEBUG:
3392 {
3393 mlc_parent_expr_t *ParentExpr = new(mlc_parent_expr_t);
3394 ParentExpr->compile = CompileFns[Parser->Token];
3395 ml_next(Parser);
3396 ParentExpr->StartLine = Parser->Source.Line;
3397 ParentExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3398 return ML_EXPR_END(ParentExpr);
3399 }
3400 case MLT_WHILE:
3401 case MLT_UNTIL:
3402 {
3403 mlc_parent_expr_t *ParentExpr = new(mlc_parent_expr_t);
3404 ParentExpr->compile = CompileFns[Parser->Token];
3405 ml_next(Parser);
3406 ParentExpr->StartLine = Parser->Source.Line;
3407 ParentExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3408 ML_EXPR(ExitExpr, parent, exit);
3409 if (ml_parse(Parser, MLT_COMMA)) {
3410 ExitExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3411 } else {
3412 mlc_expr_t *RegisterExpr = new(mlc_expr_t);
3413 RegisterExpr->compile = ml_register_expr_compile;
3414 RegisterExpr->StartLine = RegisterExpr->EndLine = Parser->Source.Line;
3415 ExitExpr->Child = RegisterExpr;
3416 }
3417 ParentExpr->Child->Next = ML_EXPR_END(ExitExpr);
3418 return ML_EXPR_END(ParentExpr);
3419 }
3420 case MLT_EXIT:
3421 case MLT_RET:
3422 {
3423 mlc_parent_expr_t *ParentExpr = new(mlc_parent_expr_t);
3424 ParentExpr->compile = CompileFns[Parser->Token];
3425 ml_next(Parser);
3426 ParentExpr->StartLine = Parser->Source.Line;
3427 ParentExpr->Child = ml_parse_expression(Parser, EXPR_DEFAULT);
3428 return ML_EXPR_END(ParentExpr);
3429 }
3430 case MLT_NEXT:
3431 case MLT_NIL:
3432 case MLT_BLANK:
3433 case MLT_OLD:
3434 {
3435 mlc_expr_t *Expr = new(mlc_expr_t);
3436 Expr->compile = CompileFns[Parser->Token];
3437 ml_next(Parser);
3438 Expr->StartLine = Expr->EndLine = Parser->Source.Line;
3439 return Expr;
3440 }
3441 case MLT_DO: {
3442 ml_next(Parser);
3443 mlc_expr_t *BlockExpr = ml_accept_block(Parser);
3444 ml_accept(Parser, MLT_END);
3445 return BlockExpr;
3446 }
3447 case MLT_IF: {
3448 ml_next(Parser);
3449 ML_EXPR(IfExpr, if, if);
3450 mlc_if_case_t **CaseSlot = &IfExpr->Cases;
3451 do {
3452 mlc_if_case_t *Case = CaseSlot[0] = new(mlc_if_case_t);
3453 Case->Line = Parser->Source.Line;
3454 CaseSlot = &Case->Next;
3455 if (ml_parse2(Parser, MLT_VAR)) {
3456 ml_accept(Parser, MLT_IDENT);
3457 Case->Ident = Parser->Ident;
3458 Case->Token = MLT_VAR;
3459 ml_accept(Parser, MLT_ASSIGN);
3460 } else if (ml_parse2(Parser, MLT_LET)) {
3461 ml_accept(Parser, MLT_IDENT);
3462 Case->Ident = Parser->Ident;
3463 Case->Token = MLT_LET;
3464 ml_accept(Parser, MLT_ASSIGN);
3465 }
3466 Case->Condition = ml_accept_expression(Parser, EXPR_DEFAULT);
3467 ml_accept(Parser, MLT_THEN);
3468 Case->Body = ml_accept_block(Parser);
3469 } while (ml_parse2(Parser, MLT_ELSEIF));
3470 if (ml_parse2(Parser, MLT_ELSE)) IfExpr->Else = ml_accept_block(Parser);
3471 ml_accept(Parser, MLT_END);
3472 return ML_EXPR_END(IfExpr);
3473 }
3474 case MLT_SWITCH: {
3475 ml_next(Parser);
3476 ML_EXPR(CaseExpr, parent, switch);
3477 mlc_expr_t *Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3478 mlc_expr_t *CaseExprs = NULL;
3479 if (ml_parse(Parser, MLT_COLON)) {
3480 ML_EXPR(CallExpr, parent, call);
3481 ML_EXPR(InlineExpr, parent, inline);
3482 ML_EXPR(SwitchExpr, parent_value, const_call);
3483 SwitchExpr->Value = MLCompilerSwitch;
3484 SwitchExpr->Child = CaseExprs = ml_accept_expression(Parser, EXPR_DEFAULT);
3485 InlineExpr->Child = ML_EXPR_END(SwitchExpr);
3486 InlineExpr->Next = Child;
3487 CallExpr->Child = ML_EXPR_END(InlineExpr);
3488 Child = ML_EXPR_END(CallExpr);
3489 }
3490 CaseExpr->Child = Child;
3491 while (ml_parse2(Parser, MLT_CASE)) {
3492 if (CaseExprs) {
3493 ML_EXPR(ListExpr, parent, list);
3494 mlc_expr_t *ListChild = ListExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3495 while (ml_parse(Parser, MLT_COMMA)) {
3496 ListChild = ListChild->Next = ml_accept_expression(Parser, EXPR_DEFAULT);
3497 }
3498 CaseExprs = CaseExprs->Next = ML_EXPR_END(ListExpr);
3499 ml_accept(Parser, MLT_DO);
3500 }
3501 Child = Child->Next = ml_accept_block(Parser);
3502 }
3503 if (ml_parse2(Parser, MLT_ELSE)) {
3504 Child->Next = ml_accept_block(Parser);
3505 } else {
3506 mlc_expr_t *NilExpr = new(mlc_expr_t);
3507 NilExpr->compile = ml_nil_expr_compile;
3508 NilExpr->StartLine = NilExpr->EndLine = Parser->Source.Line;
3509 Child->Next = NilExpr;
3510 }
3511 ml_accept(Parser, MLT_END);
3512 return ML_EXPR_END(CaseExpr);
3513 }
3514 case MLT_WHEN: {
3515 ml_next(Parser);
3516 ML_EXPR(WhenExpr, local, with);
3517 char *Ident;
3518 asprintf(&Ident, "when:%d", Parser->Source.Line);
3519 WhenExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3520 mlc_local_t *Local = WhenExpr->Local = new(mlc_local_t);
3521 Local->Line = Parser->Source.Line;
3522 Local->Ident = Ident;
3523 ML_EXPR(IfExpr, if, if);
3524 mlc_if_case_t **CaseSlot = &IfExpr->Cases;
3525 do {
3526 mlc_if_case_t *Case = CaseSlot[0] = new(mlc_if_case_t);
3527 CaseSlot = &Case->Next;
3528 mlc_expr_t **ConditionSlot = &Case->Condition;
3529 ml_accept(Parser, MLT_IS);
3530 ml_value_t *Method = MLIsMethod;
3531 do {
3532 ML_EXPR(IdentExpr, ident, ident);
3533 IdentExpr->Ident = Ident;
3534 if (ml_parse2(Parser, MLT_NIL)) {
3535 ML_EXPR(NotExpr, parent, not);
3536 NotExpr->Child = ML_EXPR_END(IdentExpr);
3537 ConditionSlot[0] = ML_EXPR_END(NotExpr);
3538 ConditionSlot = &NotExpr->Next;
3539 Method = MLIsMethod;
3540 } else {
3541 if (ml_parse2(Parser, MLT_IN)) {
3542 Method = MLInMethod;
3543 } else if (ml_parse2(Parser, MLT_OPERATOR)) {
3544 Method = ml_method(Parser->Ident);
3545 }
3546 if (!Method) ml_parse_error(Parser, "ParseError", "Expected operator not %s", MLTokens[Parser->Token]);
3547 IdentExpr->Next = ml_accept_expression(Parser, EXPR_DEFAULT);
3548 ML_EXPR(CallExpr, parent_value, const_call);
3549 CallExpr->Value = Method;
3550 CallExpr->Child = ML_EXPR_END(IdentExpr);
3551 ConditionSlot[0] = ML_EXPR_END(CallExpr);
3552 ConditionSlot = &CallExpr->Next;
3553 }
3554 } while (ml_parse2(Parser, MLT_COMMA));
3555 if (Case->Condition->Next) {
3556 ML_EXPR(OrExpr, parent, or);
3557 OrExpr->Child = Case->Condition;
3558 Case->Condition = ML_EXPR_END(OrExpr);
3559 }
3560 ml_accept(Parser, MLT_DO);
3561 Case->Body = ml_accept_block(Parser);
3562 if (ml_parse2(Parser, MLT_ELSE)) {
3563 IfExpr->Else = ml_accept_block(Parser);
3564 ml_accept(Parser, MLT_END);
3565 break;
3566 }
3567 } while (!ml_parse2(Parser, MLT_END));
3568 WhenExpr->Child->Next = ML_EXPR_END(IfExpr);
3569 return ML_EXPR_END(WhenExpr);
3570 }
3571 case MLT_LOOP: {
3572 ml_next(Parser);
3573 ML_EXPR(LoopExpr, parent, loop);
3574 LoopExpr->Child = ml_accept_block(Parser);
3575 ml_accept(Parser, MLT_END);
3576 return ML_EXPR_END(LoopExpr);
3577 }
3578 case MLT_FOR: {
3579 ml_next(Parser);
3580 ML_EXPR(ForExpr, for, for);
3581 ml_accept_for_decl(Parser, ForExpr);
3582 ml_accept(Parser, MLT_IN);
3583 ForExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3584 ml_accept(Parser, MLT_DO);
3585 ForExpr->Child->Next = ml_accept_block(Parser);
3586 if (ml_parse2(Parser, MLT_ELSE)) {
3587 ForExpr->Child->Next->Next = ml_accept_block(Parser);
3588 }
3589 ml_accept(Parser, MLT_END);
3590 return ML_EXPR_END(ForExpr);
3591 }
3592 case MLT_FUN: {
3593 ml_next(Parser);
3594 if (ml_parse2(Parser, MLT_LEFT_PAREN)) {
3595 return ml_accept_fun_expr(Parser, NULL, MLT_RIGHT_PAREN);
3596 } else {
3597 ML_EXPR(FunExpr, fun, fun);
3598 FunExpr->Source = Parser->Source.Name;
3599 FunExpr->Body = ml_accept_expression(Parser, EXPR_DEFAULT);
3600 return ML_EXPR_END(FunExpr);
3601 }
3602 }
3603 case MLT_MACRO: {
3604 ml_next(Parser);
3605 return ml_accept_macro_expr(Parser);
3606 }
3607 case MLT_METH: {
3608 ml_next(Parser);
3609 return ml_accept_meth_expr(Parser);
3610 }
3611 case MLT_SUSP: {
3612 ml_next(Parser);
3613 ML_EXPR(SuspendExpr, parent, suspend);
3614 SuspendExpr->Child = ml_parse_expression(Parser, EXPR_DEFAULT);
3615 if (ml_parse(Parser, MLT_COMMA)) {
3616 SuspendExpr->Child->Next = ml_accept_expression(Parser, EXPR_DEFAULT);
3617 }
3618 return ML_EXPR_END(SuspendExpr);
3619 }
3620 case MLT_WITH: {
3621 ml_next(Parser);
3622 return ml_accept_with_expr(Parser, NULL);
3623 }
3624 case MLT_IDENT: {
3625 ml_next(Parser);
3626 ML_EXPR(IdentExpr, ident, ident);
3627 IdentExpr->Ident = Parser->Ident;
3628 return ML_EXPR_END(IdentExpr);
3629 }
3630 case MLT_VALUE: {
3631 ml_next(Parser);
3632 ML_EXPR(ValueExpr, value, value);
3633 ValueExpr->Value = Parser->Value;
3634 return ML_EXPR_END(ValueExpr);
3635 }
3636 case MLT_EXPR: {
3637 ml_next(Parser);
3638 return Parser->Expr;
3639 }
3640 case MLT_INLINE: {
3641 ml_next(Parser);
3642 ML_EXPR(InlineExpr, parent, inline);
3643 InlineExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3644 ml_accept(Parser, MLT_RIGHT_PAREN);
3645 return ML_EXPR_END(InlineExpr);
3646 }
3647 case MLT_EXPAND: {
3648 ml_next(Parser);
3649 ml_accept(Parser, MLT_IDENT);
3650 ML_EXPR(DefineExpr, ident, define);
3651 DefineExpr->Ident = Parser->Ident;
3652 return ML_EXPR_END(DefineExpr);
3653 }
3654 case MLT_LEFT_PAREN: {
3655 ml_next(Parser);
3656 if (ml_parse2(Parser, MLT_SEMICOLON)) {
3657 ML_EXPR(TupleExpr, parent, tuple);
3658 TupleExpr->Child = ml_accept_fun_expr(Parser, NULL, MLT_RIGHT_PAREN);
3659 return ML_EXPR_END(TupleExpr);
3660 }
3661 mlc_expr_t *Expr = ml_accept_expression(Parser, EXPR_DEFAULT);
3662 if (ml_parse2(Parser, MLT_COMMA)) {
3663 ML_EXPR(TupleExpr, parent, tuple);
3664 TupleExpr->Child = Expr;
3665 ml_accept_arguments(Parser, MLT_RIGHT_PAREN, &Expr->Next);
3666 Expr = ML_EXPR_END(TupleExpr);
3667 } else if (ml_parse2(Parser, MLT_SEMICOLON)) {
3668 ML_EXPR(TupleExpr, parent, tuple);
3669 TupleExpr->Child = Expr;
3670 Expr->Next = ml_accept_fun_expr(Parser, NULL, MLT_RIGHT_PAREN);
3671 Expr = ML_EXPR_END(TupleExpr);
3672 } else {
3673 ml_accept(Parser, MLT_RIGHT_PAREN);
3674 }
3675 return Expr;
3676 }
3677 case MLT_LEFT_SQUARE: {
3678 ml_next(Parser);
3679 ML_EXPR(ListExpr, parent, list);
3680 mlc_expr_t **ArgsSlot = &ListExpr->Child;
3681 if (!ml_parse2(Parser, MLT_RIGHT_SQUARE)) {
3682 do {
3683 mlc_expr_t *Arg = ArgsSlot[0] = ml_accept_expression(Parser, EXPR_DEFAULT);
3684 ArgsSlot = &Arg->Next;
3685 } while (ml_parse2(Parser, MLT_COMMA));
3686 ml_accept(Parser, MLT_RIGHT_SQUARE);
3687 }
3688 return ML_EXPR_END(ListExpr);
3689 }
3690 case MLT_LEFT_BRACE: {
3691 ml_next(Parser);
3692 ML_EXPR(MapExpr, parent, map);
3693 mlc_expr_t **ArgsSlot = &MapExpr->Child;
3694 if (!ml_parse2(Parser, MLT_RIGHT_BRACE)) {
3695 do {
3696 mlc_expr_t *Arg = ArgsSlot[0] = ml_accept_expression(Parser, EXPR_DEFAULT);
3697 ArgsSlot = &Arg->Next;
3698 if (ml_parse2(Parser, MLT_IS)) {
3699 mlc_expr_t *ArgExpr = ArgsSlot[0] = ml_accept_expression(Parser, EXPR_DEFAULT);
3700 ArgsSlot = &ArgExpr->Next;
3701 } else {
3702 ML_EXPR(ArgExpr, value, value);
3703 ArgExpr->Value = MLSome;
3704 ArgsSlot[0] = ML_EXPR_END(ArgExpr);
3705 ArgsSlot = &ArgExpr->Next;
3706 }
3707 } while (ml_parse2(Parser, MLT_COMMA));
3708 ml_accept(Parser, MLT_RIGHT_BRACE);
3709 }
3710 return ML_EXPR_END(MapExpr);
3711 }
3712 case MLT_OPERATOR: {
3713 ml_next(Parser);
3714 ml_value_t *Operator = ml_method(Parser->Ident);
3715 if (MethDecl) {
3716 ML_EXPR(ValueExpr, value, value);
3717 ValueExpr->Value = Operator;
3718 return ML_EXPR_END(ValueExpr);
3719 } else if (ml_parse(Parser, MLT_LEFT_PAREN)) {
3720 ML_EXPR(CallExpr, parent_value, const_call);
3721 CallExpr->Value = Operator;
3722 ml_accept_arguments(Parser, MLT_RIGHT_PAREN, &CallExpr->Child);
3723 return ML_EXPR_END(CallExpr);
3724 } else {
3725 mlc_expr_t *Child = ml_parse_term(Parser, 0);
3726 if (Child) {
3727 ML_EXPR(CallExpr, parent_value, const_call);
3728 CallExpr->Value = Operator;
3729 CallExpr->Child = Child;
3730 return ML_EXPR_END(CallExpr);
3731 } else {
3732 ML_EXPR(ValueExpr, value, value);
3733 ValueExpr->Value = Operator;
3734 return ML_EXPR_END(ValueExpr);
3735 }
3736 }
3737 }
3738 case MLT_METHOD: {
3739 ml_next(Parser);
3740 ML_EXPR(ValueExpr, value, value);
3741 ValueExpr->Value = ml_method(Parser->Ident);
3742 return ML_EXPR_END(ValueExpr);
3743 }
3744 default: return NULL;
3745 }
3746 }
3747
ml_parse_term(ml_parser_t * Parser,int MethDecl)3748 static mlc_expr_t *ml_parse_term(ml_parser_t *Parser, int MethDecl) {
3749 mlc_expr_t *Expr = ml_parse_factor(Parser, MethDecl);
3750 if (!Expr) return NULL;
3751 for (;;) {
3752 switch (ml_current(Parser)) {
3753 case MLT_LEFT_PAREN: {
3754 if (MethDecl) return Expr;
3755 ml_next(Parser);
3756 ML_EXPR(CallExpr, parent, call);
3757 CallExpr->Child = Expr;
3758 ml_accept_arguments(Parser, MLT_RIGHT_PAREN, &Expr->Next);
3759 Expr = ML_EXPR_END(CallExpr);
3760 break;
3761 }
3762 case MLT_LEFT_SQUARE: {
3763 ml_next(Parser);
3764 ML_EXPR(IndexExpr, parent_value, const_call);
3765 IndexExpr->Value = IndexMethod;
3766 IndexExpr->Child = Expr;
3767 ml_accept_arguments(Parser, MLT_RIGHT_SQUARE, &Expr->Next);
3768 Expr = ML_EXPR_END(IndexExpr);
3769 break;
3770 }
3771 case MLT_METHOD: {
3772 ml_next(Parser);
3773 ML_EXPR(CallExpr, parent_value, const_call);
3774 CallExpr->Value = ml_method(Parser->Ident);
3775 CallExpr->Child = Expr;
3776 if (ml_parse(Parser, MLT_LEFT_PAREN)) {
3777 ml_accept_arguments(Parser, MLT_RIGHT_PAREN, &Expr->Next);
3778 }
3779 Expr = ML_EXPR_END(CallExpr);
3780 break;
3781 }
3782 case MLT_IMPORT: {
3783 ml_next(Parser);
3784 ML_EXPR(ResolveExpr, parent_value, resolve);
3785 ResolveExpr->Value = ml_string(Parser->Ident, -1);
3786 ResolveExpr->Child = Expr;
3787 Expr = ML_EXPR_END(ResolveExpr);
3788 break;
3789 }
3790 default: {
3791 return Expr;
3792 }
3793 }
3794 }
3795 return NULL; // Unreachable
3796 }
3797
ml_accept_term(ml_parser_t * Parser)3798 static mlc_expr_t *ml_accept_term(ml_parser_t *Parser) {
3799 ml_skip_eol(Parser);
3800 mlc_expr_t *Expr = ml_parse_term(Parser, 0);
3801 if (!Expr) ml_parse_error(Parser, "ParseError", "expected <expression> not %s", MLTokens[Parser->Token]);
3802 return Expr;
3803 }
3804
ml_parse_expression(ml_parser_t * Parser,ml_expr_level_t Level)3805 static mlc_expr_t *ml_parse_expression(ml_parser_t *Parser, ml_expr_level_t Level) {
3806 mlc_expr_t *Expr = ml_parse_term(Parser, 0);
3807 if (!Expr) return NULL;
3808 for (;;) switch (ml_current(Parser)) {
3809 case MLT_OPERATOR: case MLT_IDENT: {
3810 ml_next(Parser);
3811 ML_EXPR(CallExpr, parent_value, const_call);
3812 CallExpr->Value = ml_method(Parser->Ident);
3813 CallExpr->Child = Expr;
3814 if (ml_parse2(Parser, MLT_LEFT_PAREN)) {
3815 ml_accept_arguments(Parser, MLT_RIGHT_PAREN, &Expr->Next);
3816 } else {
3817 Expr->Next = ml_accept_term(Parser);
3818 }
3819 Expr = ML_EXPR_END(CallExpr);
3820 break;
3821 }
3822 case MLT_ASSIGN: {
3823 ml_next(Parser);
3824 ML_EXPR(AssignExpr, parent, assign);
3825 AssignExpr->Child = Expr;
3826 Expr->Next = ml_accept_expression(Parser, EXPR_DEFAULT);
3827 Expr = ML_EXPR_END(AssignExpr);
3828 break;
3829 }
3830 case MLT_IN: {
3831 ml_next(Parser);
3832 ML_EXPR(CallExpr, parent_value, const_call);
3833 CallExpr->Value = MLInMethod;
3834 CallExpr->Child = Expr;
3835 Expr->Next = ml_accept_expression(Parser, EXPR_SIMPLE);
3836 Expr = ML_EXPR_END(CallExpr);
3837 break;
3838 }
3839 default: goto done;
3840 }
3841 done:
3842 if (Level >= EXPR_AND && ml_parse(Parser, MLT_AND)) {
3843 ML_EXPR(AndExpr, parent, and);
3844 mlc_expr_t *LastChild = AndExpr->Child = Expr;
3845 do {
3846 LastChild = LastChild->Next = ml_accept_expression(Parser, EXPR_SIMPLE);
3847 } while (ml_parse(Parser, MLT_AND));
3848 Expr = ML_EXPR_END(AndExpr);
3849 }
3850 if (Level >= EXPR_OR && ml_parse(Parser, MLT_OR)) {
3851 ML_EXPR(OrExpr, parent, or);
3852 mlc_expr_t *LastChild = OrExpr->Child = Expr;
3853 do {
3854 LastChild = LastChild->Next = ml_accept_expression(Parser, EXPR_AND);
3855 } while (ml_parse(Parser, MLT_OR));
3856 Expr = ML_EXPR_END(OrExpr);
3857 }
3858 if (Level >= EXPR_FOR) {
3859 if (ml_parse(Parser, MLT_WITH)) {
3860 Expr = ml_accept_with_expr(Parser, Expr);
3861 }
3862 int IsComprehension = 0;
3863 if (ml_parse(Parser, MLT_TO)) {
3864 Expr->Next = ml_accept_expression(Parser, EXPR_OR);
3865 ml_accept(Parser, MLT_FOR);
3866 IsComprehension = 1;
3867 } else {
3868 IsComprehension = ml_parse(Parser, MLT_FOR);
3869 }
3870 if (IsComprehension) {
3871 ML_EXPR(FunExpr, fun, fun);
3872 FunExpr->Source = Parser->Source.Name;
3873 ML_EXPR(SuspendExpr, parent, suspend);
3874 SuspendExpr->Child = Expr;
3875 mlc_expr_t *Body = ML_EXPR_END(SuspendExpr);
3876 do {
3877 ML_EXPR(ForExpr, for, for);
3878 ml_accept_for_decl(Parser, ForExpr);
3879 ml_accept(Parser, MLT_IN);
3880 ForExpr->Child = ml_accept_expression(Parser, EXPR_OR);
3881 for (;;) {
3882 if (ml_parse2(Parser, MLT_IF)) {
3883 ML_EXPR(IfExpr, if, if);
3884 mlc_if_case_t *IfCase = IfExpr->Cases = new(mlc_if_case_t);
3885 IfCase->Condition = ml_accept_expression(Parser, EXPR_OR);
3886 IfCase->Body = Body;
3887 Body = ML_EXPR_END(IfExpr);
3888 } else if (ml_parse2(Parser, MLT_WITH)) {
3889 Body = ml_accept_with_expr(Parser, Body);
3890 } else {
3891 break;
3892 }
3893 }
3894 ForExpr->Child->Next = Body;
3895 Body = ML_EXPR_END(ForExpr);
3896 } while (ml_parse2(Parser, MLT_FOR));
3897 FunExpr->Body = Body;
3898 FunExpr->StartLine = FunExpr->Body->StartLine;
3899 Expr = ML_EXPR_END(FunExpr);
3900 }
3901 }
3902 return Expr;
3903 }
3904
ml_accept_expression(ml_parser_t * Parser,ml_expr_level_t Level)3905 static mlc_expr_t *ml_accept_expression(ml_parser_t *Parser, ml_expr_level_t Level) {
3906 ml_skip_eol(Parser);
3907 mlc_expr_t *Expr = ml_parse_expression(Parser, Level);
3908 if (!Expr) ml_parse_error(Parser, "ParseError", "expected <expression> not %s", MLTokens[Parser->Token]);
3909 return Expr;
3910 }
3911
3912 typedef struct {
3913 mlc_expr_t **ExprSlot;
3914 mlc_local_t **VarsSlot;
3915 mlc_local_t **LetsSlot;
3916 mlc_local_t **DefsSlot;
3917 } ml_accept_block_t;
3918
ml_accept_block_var(ml_parser_t * Parser,ml_accept_block_t * Accept)3919 static void ml_accept_block_var(ml_parser_t *Parser, ml_accept_block_t *Accept) {
3920 do {
3921 if (ml_parse2(Parser, MLT_LEFT_PAREN)) {
3922 int Count = 0;
3923 mlc_local_t *Locals, **Slot = &Locals;
3924 do {
3925 if (!ml_parse2(Parser, MLT_BLANK)) ml_accept(Parser, MLT_IDENT);
3926 ++Count;
3927 mlc_local_t *Local = Slot[0] = new(mlc_local_t);
3928 Local->Line = Parser->Source.Line;
3929 Local->Ident = Parser->Ident;
3930 Slot = &Local->Next;
3931 } while (ml_parse2(Parser, MLT_COMMA));
3932 Accept->VarsSlot[0] = Locals;
3933 Accept->VarsSlot = Slot;
3934 ml_accept(Parser, MLT_RIGHT_PAREN);
3935 if (ml_parse2(Parser, MLT_IN)) {
3936 ML_EXPR(LocalExpr, local, var_in);
3937 LocalExpr->Local = Locals;
3938 LocalExpr->Count = Count;
3939 LocalExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3940 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
3941 Accept->ExprSlot = &LocalExpr->Next;
3942 } else {
3943 ml_accept(Parser, MLT_ASSIGN);
3944 ML_EXPR(LocalExpr, local, var_unpack);
3945 LocalExpr->Local = Locals;
3946 LocalExpr->Count = Count;
3947 LocalExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3948 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
3949 Accept->ExprSlot = &LocalExpr->Next;
3950 }
3951 } else {
3952 ml_accept(Parser, MLT_IDENT);
3953 mlc_local_t *Local = Accept->VarsSlot[0] = new(mlc_local_t);
3954 Local->Line = Parser->Source.Line;
3955 Local->Ident = Parser->Ident;
3956 Accept->VarsSlot = &Local->Next;
3957 if (ml_parse(Parser, MLT_COLON)) {
3958 ML_EXPR(TypeExpr, local, var_type);
3959 TypeExpr->Local = Local;
3960 TypeExpr->Child = ml_accept_term(Parser);
3961 Accept->ExprSlot[0] = ML_EXPR_END(TypeExpr);
3962 Accept->ExprSlot = &TypeExpr->Next;
3963 }
3964 mlc_expr_t *Child = NULL;
3965 if (ml_parse(Parser, MLT_LEFT_PAREN)) {
3966 Child = ml_accept_fun_expr(Parser, Local->Ident, MLT_RIGHT_PAREN);
3967 } else if (ml_parse(Parser, MLT_ASSIGN)) {
3968 Child = ml_accept_expression(Parser, EXPR_DEFAULT);
3969 }
3970 if (Child) {
3971 ML_EXPR(LocalExpr, local, var);
3972 LocalExpr->Local = Local;
3973 LocalExpr->Child = Child;
3974 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
3975 Accept->ExprSlot = &LocalExpr->Next;
3976 }
3977 }
3978 } while (ml_parse(Parser, MLT_COMMA));
3979 }
3980
ml_accept_block_let(ml_parser_t * Parser,ml_accept_block_t * Accept,int Flags)3981 static void ml_accept_block_let(ml_parser_t *Parser, ml_accept_block_t *Accept, int Flags) {
3982 do {
3983 if (ml_parse2(Parser, MLT_LEFT_PAREN)) {
3984 int Count = 0;
3985 mlc_local_t *Locals, **Slot = &Locals;
3986 do {
3987 if (!ml_parse2(Parser, MLT_BLANK)) ml_accept(Parser, MLT_IDENT);
3988 ++Count;
3989 mlc_local_t *Local = Slot[0] = new(mlc_local_t);
3990 Local->Line = Parser->Source.Line;
3991 Local->Ident = Parser->Ident;
3992 Slot = &Local->Next;
3993 } while (ml_parse2(Parser, MLT_COMMA));
3994 Accept->LetsSlot[0] = Locals;
3995 Accept->LetsSlot = Slot;
3996 ml_accept(Parser, MLT_RIGHT_PAREN);
3997 if (ml_parse2(Parser, MLT_IN)) {
3998 ML_EXPR(LocalExpr, local, let_in);
3999 LocalExpr->Local = Locals;
4000 LocalExpr->Count = Count;
4001 LocalExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
4002 LocalExpr->Flags = Flags;
4003 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
4004 Accept->ExprSlot = &LocalExpr->Next;
4005 } else {
4006 ml_accept(Parser, MLT_ASSIGN);
4007 ML_EXPR(LocalExpr, local, let_unpack);
4008 LocalExpr->Local = Locals;
4009 LocalExpr->Count = Count;
4010 LocalExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
4011 LocalExpr->Flags = Flags;
4012 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
4013 Accept->ExprSlot = &LocalExpr->Next;
4014 }
4015 } else {
4016 ml_accept(Parser, MLT_IDENT);
4017 mlc_local_t *Local = Accept->LetsSlot[0] = new(mlc_local_t);
4018 Local->Line = Parser->Source.Line;
4019 Local->Ident = Parser->Ident;
4020 Accept->LetsSlot = &Local->Next;
4021 ML_EXPR(LocalExpr, local, let);
4022 if (ml_parse2(Parser, MLT_LEFT_PAREN)) {
4023 LocalExpr->Child = ml_accept_fun_expr(Parser, Local->Ident, MLT_RIGHT_PAREN);
4024 } else {
4025 ml_accept(Parser, MLT_ASSIGN);
4026 LocalExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
4027 }
4028 LocalExpr->Local = Local;
4029 LocalExpr->Flags = Flags;
4030 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
4031 Accept->ExprSlot = &LocalExpr->Next;
4032 }
4033 } while (ml_parse(Parser, MLT_COMMA));
4034 }
4035
ml_accept_block_def(ml_parser_t * Parser,ml_accept_block_t * Accept)4036 static void ml_accept_block_def(ml_parser_t *Parser, ml_accept_block_t *Accept) {
4037 do {
4038 if (ml_parse2(Parser, MLT_LEFT_PAREN)) {
4039 int Count = 0;
4040 mlc_local_t *Locals, **Slot = &Locals;
4041 do {
4042 if (!ml_parse2(Parser, MLT_BLANK)) ml_accept(Parser, MLT_IDENT);
4043 ++Count;
4044 mlc_local_t *Local = Slot[0] = new(mlc_local_t);
4045 Local->Line = Parser->Source.Line;
4046 Local->Ident = Parser->Ident;
4047 Slot = &Local->Next;
4048 } while (ml_parse2(Parser, MLT_COMMA));
4049 Accept->DefsSlot[0] = Locals;
4050 Accept->DefsSlot = Slot;
4051 ml_accept(Parser, MLT_RIGHT_PAREN);
4052 if (ml_parse2(Parser, MLT_IN)) {
4053 ML_EXPR(LocalExpr, local, def_in);
4054 LocalExpr->Local = Locals;
4055 LocalExpr->Count = Count;
4056 LocalExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
4057 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
4058 Accept->ExprSlot = &LocalExpr->Next;
4059 } else {
4060 ml_accept(Parser, MLT_ASSIGN);
4061 ML_EXPR(LocalExpr, local, def_unpack);
4062 LocalExpr->Local = Locals;
4063 LocalExpr->Count = Count;
4064 LocalExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
4065 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
4066 Accept->ExprSlot = &LocalExpr->Next;
4067 }
4068 } else {
4069 ml_accept(Parser, MLT_IDENT);
4070 mlc_local_t *Local = Accept->DefsSlot[0] = new(mlc_local_t);
4071 Local->Line = Parser->Source.Line;
4072 Local->Ident = Parser->Ident;
4073 Accept->DefsSlot = &Local->Next;
4074 ML_EXPR(LocalExpr, local, def);
4075 if (ml_parse2(Parser, MLT_LEFT_PAREN)) {
4076 LocalExpr->Child = ml_accept_fun_expr(Parser, Local->Ident, MLT_RIGHT_PAREN);
4077 } else {
4078 ml_accept(Parser, MLT_ASSIGN);
4079 LocalExpr->Child = ml_accept_expression(Parser, EXPR_DEFAULT);
4080 }
4081 LocalExpr->Local = Local;
4082 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
4083 Accept->ExprSlot = &LocalExpr->Next;
4084 }
4085 } while (ml_parse(Parser, MLT_COMMA));
4086 }
4087
ml_accept_block_fun(ml_parser_t * Parser,ml_accept_block_t * Accept)4088 static void ml_accept_block_fun(ml_parser_t *Parser, ml_accept_block_t *Accept) {
4089 if (ml_parse2(Parser, MLT_IDENT)) {
4090 mlc_local_t *Local = Accept->LetsSlot[0] = new(mlc_local_t);
4091 Local->Line = Parser->Source.Line;
4092 Local->Ident = Parser->Ident;
4093 Accept->LetsSlot = &Local->Next;
4094 ml_accept(Parser, MLT_LEFT_PAREN);
4095 ML_EXPR(LocalExpr, local, let);
4096 LocalExpr->Local = Local;
4097 LocalExpr->Child = ml_accept_fun_expr(Parser, Local->Ident, MLT_RIGHT_PAREN);
4098 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
4099 Accept->ExprSlot = &LocalExpr->Next;
4100 } else {
4101 ml_accept(Parser, MLT_LEFT_PAREN);
4102 mlc_expr_t *Expr = ml_accept_fun_expr(Parser, NULL, MLT_RIGHT_PAREN);
4103 Accept->ExprSlot[0] = Expr;
4104 Accept->ExprSlot = &Expr->Next;
4105 }
4106 }
4107
ml_accept_block_macro(ml_parser_t * Parser,ml_accept_block_t * Accept)4108 static void ml_accept_block_macro(ml_parser_t *Parser, ml_accept_block_t *Accept) {
4109 if (ml_parse2(Parser, MLT_IDENT)) {
4110 mlc_local_t *Local = Accept->DefsSlot[0] = new(mlc_local_t);
4111 Local->Line = Parser->Source.Line;
4112 Local->Ident = Parser->Ident;
4113 Accept->DefsSlot = &Local->Next;
4114 ML_EXPR(LocalExpr, local, def);
4115 LocalExpr->Local = Local;
4116 LocalExpr->Child = ml_accept_macro_expr(Parser);
4117 Accept->ExprSlot[0] = ML_EXPR_END(LocalExpr);
4118 Accept->ExprSlot = &LocalExpr->Next;
4119 } else {
4120 mlc_expr_t *Expr = ml_accept_macro_expr(Parser);
4121 Accept->ExprSlot[0] = Expr;
4122 Accept->ExprSlot = &Expr->Next;
4123 }
4124 }
4125
ml_accept_block_export(ml_parser_t * Parser,mlc_expr_t * Expr,mlc_local_t * Export)4126 static mlc_expr_t *ml_accept_block_export(ml_parser_t *Parser, mlc_expr_t *Expr, mlc_local_t *Export) {
4127 ML_EXPR(CallExpr, parent, call);
4128 CallExpr->Child = Expr;
4129 ml_value_t *Names = ml_names();
4130 ML_EXPR(NamesExpr, value, value);
4131 NamesExpr->Value = Names;
4132 Expr->Next = ML_EXPR_END(NamesExpr);
4133 mlc_expr_t **ArgsSlot = &NamesExpr->Next;
4134 while (Export) {
4135 ml_names_add(Names, ml_cstring(Export->Ident));
4136 ML_EXPR(IdentExpr, ident, ident);
4137 IdentExpr->Ident = Export->Ident;
4138 ArgsSlot[0] = ML_EXPR_END(IdentExpr);
4139 ArgsSlot = &IdentExpr->Next;
4140 Export = Export->Next;
4141 }
4142 return ML_EXPR_END(CallExpr);
4143 }
4144
ml_parse_block_expr(ml_parser_t * Parser,ml_accept_block_t * Accept)4145 static mlc_expr_t *ml_parse_block_expr(ml_parser_t *Parser, ml_accept_block_t *Accept) {
4146 mlc_expr_t *Expr = ml_parse_expression(Parser, EXPR_DEFAULT);
4147 if (!Expr) return NULL;
4148 if (ml_parse(Parser, MLT_COLON)) {
4149 if (ml_parse2(Parser, MLT_VAR)) {
4150 mlc_local_t **Exports = Accept->VarsSlot;
4151 ml_accept_block_var(Parser, Accept);
4152 Expr = ml_accept_block_export(Parser, Expr, Exports[0]);
4153 } else if (ml_parse2(Parser, MLT_LET)) {
4154 mlc_local_t **Exports = Accept->LetsSlot;
4155 ml_accept_block_let(Parser, Accept, MLT_LET);
4156 Expr = ml_accept_block_export(Parser, Expr, Exports[0]);
4157 } else if (ml_parse2(Parser, MLT_REF)) {
4158 mlc_local_t **Exports = Accept->LetsSlot;
4159 ml_accept_block_let(Parser, Accept, MLT_REF);
4160 Expr = ml_accept_block_export(Parser, Expr, Exports[0]);
4161 } else if (ml_parse2(Parser, MLT_DEF)) {
4162 mlc_local_t **Exports = Accept->DefsSlot;
4163 ml_accept_block_def(Parser, Accept);
4164 Expr = ml_accept_block_export(Parser, Expr, Exports[0]);
4165 } else if (ml_parse2(Parser, MLT_FUN)) {
4166 mlc_local_t **Exports = Accept->LetsSlot;
4167 ml_accept_block_fun(Parser, Accept);
4168 Expr = ml_accept_block_export(Parser, Expr, Exports[0]);
4169 } else {
4170 ml_accept_block_t Previous = *Accept;
4171 mlc_expr_t *Child = ml_parse_block_expr(Parser, Accept);
4172 if (!Child) ml_parse_error(Parser, "ParseError", "Expected expression");
4173 if (Accept->VarsSlot != Previous.VarsSlot) {
4174 Accept->ExprSlot[0] = Child;
4175 Accept->ExprSlot = &Child->Next;
4176 Expr = ml_accept_block_export(Parser, Expr, Previous.VarsSlot[0]);
4177 } else if (Accept->LetsSlot != Previous.LetsSlot) {
4178 Accept->ExprSlot[0] = Child;
4179 Accept->ExprSlot = &Child->Next;
4180 Expr = ml_accept_block_export(Parser, Expr, Previous.LetsSlot[0]);
4181 } else if (Accept->DefsSlot != Previous.DefsSlot) {
4182 Accept->ExprSlot[0] = Child;
4183 Accept->ExprSlot = &Child->Next;
4184 Expr = ml_accept_block_export(Parser, Expr, Previous.DefsSlot[0]);
4185 } else {
4186 mlc_parent_expr_t *CallExpr = (mlc_parent_expr_t *)Child;
4187 if (CallExpr->compile != ml_call_expr_compile) {
4188 ml_parse_error(Parser, "ParseError", "Invalid declaration");
4189 }
4190 mlc_ident_expr_t *IdentExpr = (mlc_ident_expr_t *)CallExpr->Child;
4191 if (!IdentExpr || IdentExpr->compile != ml_ident_expr_compile) {
4192 ml_parse_error(Parser, "ParseError", "Invalid declaration");
4193 }
4194 mlc_local_t *Local = Accept->DefsSlot[0] = new(mlc_local_t);
4195 Local->Line = IdentExpr->StartLine;
4196 Local->Ident = IdentExpr->Ident;
4197 Accept->DefsSlot = &Local->Next;
4198 ML_EXPR(LocalExpr, local, def);
4199 LocalExpr->Local = Local;
4200 Expr->Next = IdentExpr->Next;
4201 CallExpr->Child = Expr;
4202 LocalExpr->Child = ML_EXPR_END(CallExpr);
4203 Expr = ML_EXPR_END(LocalExpr);
4204 }
4205 }
4206 }
4207 return Expr;
4208 }
4209
ml_accept_block_body(ml_parser_t * Parser)4210 static mlc_block_expr_t *ml_accept_block_body(ml_parser_t *Parser) {
4211 ML_EXPR(BlockExpr, block, block);
4212 ml_accept_block_t Accept[1];
4213 Accept->ExprSlot = &BlockExpr->Child;
4214 Accept->VarsSlot = &BlockExpr->Vars;
4215 Accept->LetsSlot = &BlockExpr->Lets;
4216 Accept->DefsSlot = &BlockExpr->Defs;
4217 do {
4218 ml_skip_eol(Parser);
4219 switch (ml_current(Parser)) {
4220 case MLT_VAR: {
4221 ml_next(Parser);
4222 ml_accept_block_var(Parser, Accept);
4223 break;
4224 }
4225 case MLT_LET: {
4226 ml_next(Parser);
4227 ml_accept_block_let(Parser, Accept, MLT_LET);
4228 break;
4229 }
4230 case MLT_REF: {
4231 ml_next(Parser);
4232 ml_accept_block_let(Parser, Accept, MLT_REF);
4233 break;
4234 }
4235 case MLT_DEF: {
4236 ml_next(Parser);
4237 ml_accept_block_def(Parser, Accept);
4238 break;
4239 }
4240 case MLT_FUN: {
4241 ml_next(Parser);
4242 ml_accept_block_fun(Parser, Accept);
4243 break;
4244 }
4245 case MLT_MACRO: {
4246 ml_next(Parser);
4247 ml_accept_block_macro(Parser, Accept);
4248 break;
4249 }
4250 default: {
4251 mlc_expr_t *Expr = ml_parse_block_expr(Parser, Accept);
4252 if (!Expr) goto finish;
4253 Accept->ExprSlot[0] = Expr;
4254 Accept->ExprSlot = &Expr->Next;
4255 break;
4256 }
4257 }
4258 } while (ml_parse(Parser, MLT_SEMICOLON) || ml_parse(Parser, MLT_EOL));
4259 finish: {
4260 int Index = 0, First = 0;
4261 for (mlc_local_t *Local = BlockExpr->Vars; Local; Local = Local->Next) {
4262 Local->Index = Index++;
4263 }
4264 BlockExpr->NumVars = Index;
4265 First = Index;
4266 for (mlc_local_t *Local = BlockExpr->Lets; Local; Local = Local->Next) {
4267 Local->Index = Index++;
4268 }
4269 BlockExpr->NumLets = Index - First;
4270 First = Index;
4271 for (mlc_local_t *Local = BlockExpr->Defs; Local; Local = Local->Next) {
4272 Local->Index = Index++;
4273 }
4274 BlockExpr->NumDefs = Index - First;
4275 }
4276 return BlockExpr;
4277 }
4278
ml_accept_block(ml_parser_t * Parser)4279 static mlc_expr_t *ml_accept_block(ml_parser_t *Parser) {
4280 mlc_block_expr_t *BlockExpr = ml_accept_block_body(Parser);
4281 if (ml_parse(Parser, MLT_ON)) {
4282 mlc_catch_expr_t **CatchSlot = &BlockExpr->Catches;
4283 do {
4284 mlc_catch_expr_t *CatchExpr = CatchSlot[0] = new(mlc_catch_expr_t);
4285 CatchExpr->Line = Parser->Source.Line;
4286 CatchSlot = &CatchExpr->Next;
4287 ml_accept(Parser, MLT_IDENT);
4288 CatchExpr->Ident = Parser->Ident;
4289 if (ml_parse2(Parser, MLT_COLON)) {
4290 mlc_catch_type_t **TypeSlot = &CatchExpr->Types;
4291 do {
4292 ml_accept(Parser, MLT_VALUE);
4293 ml_value_t *Value = Parser->Value;
4294 if (!ml_is(Value, MLStringT)) {
4295 ml_parse_error(Parser, "ParseError", "Expected <string> not <%s>", ml_typeof(Value)->Name);
4296 }
4297 mlc_catch_type_t *Type = TypeSlot[0] = new(mlc_catch_type_t);
4298 TypeSlot = &Type->Next;
4299 Type->Type = ml_string_value(Value);
4300 } while (ml_parse2(Parser, MLT_COMMA));
4301 }
4302 ml_accept(Parser, MLT_DO);
4303 mlc_block_expr_t *Body = ml_accept_block_body(Parser);
4304 CatchExpr->Body = ML_EXPR_END(Body);
4305 } while (ml_parse(Parser, MLT_ON));
4306 }
4307 return ML_EXPR_END(BlockExpr);
4308 }
4309
ml_accept_file(ml_parser_t * Parser)4310 mlc_expr_t *ml_accept_file(ml_parser_t *Parser) {
4311 if (setjmp(Parser->OnError)) return NULL;
4312 mlc_expr_t *Expr = ml_accept_block(Parser);
4313 ml_accept_eoi(Parser);
4314 return Expr;
4315 }
4316
ml_function_compile2(mlc_function_t * Function,ml_value_t * Value,mlc_compile_frame_t * Frame)4317 static void ml_function_compile2(mlc_function_t *Function, ml_value_t *Value, mlc_compile_frame_t *Frame) {
4318 ml_closure_info_t *Info = Frame->Info;
4319 mlc_expr_t *Expr = Frame->Expr;
4320 Info->Return = MLC_EMIT(Expr->EndLine, MLI_RETURN, 0);
4321 MLC_LINK(Function->Returns, Info->Return);
4322 Info->Halt = Function->Next;
4323 Info->StartLine = Expr->StartLine;
4324 Info->EndLine = Expr->EndLine;
4325 Info->FrameSize = Function->Size;
4326 Info->Decls = Function->Decls;
4327 ml_state_t *Caller = Function->Base.Caller;
4328 ML_RETURN(ml_closure(Info));
4329 }
4330
ml_function_compile(ml_state_t * Caller,mlc_expr_t * Expr,ml_compiler_t * Compiler,const char ** Parameters)4331 void ml_function_compile(ml_state_t *Caller, mlc_expr_t *Expr, ml_compiler_t *Compiler, const char **Parameters) {
4332 mlc_function_t *Function = new(mlc_function_t);
4333 Function->Base.Type = MLCompilerFunctionT;
4334 Function->Base.Caller = Caller;
4335 Function->Base.Context = Caller->Context;
4336 Function->Base.run = (ml_state_fn)mlc_function_run;
4337 Function->Compiler = Compiler;
4338 Function->Source = Expr->Source;
4339 ml_closure_info_t *Info = new(ml_closure_info_t);
4340 int NumParams = 0;
4341 if (Parameters) {
4342 ml_decl_t **ParamSlot = &Function->Decls;
4343 for (const char **P = Parameters; P[0]; ++P) {
4344 ml_decl_t *Param = new(ml_decl_t);
4345 Param->Source.Name = Function->Source;
4346 Param->Source.Line = Expr->StartLine;
4347 Param->Ident = P[0];
4348 Param->Hash = ml_ident_hash(P[0]);
4349 Param->Index = Function->Top++;
4350 stringmap_insert(Info->Params, Param->Ident, (void *)(intptr_t)Function->Top);
4351 ParamSlot[0] = Param;
4352 ParamSlot = &Param->Next;
4353 }
4354 NumParams = Function->Top;
4355 Function->Size = Function->Top + 1;
4356 }
4357 Info->NumParams = NumParams;
4358 Info->Source = Function->Source;
4359 Info->StartLine = Expr->StartLine;
4360 Info->EndLine = Expr->EndLine;
4361 asprintf((char **)&Info->Name, "<%s:%d>", Info->Source, Info->StartLine);
4362 Function->Next = Info->Entry = anew(ml_inst_t, 128);
4363 Function->Space = 126;
4364 Function->Returns = NULL;
4365 MLC_FRAME(mlc_compile_frame_t, ml_function_compile2);
4366 Frame->Info = Info;
4367 Frame->Expr = Expr;
4368 mlc_compile(Function, Expr, 0);
4369 }
4370
4371 ML_METHODX("compile", MLParserT, MLCompilerT) {
4372 //<Parser
4373 //<Compiler
4374 //>any
4375 ml_parser_t *Parser = (ml_parser_t *)Args[0];
4376 ml_compiler_t *Compiler = (ml_compiler_t *)Args[1];
4377 mlc_expr_t *Expr = ml_accept_file(Parser);
4378 if (!Expr) ML_RETURN(Parser->Value);
4379 return ml_function_compile(Caller, Expr, Compiler, NULL);
4380 }
4381
4382 ML_METHODX("compile", MLParserT, MLCompilerT, MLListT) {
4383 //<Parser
4384 //<Compiler
4385 //<Parameters
4386 //>any
4387 ml_parser_t *Parser = (ml_parser_t *)Args[0];
4388 ml_compiler_t *Compiler = (ml_compiler_t *)Args[1];
4389 mlc_expr_t *Expr = ml_accept_file(Parser);
4390 if (!Expr) ML_RETURN(Parser->Value);
4391 const char **Parameters = anew(const char *, ml_list_length(Args[2]));
4392 int I = 0;
ML_LIST_FOREACH(Args[2],Iter)4393 ML_LIST_FOREACH(Args[2], Iter) {
4394 if (!ml_is(Iter->Value, MLStringT)) ML_ERROR("TypeError", "Parameter name must be a string");
4395 Parameters[I++] = ml_string_value(Iter->Value);
4396 }
4397 return ml_function_compile(Caller, Expr, Compiler, Parameters);
4398 }
4399
4400 ML_METHOD("source", MLParserT, MLStringT, MLIntegerT) {
4401 //<Parser
4402 //<Source
4403 //<Line
4404 //>tuple
4405 ml_parser_t *Parser = (ml_parser_t *)Args[0];
4406 ml_source_t Source = {ml_string_value(Args[1]), ml_integer_value(Args[2])};
4407 Source = ml_parser_source(Parser, Source);
4408 ml_value_t *Tuple = ml_tuple(2);
4409 ml_tuple_set(Tuple, 1, ml_cstring(Source.Name));
4410 ml_tuple_set(Tuple, 2, ml_integer(Source.Line));
4411 return Tuple;
4412 }
4413
4414 ML_METHOD("reset", MLParserT) {
4415 //<Parser
4416 //>parser
4417 ml_parser_t *Parser = (ml_parser_t *)Args[0];
4418 ml_parser_reset(Parser);
4419 return Args[0];
4420 }
4421
4422 ML_METHOD("input", MLParserT, MLStringT) {
4423 //<Parser
4424 //<String
4425 //>compiler
4426 ml_parser_t *Parser = (ml_parser_t *)Args[0];
4427 ml_parser_input(Parser, ml_string_value(Args[1]));
4428 return Args[0];
4429 }
4430
4431 ML_METHOD("clear", MLParserT) {
4432 //<Parser
4433 //>string
4434 ml_parser_t *Parser = (ml_parser_t *)Args[0];
4435 return ml_cstring(ml_parser_clear(Parser));
4436 }
4437
4438 ML_METHODX("evaluate", MLParserT, MLCompilerT) {
4439 //<Parser
4440 //<Compiler
4441 //>any
4442 ml_parser_t *Parser = (ml_parser_t *)Args[0];
4443 ml_compiler_t *Compiler = (ml_compiler_t *)Args[1];
4444 return ml_command_evaluate(Caller, Parser, Compiler);
4445 }
4446
4447 typedef struct {
4448 ml_state_t Base;
4449 ml_parser_t *Parser;
4450 ml_compiler_t *Compiler;
4451 } ml_evaluate_state_t;
4452
ml_evaluate_state_run(ml_evaluate_state_t * State,ml_value_t * Value)4453 static void ml_evaluate_state_run(ml_evaluate_state_t *State, ml_value_t *Value) {
4454 if (Value == MLEndOfInput) ML_CONTINUE(State->Base.Caller, MLNil);
4455 return ml_command_evaluate((ml_state_t *)State, State->Parser, State->Compiler);
4456 }
4457
4458 ML_METHODX("run", MLParserT, MLCompilerT) {
4459 //<Compiler
4460 //>any
4461 ml_parser_t *Parser = (ml_parser_t *)Args[0];
4462 ml_compiler_t *Compiler = (ml_compiler_t *)Args[1];
4463 ml_evaluate_state_t *State = new(ml_evaluate_state_t);
4464 State->Base.Caller = Caller;
4465 State->Base.Context = Caller->Context;
4466 State->Base.run = (ml_state_fn)ml_evaluate_state_run;
4467 State->Parser = Parser;
4468 State->Compiler = Compiler;
4469 return ml_command_evaluate((ml_state_t *)State, Parser, Compiler);
4470 }
4471
4472 ML_METHOD("[]", MLCompilerT, MLStringT) {
4473 //<Compiler
4474 //<Name
4475 //>any
4476 ml_compiler_t *Compiler = (ml_compiler_t *)Args[0];
4477 return (ml_value_t *)stringmap_search(Compiler->Vars, ml_string_value(Args[1])) ?: MLNil;
4478 }
4479
4480 ml_value_t MLEndOfInput[1] = {{MLAnyT}};
4481 ml_value_t MLNotFound[1] = {{MLAnyT}};
4482
ml_stringmap_global(stringmap_t * Globals,int Count,ml_value_t ** Args)4483 static ml_value_t *ml_stringmap_global(stringmap_t *Globals, int Count, ml_value_t **Args) {
4484 ML_CHECK_ARG_COUNT(1);
4485 ML_CHECK_ARG_TYPE(0, MLStringT);
4486 ml_value_t *Value = (ml_value_t *)stringmap_search(Globals, ml_string_value(Args[0]));
4487 return Value ?: MLNotFound;
4488 }
4489
ml_stringmap_globals(stringmap_t * Globals)4490 ml_value_t *ml_stringmap_globals(stringmap_t *Globals) {
4491 return ml_cfunction(Globals, (ml_callback_t)ml_stringmap_global);
4492 }
4493
4494 ML_METHOD("var", MLCompilerT, MLStringT) {
4495 //<Compiler
4496 //<Name
4497 //>variable
4498 ml_compiler_t *Compiler = (ml_compiler_t *)Args[0];
4499 const char *Name = ml_string_value(Args[1]);
4500 ml_value_t *Var = ml_variable(MLNil, NULL);
4501 stringmap_insert(Compiler->Vars, Name, Var);
4502 return Var;
4503 }
4504
4505 ML_METHOD("var", MLCompilerT, MLStringT, MLTypeT) {
4506 //<Compiler
4507 //<Name
4508 //<Type
4509 //>variable
4510 ml_compiler_t *Compiler = (ml_compiler_t *)Args[0];
4511 const char *Name = ml_string_value(Args[1]);
4512 ml_value_t *Var = ml_variable(MLNil, (ml_type_t *)Args[2]);
4513 stringmap_insert(Compiler->Vars, Name, Var);
4514 return Var;
4515 }
4516
4517 ML_METHOD("let", MLCompilerT, MLStringT, MLAnyT) {
4518 //<Compiler
4519 //<Name
4520 //<Value
4521 //>any
4522 ml_compiler_t *Compiler = (ml_compiler_t *)Args[0];
4523 const char *Name = ml_string_value(Args[1]);
4524 stringmap_insert(Compiler->Vars, Name, Args[2]);
4525 return Args[2];
4526 }
4527
4528 ML_METHOD("def", MLCompilerT, MLStringT, MLAnyT) {
4529 //<Compiler
4530 //<Name
4531 //<Value
4532 //>any
4533 ml_compiler_t *Compiler = (ml_compiler_t *)Args[0];
4534 const char *Name = ml_string_value(Args[1]);
4535 stringmap_insert(Compiler->Vars, Name, Args[2]);
4536 return Args[2];
4537 }
4538
ml_compiler_var_fn(const char * Name,ml_value_t * Value,ml_value_t * Vars)4539 static int ml_compiler_var_fn(const char *Name, ml_value_t *Value, ml_value_t *Vars) {
4540 ml_map_insert(Vars, ml_cstring(Name), ml_deref(Value));
4541 return 0;
4542 }
4543
4544 ML_METHOD("vars", MLCompilerT) {
4545 //<Compiler
4546 //>map
4547 ml_compiler_t *Compiler = (ml_compiler_t *)Args[0];
4548 ml_value_t *Vars = ml_map();
4549 stringmap_foreach(Compiler->Vars, Vars, (void *)ml_compiler_var_fn);
4550 return Vars;
4551 }
4552
4553 typedef struct {
4554 ml_type_t *Type;
4555 ml_value_t *Value;
4556 const char *Name;
4557 } ml_global_t;
4558
ml_global_deref(ml_global_t * Global)4559 static ml_value_t *ml_global_deref(ml_global_t *Global) {
4560 if (!Global->Value) return ml_error("NameError", "identifier %s not declared", Global->Name);
4561 return ml_deref(Global->Value);
4562 }
4563
ml_global_assign(ml_global_t * Global,ml_value_t * Value)4564 static ml_value_t *ml_global_assign(ml_global_t *Global, ml_value_t *Value) {
4565 if (!Global->Value) return ml_error("NameError", "identifier %s not declared", Global->Name);
4566 return ml_assign(Global->Value, Value);
4567 }
4568
ml_global_call(ml_state_t * Caller,ml_global_t * Global,int Count,ml_value_t ** Args)4569 static void ml_global_call(ml_state_t *Caller, ml_global_t *Global, int Count, ml_value_t **Args) {
4570 return ml_call(Caller, Global->Value, Count, Args);
4571 }
4572
4573 ML_TYPE(MLGlobalT, (), "global",
4574 //!compiler
4575 .deref = (void *)ml_global_deref,
4576 .assign = (void *)ml_global_assign,
4577 .call = (void *)ml_global_call
4578 );
4579
ml_global(const char * Name)4580 ml_value_t *ml_global(const char *Name) {
4581 ml_global_t *Global = new(ml_global_t);
4582 Global->Type = MLGlobalT;
4583 Global->Name = Name;
4584 return (ml_value_t *)Global;
4585 }
4586
ml_global_get(ml_value_t * Global)4587 ml_value_t *ml_global_get(ml_value_t *Global) {
4588 return ((ml_global_t *)Global)->Value;
4589 }
4590
ml_global_set(ml_value_t * Global,ml_value_t * Value)4591 ml_value_t *ml_global_set(ml_value_t *Global, ml_value_t *Value) {
4592 return ((ml_global_t *)Global)->Value = Value;
4593 }
4594
ML_TYPED_FN(ml_unpack,MLGlobalT,ml_global_t * Global,int Index)4595 static ml_value_t *ML_TYPED_FN(ml_unpack, MLGlobalT, ml_global_t *Global, int Index) {
4596 return ml_unpack(Global->Value, Index);
4597 }
4598
4599 ML_METHOD("command_var", MLCompilerT, MLStringT) {
4600 //<Compiler
4601 //<Name
4602 //>variable
4603 ml_compiler_t *Compiler = (ml_compiler_t *)Args[0];
4604 const char *Name = ml_string_value(Args[1]);
4605 ml_value_t **Slot = (ml_value_t **)stringmap_slot(Compiler->Vars, Name);
4606 if (!Slot[0] || ml_typeof(Slot[0]) != MLGlobalT) {
4607 Slot[0] = ml_global(Name);
4608 }
4609 return ml_global_set(Slot[0], ml_variable(MLNil, NULL));
4610 }
4611
4612 ML_METHOD("command_var", MLCompilerT, MLStringT, MLTypeT) {
4613 //<Compiler
4614 //<Name
4615 //<Type
4616 //>variable
4617 ml_compiler_t *Compiler = (ml_compiler_t *)Args[0];
4618 const char *Name = ml_string_value(Args[1]);
4619 ml_value_t **Slot = (ml_value_t **)stringmap_slot(Compiler->Vars, Name);
4620 if (!Slot[0] || ml_typeof(Slot[0]) != MLGlobalT) {
4621 Slot[0] = ml_global(Name);
4622 }
4623 return ml_global_set(Slot[0], ml_variable(MLNil, (ml_type_t *)Args[2]));
4624 }
4625
4626 ML_METHOD("command_let", MLCompilerT, MLStringT, MLAnyT) {
4627 //<Compiler
4628 //<Name
4629 //<Value
4630 //>any
4631 ml_compiler_t *Compiler = (ml_compiler_t *)Args[0];
4632 const char *Name = ml_string_value(Args[1]);
4633 ml_value_t **Slot = (ml_value_t **)stringmap_slot(Compiler->Vars, Name);
4634 if (!Slot[0] || ml_typeof(Slot[0]) != MLGlobalT) {
4635 Slot[0] = ml_global(Name);
4636 }
4637 return ml_global_set(Slot[0], Args[2]);
4638 }
4639
4640 ML_METHOD("command_def", MLCompilerT, MLStringT, MLAnyT) {
4641 //<Compiler
4642 //<Name
4643 //<Value
4644 //>any
4645 ml_compiler_t *Compiler = (ml_compiler_t *)Args[0];
4646 const char *Name = ml_string_value(Args[1]);
4647 ml_value_t **Slot = (ml_value_t **)stringmap_slot(Compiler->Vars, Name);
4648 if (!Slot[0] || ml_typeof(Slot[0]) != MLGlobalT) {
4649 Slot[0] = ml_global(Name);
4650 }
4651 return ml_global_set(Slot[0], Args[2]);
4652 }
4653
ml_command_global(stringmap_t * Globals,const char * Name)4654 static ml_global_t *ml_command_global(stringmap_t *Globals, const char *Name) {
4655 ml_value_t **Slot = (ml_value_t **)stringmap_slot(Globals, Name);
4656 if (!Slot[0]) {
4657 Slot[0] = ml_global(Name);
4658 } else if (ml_typeof(Slot[0]) == MLGlobalT) {
4659 } else if (ml_typeof(Slot[0]) == MLUninitializedT) {
4660 ml_value_t *Global = ml_global(Name);
4661 ml_uninitialized_set(Slot[0], Global);
4662 Slot[0] = Global;
4663 } else {
4664 Slot[0] = ml_global(Name);
4665 }
4666 return (ml_global_t *)Slot[0];
4667 }
4668
4669 typedef struct {
4670 ml_value_t *Args[2];
4671 int Index;
4672 ml_token_t Type;
4673 ml_global_t *Globals[];
4674 } ml_command_idents_frame_t;
4675
ml_command_idents_in2(mlc_function_t * Function,ml_value_t * Value,ml_command_idents_frame_t * Frame)4676 static void ml_command_idents_in2(mlc_function_t *Function, ml_value_t *Value, ml_command_idents_frame_t *Frame) {
4677 if (ml_is_error(Value)) {
4678 ml_state_t *Caller = Function->Base.Caller;
4679 ML_RETURN(Value);
4680 }
4681 ml_global_t *Global = Frame->Globals[Frame->Index];
4682 if (Frame->Type != MLT_REF) Value = ml_deref(Value);
4683 if (Frame->Type == MLT_VAR) {
4684 Value = ml_variable(Value, NULL);
4685 }
4686 Global->Value = Value;
4687 if (Frame->Index) {
4688 int Index = --Frame->Index;
4689 Frame->Args[1] = ml_cstring(Frame->Globals[Index]->Name);
4690 return ml_call(Function, SymbolMethod, 2, Frame->Args);
4691 } else {
4692 MLC_POP();
4693 MLC_RETURN(Frame->Args[0]);
4694 }
4695 }
4696
ml_command_idents_in(mlc_function_t * Function,ml_value_t * Value,ml_command_idents_frame_t * Frame)4697 static void ml_command_idents_in(mlc_function_t *Function, ml_value_t *Value, ml_command_idents_frame_t *Frame) {
4698 Frame->Args[0] = Value;
4699 Frame->Args[1] = ml_cstring(Frame->Globals[Frame->Index]->Name);
4700 Function->Frame->run = (mlc_frame_fn)ml_command_idents_in2;
4701 return ml_call(Function, SymbolMethod, 2, Frame->Args);
4702 }
4703
ml_command_idents_unpack(mlc_function_t * Function,ml_value_t * Packed,ml_command_idents_frame_t * Frame)4704 static void ml_command_idents_unpack(mlc_function_t *Function, ml_value_t *Packed, ml_command_idents_frame_t *Frame) {
4705 for (int Index = 0; Index <= Frame->Index; ++Index) {
4706 ml_value_t *Value = ml_unpack(Packed, Index + 1);
4707 ml_global_t *Global = Frame->Globals[Index];
4708 if (Frame->Type != MLT_REF) Value = ml_deref(Value);
4709 if (Frame->Type == MLT_VAR) {
4710 Value = ml_variable(Value, NULL);
4711 }
4712 Global->Value = Value;
4713 }
4714 MLC_POP();
4715 MLC_RETURN(Packed);
4716 }
4717
ml_accept_command_idents(mlc_function_t * Function,ml_parser_t * Parser,int Index)4718 static ml_command_idents_frame_t *ml_accept_command_idents(mlc_function_t *Function, ml_parser_t *Parser, int Index) {
4719 if (!ml_parse2(Parser, MLT_BLANK)) ml_accept(Parser, MLT_IDENT);
4720 const char *Ident = Parser->Ident;
4721 if (ml_parse(Parser, MLT_COMMA)) {
4722 ml_command_idents_frame_t *Frame = ml_accept_command_idents(Function, Parser, Index + 1);
4723 Frame->Globals[Index] = ml_command_global(Function->Compiler->Vars, Ident);
4724 return Frame;
4725 }
4726 ml_accept(Parser, MLT_RIGHT_PAREN);
4727 mlc_frame_fn FrameFn;
4728 if (ml_parse(Parser, MLT_IN)) {
4729 FrameFn = (mlc_frame_fn)ml_command_idents_in;
4730 } else {
4731 ml_accept(Parser, MLT_ASSIGN);
4732 FrameFn = (mlc_frame_fn)ml_command_idents_unpack;
4733 }
4734 int Count = Index + 1;
4735 MLC_XFRAME(ml_command_idents_frame_t, Count, const char *, FrameFn);
4736 Frame->Index = Index;
4737 Frame->Globals[Index] = ml_command_global(Function->Compiler->Vars, Ident);
4738 return Frame;
4739 }
4740
4741 typedef struct {
4742 ml_global_t *Global;
4743 mlc_expr_t *VarType;
4744 ml_token_t Type;
4745 } ml_command_ident_frame_t;
4746
ml_command_ident_run(mlc_function_t * Function,ml_value_t * Value,ml_command_ident_frame_t * Frame)4747 static void ml_command_ident_run(mlc_function_t *Function, ml_value_t *Value, ml_command_ident_frame_t *Frame) {
4748 if (ml_is_error(Value)) {
4749 ml_state_t *Caller = Function->Base.Caller;
4750 ML_RETURN(Value);
4751 }
4752 /*ml_compiler_t *Compiler = Function->Compiler;
4753 if (!ml_is(VarType, MLTypeT)) {
4754 ml_parse_error(Compiler, "TypeError", "Expected <type> not <%s>", ml_typeof(VarType)->Name);
4755 }*/
4756 ml_global_t *Global = Frame->Global;
4757 if (Frame->Type != MLT_REF) Value = ml_deref(Value);
4758 switch (Frame->Type) {
4759 case MLT_VAR:
4760 Value = ml_variable(Value, NULL);
4761 break;
4762 case MLT_LET:
4763 case MLT_DEF:
4764 ml_value_set_name(Value, Global->Name);
4765 break;
4766 default:
4767 break;
4768 }
4769 Global->Value = Value;
4770 MLC_POP();
4771 MLC_RETURN(Value);
4772 }
4773
ml_accept_command_decl2(mlc_function_t * Function,ml_parser_t * Parser,ml_token_t Type)4774 static void ml_accept_command_decl2(mlc_function_t *Function, ml_parser_t *Parser, ml_token_t Type) {
4775 if (ml_parse(Parser, MLT_LEFT_PAREN)) {
4776 ml_command_idents_frame_t *Frame = ml_accept_command_idents(Function, Parser, 0);
4777 Frame->Type = Type;
4778 mlc_expr_t *Expr = ml_accept_expression(Parser, EXPR_DEFAULT);
4779 return mlc_expr_call(Function, Expr);
4780 } else {
4781 MLC_FRAME(ml_command_ident_frame_t, ml_command_ident_run);
4782 ml_accept(Parser, MLT_IDENT);
4783 Frame->Global = ml_command_global(Function->Compiler->Vars, Parser->Ident);
4784 Frame->VarType = NULL;
4785 Frame->Type = Type;
4786 if (ml_parse(Parser, MLT_LEFT_PAREN)) {
4787 mlc_expr_t *Expr = ml_accept_fun_expr(Parser, Frame->Global->Name, MLT_RIGHT_PAREN);
4788 return mlc_expr_call(Function, Expr);
4789 } else {
4790 if (ml_parse(Parser, MLT_COLON)) Frame->VarType = ml_accept_term(Parser);
4791 if (Type == MLT_VAR) {
4792 if (ml_parse(Parser, MLT_ASSIGN)) {
4793 mlc_expr_t *Expr = ml_accept_expression(Parser, EXPR_DEFAULT);
4794 return mlc_expr_call(Function, Expr);
4795 } else {
4796 return ml_command_ident_run(Function, MLNil, Frame);
4797 }
4798 } else {
4799 ml_accept(Parser, MLT_ASSIGN);
4800 mlc_expr_t *Expr = ml_accept_expression(Parser, EXPR_DEFAULT);
4801 return mlc_expr_call(Function, Expr);
4802 }
4803 }
4804 }
4805 }
4806
4807 typedef struct {
4808 ml_parser_t *Parser;
4809 ml_token_t Type;
4810 } ml_command_decl_frame_t;
4811
ml_command_decl_run(mlc_function_t * Function,ml_value_t * Value,ml_command_decl_frame_t * Frame)4812 static void ml_command_decl_run(mlc_function_t *Function, ml_value_t *Value, ml_command_decl_frame_t *Frame) {
4813 ml_parser_t *Parser = Frame->Parser;
4814 if (setjmp(Parser->OnError)) MLC_RETURN(Parser->Value);
4815 if (ml_parse(Parser, MLT_COMMA)) {
4816 return ml_accept_command_decl2(Function, Parser, Frame->Type);
4817 }
4818 ml_parse(Parser, MLT_SEMICOLON);
4819 MLC_POP();
4820 MLC_RETURN(Value);
4821 }
4822
ml_accept_command_decl(mlc_function_t * Function,ml_parser_t * Parser,ml_token_t Type)4823 static void ml_accept_command_decl(mlc_function_t *Function, ml_parser_t *Parser, ml_token_t Type) {
4824 MLC_FRAME(ml_command_decl_frame_t, ml_command_decl_run);
4825 Frame->Parser = Parser;
4826 Frame->Type = Type;
4827 return ml_accept_command_decl2(Function, Parser, Type);
4828 }
4829
ml_command_evaluate2(mlc_function_t * Function,ml_value_t * Value,void * Data)4830 static void ml_command_evaluate2(mlc_function_t *Function, ml_value_t *Value, void *Data) {
4831 ml_state_t *Caller = Function->Base.Caller;
4832 ML_RETURN(ml_deref(Value));
4833 }
4834
ml_command_evaluate(ml_state_t * Caller,ml_parser_t * Parser,ml_compiler_t * Compiler)4835 void ml_command_evaluate(ml_state_t *Caller, ml_parser_t *Parser, ml_compiler_t *Compiler) {
4836 mlc_function_t *Function = new(mlc_function_t);
4837 Function->Base.Type = MLCompilerFunctionT;
4838 Function->Base.Caller = (ml_state_t *)Caller;
4839 Function->Base.Context = Caller->Context;
4840 Function->Base.run = (ml_state_fn)mlc_function_run;
4841 Function->Compiler = Compiler;
4842 Function->Source = Parser->Source.Name;
4843 Function->Up = NULL;
4844 __attribute__((unused)) MLC_FRAME(void, ml_command_evaluate2);
4845 if (setjmp(Parser->OnError)) MLC_RETURN(Parser->Value);
4846 ml_skip_eol(Parser);
4847 if (ml_parse(Parser, MLT_EOI)) MLC_RETURN(MLEndOfInput);
4848 if (ml_parse(Parser, MLT_VAR)) {
4849 return ml_accept_command_decl(Function, Parser, MLT_VAR);
4850 } else if (ml_parse(Parser, MLT_LET)) {
4851 return ml_accept_command_decl(Function, Parser, MLT_LET);
4852 } else if (ml_parse(Parser, MLT_REF)) {
4853 return ml_accept_command_decl(Function, Parser, MLT_REF);
4854 } else if (ml_parse(Parser, MLT_DEF)) {
4855 return ml_accept_command_decl(Function, Parser, MLT_DEF);
4856 } else if (ml_parse(Parser, MLT_FUN)) {
4857 if (ml_parse(Parser, MLT_IDENT)) {
4858 MLC_FRAME(ml_command_ident_frame_t, ml_command_ident_run);
4859 Frame->Global = ml_command_global(Compiler->Vars, Parser->Ident);
4860 Frame->VarType = NULL;
4861 Frame->Type = MLT_LET;
4862 ml_accept(Parser, MLT_LEFT_PAREN);
4863 mlc_expr_t *Expr = ml_accept_fun_expr(Parser, Frame->Global->Name, MLT_RIGHT_PAREN);
4864 ml_parse(Parser, MLT_SEMICOLON);
4865 return mlc_expr_call(Function, Expr);
4866 } else {
4867 ml_accept(Parser, MLT_LEFT_PAREN);
4868 mlc_expr_t *Expr = ml_accept_fun_expr(Parser, NULL, MLT_RIGHT_PAREN);
4869 ml_parse(Parser, MLT_SEMICOLON);
4870 return mlc_expr_call(Function, Expr);
4871 }
4872 } else if (ml_parse(Parser, MLT_MACRO)) {
4873 if (ml_parse(Parser, MLT_IDENT)) {
4874 const char *Name = Parser->Ident;
4875 ml_accept(Parser, MLT_LEFT_PAREN);
4876 ml_template_macro_t *Macro = new(ml_template_macro_t);
4877 Macro->Base.Type = MLMacroT;
4878 Macro->Base.apply = (void *)ml_template_macro_apply;
4879 ml_decl_t **ParamSlot = &Macro->Params;
4880 if (!ml_parse2(Parser, MLT_RIGHT_PAREN)) {
4881 do {
4882 ml_accept(Parser, MLT_IDENT);
4883 ml_decl_t *Param = ParamSlot[0] = new(ml_decl_t);
4884 Param->Ident = Parser->Ident;
4885 Param->Hash = ml_ident_hash(Parser->Ident);
4886 ParamSlot = &Param->Next;
4887 } while (ml_parse2(Parser, MLT_COMMA));
4888 ml_accept(Parser, MLT_RIGHT_PAREN);
4889 }
4890 Macro->Expr = ml_accept_expression(Parser, EXPR_DEFAULT);
4891 ml_parse(Parser, MLT_SEMICOLON);
4892 stringmap_insert(Compiler->Vars, Name, Macro);
4893 MLC_RETURN((ml_value_t *)Macro);
4894 } else {
4895 mlc_expr_t *Expr = ml_accept_macro_expr(Parser);
4896 ml_parse(Parser, MLT_SEMICOLON);
4897 return mlc_expr_call(Function, Expr);
4898 }
4899 } else {
4900 mlc_expr_t *Expr = ml_accept_expression(Parser, EXPR_DEFAULT);
4901 if (ml_parse(Parser, MLT_COLON)) {
4902 ml_accept(Parser, MLT_IDENT);
4903 const char *Ident = Parser->Ident;
4904 ML_EXPR(CallExpr, parent, call);
4905 CallExpr->Child = Expr;
4906 ml_accept(Parser, MLT_LEFT_PAREN);
4907 ml_accept_arguments(Parser, MLT_RIGHT_PAREN, &Expr->Next);
4908 ml_parse(Parser, MLT_SEMICOLON);
4909 MLC_FRAME(ml_command_ident_frame_t, ml_command_ident_run);
4910 Frame->Global = ml_command_global(Compiler->Vars, Ident);
4911 Frame->VarType = NULL;
4912 Frame->Type = MLT_LET;
4913 return mlc_expr_call(Function, ML_EXPR_END(CallExpr));
4914 } else {
4915 ml_parse(Parser, MLT_SEMICOLON);
4916 return mlc_expr_call(Function, Expr);
4917 }
4918 }
4919 }
4920
4921 #ifdef __MINGW32__
ml_read_line(FILE * File,ssize_t Offset,char ** Result)4922 static ssize_t ml_read_line(FILE *File, ssize_t Offset, char **Result) {
4923 char Buffer[129];
4924 if (fgets(Buffer, 129, File) == NULL) return -1;
4925 int Length = strlen(Buffer);
4926 if (Length == 128) {
4927 ssize_t Total = ml_read_line(File, Offset + 128, Result);
4928 memcpy(*Result + Offset, Buffer, 128);
4929 return Total;
4930 } else {
4931 *Result = GC_MALLOC_ATOMIC(Offset + Length + 1);
4932 strcpy(*Result + Offset, Buffer);
4933 return Offset + Length;
4934 }
4935 }
4936 #endif
4937
ml_file_read(void * Data)4938 static const char *ml_file_read(void *Data) {
4939 FILE *File = (FILE *)Data;
4940 char *Line = NULL;
4941 size_t Length = 0;
4942 #ifdef __MINGW32__
4943 Length = ml_read_line(File, 0, &Line);
4944 if (Length < 0) return NULL;
4945 #else
4946 if (getline(&Line, &Length, File) < 0) return NULL;
4947 #endif
4948 return Line;
4949 }
4950
4951 typedef struct {
4952 ml_state_t Base;
4953 FILE *File;
4954 } ml_load_file_state_t;
4955
ml_load_file_state_run(ml_load_file_state_t * State,ml_value_t * Value)4956 static void ml_load_file_state_run(ml_load_file_state_t *State, ml_value_t *Value) {
4957 fclose(State->File);
4958 ml_state_t *Caller = State->Base.Caller;
4959 ML_RETURN(Value);
4960 }
4961
ml_load_file(ml_state_t * Caller,ml_getter_t GlobalGet,void * Globals,const char * FileName,const char * Parameters[])4962 void ml_load_file(ml_state_t *Caller, ml_getter_t GlobalGet, void *Globals, const char *FileName, const char *Parameters[]) {
4963 static const char *DefaultParameters[] = {"Args", NULL};
4964 if (!Parameters) Parameters = DefaultParameters;
4965 FILE *File = fopen(FileName, "r");
4966 if (!File) ML_RETURN(ml_error("LoadError", "error opening %s", FileName));
4967 ml_parser_t *Parser = ml_parser(ml_file_read, File);
4968 Parser->Source.Name = FileName;
4969 const char *Line = ml_file_read(File);
4970 if (!Line) ML_RETURN(ml_error("LoadError", "empty file %s", FileName));
4971 if (Line[0] == '#' && Line[1] == '!') {
4972 Parser->Line = 2;
4973 Line = ml_file_read(File);
4974 if (!Line) ML_RETURN(ml_error("LoadError", "empty file %s", FileName));
4975 } else {
4976 Parser->Line = 1;
4977 }
4978 Parser->Next = Line;
4979 mlc_expr_t *Expr = ml_accept_file(Parser);
4980 if (!Expr) ML_RETURN(Parser->Value);
4981 ml_compiler_t *Compiler = ml_compiler(GlobalGet, Globals);
4982 ml_load_file_state_t *State = new(ml_load_file_state_t);
4983 State->Base.Caller = Caller;
4984 State->Base.Context = Caller->Context;
4985 State->Base.run = (ml_state_fn)ml_load_file_state_run;
4986 State->File = File;
4987 return ml_function_compile((ml_state_t *)State, Expr, Compiler, Parameters);
4988 }
4989
ml_compiler_init()4990 void ml_compiler_init() {
4991 #include "ml_compiler_init.c"
4992 stringmap_insert(MLCompilerT->Exports, "EOI", MLEndOfInput);
4993 stringmap_insert(MLCompilerT->Exports, "NotFound", MLNotFound);
4994 stringmap_insert(MLCompilerT->Exports, "switch", MLCompilerSwitch);
4995 stringmap_insert(StringFns, "r", ml_regex);
4996 stringmap_insert(StringFns, "ri", ml_regexi);
4997 }
4998