aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/lua/function.lux
blob: 6066313730f8a46fe4d5c63042e47c03abe04ed2 (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
... 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/.

(.require
 [library
  [lux (.except Label function)
   [abstract
    ["[0]" monad (.only do)]]
   [data
    ["[0]" product]
    [text
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]]]
   [meta
    [compiler
     [target
      ["_" lua (.only Var Expression Label Statement)]]]]]]
 ["[0]" //
  ["[1][0]" runtime (.only Operation Phase Phase! Translator)]
  ["[1][0]" reference]
  ["[1][0]" when]
  ["/[1]" //
   ["[1][0]" reference]
   ["//[1]" ///
    [analysis (.only Abstraction Reification)]
    ["[0]" synthesis]
    ["[0]" phase (.use "[1]#[0]" monad)]
    ["[1][0]" translation]
    ["//[1]" ///
     [arity (.only Arity)]
     [meta
      [archive
       ["[0]" unit]]
      ["[0]" cache
       [dependency
        ["[1]" artifact]]]]
     [reference
      [variable (.only Register Variable)]]]]]])

(def .public (apply expression archive [functionS argsS+])
  (Translator (Reification synthesis.Term))
  (do [! phase.monad]
    [functionO (expression archive functionS)
     argsO+ (monad.each ! (expression archive) argsS+)]
    (in (_.apply argsO+ functionO))))

(def capture
  (-> Register Var)
  (|>> (///reference.foreign //reference.system) as_expected))

(def (with_closure inits @self @args body!)
  (-> (List Expression) Var (List Var) Statement [Statement Expression])
  (when inits
    {.#End}
    [(_.function @self @args body!)
     @self]

    _
    (let [@inits (|> (list.enumeration inits)
                     (list#each (|>> product.left ..capture)))]
      [(_.function @self @inits
         (all _.then
              (_.local_function @self @args body!)
              (_.return @self)))
       (_.apply inits @self)])))

(def input
  (|>> ++ //when.register))

(def (@scope function_name)
  (-> unit.ID Label)
  (_.label (format (///reference.artifact function_name) "_scope")))

(def .public (function statement expression archive [environment arity bodyS])
  (-> Phase! (Translator (Abstraction synthesis.Term)))
  (do [! phase.monad]
    [dependencies (cache.dependencies archive bodyS)
     [function_name body!] (/////translation.with_new_context archive dependencies
                             (do !
                               [@scope (of ! each ..@scope
                                           (/////translation.context archive))]
                               (/////translation.with_anchor [1 @scope]
                                 (statement expression archive bodyS))))
     closureO+ (monad.each ! (expression archive) environment)
     .let [@curried (_.var "curried")
           arityO (|> arity .int _.int)
           @num_args (_.var "num_args")
           @scope (..@scope function_name)
           @self (_.var (///reference.artifact function_name))
           initialize_self! (_.local/1 (//when.register 0) @self)
           initialize! (list#mix (.function (_ post pre!)
                                   (all _.then
                                        pre!
                                        (_.local/1 (..input post) (_.item (|> post ++ .int _.int) @curried))))
                                 initialize_self!
                                 (list.indices arity))
           pack (|>> (list) _.array)
           unpack (is (-> Expression Expression)
                      (.function (_ it)
                        (_.apply (list it) (_.var "table.unpack"))))
           @var_args (_.var "...")]
     .let [[definition instantiation] (with_closure closureO+ @self (list @var_args)
                                        (all _.then
                                             (_.local/1 @curried (pack @var_args))
                                             (_.local/1 @num_args (_.length @curried))
                                             (<| (_.if (|> @num_args (_.= arityO))
                                                   (all _.then
                                                        initialize!
                                                        (_.set_label @scope)
                                                        body!))
                                                 (_.if (|> @num_args (_.> arityO))
                                                   (let [arity_inputs (_.apply (list @curried
                                                                                     (_.int +1)
                                                                                     arityO
                                                                                     (_.int +1)
                                                                                     (_.array (list)))
                                                                               (_.var "table.move"))
                                                         extra_inputs (_.apply (list @curried
                                                                                     (_.+ (_.int +1) arityO)
                                                                                     @num_args
                                                                                     (_.int +1)
                                                                                     (_.array (list)))
                                                                               (_.var "table.move"))]
                                                     (_.return (|> @self
                                                                   (_.apply (list (unpack arity_inputs)))
                                                                   (_.apply (list (unpack extra_inputs)))))))
                                                 ... (|> @num_args (_.< arityO))
                                                 (_.return (_.closure (list @var_args)
                                                                      (let [@extra_args (_.var "extra_args")]
                                                                        (all _.then
                                                                             (_.local/1 @extra_args (pack @var_args))
                                                                             (_.return (_.apply (list (unpack (_.apply (list @extra_args
                                                                                                                             (_.int +1)
                                                                                                                             (_.length @extra_args)
                                                                                                                             (_.+ (_.int +1) @num_args)
                                                                                                                             (_.apply (list @curried
                                                                                                                                            (_.int +1)
                                                                                                                                            @num_args
                                                                                                                                            (_.int +1)
                                                                                                                                            (_.array (list)))
                                                                                                                                      (_.var "table.move")))
                                                                                                                       (_.var "table.move"))))
                                                                                                @self)))))))
                                             ))]
     _ (/////translation.execute! definition)
     _ (/////translation.save! (product.right function_name) {.#None} definition)]
    (in instantiation)))