lambda_mountain 1.13.53

Compiler Backend / Programming Language Scaffolding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459

index-typedefs := λ(: program AST). (: (
   (while (non-zero program) (match program (
      ()
      ( (Seq( rst (ASTType( lhs rhs )) )) (tail(
         (index-typedefs( rhs 0_u64 ))
         (set program rst)
      )))
      ( (Seq( rst _ )) (
         (set program rst)
      ))
      ( _ (set program ASTEOF) )
   )))
) Nil);

index-typedefs := λ(: def AST)(: index U64) . (: (
   (match def (
      ()
      ( (App( (App( tds (Var '|_s) )) (Lit tag) )) (tail(
         (index-index-of-tag( tag index ))
         (index-typedefs( tds (+( index 1_u64 )) ))
      )))
      ( (App( (App( tds (Var '|_s) )) (App( (Lit tag) _ )) )) (tail(
         (index-index-of-tag( tag index ))
         (index-typedefs( tds (+( index 1_u64 )) ))
      )))
      ( (Lit tag) (
         (index-index-of-tag( tag index ))
      ))
      ( (App( (Lit tag) _ )) (
         (index-index-of-tag( tag index ))
      ))
      ( ASTEOF () )
      ( _ (exit-error( 'Invalid\sTypedef_s def )) )
   ))
) Nil);

preprocess := λ. (: (tail(
   (index-typedefs ast-parsed-program)
   (set ast-parsed-program (preprocess-apply ast-parsed-program))
)) Nil);

union := λ(: l Context)(: r Context). (: (tail(
   (let return l)
   (if (not(non-zero r)) (set return CtxEOF) ())
   (if (non-zero return) (
      (match r (
         ()
         ( (CtxBind( rst k v )) (
            (set return (CtxBind(
               (close(union( l rst ))) k v
            )))
         ))
         ( _ (set r CtxEOF) )
      ))
   ) ())
   return
)) Context);

is-macro-head := λ(: s String)(: m AST). (: (tail(
   (let r 0_u64)
   (match m (
      ()
      ( (Lit mv) (set r (==( s mv ))) )
      ( (App( l1 l2 )) (set r (is-macro-head( s l1 ))) )
      ( _ (exit-error( 'Unrecognized\sMacro\sLHS_s m )) )
   ))
   r
)) U64);

is-macro-head := λ(: s String). (: (tail(
   (let macros preprocess-macros)
   (let found 0_u64)
   (while (non-zero macros) (match macros (
      ()
      ( (MSeq( rst (Macro( mlhs mrhs )) )) (
         (if (is-macro-head( s mlhs )) (tail(
            (set found 1_u64)
            (set macros MEOF)
         )) (
            (set macros rst)
         ))
      ))
   )))
   found
)) U64);

preprocess-apply := λ(: program AST). (: (tail(
   (let r program)
   (match program (
      ()
      ( (App( (App( (Lit ':_s) mvar )) (Lit mtype) )) (
         (set r (Asc( (close(preprocess-apply mvar)) (close(parse-type mtype)) )))
      ))
      ( (App( (App( (Lit ':_s) mvar )) (Var mtype) )) (
         (set r (Asc( (close(preprocess-apply mvar)) (close(parse-type mtype)) )))
      ))
      ( (App( (App( (Var 'as_s) mvar )) (Lit mtype) )) (
         (set r (As( (close(preprocess-apply mvar)) (close(parse-type mtype)) )))
      ))
      ( (App( (App( (Var 'as_s) mvar )) (Var mtype) )) (
         (set r (As( (close(preprocess-apply mvar)) (close(parse-type mtype)) )))
      ))
      ( (App( (Var 'sizeof_s) (Var mtype) )) (
         (set r (Sizeof( (close(parse-type mtype)) )))
      ))
      ( (App( (Var 'sizeof_s) (Lit mtype) )) (
         (set r (Sizeof( (close(parse-type mtype)) )))
      ))
      ( (Lit l) (tail(
         (let suffixes parse-suffixes)
         (while (non-zero suffixes) (match suffixes (
            ()
            ( (SfxSeq( rst sfxs sfxtt )) (
               (if (has-suffix( l sfxs )) (tail(
                  (let lloc (location-of l))
                  (let lpfx (remove-suffix( l sfxs )))
                  (map-location( lpfx lloc ))
                  (set r (Asc( (close (Lit lpfx)) (close sfxtt) )))
                  (set suffixes SfxEOF)
               )) (
                  (set suffixes rst)
               ))
            ))
         )))
      )))
      ( (Var l) (tail(
         (let suffixes parse-suffixes)
         (while (non-zero suffixes) (match suffixes (
            ()
            ( (SfxSeq( rst sfxs sfxtt )) (
               (if (has-suffix( l sfxs )) (tail(
                  (let lloc (location-of l))
                  (let lpfx (remove-suffix( l sfxs )))
                  (map-location( lpfx lloc ))
                  (set r (Asc( (close (Lit lpfx)) (close sfxtt) )))
                  (set suffixes SfxEOF)
               )) (
                  (set suffixes rst)
               ))
            ))
         )))
      )))
      ( (App( (Var vn) vt )) (
         if (is-macro-head vn) (tail(
            (let applied (preprocess-apply-maybe program))
            (set r applied)
         )) (
            (match program (
               ()
               ( (App( v1 v2 )) (
                  (set r (App( (close(preprocess-apply v1)) (close(preprocess-apply v2)) )))
               ))
            ))
         )
      ))
      ( (App( (App( (Var vn) vt1 )) vt2 )) (
         if (is-macro-head vn) (tail(
            (let applied (preprocess-apply-maybe program))
            (set r applied)
         )) (
            (match program (
               ()
               ( (App( (App( v1 v2 )) v3 )) (
                  (set r (App(
                     (close(App(
                        (close(preprocess-apply v1))
                        (close(preprocess-apply v2))
                     )))
                     (close(preprocess-apply v3))
                  )))
               ))
            ))
         )
      ))
      ( (Seq( al ar )) (set r (Seq(
         (close(preprocess-apply al))
         (close(preprocess-apply ar))
      ))))
      ( (App( al ar )) (set r (App(
         (close(preprocess-apply al))
         (close(preprocess-apply ar))
      ))))
      ( (Abs( al ar tlt )) (set r (Abs(
         (close(preprocess-apply al))
         (close(preprocess-apply ar))
         tlt
      ))))
      ( (Asc( al at )) (set r (Asc(
         (close(preprocess-apply al))
         (close at)
      ))))
      ( (Frg( al ar atlt )) (
         (set r (Frg(
            al
            (close(preprocess-apply ar))
            atlt
         )))
      ))
      ( (Glb( k ar )) (set r (Glb(
         k
         (close(preprocess-apply ar))
      ))))
      ( u (set r u))
   ))
   r
)) AST);

preprocess-apply-maybe := λ(: program AST). (: (tail(
   (let return program)
   (let macros preprocess-macros)
   (let matched False_u8)
   (while (non-zero macros) (match macros (
      ()
      ( (MSeq( rst (Macro( lhs rhs )) )) (
         (match (try-destructure-macro( lhs program )) (
            ()
            ( CtxEOF (set macros rst) )
            ( ctx (tail(
               (let p (apply( ctx rhs )))
               (let c (maybe-deref(extract-uuids( program CtxEOF p ))))
               (let u (maybe-deref(substitute-uuids( c p ))))
               (let n (preprocess-apply u))
               (set return (relocate( n (location-of program) )))
               (set macros MEOF)
               (set matched True_u8)
            )))
         ))
      ))
      ( _ (set macros MEOF) )
   )))
   (match return (
      ()
      ( (App( l r )) (
         (set return (App(
            (close(preprocess-apply l))
            (close(preprocess-apply r))
         )))
      ))
      ( _ () )
   ))
   return
)) AST);

apply := λ(: ctx Context)(: term AST). (: (tail(
   (let return term)
   (match term (
      ()
      ( ASTEOF () )
      ( ASTNil () )
      ( (Lit _) () )
      ( (Sizeof _) () )
      ( (Var n) (
         (while (non-zero ctx) (match ctx (
            ()
            ( (CtxBind( rst k v )) (
               (if (==( k n )) (tail(
                  (set return v)
                  (set ctx CtxEOF)
               )) (
                  (set ctx rst)
               ))
            ))
            ( _ (set ctx CtxEOF))
         )))
      ))
      ( (App( vl vr )) (
         (set return (App(
            (close(apply( ctx vl )))
            (close(apply( ctx vr )))
         )))
      ))
      ( (Abs( vl vr tlt )) (
         (set return (Abs(
            (close(apply( ctx vl )))
            (close(apply( ctx vr )))
            tlt
         )))
      ))
      ( (Seq( vl vr )) (
         (set return (Seq(
            (close(apply( ctx vl )))
            (close(apply( ctx vr )))
         )))
      ))
      ( (ASTType( vl vr )) (
         (set return (ASTType(
            (close(apply( ctx vl )))
            (close(apply( ctx vr )))
         )))
      ))
      ( (Glb( k vr )) (
         (set return (Glb(
            k
            (close(apply( ctx vr )))
         )))
      ))
      ( (Frg( k vr tlt )) (
         (set return (Frg(
            k
            (close(apply( ctx vr )))
            tlt
         )))
      ))
      ( (Asc( t tt )) (
         (set return (Asc(
            (close(apply( ctx t )))
            (close tt)
         )))
      ))
      ( (As( t tt )) (
         (set return (As(
            (close(apply( ctx t )))
            (close tt)
         )))
      ))
   ))
   return
)) AST);

try-destructure-macro := λ(: lhs AST)(: term AST). (: (tail(
   (let r CtxEOF)
   (match (APair( lhs term )) (
      ()
      ( (APair( ASTNil ASTNil )) (set r CtxNil) )
      ( (APair( (App( (App( (Var _) (Var _) )) (Var _) )) (App( (App( (Lit _) _ )) _ )) )) () )
      ( (APair( (App(pl pr)) (App(el er)) )) (tail(
         (let ll (try-destructure-macro( pl el )))
         (if (non-zero ll) (tail(
            (let rl (try-destructure-macro( pr er )))
            (if (non-zero rl) (
               (set r (union( ll rl )))
            ) ())
         )) ())
      )))
      ( (APair( (Abs(pl pr ptlt)) (Abs(el er etlt)) )) (tail(
         (let ll (try-destructure-macro( pl el )))
         (if (non-zero ll) (tail(
            (let rl (try-destructure-macro( pr er )))
            (if (non-zero rl) (
               (set r (union( ll rl )))
            ) ())
         )) ())
      )))
      ( (APair( (Lit ':Any:_s) (Var '__s) )) (
         (set r CtxNil)
      ))
      ( (APair( (Lit pl) (Var el) )) (
         (if (==( pl el )) (set r CtxNil) ())
      ))
      ( (APair( (Lit pl) (Lit el) )) (
         (if (==( pl el )) (set r CtxNil) ())
      ))
      ( (APair( (App( (Lit ':Literal:_s) (Var pv) )) (Lit el) )) (
         (if (==( (index-of-tag el) unknown-index-of-tag ))
             (set r (CtxBind( (close CtxNil) pv term )))
             ()
         )
      ))
      ( (APair( (App( (Lit ':Variable:_s) _ )) (Var '__s) )) (
         ()
      ))
      ( (APair( (App( (Lit ':Variable:_s) (Var pv) )) (Var el) )) (
         (set r (CtxBind( (close CtxNil) pv term )))
      ))
      ( (APair( (App( (App( (Lit ':Tag:_s) (Var pv) )) (Var pt) )) (Lit el) )) (
         (if (==( (index-of-tag el) unknown-index-of-tag )) () (tail(
            (set r CtxNil)
            (set r (CtxBind( (close r) pv (
               (Asc(
                  (close(Lit (to-string(index-of-tag el)) ))
                  (close(parse-type 'Constant+Literal+U64_s))
               ))
            ) )))
            (set r (CtxBind( (close r) pt (Lit el) )))
         )))
      ))
      ( (APair( (Var pv) _ )) (
         (set r (CtxBind( (close CtxNil) pv term )))
      ))
      ( _ () )
   ))
   r
)) Context);

relocate := λ(: term AST)(: loc SourceLocation). (: (tail(
   (let return term)
   (match term (
      ()
      ( ASTEOF () )
      ( ASTNil () )
      ( (Sizeof _) () )
      ( (Var v) (tail(
         (set v (clone-rope(SAtom v)))
         (map-location( v loc ))
         (set return (Var v))
      )))
      ( (Lit v) (tail(
         (set v (clone-rope(SAtom v)))
         (map-location( v loc ))
         (set return (Lit v))
      )))
      ( (App( lt rt )) (
         (set return (App(
            (close(relocate( lt loc )))
            (close(relocate( rt loc )))
         )))
      ))
      ( (Abs( lt rt tlt )) (
         (set return (Abs(
            (close(relocate( lt loc )))
            (close(relocate( rt loc )))
            tlt
         )))
      ))
      ( (Seq( lt rt )) (
         (set return (Seq(
            (close(relocate( lt loc )))
            (close(relocate( rt loc )))
         )))
      ))
      ( (ASTType( lt rt )) (
         (set return (ASTType(
            (close(relocate( lt loc )))
            (close(relocate( rt loc )))
         )))
      ))
      ( (Asc( lt rt )) (
         (set return (Asc(
            (close(relocate( lt loc )))
            (close rt)
         )))
      ))
      ( (As( lt rt )) (
         (set return (As(
            (close(relocate( lt loc )))
            (close rt)
         )))
      ))
      ( (Glb( k t )) (tail(
         (set k (clone-rope(SAtom k)))
         (map-location( k loc ))
         (set return (Glb(
            k
            (close(relocate( t loc )))
         )))
      )))
      ( (Frg( k t tlt )) (tail(
         (set k (clone-rope(SAtom k)))
         (map-location( k loc ))
         (set return (Frg(
            k
            (close(relocate( t loc )))
            tlt
         )))
      )))
   ))
   return
)) AST);