aboutsummaryrefslogtreecommitdiff
path: root/src/lux/analyser/lux.clj
blob: f1c7a603590100c83eb09149a32be7ed6d309160 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
(ns lux.analyser.lux
  (:require (clojure [template :refer [do-template]])
            [clojure.core.match :as M :refer [matchv]]
            clojure.core.match.array
            (lux [base :as & :refer [|do return return* fail fail* |let |list]]
                 [parser :as &parser]
                 [type :as &type]
                 [macro :as &macro]
                 [host :as &host])
            (lux.analyser [base :as &&]
                          [lambda :as &&lambda]
                          [case :as &&case]
                          [env :as &&env]
                          [def :as &&def])))

(defn ^:private analyse-1+ [analyse ?token]
  (&type/with-var
    (fn [$var]
      (prn 'analyse-1+ (aget $var 1) (&/show-ast ?token))
      (|do [=expr (&&/analyse-1 analyse $var ?token)]
        (matchv ::M/objects [=expr]
          [["Expression" [?item ?type]]]
          (|do [=type (&type/clean $var ?type)]
            (return (&/V "Expression" (&/T ?item =type))))
          )))))

;; [Exports]
(defn analyse-tuple [analyse exo-type ?elems]
  ;; (prn "^^ analyse-tuple ^^")
  ;; (prn 'analyse-tuple (str "[" (->> ?elems (&/|map &/show-ast) (&/|interpose " ") (&/fold str "")) "]")
  ;;      (&type/show-type exo-type))
  (matchv ::M/objects [exo-type]
    [["lux;TupleT" ?members]]
    (|do [=elems (&/map% (fn [ve]
                           (|let [[elem-t elem] ve]
                             (&&/analyse-1 analyse elem-t elem)))
                         (&/zip2 ?members ?elems))]
      (return (&/|list (&/V "Expression" (&/T (&/V "tuple" =elems)
                                              exo-type)))))

    [_]
    (fail "[Analyser Error] Tuples require tuple-types.")))

(defn analyse-variant [analyse exo-type ident ?value]
  ;; (prn "^^ analyse-variant ^^")
  (|do [;; :let [_ (prn 'analyse-variant/exo-type (&type/show-type exo-type))]
        ?tag (&&/resolved-ident ident)
        exo-type* (matchv ::M/objects [exo-type]
                    [["lux;VarT" ?id]]
                    (&/try-all% (&/|list (|do [exo-type* (&type/deref ?id)]
                                           (&type/actual-type exo-type*))
                                         (|do [_ (&type/set-var ?id &type/Type)]
                                           (&type/actual-type &type/Type))))

                    [_]
                    (&type/actual-type exo-type))
        ;; :let [_ (prn 'analyse-variant/exo-type* (&type/show-type exo-type*))]
        ]
    (matchv ::M/objects [exo-type*]
      [["lux;VariantT" ?cases]]
      (if-let [vtype (&/|get ?tag ?cases)]
        (|do [;; :let [_ (prn 'VARIANT_BODY ?tag (&/show-ast ?value) (&type/show-type vtype))]
              =value (&&/analyse-1 analyse vtype ?value)
              ;; :let [_ (prn 'GOT_VALUE =value)]
              ]
          (return (&/|list (&/V "Expression" (&/T (&/V "variant" (&/T ?tag =value))
                                                  exo-type)))))
        (fail (str "[Analyser Error] There is no case " ?tag " for variant type " (&type/show-type exo-type*))))

      [_]
      (fail (str "[Analyser Error] Can't create a variant if the expected type is " (&type/show-type exo-type*))))))

(defn analyse-record [analyse exo-type ?elems]
  (|do [=elems (&/map% (fn [kv]
                         (matchv ::M/objects [kv]
                           [[k v]]
                           (|do [=v (&&/analyse-1 analyse v)]
                             (return (to-array [k =v])))))
                       ?elems)
        =elems-types (&/map% (fn [kv]
                               (matchv ::M/objects [kv]
                                 [[k v]]
                                 (|do [module (if (= "" k)
                                                &/get-module-name
                                                (return k))
                                       =v (&&/expr-type v)]
                                   (return (to-array [module =v])))))
                             =elems)
        ;; :let [_ (prn 'analyse-tuple =elems)]
        ]
    (return (&/|list (&/V "Expression" (&/T (&/V "lux;record" =elems) (&/V "lux;RecordT" =elems-types)))))))

(defn ^:private show-frame [frame]
  (str "{{" (->> frame (&/get$ "lux;locals") (&/get$ "lux;mappings")
                 &/|keys &/->seq (interpose " ") (reduce str ""))
       "}}"))

(defn ^:private type-test [exo-type binding]
  (|do [btype (&&/expr-type binding)
        o?? (&type/is-Type? exo-type)]
    (if o??
      (|do [i?? (&type/is-Type? btype)]
        (if i??
          (do ;; (println "FOUND TWO TYPES!")
            (return (&/|list binding)))
          (fail "[Type Error] Types don't match.")))
      (|do [_ (&type/check exo-type btype)]
        (return (&/|list binding))))))

(defn analyse-symbol [analyse exo-type ident]
  (|do [module-name &/get-module-name]
    (fn [state]
      (|let [[?module ?name] ident
             local-ident (str ?module ";" ?name)
             global-ident (str (if (= "" ?module) module-name ?module) ";" ?name)
             stack (&/get$ "lux;local-envs" state)
             no-binding? #(and (->> % (&/get$ "lux;locals")  (&/get$ "lux;mappings") (&/|contains? local-ident) not)
                               (->> % (&/get$ "lux;closure") (&/get$ "lux;mappings") (&/|contains? local-ident) not))
             [inner outer] (&/|split-with no-binding? stack)]
        (matchv ::M/objects [outer]
          [["lux;Nil" _]]
          (if-let [global (->> state (&/get$ "lux;global-env") &/from-some (&/get$ "lux;locals") (&/get$ "lux;mappings") (&/|get global-ident))]
            (&/run-state (type-test exo-type global)
                         ;; (|do [btype (&&/expr-type global)
                         ;;       _ (&type/check exo-type btype)]
                         ;;   (return (&/|list global)))
                         state)
            (do (prn (str "((" (->> stack (&/|map show-frame) &/->seq (interpose " ") (reduce str "")) "))"))
              (fail* (str "[Analyser Error] Unrecognized identifier: " local-ident))))

          [["lux;Cons" [top-outer _]]]
          (|let [scopes (&/|tail (&/folds #(&/|cons (&/get$ "lux;name" %2) %1)
                                          (&/|map #(&/get$ "lux;name" %) outer)
                                          (&/|reverse inner)))
                 [=local inner*] (&/fold (fn [register+new-inner frame+in-scope]
                                           (|let [[register new-inner] register+new-inner
                                                  [frame in-scope] frame+in-scope
                                                  [register* frame*] (&&lambda/close-over (&/|cons module-name (&/|reverse in-scope)) ident register frame)]
                                             (&/T register* (&/|cons frame* new-inner))))
                                         (&/T (or (->> top-outer (&/get$ "lux;locals")  (&/get$ "lux;mappings") (&/|get local-ident))
                                                  (->> top-outer (&/get$ "lux;closure") (&/get$ "lux;mappings") (&/|get local-ident)))
                                              (&/|list))
                                         (&/zip2 (&/|reverse inner) scopes))]
            (&/run-state (type-test exo-type =local)
                         ;; (|do [btype (&&/expr-type =local)
                         ;;       _ (&type/check exo-type btype)]
                         ;;   (return (&/|list =local)))
                         (&/set$ "lux;local-envs" (&/|++ inner* outer) state)))
          )))
    ))

(defn ^:private analyse-apply* [analyse exo-type =fn ?args]
  (prn 'analyse-apply*/exo-type (&type/show-type exo-type))
  (matchv ::M/objects [=fn]
    [["Statement" _]]
    (fail "[Analyser Error] Can't apply a statement!")

    [["Expression" [?fun-expr ?fun-type]]]
    (matchv ::M/objects [?args]
      [["lux;Nil" _]]
      (|do [_ (&type/check exo-type ?fun-type)]
        (return (&/|list =fn)))
      
      [["lux;Cons" [?arg ?args*]]]
      (do (prn 'analyse-apply*/=fn (&type/show-type ?fun-type))
        (matchv ::M/objects [?fun-type]
          [["lux;AllT" _]]
          (&type/with-var
            (fn [$var]
              (|do [type* (&type/apply-type ?fun-type $var)
                    output (analyse-apply* analyse exo-type (&/V "Expression" (&/T ?fun-expr type*)) ?args)]
                (matchv ::M/objects [output]
                  [["lux;Cons" [["Expression" [?expr* ?type*]] ["lux;Nil" _]]]]
                  (|do [type** (&type/clean $var ?type*)]
                    (return (&/|list (&/V "Expression" (&/T ?expr* type**)))))

                  [_]
                  (do (prn 'analyse-apply*/output (aget output 0))
                    (assert false))))))

          [["lux;LambdaT" [?input-t ?output-t]]]
          (|do [=arg (&&/analyse-1 analyse ?input-t ?arg)]
            (return (&/|list (&/V "Expression" (&/T (&/V "apply" (&/T =fn =arg))
                                                    ?output-t)))))

          [_]
          (fail "[Analyser Error] Can't apply a non-function.")))
      )))

(defn analyse-apply [analyse exo-type =fn ?args]
  ;; (prn 'analyse-apply1 (aget =fn 0))
  (|do [loader &/loader]
    (matchv ::M/objects [=fn]
      [["Expression" [=fn-form =fn-type]]]
      (do ;; (prn 'analyse-apply2 (aget =fn-form 0))
          (matchv ::M/objects [=fn-form]
            [["global" [?module ?name]]]
            (|do [macro? (&&def/macro? ?module ?name)]
              (if macro?
                (let [macro-class (&host/location (&/|list ?module ?name))]
                  (|do [macro-expansion (&macro/expand loader macro-class ?args)
                        :let [_ (prn 'EXPANDING (&type/show-type exo-type))]
                        output (&/flat-map% (partial analyse exo-type) macro-expansion)]
                    (return output)))
                (analyse-apply* analyse exo-type =fn ?args)))
            
            [_]
            (analyse-apply* analyse exo-type =fn ?args)))

      [_]
      (fail "[Analyser Error] Can't call a statement!"))
    ))

(defn analyse-case [analyse exo-type ?value ?branches]
  (prn 'analyse-case 'exo-type (&type/show-type exo-type) (&/show-ast ?value))
  (|do [:let [num-branches (&/|length ?branches)]
        _ (&/assert! (> num-branches 0) "[Analyser Error] Can't have empty branches in \"case'\" expression.")
        _ (&/assert! (even? num-branches) "[Analyser Error] Unbalanced branches in \"case'\" expression.")
        =value (analyse-1+ analyse ?value)
        =value-type (&&/expr-type =value)
        :let [_ (prn 'analyse-case/GOT_VALUE (&type/show-type =value-type))]
        =match (&&case/analyse-branches analyse exo-type =value-type (&/|as-pairs ?branches))
        :let [_ (prn 'analyse-case/GOT_MATCH)]]
    (return (&/|list (&/V "Expression" (&/T (&/V "case" (&/T =value =match))
                                            exo-type))))))

(defn analyse-lambda* [analyse exo-type ?self ?arg ?body]
  ;; (prn 'analyse-lambda ?self ?arg ?body)
  (matchv ::M/objects [exo-type]
    [["lux;LambdaT" [?arg-t ?return-t]]]
    (|do [[=scope =captured =body] (&&lambda/with-lambda ?self exo-type
                                     ?arg ?arg-t
                                     (&&/analyse-1 analyse ?return-t ?body))]
      (return (&/V "Expression" (&/T (&/V "lambda" (&/T =scope =captured =body)) exo-type))))
    
    [_]
    (fail (str "[Analyser Error] Functions require function types: " (&type/show-type exo-type)))))

(defn analyse-lambda** [analyse exo-type ?self ?arg ?body]
  (prn 'analyse-lambda**/&& (aget exo-type 0))
  (matchv ::M/objects [exo-type]
    [["lux;AllT" _]]
    (&type/with-var
      (fn [$var]
        (|do [exo-type* (&type/apply-type exo-type $var)
              output (analyse-lambda** analyse exo-type* ?self ?arg ?body)]
          (matchv ::M/objects [$var]
            [["lux;VarT" ?id]]
            (|do [? (&type/bound? ?id)]
              (if ?
                (fail "[Analyser Error] Can't use type-var in any type-specific way inside polymorphic functions.")
                (return output)))))))
    
    [_]
    (analyse-lambda* analyse exo-type ?self ?arg ?body)))

(defn analyse-lambda [analyse exo-type ?self ?arg ?body]
  (|do [output (analyse-lambda** analyse exo-type ?self ?arg ?body)]
    (return (&/|list output))))

(defn analyse-def [analyse ?name ?value]
  (prn 'analyse-def/CODE ?name (&/show-ast ?value))
  (|do [module-name &/get-module-name
        ? (&&def/defined? module-name ?name)]
    (if ?
      (fail (str "[Analyser Error] Can't redefine " ?name))
      (|do [;; :let [_ (prn 'analyse-def/_0)]
            =value (&/with-scope ?name
                     (analyse-1+ analyse ?value))
            ;; :let [_ (prn 'analyse-def/_1)]
            =value-type (&&/expr-type =value)
            ;; :let [_ (prn 'analyse-def/_2)]
            :let [_ (prn 'analyse-def/TYPE ?name (&type/show-type =value-type))]
            _ (&&def/define module-name ?name =value-type)
            ;; :let [_ (prn 'analyse-def/_3)]
            ]
        (return (&/|list (&/V "Statement" (&/V "def" (&/T ?name =value)))))))))

(defn analyse-declare-macro [ident]
  (|do [current-module &/get-module-name
        :let [_ (prn 'analyse-declare-macro/current-module current-module)]
        [?module ?name] (&&/resolved-ident* ident)
        :let [_ (prn 'analyse-declare-macro '[?module ?name] [?module ?name])]]
    (if (= ?module current-module)
      (|do [_ (&&def/declare-macro ?module ?name)]
        (return (&/|list)))
      (fail "Can't declare macros from foreign modules."))))

(defn analyse-import [analyse exo-type ?path]
  (assert false)
  (return (&/|list)))

(defn analyse-check [analyse eval! exo-type ?type ?value]
  ;; (println "analyse-check#0")
  (|do [=type (&&/analyse-1 analyse &type/Type ?type)
        ;; =type (analyse-1+ analyse ?type)
        ;; :let [_ (println "analyse-check#1")]
        ==type (eval! =type)
        _ (&type/check exo-type ==type)
        ;; :let [_ (println "analyse-check#4" (&type/show-type ==type))]
        =value (&&/analyse-1 analyse ==type ?value)
        ;; :let [_ (println "analyse-check#5")]
        ]
    (matchv ::M/objects [=value]
      [["Expression" [?expr ?expr-type]]]
      (return (&/|list (&/V "Expression" (&/T ?expr ==type)))))))

(defn analyse-coerce [analyse eval! exo-type ?type ?value]
  (|do [=type (&&/analyse-1 analyse &type/Type ?type)
        ==type (eval! =type)
        =value (&&/analyse-1 analyse ==type ?value)]
    (matchv ::M/objects [=value]
      [["Expression" [?expr ?expr-type]]]
      (return (&/|list (&/V "Expression" (&/T ?expr ==type)))))))