aboutsummaryrefslogtreecommitdiff
path: root/lux-bootstrapper/src/lux/analyser/function.clj
blob: 1ab6fa10dc6bdaa15672362723c03d5e9cf067fe (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
;; This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
;; If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(ns lux.analyser.function
  (:require clojure.core.match
            clojure.core.match.array
            (lux [base :as & :refer [|let |do return |case]]
                 [host :as &host])
            (lux.analyser [base :as &&]
                          [env :as &env])))

;; [Resource]
(defn with-function [self self-type arg arg-type body]
  (&/with-closure
    (|do [scope-name &/get-scope-name]
      (&env/with-local self self-type
        (&env/with-local arg arg-type
          (|do [=return body
                =captured &env/captured-vars]
            (return (&/T [scope-name =captured =return]))))))))

(defn close-over [scope name register frame]
  (|let [[[register-type register-location] _] register
         register* (&&/|meta register-type register-location
                             (&&/$captured (&/T [scope
                                                 (->> frame (&/get$ &/$captured) (&/get$ &/$counter))
                                                 register])))]
    (&/T [register* (&/update$ &/$captured #(->> %
                                                 (&/update$ &/$counter inc)
                                                 (&/update$ &/$mappings (fn [mps] (&/|put name (&/T [register-type register*]) mps))))
                               frame)])))