aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/lua/host.lux
blob: 1d3f9cae425079944e3bebbe8853e8ef7660f794 (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
(.using
 [library
  [lux "*"
   [abstract
    ["[0]" monad {"+" do}]]
   [control
    ["[0]" function]
    ["<>" parser
     ["<s>" synthesis {"+" Parser}]]]
   [data
    [collection
     ["[0]" dictionary]
     ["[0]" list]]
    [text
     ["%" format {"+" format}]]]
   [target
    ["_" lua {"+" Var Expression}]]]]
 ["[0]" // "_"
  ["[1][0]" common {"+" custom}]
  ["//[1]" /// "_"
   ["/" bundle]
   ["/[1]" // "_"
    ["[0]" extension]
    [generation
     [extension {"+" Nullary Unary Binary Trinary
                 nullary unary binary trinary}]
     ["[0]" reference]
     ["//" lua "_"
      ["[1][0]" runtime {"+" Operation Phase Handler Bundle
                         with_vars}]]]
    ["/[1]" // "_"
     ["[0]" generation]
     ["//[1]" /// "_"
      ["[1][0]" phase]]]]]])

(def: array::new
  (Unary Expression)
  (|>> ["n"] list _.table))

(def: array::length
  (Unary Expression)
  (_.the "n"))

(def: (array::read [indexG arrayG])
  (Binary Expression)
  (_.item (_.+ (_.int +1) indexG) arrayG))

(def: (array::write [indexG valueG arrayG])
  (Trinary Expression)
  (//runtime.array//write indexG valueG arrayG))

(def: (array::delete [indexG arrayG])
  (Binary Expression)
  (//runtime.array//write indexG _.nil arrayG))

(def: array
  Bundle
  (<| (/.prefix "array")
      (|> /.empty
          (/.install "new" (unary array::new))
          (/.install "length" (unary array::length))
          (/.install "read" (binary array::read))
          (/.install "write" (trinary array::write))
          (/.install "delete" (binary array::delete))
          )))

(def: object::get
  Handler
  (custom
   [(all <>.and <s>.text <s>.any)
    (function (_ extension phase archive [fieldS objectS])
      (do ////////phase.monad
        [objectG (phase archive objectS)]
        (in (_.the fieldS objectG))))]))

(def: object::do
  Handler
  (custom
   [(all <>.and <s>.text <s>.any (<>.some <s>.any))
    (function (_ extension phase archive [methodS objectS inputsS])
      (do [! ////////phase.monad]
        [objectG (phase archive objectS)
         inputsG (monad.each ! (phase archive) inputsS)]
        (in (_.do methodS inputsG objectG))))]))

(template [<!> <?> <unit>]
  [(def: <!> (Nullary Expression) (function.constant <unit>))
   (def: <?> (Unary Expression) (_.= <unit>))]

  [object::nil object::nil? _.nil]
  )

(def: object
  Bundle
  (<| (/.prefix "object")
      (|> /.empty
          (/.install "get" object::get)
          (/.install "do" object::do)
          (/.install "nil" (nullary object::nil))
          (/.install "nil?" (unary object::nil?))
          )))

(def: $input
  (_.var "input"))

(def: utf8::encode
  (custom
   [<s>.any
    (function (_ extension phase archive inputS)
      (do [! ////////phase.monad]
        [inputG (phase archive inputS)]
        (in (<| (_.apply (list inputG))
                (_.closure (list $input))
                (_.return (_.apply (list (_.apply (list $input (_.int +1) (_.length $input))
                                                  (_.var "string.byte")))
                                   (_.var "table.pack")))))))]))

(def: utf8::decode
  (custom
   [<s>.any
    (function (_ extension phase archive inputS)
      (do [! ////////phase.monad]
        [inputG (phase archive inputS)]
        (in (_.apply (list (_.apply (list inputG)
                                    (_.var "table.unpack")))
                     (_.var "string.char")))))]))

(def: utf8
  Bundle
  (<| (/.prefix "utf8")
      (|> /.empty
          (/.install "encode" utf8::encode)
          (/.install "decode" utf8::decode)
          )))

(def: lua::constant
  (custom
   [<s>.text
    (function (_ extension phase archive name)
      (# ////////phase.monad in (_.var name)))]))

(def: lua::apply
  (custom
   [(all <>.and <s>.any (<>.some <s>.any))
    (function (_ extension phase archive [abstractionS inputsS])
      (do [! ////////phase.monad]
        [abstractionG (phase archive abstractionS)
         inputsG (monad.each ! (phase archive) inputsS)]
        (in (_.apply inputsG abstractionG))))]))

(def: lua::power
  (custom
   [(all <>.and <s>.any <s>.any)
    (function (_ extension phase archive [powerS baseS])
      (do [! ////////phase.monad]
        [powerG (phase archive powerS)
         baseG (phase archive baseS)]
        (in (_.^ powerG baseG))))]))

(def: lua::import
  (custom
   [<s>.text
    (function (_ extension phase archive module)
      (# ////////phase.monad in
         (_.require/1 (_.string module))))]))

(def: lua::function
  (custom
   [(all <>.and <s>.i64 <s>.any)
    (function (_ extension phase archive [arity abstractionS])
      (do [! ////////phase.monad]
        [abstractionG (phase archive abstractionS)
         .let [variable (is (-> Text (Operation Var))
                            (|>> generation.symbol
                                 (# ! each _.var)))]
         g!inputs (monad.each ! (function (_ _)
                                  (variable "input"))
                              (list.repeated (.nat arity) []))]
        (in (<| (_.closure g!inputs)
                _.return
                (case (.nat arity)
                  0 (_.apply (list //runtime.unit) abstractionG)
                  1 (_.apply g!inputs abstractionG)
                  _ (_.apply (list (_.array g!inputs)) abstractionG))))))]))

(def: .public bundle
  Bundle
  (<| (/.prefix "lua")
      (|> /.empty
          (dictionary.composite ..array)
          (dictionary.composite ..object)
          (dictionary.composite ..utf8)

          (/.install "constant" lua::constant)
          (/.install "apply" lua::apply)
          (/.install "power" lua::power)
          (/.install "import" lua::import)
          (/.install "function" lua::function)
          (/.install "script universe" (nullary (function.constant (_.boolean reference.universe))))
          )))