aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/js/function.lux
blob: b4bf4fa406fb769c292838d6db895718c7f21723 (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
... 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 function)
   [abstract
    ["[0]" monad (.only do)]]
   [data
    ["[0]" product]
    [text
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]]]
   [meta
    [compiler
     [target
      ["_" js (.only Expression Computation Var 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]" phase (.use "[1]#[0]" monad)]
    ["[0]" synthesis]
    ["[1][0]" translation]
    ["//[1]" ///
     [arity (.only Arity)]
     [reference
      [variable (.only Register Variable)]]
     [meta
      [archive
       ["[0]" unit]]
      ["[0]" cache
       [dependency
        ["[1]" artifact]]]]]]]])

(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 functionO argsO+))))

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

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

    _
    [(_.function_definition @self
                            (|> (list.enumeration inits)
                                (list#each (|>> product.left ..capture)))
                            (_.return (_.function @self (list) body!)))
     (_.apply @self inits)]))

(def @curried
  (_.var "curried"))

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

(def @@arguments
  (_.var "arguments"))

(def (@scope function_name)
  (-> unit.ID Text)
  (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))))
     .let [arityO (|> arity .int _.i32)
           @num_args (_.var "num_args")
           @scope (..@scope function_name)
           @self (_.var (///reference.artifact function_name))
           apply_poly (.function (_ args func)
                        (|> func (_.do "apply" (list _.null args))))
           initialize_self! (_.define (//when.register 0) @self)
           initialize! (list#mix (.function (_ post pre!)
                                   (all _.then
                                        pre!
                                        (_.define (..input post) (_.at (_.i32 (.int post)) @@arguments))))
                                 initialize_self!
                                 (list.indices arity))]
     environment (monad.each ! (expression archive) environment)
     .let [[definition instantiation] (with_closure @self environment
                                        (all _.then
                                             (_.define @num_args (_.the "length" @@arguments))
                                             (<| (_.if (|> @num_args (_.= arityO))
                                                   (all _.then
                                                        initialize!
                                                        (_.with_label (_.label @scope)
                                                          (_.do_while (_.boolean true)
                                                                      body!))))
                                                 (_.if (|> @num_args (_.> arityO))
                                                   (let [arity_inputs (|> (_.array (list))
                                                                          (_.the "slice")
                                                                          (_.do "call" (list @@arguments (_.i32 +0) arityO)))
                                                         extra_inputs (|> (_.array (list))
                                                                          (_.the "slice")
                                                                          (_.do "call" (list @@arguments arityO)))]
                                                     (_.return (|> @self
                                                                   (apply_poly arity_inputs)
                                                                   (apply_poly extra_inputs)))))
                                                 ... (|> @num_args (_.< arityO))
                                                 (let [all_inputs (|> (_.array (list))
                                                                      (_.the "slice")
                                                                      (_.do "call" (list @@arguments)))]
                                                   (all _.then
                                                        (_.define @curried all_inputs)
                                                        (_.return (_.closure (list)
                                                                             (let [@missing all_inputs]
                                                                               (_.return (apply_poly (_.do "concat" (list @missing) @curried)
                                                                                                     @self))))))))
                                             ))]
     _ (/////translation.execute! definition)
     _ (/////translation.save! (product.right function_name) {.#None} definition)]
    (in instantiation)))