aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/base.lux
blob: c0108da7e9b09fa72e6ce21fa50d9d3ea6a8d3b9 (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
(;module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:])
       (data [text "text/" Eq<Text>]
             text/format
             [product]
             ["R" result])
       [macro]
       (type ["tc" check]))
  (luxc (lang ["la" analysis])))

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

(type: #export Analyser
  (-> Code (Lux la;Analysis)))

(type: #export Path Text)

(type: #export Mode
  #Build
  #Eval
  #REPL)

(def: #export compiler-version Text "0.6.0")

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

(def: #export (assert message test)
  (-> Text Bool (Lux Unit))
  (if test
    (:: macro;Monad<Lux> wrap [])
    (fail message)))

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

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

      (#R;Error error)
      (#R;Error error))))

(def: #export (within-type-env action)
  (All [a] (-> (tc;Check a) (Lux a)))
  (function [compiler]
    (case (action (get@ #;type-context compiler))
      (#R;Error error)
      (#R;Error error)

      (#R;Success [context' output])
      (#R;Success [(set@ #;type-context context' compiler)
                   output]))))

(def: #export (infer actualT)
  (-> Type (Lux Unit))
  (do macro;Monad<Lux>
    [expectedT macro;expected-type]
    (within-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] (-> [Cursor Text] (Lux a) (Lux a)))
  (function [compiler]
    (let [old-source (get@ #;source compiler)]
      (case (action (set@ #;source source compiler))
        (#R;Error error)
        (#R;Error error)

        (#R;Success [compiler' output])
        (#R;Success [(set@ #;source old-source compiler')
                     output])))))

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

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

(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] (-> (Lux a) (Lux [Scope a])))
  (function [compiler]
    (case (action (update@ #;scopes (|>. (#;Cons fresh-scope)) compiler))
      (#R;Success [compiler' output])
      (case (get@ #;scopes compiler')
        #;Nil
        (#R;Error "Impossible error: Drained scopes!")

        (#;Cons head tail)
        (#R;Success [(set@ #;scopes tail compiler')
                     [head output]]))

      (#R;Error error)
      (#R;Error error))))

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

          (#R;Error error)
          (#R;Error error))))))