aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/function/method/apply.lux
blob: c18822e3c6f08ea33e8ba9f40491a210a7bfdd04 (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
... 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 Type Label)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" try]]
   [data
    [collection
     ["[0]" list (.use "[1]#[0]" monoid functor)]]]
   [math
    [number
     ["n" nat]
     ["i" int]
     ["[0]" i32]]]
   [meta
    [compiler
     [target
      [jvm
       ["_" bytecode (.only Label Bytecode) (.use "[1]#[0]" monad)]
       ["[0]" method (.only Method)]
       [constant
        [pool (.only Resource)]]
       [encoding
        ["[0]" signed]]
       ["[0]" type (.only Type)
        ["[0]" category (.only Class)]]]]]]]]
 ["[0]" // (.only)
  ["[1][0]" reset]
  ["[1][0]" implementation]
  ["[1][0]" init]
  ["/[1]" //
   ["[1][0]" abstract]
   [field
    [constant
     ["[1][0]" arity]]
    [variable
     ["[1][0]" partial]
     ["[1][0]" count]
     ["[1][0]" foreign]]]
   ["/[1]" //
    ["[1][0]" runtime]
    ["[1][0]" value]
    ["[1][0]" reference]
    [////
     [analysis (.only Environment)]
     ["[0]" synthesis]
     [///
      [arity (.only Arity)]
      [reference
       [variable (.only Register)]]]]]]])

(def (increment by)
  (-> Nat
      (Bytecode Any))
  (all _.composite
       (<| _.int .i64 by)
       _.iadd))

(def (inputs offset amount)
  (-> Register Nat
      (Bytecode Any))
  (all _.composite
       (|> amount
           list.indices
           (monad.each _.monad (|>> (n.+ offset) _.aload)))
       (_#in [])
       ))

(def (apply offset amount)
  (-> Register Nat
      (Bytecode Any))
  (let [arity (n.min amount ///arity.maximum)]
    (all _.composite
         (_.checkcast ///abstract.class)
         (..inputs offset arity)
         (_.invokevirtual ///abstract.class ////runtime.apply::name (////runtime.apply::type arity))
         (if (n.> ///arity.maximum amount)
           (apply (n.+ ///arity.maximum offset)
                  (n.- ///arity.maximum amount))
           (_#in []))
         )))

(def this_offset 1)

(def .public (method class environment function_arity @begin apply_arity)
  (-> (Type Class) (Environment synthesis.Term) Arity Label Arity
      (Resource Method))
  (let [num_partials (-- function_arity)
        over_extent (i.- (.int apply_arity)
                         (.int function_arity))]
    (method.method //.modifier ////runtime.apply::name
      false (////runtime.apply::type apply_arity)
      (list)
      {.#Some (when num_partials
                0 (all _.composite
                       ////reference.this
                       (..inputs ..this_offset apply_arity)
                       (//implementation.call class function_arity)
                       _.areturn)
                _ (do _.monad
                    [@default _.new_label
                     @labelsH _.new_label
                     @labelsT (|> _.new_label
                                  (list.repeated (-- num_partials))
                                  (monad.all _.monad))
                     .let [cases (|> (list#composite {.#Item [@labelsH @labelsT]}
                                                     (list @default))
                                     list.enumeration
                                     (list#each (function (_ [stage @case])
                                                  (let [current_partials (|> (list.indices stage)
                                                                             (list#each (///partial.get class))
                                                                             (monad.all _.monad))
                                                        already_partial? (n.> 0 stage)
                                                        exact_match? (i.= over_extent (.int stage))
                                                        has_more_than_necessary? (i.> over_extent (.int stage))]
                                                    (all _.composite
                                                         (_.set_label @case)
                                                         (cond exact_match?
                                                               (all _.composite
                                                                    ////reference.this
                                                                    (if already_partial?
                                                                      (_.invokevirtual class //reset.name (//reset.type class))
                                                                      (_#in []))
                                                                    current_partials
                                                                    (..inputs ..this_offset apply_arity)
                                                                    (//implementation.call class function_arity)
                                                                    _.areturn)
                                                               
                                                               has_more_than_necessary?
                                                               (let [arity_inputs (|> function_arity (n.- stage))
                                                                     additional_inputs (|> apply_arity (n.- arity_inputs))]
                                                                 (all _.composite
                                                                      ////reference.this
                                                                      (_.invokevirtual class //reset.name (//reset.type class))
                                                                      current_partials
                                                                      (..inputs ..this_offset arity_inputs)
                                                                      (//implementation.call class function_arity)
                                                                      (apply (n.+ ..this_offset arity_inputs) additional_inputs)
                                                                      _.areturn))

                                                               ... (i.< over_extent (.int stage))
                                                               (let [current_environment (|> (list.indices (list.size environment))
                                                                                             (list#each (///foreign.get class))
                                                                                             (monad.all _.monad))
                                                                     missing_partials (|> _.aconst_null
                                                                                          (list.repeated (|> num_partials (n.- apply_arity) (n.- stage)))
                                                                                          (monad.all _.monad))]
                                                                 (all _.composite
                                                                      (_.new class)
                                                                      _.dup
                                                                      current_environment
                                                                      ////reference.this
                                                                      ///count.value
                                                                      (..increment apply_arity)
                                                                      current_partials
                                                                      (..inputs ..this_offset apply_arity)
                                                                      missing_partials
                                                                      (_.invokespecial class //init.name (//init.type environment function_arity))
                                                                      _.areturn)))))))
                                     (monad.all _.monad))]]
                    (all _.composite
                         ////reference.this
                         ///count.value
                         (_.tableswitch (try.trusted (signed.s4 +0)) @default [@labelsH @labelsT])
                         cases)))})))