aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang.lux
blob: 5a00794f87264ac9a24f0e28aa1c37634a580db7 (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
(.module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:])
       (data [maybe]
             [product]
             ["e" error]
             [text "text/" Eq<Text>]
             text/format
             (coll [list]))
       [macro]
       (macro ["s" syntax #+ syntax:])
       (lang (type ["tc" check])))
  (luxc (lang ["la" analysis])))

(type: #export Eval
  (-> Type Code (Meta Top)))

(type: #export Analyser
  (-> Code (Meta la.Analysis)))

(def: #export version Text "0.6.0")

(def: #export (fail message)
  (All [a] (-> Text (Meta a)))
  (do macro.Monad<Meta>
    [[file line col] macro.cursor
     #let [location (format file
                            "," (|> line nat-to-int %i)
                            "," (|> col nat-to-int %i))]]
    (macro.fail (format message "\n\n"
                        "@ " location))))

(def: #export (throw exception message)
  (All [a] (-> ex.Exception Text (Meta a)))
  (fail (exception message)))

(syntax: #export (assert exception message test)
  (wrap (list (` (if (~ test)
                   (:: macro.Monad<Meta> (~' wrap) [])
                   (..throw (~ exception) (~ message)))))))

(def: #export (with-type expected action)
  (All [a] (-> Type (Meta a) (Meta a)))
  (function [compiler]
    (case (action (set@ #.expected (#.Some expected) compiler))
      (#e.Success [compiler' output])
      (let [old-expected (get@ #.expected compiler)]
        (#e.Success [(set@ #.expected old-expected compiler')
                     output]))

      (#e.Error error)
      (#e.Error error))))

(def: #export (with-type-env action)
  (All [a] (-> (tc.Check a) (Meta a)))
  (function [compiler]
    (case (action (get@ #.type-context compiler))
      (#e.Error error)
      ((fail error) compiler)

      (#e.Success [context' output])
      (#e.Success [(set@ #.type-context context' compiler)
                   output]))))

(def: #export (with-fresh-type-env action)
  (All [a] (-> (Meta a) (Meta a)))
  (function [compiler]
    (let [old (get@ #.type-context compiler)]
      (case (action (set@ #.type-context tc.fresh-context compiler))
        (#e.Success [compiler' output])
        (#e.Success [(set@ #.type-context old compiler')
                     output])

        output
        output))))

(def: #export (infer actualT)
  (-> Type (Meta Unit))
  (do macro.Monad<Meta>
    [expectedT macro.expected-type]
    (with-type-env
      (tc.check expectedT actualT))))

(def: #export (pl-get key table)
  (All [a] (-> Text (List [Text a]) (Maybe a)))
  (case table
    #.Nil
    #.None

    (#.Cons [k' v'] table')
    (if (text/= key k')
      (#.Some v')
      (pl-get key table'))))

(def: #export (pl-contains? key table)
  (All [a] (-> Text (List [Text a]) Bool))
  (case (pl-get key table)
    (#.Some _)
    true

    #.None
    false))

(def: #export (pl-put key val table)
  (All [a] (-> Text a (List [Text a]) (List [Text a])))
  (case table
    #.Nil
    (list [key val])

    (#.Cons [k' v'] table')
    (if (text/= key k')
      (#.Cons [key val]
              table')
      (#.Cons [k' v']
              (pl-put key val table')))))

(def: #export (pl-update key f table)
  (All [a] (-> Text (-> a a) (List [Text a]) (List [Text a])))
  (case table
    #.Nil
    #.Nil

    (#.Cons [k' v'] table')
    (if (text/= key k')
      (#.Cons [k' (f v')] table')
      (#.Cons [k' v'] (pl-update key f table')))))

(def: #export (with-source-code source action)
  (All [a] (-> Source (Meta a) (Meta a)))
  (function [compiler]
    (let [old-source (get@ #.source compiler)]
      (case (action (set@ #.source source compiler))
        (#e.Error error)
        (#e.Error error)

        (#e.Success [compiler' output])
        (#e.Success [(set@ #.source old-source compiler')
                     output])))))

(def: #export (with-stacked-errors handler action)
  (All [a] (-> (-> [] Text) (Meta a) (Meta a)))
  (function [compiler]
    (case (action compiler)
      (#e.Success [compiler' output])
      (#e.Success [compiler' output])

      (#e.Error error)
      (#e.Error (if (text/= "" error)
                  (handler [])
                  (format (handler []) "\n\n-----------------------------------------\n\n" error))))))

(def: fresh-bindings
  (All [k v] (Bindings k v))
  {#.counter +0
   #.mappings (list)})

(def: fresh-scope
  Scope
  {#.name     (list)
   #.inner    +0
   #.locals   fresh-bindings
   #.captured fresh-bindings})

(def: #export (with-scope action)
  (All [a] (-> (Meta a) (Meta [Scope a])))
  (function [compiler]
    (case (action (update@ #.scopes (|>> (#.Cons fresh-scope)) compiler))
      (#e.Success [compiler' output])
      (case (get@ #.scopes compiler')
        #.Nil
        (#e.Error "Impossible error: Drained scopes!")

        (#.Cons head tail)
        (#e.Success [(set@ #.scopes tail compiler')
                     [head output]]))

      (#e.Error error)
      (#e.Error error))))

(def: #export (with-current-module name action)
  (All [a] (-> Text (Meta a) (Meta a)))
  (function [compiler]
    (case (action (set@ #.current-module (#.Some name) compiler))
      (#e.Success [compiler' output])
      (#e.Success [(set@ #.current-module
                         (get@ #.current-module compiler)
                         compiler')
                   output])

      (#e.Error error)
      (#e.Error error))))

(def: #export (with-cursor cursor action)
  (All [a] (-> Cursor (Meta a) (Meta a)))
  (if (text/= "" (product.left cursor))
    action
    (function [compiler]
      (let [old-cursor (get@ #.cursor compiler)]
        (case (action (set@ #.cursor cursor compiler))
          (#e.Success [compiler' output])
          (#e.Success [(set@ #.cursor old-cursor compiler')
                       output])

          (#e.Error error)
          (#e.Error error))))))

(def: (normalize-char char)
  (-> Nat Text)
  (case char
    (^ (char "*")) "_ASTER_"
    (^ (char "+")) "_PLUS_"
    (^ (char "-")) "_DASH_"
    (^ (char "/")) "_SLASH_"
    (^ (char "\\")) "_BSLASH_"
    (^ (char "_")) "_UNDERS_"
    (^ (char "%")) "_PERCENT_"
    (^ (char "$")) "_DOLLAR_"
    (^ (char "'")) "_QUOTE_"
    (^ (char "`")) "_BQUOTE_"
    (^ (char "@")) "_AT_"
    (^ (char "^")) "_CARET_"
    (^ (char "&")) "_AMPERS_"
    (^ (char "=")) "_EQ_"
    (^ (char "!")) "_BANG_"
    (^ (char "?")) "_QM_"
    (^ (char ":")) "_COLON_"
    (^ (char ".")) "_PERIOD_"
    (^ (char ",")) "_COMMA_"
    (^ (char "<")) "_LT_"
    (^ (char ">")) "_GT_"
    (^ (char "~")) "_TILDE_"
    (^ (char "|")) "_PIPE_"
    _
    (text.from-code char)))

(def: underflow Nat (n/dec +0))

(def: #export (normalize-name name)
  (-> Text Text)
  (loop [idx (n/dec (text.size name))
         output ""]
    (if (n/= underflow idx)
      output
      (recur (n/dec idx) (format (|> (text.nth idx name) maybe.assume normalize-char) output)))))

(exception: #export Error)

(def: #export (with-error-tracking action)
  (All [a] (-> (Meta a) (Meta a)))
  (function [compiler]
    (case (action compiler)
      (#e.Error error)
      ((throw Error error) compiler)

      output
      output)))