aboutsummaryrefslogtreecommitdiff
path: root/source/lux/meta/lux.lux
blob: b6ff09f59d0ab0480e3bdb0155f5955010d35392 (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
##  Copyright (c) Eduardo Julian. All rights reserved.
##  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
##  If a copy of the MPL was not distributed with this file,
##  You can obtain one at http://mozilla.org/MPL/2.0/.

(;import lux
         (.. ast)
         (lux/control (monoid #as m)
                      (functor #as F)
                      (monad #as M #refer (#only do))
                      (show #as S))
         (lux/data (list #refer #all #open ("list:" List/Monoid List/Functor))
                   (text #as T #open ("text:" Text/Monoid Text/Eq))
                   (number/int #as I #open ("i" Int/Number))
                   (tuple #as t)
                   ident))

## [Types]
## (deftype (Lux a)
##   (-> Compiler (Either Text (, Compiler a))))

## [Utils]
(def (ident->text ident)
  (-> Ident Text)
  (let [[pre post] ident]
    ($ text:++ pre ";" post)))

## [Structures]
(defstruct #export Lux/Functor (F;Functor Lux)
  (def (map f fa)
    (lambda [state]
      (case (fa state)
        (#;Left msg)
        (#;Left msg)

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

(defstruct #export Lux/Monad (M;Monad Lux)
  (def _functor Lux/Functor)
  (def (wrap x)
    (lambda [state]
      (#;Right [state x])))
  (def (join mma)
    (lambda [state]
      (case (mma state)
        (#;Left msg)
        (#;Left msg)

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

## Functions
(def #export (get-module-name state)
  (Lux Text)
  (case (reverse (get@ #;envs state))
    #;Nil
    (#;Left "Can't get the module name without a module!")

    (#;Cons [env _])
    (#;Right [state (get@ #;name env)])))

(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 (find-macro' modules current-module module name)
  (-> (List (, Text (Module Compiler))) Text Text Text
      (Maybe Macro))
  (case (get module modules)
    (#;Some $module)
    (case (|> (: (Module Compiler) $module) (get@ #;defs) (get name))
      (#;Some gdef)
      (case (: Definition gdef)
        [exported? (#;MacroD macro')]
        (if (or exported? (text:= module current-module))
          (#;Some macro')
          #;None)

        [_ (#;AliasD [r-module r-name])]
        (find-macro' modules current-module r-module r-name)
        
        _
        #;None)
      
      _
      #;None)
    
    _
    #;None))

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

(def #export (normalize ident)
  (-> Ident (Lux Ident))
  (case ident
    ["" name]
    (do Lux/Monad
      [module-name get-module-name]
      (wrap [module-name name]))

    _
    (:: Lux/Monad (wrap ident))))

(def #export (macro-expand syntax)
  (-> AST (Lux (List AST)))
  (case syntax
    [_ (#;FormS (#;Cons [[_ (#;SymbolS macro-name)] args]))]
    (do Lux/Monad
      [macro-name' (normalize macro-name)
       ?macro (find-macro macro-name')]
      (case ?macro
        (#;Some macro)
        (do Lux/Monad
          [expansion (macro args)
           expansion' (M;map% Lux/Monad macro-expand expansion)]
          (wrap (:: List/Monad (join expansion'))))
        
        #;None
        (:: Lux/Monad (wrap (@list syntax)))))

    _
    (:: Lux/Monad (wrap (@list syntax)))))

(def #export (macro-expand-all syntax)
  (-> AST (Lux (List AST)))
  (case syntax
    [_ (#;FormS (#;Cons [[_ (#;SymbolS macro-name)] args]))]
    (do Lux/Monad
      [macro-name' (normalize macro-name)
       ?macro (find-macro macro-name')]
      (case ?macro
        (#;Some macro)
        (do Lux/Monad
          [expansion (macro args)
           expansion' (M;map% Lux/Monad macro-expand-all expansion)]
          (wrap (:: List/Monad (join expansion'))))
        
        #;None
        (do Lux/Monad
          [parts' (M;map% Lux/Monad macro-expand-all (@list& (symbol$ macro-name) args))]
          (wrap (@list (form$ (:: List/Monad (join parts'))))))))

    [_ (#;FormS (#;Cons [harg targs]))]
    (do Lux/Monad
      [harg+ (macro-expand-all harg)
       targs+ (M;map% Lux/Monad macro-expand-all targs)]
      (wrap (@list (form$ (list:++ harg+ (:: List/Monad (join (: (List (List AST)) targs+))))))))

    [_ (#;TupleS members)]
    (do Lux/Monad
      [members' (M;map% Lux/Monad macro-expand-all members)]
      (wrap (@list (tuple$ (:: List/Monad (join members'))))))

    _
    (:: Lux/Monad (wrap (@list syntax)))))

(def #export (gensym prefix state)
  (-> Text (Lux AST))
  (#;Right [(update@ #;seed (i+ 1) state)
            (symbol$ ["" ($ text:++ "__gensym__" prefix (:: I;Int/Show (show (get@ #;seed state))))])]))

(def #export (emit datum)
  (All [a]
    (-> a (Lux a)))
  (lambda [state]
    (#;Right [state datum])))

(def #export (fail msg)
  (All [a]
    (-> Text (Lux a)))
  (lambda [_]
    (#;Left msg)))

(def #export (macro-expand-1 token)
  (-> AST (Lux AST))
  (do Lux/Monad
    [token+ (macro-expand token)]
    (case token+
      (\ (@list token'))
      (wrap token')

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

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

(def #export (exported-defs module state)
  (-> Text (Lux (List Text)))
  (case (get module (get@ #;modules state))
    (#;Some =module)
    (using List/Monad
      (#;Right [state (join (map (: (-> (, Text Definition)
                                        (List Text))
                                    (lambda [gdef]
                                      (let [[name [export? _]] gdef]
                                        (if export?
                                          (@list name)
                                          (@list)))))
                                 (get@ #;defs =module)))]))
    
    #;None
    (#;Left ($ text:++ "Unknown module: " module))))

(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-in-env name state)
  (-> Text Compiler (Maybe Type))
  (case state
    {#;source source #;modules modules
     #;envs   envs   #;type-vars   types             #;host  host
     #;seed   seed   #;eval? eval? #;expected expected
     #;cursor cursor}
    (some (: (-> (Env Text (Meta (, Type Cursor) Analysis)) (Maybe Type))
             (lambda [env]
               (case env
                 {#;name _ #;inner-closures _ #;locals {#;counter _ #;mappings locals} #;closure {#;counter _ #;mappings closure}}
                 (try-both (some (: (-> (, Text (Meta (, Type Cursor) Analysis)) (Maybe Type))
                                    (lambda [binding]
                                      (let [[bname [[type _] _]] binding]
                                        (if (text:= name bname)
                                          (#;Some type)
                                          #;None)))))
                           locals
                           closure))))
          envs)))

(def (find-in-defs' name state)
  (-> Ident Compiler (Maybe Definition))
  (let [[v-prefix v-name] name
        {#;source source #;modules modules
         #;envs   envs   #;type-vars   types   #;host           host
         #;seed   seed   #;eval? eval? #;expected expected
         #;cursor cursor} state]
    (case (get v-prefix modules)
      #;None
      #;None

      (#;Some {#;defs defs #;module-aliases _ #;imports _ #;tags _ #;types _})
      (case (get v-name defs)
        #;None
        #;None

        (#;Some def)
        (case def
          [_ (#;AliasD name')]  (find-in-defs' name' state)
          _                     (#;Some def)
          )))
    ))

(def #export (find-in-defs name state)
  (-> Ident Compiler (Maybe Type))
  (case (find-in-defs' name state)
    (#;Some [_ def-data])
    (case def-data
      (#;ValueD [type value]) (#;Some type)
      (#;MacroD _)            (#;Some Macro)
      (#;TypeD _)             (#;Some Type)
      _                       #;None)
    
    #;None
    #;None))

(def #export (find-var-type name)
  (-> Ident (Lux Type))
  (do Lux/Monad
    [#let [[_ _name] name]
     name' (normalize name)]
    (: (Lux Type)
       (lambda [state]
         (case (find-in-env _name state)
           (#;Some struct-type)
           (#;Right [state struct-type])

           _
           (case (find-in-defs name' state)
             (#;Some struct-type)
             (#;Right [state struct-type])

             _
             (#;Left ($ text:++ "Unknown var: " (ident->text name)))))))
    ))

(def #export (find-type name)
  (-> Ident (Lux Type))
  (do Lux/Monad
    [name' (normalize name)]
    (: (Lux Type)
       (lambda [state]
         (case (find-in-defs' name' state)
           (#;Some def-data)
           (case def-data
             [_ (#;TypeD type)] (#;Right [state type])
             _                  (#;Left ($ text:++ "Definition is not a type: " (ident->text name))))

           _
           (#;Left ($ text:++ "Unknown var: " (ident->text name))))))
    ))

(def #export (defs module-name state)
  (-> Text (Lux (List (, Text Definition))))
  (case (get module-name (get@ #;modules state))
    #;None          (#;Left ($ text:++ "Unknown module: " module-name))
    (#;Some module) (#;Right [state (get@ #;defs module)])
    ))

(def #export (exports module-name)
  (-> Text (Lux (List (, Text Definition))))
  (do Lux/Monad
    [defs (defs module-name)]
    (wrap (filter (lambda [[name [exported? data]]] exported?)
                  defs))))

(def #export (modules state)
  (Lux (List Text))
  (|> state
      (get@ #;modules)
      (list:map t;first)
      (#;Right state)))

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

    _
    (#;Left ($ text:++ "Unknown module: " name))))

(def #export (tags-for [module name])
  (-> Ident (Lux (Maybe (List Ident))))
  (do Lux/Monad
    [module (find-module module)]
    (case (get name (get@ #;types module))
      (#;Some [tags _])
      (wrap (#;Some tags))

      _
      (wrap #;None))))