aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/generation.lux
blob: 203c5d4ab3dae12f4ae459704029f6ee013b8b05 (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
(.module:
  [lux #*
   [control
    [monad (#+ do)]
    ["." exception (#+ exception:)]]
   [data
    ["." product]
    ["." error (#+ Error)]
    ["." name ("#;." equivalence)]
    ["." text
     format]
    [collection
     ["." row (#+ Row)]
     ["." dictionary (#+ Dictionary)]]]
   [world
    [file (#+ Path)]]]
  ["." //
   ["." extension]
   [//
    [synthesis (#+ Synthesis)]]])

(exception: #export (cannot-interpret {error Text})
  (exception.report
   ["Error" error]))

(exception: #export (unknown-lux-name {name Name})
  (exception.report
   ["Name" (%name name)]))

(exception: #export (cannot-overwrite-lux-name {lux-name Name}
                                               {old-host-name Text}
                                               {new-host-name Text})
  (exception.report
   ["Lux Name" (%name lux-name)]
   ["Old Host Name" old-host-name]
   ["New Host Name" new-host-name]))

(do-template [<name>]
  [(exception: #export (<name> {name Name})
     (exception.report
      ["Output" (%name name)]))]

  [cannot-overwrite-output]
  [no-buffer-for-saving-code]
  )

(type: #export Context
  {#scope-name Text
   #inner-functions Nat})

(signature: #export (Host expression statement)
  (: (-> Text expression (Error Any))
     evaluate!)
  (: (-> Text statement (Error Any))
     execute!)
  (: (-> Name expression (Error [Text Any statement]))
     define!))

(type: #export (Buffer statement) (Row [Name statement]))

(type: #export (Output statement) (Dictionary Path (Buffer statement)))

(type: #export (State anchor expression statement)
  {#context Context
   #anchor (Maybe anchor)
   #host (Host expression statement)
   #buffer (Maybe (Buffer statement))
   #output (Output statement)
   #counter Nat
   #name-cache (Dictionary Name Text)})

(do-template [<special> <general>]
  [(type: #export (<special> anchor expression statement)
     (<general> (State anchor expression statement) Synthesis expression))]

  [State+    extension.State]
  [Operation extension.Operation]
  [Phase     extension.Phase]
  [Handler   extension.Handler]
  [Bundle    extension.Bundle]
  )

(def: #export (state host)
  (All [anchor expression statement]
    (-> (Host expression statement)
        (..State anchor expression statement)))
  {#context {#scope-name ""
             #inner-functions 0}
   #anchor #.None
   #host host
   #buffer #.None
   #output (dictionary.new text.hash)
   #counter 0
   #name-cache (dictionary.new name.hash)})

(def: #export (with-context expr)
  (All [anchor expression statement output]
    (-> (Operation anchor expression statement output)
        (Operation anchor expression statement [Text output])))
  (function (_ [bundle state])
    (let [[old-scope old-inner] (get@ #context state)
          new-scope (format old-scope "c" (%n old-inner))]
      (case (expr [bundle (set@ #context [new-scope 0] state)])
        (#error.Success [[bundle' state'] output])
        (#error.Success [[bundle' (set@ #context [old-scope (inc old-inner)] state')]
                         [new-scope output]])

        (#error.Failure error)
        (#error.Failure error)))))

(def: #export context
  (All [anchor expression statement]
    (Operation anchor expression statement Text))
  (extension.read (|>> (get@ #context)
                       (get@ #scope-name))))

(def: #export empty-buffer Buffer row.empty)

(do-template [<tag>
              <with-declaration> <with-type> <with-value>
              <set> <get> <get-type> <exception>]
  [(exception: #export <exception>)

   (def: #export <with-declaration>
     (All [anchor expression statement output] <with-type>)
     (function (_ body)
       (function (_ [bundle state])
         (case (body [bundle (set@ <tag> (#.Some <with-value>) state)])
           (#error.Success [[bundle' state'] output])
           (#error.Success [[bundle' (set@ <tag> (get@ <tag> state) state')]
                            output])

           (#error.Failure error)
           (#error.Failure error)))))

   (def: #export <get>
     (All [anchor expression statement]
       (Operation anchor expression statement <get-type>))
     (function (_ (^@ stateE [bundle state]))
       (case (get@ <tag> state)
         (#.Some output)
         (#error.Success [stateE output])

         #.None
         (exception.throw <exception> []))))

   (def: #export (<set> value)
     (All [anchor expression statement]
       (-> <get-type> (Operation anchor expression statement Any)))
     (function (_ [bundle state])
       (#error.Success [[bundle (set@ <tag> (#.Some value) state)]
                        []])))]

  [#anchor
   (with-anchor anchor)
   (-> anchor (Operation anchor expression statement output)
       (Operation anchor expression statement output))
   anchor
   set-anchor anchor anchor no-anchor]

  [#buffer
   with-buffer
   (-> (Operation anchor expression statement output)
       (Operation anchor expression statement output))
   ..empty-buffer
   set-buffer buffer (Buffer statement) no-active-buffer]
  )

(def: #export output
  (All [anchor expression statement]
    (Operation anchor expression statement (Output statement)))
  (extension.read (get@ #output)))

(def: #export next
  (All [anchor expression statement]
    (Operation anchor expression statement Nat))
  (do //.monad
    [count (extension.read (get@ #counter))
     _ (extension.update (update@ #counter inc))]
    (wrap count)))

(do-template [<name> <inputT>]
  [(def: #export (<name> label code)
     (All [anchor expression statement]
       (-> Text <inputT> (Operation anchor expression statement Any)))
     (function (_ (^@ state+ [bundle state]))
       (case (:: (get@ #host state) <name> label code)
         (#error.Success output)
         (#error.Success [state+ output])

         (#error.Failure error)
         (exception.throw cannot-interpret error))))]

  [evaluate! expression]
  [execute!  statement]
  )

(def: #export (define! name code)
  (All [anchor expression statement]
    (-> Name expression (Operation anchor expression statement [Text Any statement])))
  (function (_ (^@ stateE [bundle state]))
    (case (:: (get@ #host state) define! name code)
      (#error.Success output)
      (#error.Success [stateE output])

      (#error.Failure error)
      (exception.throw cannot-interpret error))))

(def: #export (save! name code)
  (All [anchor expression statement]
    (-> Name statement (Operation anchor expression statement Any)))
  (do //.monad
    [count ..next
     _ (execute! (format "save" (%n count)) code)
     ?buffer (extension.read (get@ #buffer))]
    (case ?buffer
      (#.Some buffer)
      (if (row.any? (|>> product.left (name;= name)) buffer)
        (//.throw cannot-overwrite-output name)
        (extension.update (set@ #buffer (#.Some (row.add [name code] buffer)))))
      
      #.None
      (//.throw no-buffer-for-saving-code name))))

(def: #export (save-buffer! target)
  (All [anchor expression statement]
    (-> Path (Operation anchor expression statement Any)))
  (do //.monad
    [buffer ..buffer]
    (extension.update (update@ #output (dictionary.put target buffer)))))

(def: #export (remember lux-name)
  (All [anchor expression statement]
    (-> Name (Operation anchor expression statement Text)))
  (function (_ (^@ stateE [_ state]))
    (let [cache (get@ #name-cache state)]
      (case (dictionary.get lux-name cache)
        (#.Some host-name)
        (#error.Success [stateE host-name])
        
        #.None
        (exception.throw unknown-lux-name lux-name)))))

(def: #export (learn lux-name host-name)
  (All [anchor expression statement]
    (-> Name Text (Operation anchor expression statement Any)))
  (function (_ [bundle state])
    (let [cache (get@ #name-cache state)]
      (case (dictionary.get lux-name cache)
        #.None
        (#error.Success [[bundle
                          (update@ #name-cache
                                   (dictionary.put lux-name host-name)
                                   state)]
                         []])

        (#.Some old-host-name)
        (exception.throw cannot-overwrite-lux-name [lux-name old-host-name host-name])))))