aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/macro/context.lux
blob: 4ec34b5df2a41882a315e25bc8b65a728a5d3a11 (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
(.require
 [library
  [lux (.except def global revised)
   [abstract
    [monad (.only do)]]
   [control
    ["?" parser]
    ["[0]" try (.only Try) (.use "[1]#[0]" functor)]
    ["[0]" exception (.only Exception)]
    ["[0]" maybe]
    [function
     [predicate (.only Predicate)]]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence monoid)]
    [collection
     ["[0]" list (.only)
      ["[0]" property]]]]
   ["[0]" meta (.only)
    [type (.only sharing by_example)]
    ["[0]" symbol (.use "[1]#[0]" codec)]
    ["[0]" code (.only)
     ["?[1]" \\parser]]]]]
 ["[0]" // (.only)
  [syntax (.only syntax)
   ["[0]" export]]])

(type .public Stack
  List)

(.def Stack'
  (template (_ value)
    [[(Stack value) Symbol]]))

(with_template [<name> <type> <format>]
  [(exception.def .public (<name> it)
     (Exception <type>)
     (exception.report
      (list ["Definition" (<format> it)])))]

  [not_a_definition Symbol symbol#encoded]
  [not_a_global Symbol symbol#encoded]
  [not_a_module Text text.format]
  [no_example Symbol symbol#encoded]
  )

(.def (global it)
  (-> Symbol (Meta Any))
  (do meta.monad
    [.let [[@ expected_name] it]
     defs (meta.definitions @)]
    (when (list.one (function (_ [actual_name [exported? [type value]]])
                      (if (text#= expected_name actual_name)
                        {.#Some value}
                        {.#None}))
                    defs)
      {.#Some it}
      (in it)
      
      {.#None}
      (meta.failure (exception.error ..not_a_definition [it])))))

(exception.def .public no_active_context)

(.def .public (peek' [_ context])
  (All (_ a) (-> (Stack' a) (Meta a)))
  (do meta.monad
    [stack (..global context)]
    (when (|> stack
              (as (Stack Any))
              list.head)
      {.#Some top}
      (in (as_expected top))
      
      {.#None}
      (meta.failure (exception.error ..no_active_context [])))))

(.def .public peek
  (syntax (_ [g!it (at ?.monad each code.symbol ?code.global)])
    (in (list (` (..peek' [(, g!it) (.symbol (, g!it))]))))))

(.def .public (search' ? [_ context])
  (All (_ a) (-> (Predicate a) (Stack' a) (Meta a)))
  (do meta.monad
    [stack (..global context)]
    (when (|> stack
              (as (Stack Any))
              (list.example (as (Predicate Any) ?)))
      {.#Some it}
      (in (as_expected it))
      
      {.#None}
      (meta.failure (exception.error ..no_example [context])))))

(.def .public search
  (syntax (_ [g!? ?code.any
              g!context (at ?.monad each code.symbol ?code.global)])
    (in (list (` (..search' (, g!?) [(, g!context) (.symbol (, g!context))]))))))

(.def (alter on_definition [_ definition])
  (All (_ value)
    (-> (-> Symbol Definition (Try Definition)) (Stack' value)
        (Meta Any)))
  (function (_ lux)
    (let [[@ context] definition
          on_global (is (-> Global (Try Global))
                        (function (_ it)
                          (when it
                            {.#Definition it}
                            (try#each (|>> {.#Definition}) (on_definition definition it))
                            
                            _
                            (exception.except ..not_a_definition [definition]))))
          on_globals (is (-> (property.List [Bit Global]) (Try (property.List [Bit Global])))
                         (function (_ globals)
                           (when (property.value context globals)
                             {.#Some [exported? global]}
                             (try#each (function (_ global)
                                         (property.has context [exported? global] globals))
                                       (on_global global))
                             
                             {.#None}
                             (exception.except ..not_a_global [definition]))))
          on_module (is (-> Module (Try Module))
                        (function (_ module)
                          (try#each (function (_ globals)
                                      (has .#definitions globals module))
                                    (on_globals (the .#definitions module)))))
          on_lux (is (-> Lux (Try Lux))
                     (function (_ lux)
                       (when (property.value @ (the .#modules lux))
                         {.#Some module}
                         (try#each (function (_ module)
                                     (.revised .#modules (property.has @ module) lux))
                                   (on_module module))
                         
                         {.#None}
                         (exception.except ..not_a_module [@]))))]
      (when (on_lux lux)
        {try.#Success it}
        {try.#Success [it []]}
        
        {try.#Failure error}
        ((meta.failure error) lux)))))

(.def .public (push' top)
  (All (_ value)
    (-> value (Stack' value)
        (Meta Any)))
  (alter (function (_ _ [type stack])
           (|> stack
               (as (Stack Any))
               {.#Item top}
               (is (Stack Any))
               [type]
               {try.#Success}))))

(.def .public push
  (syntax (_ [g!it ?code.any
              g!context (at ?.monad each code.symbol ?code.global)])
    (in (list (` (..push' (, g!it) [(, g!context) (.symbol (, g!context))]))))))

(.def .public (revised' ? !)
  (All (_ value)
    (-> (Maybe (Predicate value)) (-> value value) (Stack' value)
        (Meta Any)))
  (alter (function (_ @ [type stack])
           (let [stack (sharing [value]
                         (is (-> value value)
                             !)
                         (is (Stack value)
                             (as_expected stack)))]
             (when ?
               {.#Some ?}
               (do try.monad
                 [stack (loop (again [stack stack])
                          (when stack
                            (list.partial top stack')
                            (if (? top)
                              (in (list.partial (! top) stack'))
                              (do try.monad
                                [stack' (again stack')]
                                (in (list.partial top stack'))))

                            _
                            (exception.except ..no_example [@])))]
                 (in [type stack]))
               
               {.#None}
               (when stack
                 (list.partial top stack')
                 (|> stack'
                     (list.partial (! top))
                     (is (Stack Any))
                     [type]
                     {try.#Success})
                 
                 _
                 (exception.except ..no_example [@])))))))

(.def .public revised
  (syntax (_ [g!predicate ?code.any
              g!revision ?code.any
              g!context (at ?.monad each code.symbol ?code.global)])
    (in (list (` (..revised' (, g!predicate)
                             (, g!revision)
                             [(, g!context) (.symbol (, g!context))]))))))

(.def .public pop''
  (All (_ value) (-> (Stack' value) (Meta Any)))
  (alter (function (_ _ [type value])
           (|> (let [value (as (Stack Any) value)]
                 (maybe.else value (list.tail value)))
               [type]
               {try.#Success}))))

(.def .public pop'
  (syntax (_ [expression? ?code.bit
              context ?code.global])
    (do meta.monad
      [_ (..pop'' [(list) context])]
      (in (if expression?
            (list (' []))
            (list))))))

(.def .public pop
  (syntax (_ [g!context (at ?.monad each code.symbol ?code.global)])
    (in (list (` (..pop'' [(, g!context) (.symbol (, g!context))]))))))

(.def .public def
  (syntax (_ [.let [! ?.monad
                    ?local (at ! each code.local ?code.local)]

              [export_$? $] (?code.tuple (export.with ?code.local))
              [export_expression? g!expression] (?code.tuple (export.with ?local))
              [export_declaration? g!declaration] (?code.tuple (export.with ?local))
              
              context_type ?code.any])
    (do [! meta.monad]
      [@ meta.current_module_name
       .let [g!context (code.symbol [@ $])]]
      (//.with_symbols [g!it g!body g!_]
        (in (list (` (.def (, export_$?) (, (code.local $))
                       (..Stack (, context_type))
                       (list)))
                  (` (.def (, export_expression?) ((, g!expression) (, g!it) (, g!body))
                       (-> (, context_type) Code (Meta Code))
                       (do meta.monad
                         [(, g!_) (..push (, g!it) (, g!context))]
                         ((,' in) (` (let [((,' ,') (, g!body)) ((,' ,) (, g!body))
                                           ((,' ,') (, g!_)) (..pop' #1 (, g!context))]
                                       ((,' ,') (, g!body))))))))
                  (` (.def (, export_declaration?) ((, g!declaration) (, g!it) (, g!body))
                       (-> (, context_type) Code (Meta (List Code)))
                       (do meta.monad
                         [(, g!_) (..push (, g!it) (, g!context))]
                         ((,' in) (list (, g!body)
                                        (` (..pop' #0 (, g!context))))))))
                  ))))))