aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/macro.lux
blob: 5ac4a524e73bf69b5f52aa24beba952aa515c14f (plain)
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
(;module: {#;doc "Functions for extracting information from the state of the compiler."}
  lux
  (lux (macro [ast])
       (control functor
                applicative
                monad)
       (data (coll [list #* "List/" Monoid<List> Monad<List>])
             [number]
             [text "Text/" Monoid<Text> Eq<Text>]
             [product]
             [ident "Ident/" Codec<Text,Ident>]
             maybe
             ["E" error #- fail])))

## (type: (Lux a)
##   (-> Compiler (Error [Compiler a])))

(struct: #export _ (Functor Lux)
  (def: (map f fa)
    (function [state]
      (case (fa state)
        (#;Left msg)
        (#;Left msg)

        (#;Right [state' a])
        (#;Right [state' (f a)])))))

(struct: #export _ (Applicative Lux)
  (def: functor Functor<Lux>)

  (def: (wrap x)
    (function [state]
      (#;Right [state x])))

  (def: (apply ff fa)
    (function [state]
      (case (ff state)
        (#;Right [state' f])
        (case (fa state')
          (#;Right [state'' a])
          (#;Right [state'' (f a)])

          (#;Left msg)
          (#;Left msg))

        (#;Left msg)
        (#;Left msg)))))

(struct: #export _ (Monad Lux)
  (def: applicative Applicative<Lux>)
  
  (def: (join mma)
    (function [state]
      (case (mma state)
        (#;Left msg)
        (#;Left msg)

        (#;Right [state' ma])
        (ma state')))))

(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] (-> Compiler (Lux a) (Error [Compiler a])))
  (action compiler))

(def: #export (run compiler action)
  (All [a] (-> Compiler (Lux a) (Error a)))
  (case (action compiler)
    (#;Left error)
    (#;Left error)

    (#;Right [_ output])
    (#;Right output)))

(def: #export (either left right)
  {#;doc "Pick whichever computation succeeds."}
  (All [a] (-> (Lux a) (Lux a) (Lux a)))
  (function [compiler]
    (case (left compiler)
      (#;Left error)
      (right compiler)

      (#;Right [compiler' output])
      (#;Right [compiler' output]))))

(def: #export (assert message test)
  {#;doc "Fails with the given message if the test is false."}
  (-> Text Bool (Lux Unit))
  (function [compiler]
    (if test
      (#;Right [compiler []])
      (#;Left message))))

(def: #export (fail msg)
  {#;doc "Fails with the given message."}
  (All [a]
    (-> Text (Lux a)))
  (function [_]
    (#;Left msg)))

(def: #export (find-module name)
  (-> Text (Lux Module))
  (function [state]
    (case (get name (get@ #;modules state))
      (#;Some module)
      (#;Right [state module])

      _
      (#;Left ($_ Text/append "Unknown module: " name)))))

(def: #export current-module-name
  (Lux Text)
  (function [state]
    (case (list;last (get@ #;scopes state))
      (#;Some scope)
      (case (get@ #;name scope)
        (#;Cons m-name #;Nil)
        (#;Right [state m-name])

        _
        (#;Left "Improper name for scope."))

      _
      (#;Left "Empty environment!")
      )))

(def: #export current-module
  (Lux Module)
  (do Monad<Lux>
    [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."}
  (-> Ident Anns (Maybe Ann-Value))
  (let [[p n] tag]
    (case anns
      (#;Cons [[p' n'] dmv] anns')
      (if (and (Text/= p p')
               (Text/= n n'))
        (#;Some dmv)
        (get-ann tag anns'))
      
      #;Nil
      #;None)))

(do-template [<name> <tag> <type>]
  [(def: #export (<name> tag anns)
     (-> Ident Anns (Maybe <type>))
     (case (get-ann tag anns)
       (#;Some (<tag> value))
       (#;Some value)

       _
       #;None))]

  [get-bool-ann  #;BoolA  Bool]
  [get-int-ann   #;IntA   Int]
  [get-real-ann  #;RealA  Real]
  [get-char-ann  #;CharA  Char]
  [get-text-ann  #;TextA  Text]
  [get-ident-ann #;IdentA Ident]
  [get-list-ann  #;ListA  (List Ann-Value)]
  [get-dict-ann  #;DictA  (List [Text Ann-Value])]
  )

(def: #export (get-doc anns)
  {#;doc "Looks-up a definition's documentation."}
  (-> Anns (Maybe Text))
  (get-text-ann ["lux" "doc"] anns))

(def: #export (flag-set? flag-name anns)
  {#;doc "Finds out whether an annotation-as-a-flag is set (has value 'true')."}
  (-> Ident Anns Bool)
  (case (get-ann flag-name anns)
    (#;Some (#;BoolA true))
    true

    _
    false))

(do-template [<name> <tag> <desc>]
  [(def: #export <name>
     {#;doc (#;TextA ($_ Text/append "Checks whether a definition is " <desc> "."))}
     (-> Anns Bool)
     (flag-set? (ident-for <tag>)))]

  [export?   #;export?   "exported"]
  [hidden?   #;hidden?   "hidden"]
  [macro?    #;macro?    "a macro"]
  [type?     #;type?     "a type"]
  [struct?   #;struct?   "a structure"]
  [type-rec? #;type-rec? "a recursive type"]
  [sig?      #;sig?      "a signature"]
  )

(do-template [<name> <tag> <type>]
  [(def: (<name> dmv)
     (-> Ann-Value (Maybe <type>))
     (case dmv
       (<tag> actual-value)
       (#;Some actual-value)

       _
       #;None))]

  [try-mlist #;ListA (List Ann-Value)]
  [try-mtext #;TextA Text]
  )

(do-template [<name> <tag> <desc>]
  [(def: #export (<name> anns)
     {#;doc (#;TextA ($_ Text/append "Looks up the arguments of a " <desc> "."))}
     (-> Anns (List Text))
     (default (list)
       (do Monad<Maybe>
         [_args (get-ann (ident-for <tag>) anns)
          args (try-mlist _args)]
         (mapM @ try-mtext args))))]

  [func-args #;func-args "function"]
  [type-args #;type-args "parameterized type"]
  )

(def: (find-macro' modules this-module module name)
  (-> (List [Text Module]) Text Text Text
      (Maybe Macro))
  (do Monad<Maybe>
    [$module (get module modules)
     [def-type def-anns def-value] (: (Maybe Def) (|> (: Module $module) (get@ #;defs) (get name)))]
    (if (and (macro? def-anns)
             (or (export? def-anns) (Text/= module this-module)))
      (#;Some (:! Macro def-value))
      (case (get-ann ["lux" "alias"] def-anns)
        (#;Some (#;IdentA [r-module r-name]))
        (find-macro' modules this-module r-module r-name)

        _
        #;None))))

(def: #export (find-macro ident)
  (-> Ident (Lux (Maybe Macro)))
  (do Monad<Lux>
    [this-module current-module-name]
    (let [[module name] ident]
      (: (Lux (Maybe Macro))
         (function [state]
           (#;Right [state (find-macro' (get@ #;modules state) this-module module name)]))))))

(def: #export (normalize ident)
  {#;doc "If given an identifier without a module prefix, gives it the current module's name as prefix.

          Otherwise, returns the identifier as-is."}
  (-> Ident (Lux Ident))
  (case ident
    ["" name]
    (do Monad<Lux>
      [module-name current-module-name]
      (wrap [module-name name]))

    _
    (:: Monad<Lux> wrap ident)))

(def: #export (macro-expand-once syntax)
  {#;doc "Given code that requires applying a macro, does it once and returns the result.

          Otherwise, returns the code as-is."}
  (-> AST (Lux (List AST)))
  (case syntax
    [_ (#;Form (#;Cons [[_ (#;Symbol macro-name)] args]))]
    (do Monad<Lux>
      [macro-name' (normalize macro-name)
       ?macro (find-macro macro-name')]
      (case ?macro
        (#;Some macro)
        (macro args)
        
        #;None
        (:: Monad<Lux> wrap (list syntax))))

    _
    (:: Monad<Lux> wrap (list syntax))))

(def: #export (macro-expand syntax)
  {#;doc "Given code that requires applying a macro, expands repeatedly until no more direct macro-calls are left.

          Otherwise, returns the code as-is."}
  (-> AST (Lux (List AST)))
  (case syntax
    [_ (#;Form (#;Cons [[_ (#;Symbol macro-name)] args]))]
    (do Monad<Lux>
      [macro-name' (normalize macro-name)
       ?macro (find-macro macro-name')]
      (case ?macro
        (#;Some macro)
        (do Monad<Lux>
          [expansion (macro args)
           expansion' (mapM Monad<Lux> macro-expand expansion)]
          (wrap (:: Monad<List> join expansion')))
        
        #;None
        (:: Monad<Lux> wrap (list syntax))))

    _
    (:: Monad<Lux> wrap (list syntax))))

(def: #export (macro-expand-all syntax)
  {#;doc "Expands all macro-calls everywhere recursively, until only primitive/base code remains."}
  (-> AST (Lux (List AST)))
  (case syntax
    [_ (#;Form (#;Cons [[_ (#;Symbol macro-name)] args]))]
    (do Monad<Lux>
      [macro-name' (normalize macro-name)
       ?macro (find-macro macro-name')]
      (case ?macro
        (#;Some macro)
        (do Monad<Lux>
          [expansion (macro args)
           expansion' (mapM Monad<Lux> macro-expand-all expansion)]
          (wrap (:: Monad<List> join expansion')))
        
        #;None
        (do Monad<Lux>
          [parts' (mapM Monad<Lux> macro-expand-all (list& (ast;symbol macro-name) args))]
          (wrap (list (ast;form (:: Monad<List> join parts')))))))

    [_ (#;Form (#;Cons [harg targs]))]
    (do Monad<Lux>
      [harg+ (macro-expand-all harg)
       targs+ (mapM Monad<Lux> macro-expand-all targs)]
      (wrap (list (ast;form (List/append harg+ (:: Monad<List> join (: (List (List AST)) targs+)))))))

    [_ (#;Tuple members)]
    (do Monad<Lux>
      [members' (mapM Monad<Lux> macro-expand-all members)]
      (wrap (list (ast;tuple (:: Monad<List> join members')))))

    _
    (:: Monad<Lux> wrap (list syntax))))

(def: #export (gensym prefix)
  {#;doc "Generates a unique identifier as an AST 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 (Lux AST))
  (function [state]
    (#;Right [(update@ #;seed n.inc state)
              (ast;symbol ["" ($_ Text/append "__gensym__" prefix (:: number;Codec<Text,Nat> encode (get@ #;seed state)))])])))

(def: (get-local-symbol ast)
  (-> AST (Lux Text))
  (case ast
    [_ (#;Symbol [_ name])]
    (:: Monad<Lux> wrap name)

    _
    (fail (Text/append "AST is not a local symbol: " (ast;to-text ast)))))

(macro: #export (with-gensyms tokens)
  {#;doc (doc "Creates new symbols 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 symbols)] body))
    (do Monad<Lux>
      [symbol-names (mapM @ get-local-symbol symbols)
       #let [symbol-defs (List/join (List/map (: (-> Text (List AST))
                                                 (function [name] (list (ast;symbol ["" name]) (` (gensym (~ (ast;text name)))))))
                                              symbol-names))]]
      (wrap (list (` (do Monad<Lux>
                       [(~@ symbol-defs)]
                       (~ body))))))

    _
    (fail "Wrong syntax for with-gensyms")))

(def: #export (macro-expand-1 token)
  {#;doc "Works just like macro-expand, except that it ensures that the output is a single AST token."}
  (-> AST (Lux AST))
  (do Monad<Lux>
    [token+ (macro-expand token)]
    (case token+
      (^ (list token'))
      (wrap token')

      _
      (fail "Macro expanded to more than 1 element."))))

(def: #export (module-exists? module)
  (-> Text (Lux Bool))
  (function [state]
    (#;Right [state (case (get module (get@ #;modules state))
                      (#;Some _)
                      true
                      
                      #;None
                      false)])))

(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: #export (find-var-type name)
  {#;doc "Looks-up the type of a local variable somewhere in the environment."}
  (-> Text (Lux Type))
  (function [state]
    (let [test (: (-> [Text [Type Top]] Bool)
                  (|>. product;left (Text/= name)))]
      (case (do Monad<Maybe>
              [scope (find (function [env]
                             (or (any? test (: (List [Text [Type Top]])
                                               (get@ [#;locals #;mappings] env)))
                                 (any? test (: (List [Text [Type Top]])
                                               (get@ [#;captured #;mappings] env)))))
                           (get@ #;scopes state))
               [_ [type _]] (try-both (find test)
                                      (: (List [Text [Type Top]])
                                         (get@ [#;locals #;mappings] scope))
                                      (: (List [Text [Type Top]])
                                         (get@ [#;captured #;mappings] scope)))]
              (wrap type))
        (#;Some var-type)
        (#;Right [state var-type])

        #;None
        (#;Left ($_ Text/append "Unknown variable: " name))))))

(def: #export (find-def name)
  {#;doc "Looks-up a definition's whole data in the available modules (including the current one)."}
  (-> Ident (Lux Def))
  (function [state]
    (case (: (Maybe Def)
             (do Monad<Maybe>
               [#let [[v-prefix v-name] name]
                (^slots [#;defs]) (get v-prefix (get@ #;modules state))]
               (get v-name defs)))
      (#;Some _anns)
      (#;Right [state _anns])

      _
      (#;Left ($_ Text/append "Unknown definition: " (Ident/encode name))))))

(def: #export (find-def-type name)
  {#;doc "Looks-up a definition's type in the available modules (including the current one)."}
  (-> Ident (Lux Type))
  (do Monad<Lux>
    [[def-type def-data def-value] (find-def name)]
    (wrap def-type)))

(def: #export (find-type name)
  {#;doc "Looks-up the type of either a local variable or a definition."}
  (-> Ident (Lux Type))
  (do Monad<Lux>
    [#let [[_ _name] name]]
    (either (find-var-type _name)
            (do @
              [name (normalize name)]
              (find-def-type name)))))

(def: #export (find-type-def name)
  {#;doc "Finds the value of a type definition (such as Int, Top or Compiler)."}
  (-> Ident (Lux Type))
  (do Monad<Lux>
    [[def-type def-data def-value] (find-def name)]
    (wrap (:! Type def-value))))

(def: #export (defs module-name)
  {#;doc "The entire list of definitions in a module (including the unexported/private ones)."}
  (-> Text (Lux (List [Text Def])))
  (function [state]
    (case (get module-name (get@ #;modules state))
      #;None          (#;Left ($_ Text/append "Unknown module: " module-name))
      (#;Some module) (#;Right [state (get@ #;defs module)])
      )))

(def: #export (exports module-name)
  {#;doc "All the exported definitions in a module."}
  (-> Text (Lux (List [Text Def])))
  (do Monad<Lux>
    [defs (defs module-name)]
    (wrap (filter (function [[name [def-type def-anns def-value]]]
                    (and (export? def-anns)
                         (not (hidden? def-anns))))
                  defs))))

(def: #export modules
  {#;doc "All the available modules (including the current one)."}
  (Lux (List [Text Module]))
  (function [state]
    (|> state
        (get@ #;modules)
        [state]
        #;Right)))

(def: #export (tags-of type-name)
  {#;doc "All the tags associated with a type definition."}
  (-> Ident (Lux (List Ident)))
  (do Monad<Lux>
    [#let [[module name] type-name]
     module (find-module module)]
    (case (get name (get@ #;types module))
      (#;Some [tags _])
      (wrap tags)

      _
      (wrap (list)))))

(def: #export cursor
  {#;doc "The cursor of the current expression being analyzed."}
  (Lux Cursor)
  (function [state]
    (#;Right [state (get@ #;cursor state)])))

(def: #export expected-type
  {#;doc "The expected type of the current expression being analyzed."}
  (Lux Type)
  (function [state]
    (case (get@ #;expected state)
      (#;Some type)
      (#;Right [state type])

      #;None
      (#;Left "Not expecting any type."))))

(def: #export (imported-modules module-name)
  {#;doc "All the modules imported by a specified module."}
  (-> Text (Lux (List Text)))
  (do Monad<Lux>
    [(^slots [#;imports]) (find-module module-name)]
    (wrap imports)))

(def: #export (resolve-tag tag)
  {#;doc "Given a tag, finds out what is its index, its related tag-list and it's associated type."}
  (-> Ident (Lux [Nat (List Ident) Type]))
  (do Monad<Lux>
    [#let [[module name] tag]
     =module (find-module module)
     this-module-name current-module-name]
    (case (get name (get@ #;tags =module))
      (#;Some [idx tag-list exported? type])
      (if (or exported?
              (Text/= this-module-name module))
        (wrap [idx tag-list type])
        (fail ($_ Text/append "Cannot access tag: " (Ident/encode tag) " from module " this-module-name)))

      _
      (fail ($_ Text/append "Unknown tag: " (Ident/encode tag))))))

(def: #export (tag-lists module)
  {#;doc "All the tag-lists defined in a module, with their associated types."}
  (-> Text (Lux (List [(List Ident) Type])))
  (do Monad<Lux>
    [=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."}
  (Lux (List (List [Text Type])))
  (function [state]
    (case (list;inits (get@ #;scopes state))
      #;None
      (#;Left "No local environment")

      (#;Some scopes)
      (#;Right [state
                (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."}
  (-> Ident (Lux Ident))
  (do Monad<Lux>
    [def-name (normalize def-name)
     [_ def-anns _] (find-def def-name)]
    (case (get-ann (ident-for #;alias) def-anns)
      (#;Some (#;IdentA real-def-name))
      (wrap real-def-name)

      _
      (wrap def-name))))

(def: #export get-compiler
  {#;doc "Obtains the current state of the compiler."}
  (Lux Compiler)
  (function [compiler]
    (#;Right [compiler compiler])))

(def: #export type-context
  (Lux Type-Context)
  (function [compiler]
    (#;Right [compiler (get@ #;type-context compiler)])))

(do-template [<macro> <func> <desc>]
  [(macro: #export (<macro> tokens)
     {#;doc (doc "Performs a macro-expansion and logs the resulting ASTs."
                 "You can either use the resulting ASTs, 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 tokens
       (^ (list [_ (#;Tag ["" "omit"])]
                token))
       (do Monad<Lux>
         [output (<func> token)
          #let [_ (List/map (. log! ast;to-text)
                            output)]]
         (wrap (list)))

       (^ (list token))
       (do Monad<Lux>
         [output (<func> token)
          #let [_ (List/map (. log! ast;to-text)
                            output)]]
         (wrap output))

       _
       (fail ($_ Text/append "Wrong syntax for " <desc> "."))))]

  [log-expand      macro-expand      "log-macro-expand"]
  [log-expand-all  macro-expand-all  "log-macro-expand-all"]
  [log-expand-once macro-expand-once "log-macro-expand-once"]
  )