aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/extension/analysis/ruby.lux
blob: 532c98f27407a8d6ba6f6cb6e304b1cdc2ed8d7e (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
... 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)
   ["[0]" ffi]
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["<>" parser]]
   [data
    [collection
     ["[0]" array]
     ["[0]" dictionary]
     ["[0]" list]]]
   ["[0]" meta (.only)
    [compiler
     ["@" target (.only)
      ["_" ruby]]]
    ["[0]" code
     ["<[1]>" \\parser (.only Parser)]]
    ["[0]" type (.only)
     ["[0]" check]]]]]
 [//
  ["/" lux (.only custom)]
  [///
   ["[0]" extension]
   [//
    ["[0]" phase]
    ["[0]" analysis (.only Operation Phase Handler Bundle)
     ["[1]/[0]" type]]]]])

(def array::new
  (-> Text Handler)
  (custom
   [<code>.any
    (function (_ extension phase archive lengthC)
      (<| analysis/type.with_var
          (function (_ [@read :read:]))
          analysis/type.with_var
          (function (_ [@write :write:]))
          (do phase.monad
            [lengthA (<| (analysis/type.expecting Nat)
                         (phase archive lengthC))
             _ (analysis/type.inference (type_literal (array.Array' :read: :write:)))
             @ meta.location]
            (in [@ {analysis.#Extension (/.translation extension)
                                        (list lengthA)}]))))]))

(def array::length
  (-> Text Handler)
  (custom
   [<code>.any
    (function (_ extension phase archive arrayC)
      (<| analysis/type.with_var
          (function (_ [@read :read:]))
          analysis/type.with_var
          (function (_ [@write :write:]))
          (do phase.monad
            [arrayA (<| (analysis/type.expecting (type_literal (array.Array' :read: :write:)))
                        (phase archive arrayC))
             _ (analysis/type.inference Nat)
             @ meta.location]
            (in [@ {analysis.#Extension (/.translation extension)
                                        (list arrayA)}]))))]))

(def array::read
  (-> Text Handler)
  (custom
   [(<>.and <code>.any <code>.any)
    (function (_ extension phase archive [indexC arrayC])
      (<| analysis/type.with_var
          (function (_ [@read :read:]))
          analysis/type.with_var
          (function (_ [@write :write:]))
          (do phase.monad
            [indexA (<| (analysis/type.expecting Nat)
                        (phase archive indexC))
             arrayA (<| (analysis/type.expecting (type_literal (array.Array' :read: :write:)))
                        (phase archive arrayC))
             _ (analysis/type.inference :read:)
             @ meta.location]
            (in [@ {analysis.#Extension (/.translation extension)
                                        (list indexA arrayA)}]))))]))

(def array::write
  (-> Text Handler)
  (custom
   [(all <>.and <code>.any <code>.any <code>.any)
    (function (_ extension phase archive [indexC valueC arrayC])
      (<| analysis/type.with_var
          (function (_ [@read :read:]))
          analysis/type.with_var
          (function (_ [@write :write:]))
          (do phase.monad
            [indexA (<| (analysis/type.expecting Nat)
                        (phase archive indexC))
             valueA (<| (analysis/type.expecting :write:)
                        (phase archive valueC))
             arrayA (<| (analysis/type.expecting (type_literal (array.Array' :read: :write:)))
                        (phase archive arrayC))
             _ (analysis/type.inference (type_literal (array.Array' :read: :write:)))
             @ meta.location]
            (in [@ {analysis.#Extension (/.translation extension)
                                        (list indexA valueA arrayA)}]))))]))

(def array::delete
  (-> Text Handler)
  (custom
   [(all <>.and <code>.any <code>.any)
    (function (_ extension phase archive [indexC arrayC])
      (<| analysis/type.with_var
          (function (_ [@read :read:]))
          analysis/type.with_var
          (function (_ [@write :write:]))
          (do phase.monad
            [indexA (<| (analysis/type.expecting Nat)
                        (phase archive indexC))
             arrayA (<| (analysis/type.expecting (type_literal (array.Array' :read: :write:)))
                        (phase archive arrayC))
             _ (analysis/type.inference (type_literal (array.Array' :read: :write:)))
             @ meta.location]
            (in [@ {analysis.#Extension (/.translation extension)
                                        (list indexA arrayA)}]))))]))

(def with_array_extensions
  (-> Bundle Bundle)
  (|>> (/.install "ruby_array_new#" array::new)
       (/.install "ruby_array_length#" array::length)
       (/.install "ruby_array_read#" array::read)
       (/.install "ruby_array_write#" array::write)
       (/.install "ruby_array_delete#" array::delete)
       ))

(def Nil
  (for @.ruby ffi.Nil
       Any))

(def Object
  (for @.ruby (type_literal (ffi.Object Any))
       Any))

(def Function
  (for @.ruby ffi.Function
       Any))

(def object::get
  (-> Text Handler)
  (custom
   [(all <>.and <code>.text <code>.any)
    (function (_ extension phase archive [fieldC objectC])
      (do phase.monad
        [objectA (<| (analysis/type.expecting ..Object)
                     (phase archive objectC))
         _ (analysis/type.inference .Any)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list (analysis.text @ fieldC)
                                          objectA)}])))]))

(def object::do
  (-> Text Handler)
  (custom
   [(all <>.and <code>.text <code>.any (<code>.tuple (<>.some <code>.any)))
    (function (_ extension phase archive [methodC objectC inputsC])
      (do [! phase.monad]
        [objectA (<| (analysis/type.expecting ..Object)
                     (phase archive objectC))
         inputsA (monad.each ! (|>> (phase archive) (analysis/type.expecting Any)) inputsC)
         _ (analysis/type.inference .Any)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list.partial (analysis.text @ methodC)
                                                  objectA
                                                  inputsA)}])))]))

(def with_object_extensions
  (-> Bundle Bundle)
  (|>> (/.install "ruby_object_get#" object::get)
       (/.install "ruby_object_do#" object::do)
       (/.install "ruby_object_nil#" (/.nullary ..Nil))
       (/.install "ruby_object_nil?#" (/.unary Any Bit))
       ))

(def ruby::constant
  (-> Text Handler)
  (custom
   [<code>.text
    (function (_ extension phase archive name)
      (do phase.monad
        [_ (analysis/type.inference Any)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list (analysis.text @ name))}])))]))

(def ruby::apply
  (-> Text Handler)
  (custom
   [(all <>.and <code>.any (<code>.tuple (<>.some <code>.any)))
    (function (_ extension phase archive [abstractionC inputsC])
      (do [! phase.monad]
        [abstractionA (<| (analysis/type.expecting ..Function)
                          (phase archive abstractionC))
         inputsA (monad.each ! (|>> (phase archive) (analysis/type.expecting Any)) inputsC)
         _ (analysis/type.inference Any)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list.partial abstractionA inputsA)}])))]))

(def ruby::import
  (-> Text Handler)
  (custom
   [<code>.text
    (function (_ extension phase archive name)
      (do phase.monad
        [_ (analysis/type.inference Bit)
         @ meta.location]
        (in [@ {analysis.#Extension (/.translation extension)
                                    (list (analysis.text @ name))}])))]))

(def .public bundle
  Bundle
  (|> extension.empty
      with_array_extensions
      with_object_extensions

      (/.install "ruby_constant#" ruby::constant)
      (/.install "ruby_apply#" ruby::apply)
      (/.install "ruby_import#" ruby::import)
      (/.install "ruby_script_universe#" (/.nullary .Bit))
      ))