aboutsummaryrefslogtreecommitdiff
path: root/lux-ruby/source/program.lux
blob: 0eeed48ec45c8ace2f900d43d3ffb72fd8f5fdbf (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
(.module:
  [lux #*
   [abstract
    ["." monad (#+ do)]]
   [control
    pipe
    [cli (#+ program:)]
    ["p" parser]
    ["." exception (#+ exception:)]
    ["." io (#+ IO io)]]
   [data
    ["." maybe]
    ["." error (#+ Error)]
    [number
     ["." i64]]
    ["." text ("#@." hash)
     format]
    [collection
     ["." array (#+ Array)]
     ["." list ("#@." functor)]]]
   ["." macro
    ["s" syntax (#+ syntax:)]
    ["." code]
    ["." template]]
   [world
    ["." file]]
   ["." host (#+ import: interface: do-to object)
    ["_" ruby]]
   [tool
    [compiler
     ["." name]
     ["." synthesis]
     [phase
      [macro (#+ Expander)]
      ["." generation
       ["." ruby
        ["." runtime]
        ["." extension]]]]
     [default
      ["." platform (#+ Platform)]]]]]
  [program
   ["/" compositor
    ["/." cli]]])

(import: #long java/lang/String)

(import: #long (java/lang/Class a))

(import: #long java/lang/Object
  (toString [] java/lang/String)
  (getClass [] (java/lang/Class java/lang/Object)))

(import: #long java/lang/Integer)

(import: #long java/lang/Long
  (intValue [] java/lang/Integer))

(import: #long org/jruby/RubyArray
  (getLength [] int)
  (get [int] #? Object))

(import: #long org/jruby/RubyHash
  (get [Object] #? Object))

(import: #long org/jruby/runtime/builtin/IRubyObject)

(import: #long org/jruby/runtime/ThreadContext)

(template [<name>]
  [(interface: <name>
     (getValue [] java/lang/Object))

   (`` (import: (~~ (template.identifier ["program/" <name>]))
         (getValue [] java/lang/Object)))]

  [StructureValue]
  )

(syntax: (method-inputs {input-classes (s.tuple (p.some s.any))})
  (monad.map @ (function (_ class)
                 (do @
                   [var (macro.gensym "input")]
                   (wrap (code.record (list [var class])))))
             input-classes))

(import: #long org/jruby/runtime/JavaSites$CheckedSites)

(def: (lux-structure value)
  (-> (Array java/lang/Object) org/jruby/runtime/builtin/IRubyObject)
  (with-expansions [<checkers> (template [<name>]
                                 [[<name> [] boolean]]

                                 [isNil] [isTaint] [isClass] [isFrozen]
                                 [isImmediate] [isModule] [isSpecialConst] [isTrue]
                                 [isUntrusted] [hasVariables])
                    <markers> (template [<name>]
                                [[<name> [boolean] void]]

                                [setFrozen] [setTaint] [setUntrusted])
                    <nullaries> (template [<name>]
                                  [[<name> [] org/jruby/runtime/builtin/IRubyObject]]

                                  [dup] [checkArrayType] [inspect] [checkStringType]
                                  [checkStringType19] [id] [rbClone] [anyToString])
                    <class> (template [<name>]
                              [[<name> [] org/jruby/RubyClass]]

                              [getMetaClass] [getType] [getSingletonClass])
                    <call> (template [<inputs>]
                             [[callMethod <inputs>
                               org/jruby/runtime/builtin/IRubyObject]]

                             [[org/jruby/runtime/ThreadContext int java/lang/String]]
                             [[org/jruby/runtime/ThreadContext int java/lang/String org/jruby/runtime/builtin/IRubyObject]]
                             [[org/jruby/runtime/ThreadContext java/lang/String]]
                             [[org/jruby/runtime/ThreadContext java/lang/String org/jruby/runtime/builtin/IRubyObject]]
                             ## [[org/jruby/runtime/ThreadContext java/lang/String (Array org/jruby/runtime/builtin/IRubyObject)]]
                             [[org/jruby/runtime/ThreadContext java/lang/String (Array org/jruby/runtime/builtin/IRubyObject) org/jruby/runtime/Block]]
                             )
                    <placeholders> (template [<name> <inputs> <output>]
                                     [(org/jruby/runtime/builtin/IRubyObject
                                       (<name> (~~ (method-inputs <inputs>)))
                                       <output>
                                       (error! (template.text ["UNIMPLEMENTED METHOD: " <name>])))]

                                     [getJavaClass [] (java/lang/Class java/lang/Object)]
                                     [asJavaString [] java/lang/String]
                                     [getInstanceVariables [] org/jruby/runtime/builtin/InstanceVariables]
                                     [convertToInteger [] org/jruby/RubyInteger]
                                     [convertToInteger [java/lang/String] org/jruby/RubyInteger]
                                     [convertToInteger [int java/lang/String] org/jruby/RubyInteger]
                                     [convertToArray [] org/jruby/RubyArray]
                                     [convertToHash [] org/jruby/RubyHash]
                                     [convertToFloat [] org/jruby/RubyFloat]
                                     [convertToString [] org/jruby/RubyString]
                                     [asString [] org/jruby/RubyString]
                                     [respondsTo [java/lang/String] boolean]
                                     [respondsToMissing [java/lang/String] boolean]
                                     [respondsToMissing [java/lang/String boolean] boolean]
                                     [dataGetStruct [] java/lang/Object]
                                     [dataGetStructChecked [] java/lang/Object]
                                     [infectBy [org/jruby/runtime/builtin/IRubyObject] org/jruby/runtime/builtin/IRubyObject]
                                     [eql [org/jruby/runtime/builtin/IRubyObject] boolean]
                                     [toJava [(java/lang/Class java/lang/Object)] java/lang/Object]

                                     [op_eqq
                                      [org/jruby/runtime/ThreadContext
                                       org/jruby/runtime/builtin/IRubyObject]
                                      org/jruby/runtime/builtin/IRubyObject]
                                     
                                     [op_equal
                                      [org/jruby/runtime/ThreadContext
                                       org/jruby/runtime/builtin/IRubyObject]
                                      org/jruby/runtime/builtin/IRubyObject]
                                     
                                     [callSuper
                                      [org/jruby/runtime/ThreadContext
                                       (Array org/jruby/runtime/builtin/IRubyObject)
                                       org/jruby/runtime/Block]
                                      org/jruby/runtime/builtin/IRubyObject]
                                     
                                     [checkCallMethod
                                      [org/jruby/runtime/ThreadContext
                                       java/lang/String]
                                      org/jruby/runtime/builtin/IRubyObject]
                                     
                                     ## [checkCallMethod
                                     ##  [org/jruby/runtime/ThreadContext
                                     ##   org/jruby/runtime/JavaSites$CheckedSites]
                                     ##  org/jruby/runtime/builtin/IRubyObject]

                                     <checkers>
                                     <markers>
                                     <nullaries>
                                     <class>
                                     <call>
                                     )]
    (`` (object [] [program/StructureValue
                    org/jruby/runtime/builtin/IRubyObject]
          []
          ## Methods
          (program/StructureValue
           (getValue)
           java/lang/Object
           (:coerce (Array java/lang/Object) value))
          
          (org/jruby/runtime/builtin/IRubyObject
           (callMethod {thread-context org/jruby/runtime/ThreadContext}
                       {member java/lang/String}
                       {inputs (Array org/jruby/runtime/builtin/IRubyObject)})
           org/jruby/runtime/builtin/IRubyObject
           (exec
             (log! (format "Was called: " (%t member)))
             (error! "OOPS!")))

          <placeholders>
          ))))

(import: #long org/jruby/RubyProc
  (call [org/jruby/runtime/ThreadContext (Array org/jruby/runtime/builtin/IRubyObject)]
        org/jruby/runtime/builtin/IRubyObject))

(import: #long org/jruby/Ruby
  (getCurrentContext [] org/jruby/runtime/ThreadContext))

(import: #long org/jruby/javasupport/JavaArray
  (new [org/jruby/Ruby java/lang/Object]))

(type: Translator
  (-> java/lang/Object (Error Any)))

(def: (read-tuple read host-object)
  (-> Translator org/jruby/RubyArray (Error Any))
  (let [size (:coerce Nat (org/jruby/RubyArray::getLength host-object))]
    (loop [idx 0
           output (:coerce (Array Any) (array.new size))]
      (if (n/< size idx)
        (case (org/jruby/RubyArray::get (.int idx) host-object)
          #.None
          (recur (inc idx) output)
          
          (#.Some value)
          (case (read value)
            (#error.Failure error)
            (#error.Failure error)

            (#error.Success lux-value)
            (recur (inc idx) (array.write idx lux-value output))))
        (#error.Success output)))))

(exception: (unknown-kind-of-object {object java/lang/Object})
  (exception.report
   ["Class" (java/lang/Object::toString (java/lang/Object::getClass object))]
   ["Object" (java/lang/Object::toString object)]))

(def: (read-variant read host-object)
  (-> Translator org/jruby/RubyHash (Error Any))
  (case [(org/jruby/RubyHash::get runtime.variant-tag-field host-object)
         (org/jruby/RubyHash::get runtime.variant-flag-field host-object)
         (org/jruby/RubyHash::get runtime.variant-value-field host-object)]
    (^multi [(#.Some tag) ?flag (#.Some value)]
            [(read value)
             (#.Some value)])
    (#error.Success [(java/lang/Long::intValue (:coerce java/lang/Long tag))
                     (: Any (case ?flag
                              (#.Some _)
                              ""

                              #.None
                              (host.null)))
                     value])

    _
    (exception.throw ..unknown-kind-of-object host-object)))

(exception: #export nil-has-no-lux-representation)

(def: (read host-object)
  Translator
  (`` (<| (if (host.null? host-object)
            (exception.throw nil-has-no-lux-representation []))
          (~~ (template [<class> <post-processing>]
                [(case (host.check <class> host-object)
                   (#.Some typed-object)
                   (|> typed-object <post-processing>)

                   _)]

                [java/lang/Boolean #error.Success]
                [java/lang/Long #error.Success]
                [java/lang/Double #error.Success]
                [java/lang/String #error.Success]
                [org/jruby/RubyArray (read-tuple read)]
                [org/jruby/RubyHash (read-variant read)]
                [org/jruby/RubySymbol #error.Success]
                [org/jruby/RubyProc #error.Success]
                ))
          (exception.throw ..unknown-kind-of-object host-object)
          )))

(exception: (cannot-apply-a-non-function {object java/lang/Object})
  (exception.report
   ["Non-function" (java/lang/Object::toString object)]))

(import: #long org/jruby/embed/internal/LocalContextProvider
  (getRuntime [] org/jruby/Ruby))

(import: #long org/jruby/embed/ScriptingContainer
  (new [])
  (runScriptlet [java/lang/String] #try #? java/lang/Object)
  (getProvider [] org/jruby/embed/internal/LocalContextProvider))

## TODO; Figure out a way to not need "interpreter" to be a global variable.
(def: interpreter (org/jruby/embed/ScriptingContainer::new))

(def: ensure-macro
  (-> Macro (Maybe org/jruby/RubyProc))
  (|>> (:coerce java/lang/Object) (host.check org/jruby/RubyProc)))

(template: (!ruby-runtime)
  (|> ..interpreter
      org/jruby/embed/ScriptingContainer::getProvider
      org/jruby/embed/internal/LocalContextProvider::getRuntime))

(template: (!ruby-thread-context)
  (|> (!ruby-runtime)
      org/jruby/Ruby::getCurrentContext))

(def: to-host
  (-> Any org/jruby/runtime/builtin/IRubyObject)
  (|>> (:coerce (Array java/lang/Object))
       ..lux-structure
       ## (org/jruby/javasupport/JavaArray::new (!ruby-runtime))
       ))

(def: (call-macro inputs lux macro)
  (-> (List Code) Lux org/jruby/RubyProc (Error (Error [Lux (List Code)])))
  (<| :assume
      ..read
      (:coerce java/lang/Object)
      (org/jruby/RubyProc::call (!ruby-thread-context)
                                (|> (host.array org/jruby/runtime/builtin/IRubyObject 2)
                                    (host.array-write 0 (..to-host inputs))
                                    (host.array-write 1 (..to-host lux)))
                                macro)))

(def: (expander macro inputs lux)
  Expander
  (case (ensure-macro macro)
    (#.Some macro)
    (case (call-macro inputs lux macro)
      (#error.Success output)
      (|> output
          (:coerce java/lang/Object)
          ..read
          (:coerce (Error (Error [Lux (List Code)]))))

      (#error.Failure error)
      (#error.Failure error))

    #.None
    (exception.throw cannot-apply-a-non-function (:coerce java/lang/Object macro))))

(def: separator "___")

(type: Host
  (generation.Host (_.Expression Any) (_.Statement Any)))

(def: host
  (IO Host)
  (io (let [run! (: (-> Text (_.Code Any) (Error Any))
                    (function (_ dummy-name code)
                      (do error.monad
                        [output (org/jruby/embed/ScriptingContainer::runScriptlet (_.code code) ..interpreter)]
                        (..read (maybe.default (:coerce java/lang/Object [])
                                               output)))))]
        (: Host
           (structure
            (def: evaluate! run!)
            (def: execute! run!)
            (def: (define! [module name] input)
              (let [global (format (text.replace-all .module-separator ..separator module)
                                   ..separator (name.normalize name)
                                   "___" (%n (text@hash name)))
                    @global (_.global global)]
                (do error.monad
                  [#let [definition (_.set (list @global) input)]
                   _ (run! global definition)
                   value (run! global @global)]
                  (wrap [global value definition])))))))))

(def: platform
  (IO (Platform IO _.LVar (_.Expression Any) (_.Statement Any)))
  (do io.monad
    [host ..host]
    (wrap {#platform.&monad io.monad
           #platform.&file-system file.system
           #platform.host host
           #platform.phase ruby.generate
           #platform.runtime runtime.generate})))

(def: (program program)
  (-> (_.Expression Any) (_.Statement Any))
  (_.statement (_.apply/* (list (runtime.lux//program-args _.command-line-arguments)
                                _.nil)
                          program)))

(program: [{service /cli.service}]
  (/.compiler ..expander
              ..platform
              extension.bundle
              ..program
              service))