aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/compiler/default/phase/translation.lux
blob: f9b5dfbb48670b9b87606f84f2962df3acfe6c3b (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
(.module:
  [lux #*
   [control
    ["ex" exception (#+ exception:)]
    [monad (#+ do)]]
   [data
    ["." product]
    ["." error (#+ Error)]
    [name ("name/." Equivalence<Name> Codec<Text,Name>)]
    ["." text
     format]
    [collection
     ["." row (#+ Row)]
     ["dict" dictionary (#+ Dictionary)]]]
   [world
    [file (#+ File)]]]
  ["." //
   ["." extension]]
  [//synthesis (#+ Synthesis)])

(do-template [<name>]
  [(exception: #export (<name>)
     "")]

  [no-active-buffer]
  [no-anchor]
  )

(exception: #export (cannot-interpret {message Text})
  message)

(do-template [<name>]
  [(exception: #export (<name> {name Name})
     (ex.report ["Artifact" (name/encode name)]))]

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

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

(signature: #export (Host expression statement)
  (: (-> expression (Error Any))
     evaluate!)
  (: (-> statement (Error Any))
     execute!))

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

(type: #export (Artifacts statement) (Dictionary File (Buffer statement)))

(type: #export (State anchor expression statement)
  {#context Context
   #anchor (Maybe anchor)
   #host (Host expression statement)
   #buffer (Maybe (Buffer statement))
   #artifacts (Artifacts statement)
   #counter Nat})

(type: #export (Operation anchor expression statement)
  (extension.Operation (State anchor expression statement) Synthesis expression))

(type: #export (Phase anchor expression statement)
  (extension.Phase (State anchor expression statement) Synthesis expression))

(type: #export (Handler anchor expression statement)
  (extension.Handler (State anchor expression statement) Synthesis expression))

(type: #export (Bundle anchor expression statement)
  (extension.Bundle (State anchor expression statement) Synthesis expression))

(def: #export (init host)
  (All [anchor expression statement]
    (-> (Host expression statement)
        (..State anchor expression statement)))
  {#context {#scope-name ""
             #inner-functions +0}
   #anchor #.None
   #host host
   #buffer #.None
   #artifacts (dict.new text.Hash<Text>)
   #counter +0})

(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___" (%i (.int 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.Error error)
        (#error.Error error)))))

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

(do-template [<tag>
              <with-declaration> <with-type> <with-value>
              <get> <get-type> <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.Error error)
           (#error.Error 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
         (ex.throw <exception> []))))]

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

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

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

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

(do-template [<name> <inputT>]
  [(def: #export (<name> code)
     (All [anchor expression statement]
       (-> <inputT> (Operation anchor expression statement Any)))
     (function (_ (^@ stateE [bundle state]))
       (case (:: (get@ #host state) <name> code)
         (#error.Error error)
         (ex.throw cannot-interpret error)
         
         (#error.Success output)
         (#error.Success [stateE output]))))]

  [evaluate! expression]
  [execute!  statement]
  )

(def: #export (save! name code)
  (All [anchor expression statement]
    (-> Name statement (Operation anchor expression statement Any)))
  (do //.Monad<Operation>
    [_ (execute! code)
     ?buffer (extension.read (get@ #buffer))]
    (case ?buffer
      (#.Some buffer)
      (if (row.any? (|>> product.left (name/= name)) buffer)
        (//.throw cannot-overwrite-artifact 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]
    (-> File (Operation anchor expression statement Any)))
  (do //.Monad<Operation>
    [buffer ..buffer]
    (extension.update (update@ #artifacts (dict.put target buffer)))))