aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function/method/new.lux
blob: 028f8e1e92752710910bcec7f9e59ec0fa0aed93 (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
(.using
 [library
  [lux (.except Type type)
   [abstract
    ["[0]" monad (.only do)]]
   [data
    [collection
     ["[0]" list]]]
   [math
    [number
     ["n" nat]]]
   [target
    [jvm
     ["[0]" field (.only Field)]
     ["[0]" method (.only Method)]
     ["_" bytecode (.only Bytecode)]
     ["[0]" constant
      [pool (.only Resource)]]
     [type (.only Type)
      ["[0]" category (.only Class Value Return)]]]]
   [tool
    [compiler
     [meta
      ["[0]" archive (.only Archive)]]]]]]
 ["[0]" //
  ["[1][0]" init]
  ["[1][0]" implementation]
  ["/[1]" // "_"
   [field
    [constant
     ["[1][0]" arity]]
    [variable
     ["[1][0]" foreign]
     ["[1][0]" partial]]]
   ["/[1]" // "_"
    [runtime (.only Operation Phase)]
    ["[1][0]" value]
    ["[1][0]" reference]
    [////
     [analysis (.only Environment)]
     [synthesis (.only Synthesis)]
     [///
      ["[0]" arity (.only Arity)]
      ["[0]" phase]]]]]])

(def: .public (instance' foreign_setup class environment arity)
  (-> (List (Bytecode Any)) (Type Class) (Environment Synthesis) Arity (Bytecode Any))
  (all _.composite
       (_.new class)
       _.dup
       (monad.all _.monad foreign_setup)
       (///partial.new arity)
       (_.invokespecial class //init.name (//init.type environment arity))))

(def: .public (instance generate archive class environment arity)
  (-> Phase Archive (Type Class) (Environment Synthesis) Arity (Operation (Bytecode Any)))
  (do [! phase.monad]
    [foreign* (monad.each ! (generate archive) environment)]
    (in (instance' foreign* class environment arity))))

(def: .public (method class environment arity)
  (-> (Type Class) (Environment Synthesis) Arity (Resource Method))
  (let [after_this (is (-> Nat Nat)
                       (n.+ 1))
        environment_size (list.size environment)
        after_environment (is (-> Nat Nat)
                              (|>> after_this (n.+ environment_size)))
        after_arity (is (-> Nat Nat)
                        (|>> after_environment (n.+ 1)))]
    (method.method //.modifier //init.name
                   #0 (//init.type environment arity)
                   (list)
                   {.#Some (all _.composite
                                ////reference.this
                                (//init.super environment_size arity)
                                (monad.each _.monad (function (_ register)
                                                      (///foreign.put class register (_.aload (after_this register))))
                                            (list.indices environment_size))
                                (monad.each _.monad (function (_ register)
                                                      (///partial.put class register (_.aload (after_arity register))))
                                            (list.indices (n.- ///arity.minimum arity)))
                                _.areturn)})))