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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
(.module: {#.doc "Functions for extracting information from the state of the compiler."}
[lux #*
[abstract
[functor (#+ Functor)]
[apply (#+ Apply)]
["." monad (#+ Monad do)]]
[control
["." try (#+ Try)]]
[data
["." product]
["." name ("#@." codec equivalence)]
["." maybe]
[number
["n" nat]
["i" int]]
["." text ("#@." monoid equivalence)]
[collection
["." list ("#@." monoid monad)]]]]
[/
["." code]])
## (type: (Meta a)
## (-> Lux (Try [Lux a])))
(structure: #export functor (Functor Meta)
(def: (map f fa)
(function (_ compiler)
(case (fa compiler)
(#try.Failure msg)
(#try.Failure msg)
(#try.Success [compiler' a])
(#try.Success [compiler' (f a)])))))
(structure: #export apply (Apply Meta)
(def: &functor ..functor)
(def: (apply ff fa)
(function (_ compiler)
(case (ff compiler)
(#try.Success [compiler' f])
(case (fa compiler')
(#try.Success [compiler'' a])
(#try.Success [compiler'' (f a)])
(#try.Failure msg)
(#try.Failure msg))
(#try.Failure msg)
(#try.Failure msg)))))
(structure: #export monad (Monad Meta)
(def: &functor ..functor)
(def: (wrap x)
(function (_ compiler)
(#try.Success [compiler x])))
(def: (join mma)
(function (_ compiler)
(case (mma compiler)
(#try.Failure msg)
(#try.Failure msg)
(#try.Success [compiler' ma])
(ma compiler')))))
(def: (get k plist)
(All [a]
(-> Text (List [Text a]) (Maybe a)))
(case plist
#.Nil
#.None
(#.Cons [k' v] plist')
(if (text@= k k')
(#.Some v)
(get k plist'))))
(def: #export (run' compiler action)
(All [a] (-> Lux (Meta a) (Try [Lux a])))
(action compiler))
(def: #export (run compiler action)
(All [a] (-> Lux (Meta a) (Try a)))
(case (action compiler)
(#try.Failure error)
(#try.Failure error)
(#try.Success [_ output])
(#try.Success output)))
(def: #export (either left right)
{#.doc "Pick whichever computation succeeds."}
(All [a] (-> (Meta a) (Meta a) (Meta a)))
(function (_ compiler)
(case (left compiler)
(#try.Failure error)
(right compiler)
(#try.Success [compiler' output])
(#try.Success [compiler' output]))))
(def: #export (assert message test)
{#.doc "Fails with the given message if the test is #0."}
(-> Text Bit (Meta Any))
(function (_ compiler)
(if test
(#try.Success [compiler []])
(#try.Failure message))))
(def: #export (fail msg)
{#.doc "Fails with the given message."}
(All [a]
(-> Text (Meta a)))
(function (_ _)
(#try.Failure msg)))
(def: #export (find-module name)
(-> Text (Meta Module))
(function (_ compiler)
(case (get name (get@ #.modules compiler))
(#.Some module)
(#try.Success [compiler module])
_
(#try.Failure ($_ text@compose "Unknown module: " name)))))
(def: #export current-module-name
(Meta Text)
(function (_ compiler)
(case (get@ #.current-module compiler)
(#.Some current-module)
(#try.Success [compiler current-module])
_
(#try.Failure "No current module.")
)))
(def: #export current-module
(Meta Module)
(do ..monad
[this-module-name current-module-name]
(find-module this-module-name)))
(def: #export (get-ann tag anns)
{#.doc "Looks-up a particular annotation's value within the set of annotations."}
(-> Name Code (Maybe Code))
(case anns
[_ (#.Record anns)]
(loop [anns anns]
(case anns
(#.Cons [key value] anns')
(case key
[_ (#.Tag tag')]
(if (name@= tag tag')
(#.Some value)
(recur anns'))
_
(recur anns'))
#.Nil
#.None))
_
#.None))
(template [<name> <tag> <type>]
[(def: #export (<name> tag anns)
(-> Name Code (Maybe <type>))
(case (get-ann tag anns)
(#.Some [_ (<tag> value)])
(#.Some value)
_
#.None))]
[get-bit-ann #.Bit Bit]
[get-int-ann #.Int Int]
[get-frac-ann #.Frac Frac]
[get-text-ann #.Text Text]
[get-identifier-ann #.Identifier Name]
[get-tag-ann #.Tag Name]
[get-form-ann #.Form (List Code)]
[get-tuple-ann #.Tuple (List Code)]
[get-record-ann #.Record (List [Code Code])]
)
(def: #export (get-documentation anns)
{#.doc "Looks-up a definition's documentation."}
(-> Code (Maybe Text))
(get-text-ann (name-of #.doc) anns))
(def: #export (flag-set? flag-name anns)
{#.doc "Finds out whether an annotation-as-a-flag is set (has value '#1')."}
(-> Name Code Bit)
(maybe.default #0 (get-bit-ann flag-name anns)))
(template [<name> <tag> <desc>]
[(def: #export <name>
{#.doc (code.text ($_ text@compose "Checks whether a definition is " <desc> "."))}
(-> Code Bit)
(flag-set? (name-of <tag>)))]
[structure? #.struct? "a structure"]
[recursive-type? #.type-rec? "a recursive type"]
[signature? #.sig? "a signature"]
)
(template [<name> <tag> <type>]
[(def: (<name> input)
(-> Code (Maybe <type>))
(case input
[_ (<tag> actual-value)]
(#.Some actual-value)
_
#.None))]
[parse-tuple #.Tuple (List Code)]
[parse-text #.Text Text]
)
(template [<name> <tag> <desc>]
[(def: #export (<name> anns)
{#.doc <desc>}
(-> Code (List Text))
(maybe.default (list)
(do maybe.monad
[_args (get-ann (name-of <tag>) anns)
args (parse-tuple _args)]
(monad.map @ parse-text args))))]
[function-arguments #.func-args "Looks up the arguments of a function."]
[type-arguments #.type-args "Looks up the arguments of a parameterized type."]
)
(def: (macro-type? type)
(-> Type Bit)
(case type
(#.Named ["lux" "Macro"] (#.Primitive "#Macro" #.Nil))
true
_
false))
(def: (find-macro' modules this-module module name)
(-> (List [Text Module]) Text Text Text
(Maybe Macro))
(do maybe.monad
[$module (get module modules)
definition (: (Maybe Global)
(|> (: Module $module)
(get@ #.definitions)
(get name)))]
(case definition
(#.Left [r-module r-name])
(find-macro' modules this-module r-module r-name)
(#.Right [exported? def-type def-anns def-value])
(if (macro-type? def-type)
(#.Some (:coerce Macro def-value))
#.None))))
(def: #export (normalize name)
{#.doc (doc "If given a name without a module prefix, gives it the current module's name as prefix."
"Otherwise, returns the name as-is.")}
(-> Name (Meta Name))
(case name
["" name]
(do ..monad
[module-name current-module-name]
(wrap [module-name name]))
_
(:: ..monad wrap name)))
(def: #export (find-macro full-name)
(-> Name (Meta (Maybe Macro)))
(do ..monad
[[module name] (normalize full-name)
this-module current-module-name]
(: (Meta (Maybe Macro))
(function (_ compiler)
(#try.Success [compiler (find-macro' (get@ #.modules compiler) this-module module name)])))))
(def: #export (expand-once syntax)
{#.doc (doc "Given code that requires applying a macro, does it once and returns the result."
"Otherwise, returns the code as-is.")}
(-> Code (Meta (List Code)))
(case syntax
[_ (#.Form (#.Cons [[_ (#.Identifier name)] args]))]
(do ..monad
[?macro (find-macro name)]
(case ?macro
(#.Some macro)
((:coerce Macro' macro) args)
#.None
(:: ..monad wrap (list syntax))))
_
(:: ..monad wrap (list syntax))))
(def: #export (expand syntax)
{#.doc (doc "Given code that requires applying a macro, expands repeatedly until no more direct macro-calls are left."
"Otherwise, returns the code as-is.")}
(-> Code (Meta (List Code)))
(case syntax
[_ (#.Form (#.Cons [[_ (#.Identifier name)] args]))]
(do ..monad
[?macro (find-macro name)]
(case ?macro
(#.Some macro)
(do ..monad
[expansion ((:coerce Macro' macro) args)
expansion' (monad.map ..monad expand expansion)]
(wrap (list@join expansion')))
#.None
(:: ..monad wrap (list syntax))))
_
(:: ..monad wrap (list syntax))))
(def: #export (expand-all syntax)
{#.doc "Expands all macro-calls everywhere recursively, until only primitive/base code remains."}
(-> Code (Meta (List Code)))
(case syntax
[_ (#.Form (#.Cons [[_ (#.Identifier name)] args]))]
(do ..monad
[?macro (find-macro name)]
(case ?macro
(#.Some macro)
(do ..monad
[expansion ((:coerce Macro' macro) args)
expansion' (monad.map ..monad expand-all expansion)]
(wrap (list@join expansion')))
#.None
(do ..monad
[parts' (monad.map ..monad expand-all (list& (code.identifier name) args))]
(wrap (list (code.form (list@join parts')))))))
[_ (#.Form (#.Cons [harg targs]))]
(do ..monad
[harg+ (expand-all harg)
targs+ (monad.map ..monad expand-all targs)]
(wrap (list (code.form (list@compose harg+ (list@join (: (List (List Code)) targs+)))))))
[_ (#.Tuple members)]
(do ..monad
[members' (monad.map ..monad expand-all members)]
(wrap (list (code.tuple (list@join members')))))
_
(:: ..monad wrap (list syntax))))
(def: #export count
(Meta Nat)
(function (_ compiler)
(#try.Success [(update@ #.seed inc compiler)
(get@ #.seed compiler)])))
(def: #export (gensym prefix)
{#.doc (doc "Generates a unique name as an Code node (ready to be used in code templates)."
"A prefix can be given (or just be empty text) to better identify the code for debugging purposes.")}
(-> Text (Meta Code))
(function (_ compiler)
(#try.Success [(update@ #.seed inc compiler)
(|> compiler
(get@ #.seed)
(:: n.decimal encode)
($_ text@compose "__gensym__" prefix)
[""] code.identifier)])))
(def: (get-local-identifier ast)
(-> Code (Meta Text))
(case ast
[_ (#.Identifier [_ name])]
(:: ..monad wrap name)
_
(fail (text@compose "Code is not a local identifier: " (code.to-text ast)))))
(def: #export wrong-syntax-error
(-> Name Text)
(|>> name@encode
(text@compose "Wrong syntax for ")))
(macro: #export (with-gensyms tokens)
{#.doc (doc "Creates new identifiers and offers them to the body expression."
(syntax: #export (synchronized lock body)
(with-gensyms [g!lock g!body g!_]
(wrap (list (` (let [(~ g!lock) (~ lock)
(~ g!_) ("jvm monitorenter" (~ g!lock))
(~ g!body) (~ body)
(~ g!_) ("jvm monitorexit" (~ g!lock))]
(~ g!body)))))
)))}
(case tokens
(^ (list [_ (#.Tuple identifiers)] body))
(do ..monad
[identifier-names (monad.map @ get-local-identifier identifiers)
#let [identifier-defs (list@join (list@map (: (-> Text (List Code))
(function (_ name) (list (code.identifier ["" name]) (` (gensym (~ (code.text name)))))))
identifier-names))]]
(wrap (list (` ((~! do) (~! ..monad)
[(~+ identifier-defs)]
(~ body))))))
_
(fail (..wrong-syntax-error (name-of ..with-gensyms)))))
(def: #export (expand-1 token)
{#.doc "Works just like expand, except that it ensures that the output is a single Code token."}
(-> Code (Meta Code))
(do ..monad
[token+ (expand token)]
(case token+
(^ (list token'))
(wrap token')
_
(fail "Macro expanded to more than 1 element."))))
(def: #export (module-exists? module)
(-> Text (Meta Bit))
(function (_ compiler)
(#try.Success [compiler (case (get module (get@ #.modules compiler))
(#.Some _)
#1
#.None
#0)])))
(def: (try-both f x1 x2)
(All [a b]
(-> (-> a (Maybe b)) a a (Maybe b)))
(case (f x1)
#.None (f x2)
(#.Some y) (#.Some y)))
(def: (find-type-var idx bindings)
(-> Nat (List [Nat (Maybe Type)]) (Maybe Type))
(case bindings
#.Nil
#.None
(#.Cons [var bound] bindings')
(if (n.= idx var)
bound
(find-type-var idx bindings'))))
(def: (clean-type type)
(-> Type (Meta Type))
(case type
(#.Var var)
(function (_ compiler)
(case (|> compiler
(get@ [#.type-context #.var-bindings])
(find-type-var var))
(^or #.None (#.Some (#.Var _)))
(#try.Success [compiler type])
(#.Some type')
(#try.Success [compiler type'])))
_
(:: ..monad wrap type)))
(def: #export (find-var-type name)
{#.doc "Looks-up the type of a local variable somewhere in the environment."}
(-> Text (Meta Type))
(function (_ compiler)
(let [test (: (-> [Text [Type Any]] Bit)
(|>> product.left (text@= name)))]
(case (do maybe.monad
[scope (list.find (function (_ env)
(or (list.any? test (: (List [Text [Type Any]])
(get@ [#.locals #.mappings] env)))
(list.any? test (: (List [Text [Type Any]])
(get@ [#.captured #.mappings] env)))))
(get@ #.scopes compiler))
[_ [type _]] (try-both (list.find test)
(: (List [Text [Type Any]])
(get@ [#.locals #.mappings] scope))
(: (List [Text [Type Any]])
(get@ [#.captured #.mappings] scope)))]
(wrap type))
(#.Some var-type)
((clean-type var-type) compiler)
#.None
(#try.Failure ($_ text@compose "Unknown variable: " name))))))
(def: #export (find-def name)
{#.doc "Looks-up a definition's whole data in the available modules (including the current one)."}
(-> Name (Meta Global))
(do ..monad
[name (normalize name)]
(function (_ compiler)
(case (: (Maybe Global)
(do maybe.monad
[#let [[v-prefix v-name] name]
(^slots [#.definitions]) (get v-prefix (get@ #.modules compiler))]
(get v-name definitions)))
(#.Some definition)
(#try.Success [compiler definition])
_
(let [current-module (|> compiler (get@ #.current-module) (maybe.default "???"))
separator ($_ text@compose text.new-line " ")]
(#try.Failure ($_ text@compose
"Unknown definition: " (name@encode name) text.new-line
" Current module: " current-module text.new-line
(case (get current-module (get@ #.modules compiler))
(#.Some this-module)
($_ text@compose
" Imports: " (|> this-module (get@ #.imports) (text.join-with separator)) text.new-line
" Aliases: " (|> this-module (get@ #.module-aliases) (list@map (function (_ [alias real]) ($_ text@compose alias " => " real))) (text.join-with separator)) text.new-line)
_
"")
" All Known modules: " (|> compiler (get@ #.modules) (list@map product.left) (text.join-with separator)) text.new-line)))))))
(def: #export (find-def-type name)
{#.doc "Looks-up a definition's type in the available modules (including the current one)."}
(-> Name (Meta Type))
(do ..monad
[definition (find-def name)]
(case definition
(#.Left de-aliased)
(find-def-type de-aliased)
(#.Right [exported? def-type def-data def-value])
(clean-type def-type))))
(def: #export (find-type name)
{#.doc "Looks-up the type of either a local variable or a definition."}
(-> Name (Meta Type))
(do ..monad
[#let [[_ _name] name]]
(case name
["" _name]
(either (find-var-type _name)
(find-def-type name))
_
(find-def-type name))))
(def: #export (find-type-def name)
{#.doc "Finds the value of a type definition (such as Int, Any or Lux)."}
(-> Name (Meta Type))
(do ..monad
[definition (find-def name)]
(case definition
(#.Left de-aliased)
(find-type-def de-aliased)
(#.Right [exported? def-type def-data def-value])
(wrap (:coerce Type def-value)))))
(def: #export (definitions module-name)
{#.doc "The entire list of definitions in a module (including the non-exported/private ones)."}
(-> Text (Meta (List [Text Global])))
(function (_ compiler)
(case (get module-name (get@ #.modules compiler))
#.None
(#try.Failure ($_ text@compose "Unknown module: " module-name))
(#.Some module)
(#try.Success [compiler (get@ #.definitions module)]))))
(def: #export (exports module-name)
{#.doc "All the exported definitions in a module."}
(-> Text (Meta (List [Text Definition])))
(do ..monad
[constants (definitions module-name)]
(wrap (do list.monad
[[name definition] constants]
(case definition
(#.Left _)
(list)
(#.Right [exported? def-type def-data def-value])
(if exported?
(wrap [name [exported? def-type def-data def-value]])
(list)))))))
(def: #export modules
{#.doc "All the available modules (including the current one)."}
(Meta (List [Text Module]))
(function (_ compiler)
(|> compiler
(get@ #.modules)
[compiler]
#try.Success)))
(def: #export (tags-of type-name)
{#.doc "All the tags associated with a type definition."}
(-> Name (Meta (Maybe (List Name))))
(do ..monad
[#let [[module name] type-name]
module (find-module module)]
(case (get name (get@ #.types module))
(#.Some [tags _])
(wrap (#.Some tags))
_
(wrap #.None))))
(def: #export cursor
{#.doc "The cursor of the current expression being analyzed."}
(Meta Cursor)
(function (_ compiler)
(#try.Success [compiler (get@ #.cursor compiler)])))
(def: #export expected-type
{#.doc "The expected type of the current expression being analyzed."}
(Meta Type)
(function (_ compiler)
(case (get@ #.expected compiler)
(#.Some type)
(#try.Success [compiler type])
#.None
(#try.Failure "Not expecting any type."))))
(def: #export (imported-modules module-name)
{#.doc "All the modules imported by a specified module."}
(-> Text (Meta (List Text)))
(do ..monad
[(^slots [#.imports]) (find-module module-name)]
(wrap imports)))
(def: #export (imported-by? import module)
(-> Text Text (Meta Bit))
(do ..monad
[(^slots [#.imports]) (find-module module)]
(wrap (list.any? (text@= import) imports))))
(def: #export (imported? import)
(-> Text (Meta Bit))
(let [(^open ".") ..monad]
(|> current-module-name
(map find-module) join
(map (|>> (get@ #.imports) (list.any? (text@= import)))))))
(def: #export (resolve-tag tag)
{#.doc "Given a tag, finds out what is its index, its related tag-list and it's associated type."}
(-> Name (Meta [Nat (List Name) Type]))
(do ..monad
[#let [[module name] tag]
=module (find-module module)
this-module-name current-module-name
imported! (..imported? module)]
(case (get name (get@ #.tags =module))
(#.Some [idx tag-list exported? type])
(if (or (text@= this-module-name module)
(and imported! exported?))
(wrap [idx tag-list type])
(fail ($_ text@compose "Cannot access tag: " (name@encode tag) " from module " this-module-name)))
_
(fail ($_ text@compose
"Unknown tag: " (name@encode tag) text.new-line
" Known tags: " (|> =module
(get@ #.tags)
(list@map (|>> product.left [module] name@encode (text.prefix text.new-line)))
(text.join-with ""))
)))))
(def: #export (tag-lists module)
{#.doc "All the tag-lists defined in a module, with their associated types."}
(-> Text (Meta (List [(List Name) Type])))
(do ..monad
[=module (find-module module)
this-module-name current-module-name]
(wrap (|> (get@ #.types =module)
(list.filter (function (_ [type-name [tag-list exported? type]])
(or exported?
(text@= this-module-name module))))
(list@map (function (_ [type-name [tag-list exported? type]])
[tag-list type]))))))
(def: #export locals
{#.doc "All the local variables currently in scope, separated in different scopes."}
(Meta (List (List [Text Type])))
(function (_ compiler)
(case (list.inits (get@ #.scopes compiler))
#.None
(#try.Failure "No local environment")
(#.Some scopes)
(#try.Success [compiler
(list@map (|>> (get@ [#.locals #.mappings])
(list@map (function (_ [name [type _]])
[name type])))
scopes)]))))
(def: #export (un-alias def-name)
{#.doc "Given an aliased definition's name, returns the original definition being referenced."}
(-> Name (Meta Name))
(do ..monad
[constant (find-def def-name)]
(wrap (case constant
(#.Left real-def-name)
real-def-name
(#.Right _)
def-name))))
(def: #export get-compiler
{#.doc "Obtains the current state of the compiler."}
(Meta Lux)
(function (_ compiler)
(#try.Success [compiler compiler])))
(def: #export type-context
(Meta Type-Context)
(function (_ compiler)
(#try.Success [compiler (get@ #.type-context compiler)])))
(template [<macro> <func>]
[(macro: #export (<macro> tokens)
{#.doc (doc "Performs a macro-expansion and logs the resulting code."
"You can either use the resulting code, or omit them."
"By omitting them, this macro produces nothing (just like the lux.comment macro)."
(<macro> #omit
(def: (foo bar baz)
(-> Int Int Int)
(i.+ bar baz))))}
(case (: (Maybe [Bit Code])
(case tokens
(^ (list [_ (#.Tag ["" "omit"])]
token))
(#.Some [#1 token])
(^ (list token))
(#.Some [#0 token])
_
#.None))
(#.Some [omit? token])
(do ..monad
[cursor ..cursor
output (<func> token)
#let [_ (log! ($_ text@compose (name@encode (name-of <macro>)) " @ " (.cursor-description cursor)))
_ (list@map (|>> code.to-text log!)
output)
_ (log! "")]]
(wrap (if omit?
(list)
output)))
#.None
(fail (..wrong-syntax-error (name-of <macro>)))))]
[log-expand! expand]
[log-expand-all! expand-all]
[log-expand-once! expand-once]
)
|