aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/ffi.scm.lux
blob: 585bf4e2f2b798c6224e8c856361a845804fcfbf (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
(.require
 [library
  [lux (.except Alias)
   [abstract
    [monad (.only do)]]
   [control
    ["<>" parser (.use "[1]#[0]" monad)]
    ["[0]" io]
    ["[0]" maybe]]
   [data
    ["[0]" product]
    ["[0]" text
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor)]]]
   ["[0]" meta (.only)
    [type
     ["[0]" nominal (.except def)]]
    ["[0]" code (.only)
     ["<[1]>" \\parser (.only Parser)]]
    [macro (.only with_symbols)
     [syntax (.only syntax)]
     ["[0]" template]]]]])

(nominal.def .public (Object brand) Any)

(with_template [<name>]
  [(with_expansions [<brand> (template.symbol [<name> "'"])]
     (nominal.def .public <brand> Any)
     (type .public <name>
       (..Object <brand>)))]

  [Nil]
  [Function]
  )

(with_template [<name> <type>]
  [(type .public <name>
     <type>)]

  [Boolean Bit]
  [Integer Int]
  [Float   Frac]
  [String  Text]
  )

(type Nilable
  [Bit Code])

(def nilable
  (Parser Nilable)
  (let [token (' "?")]
    (<| (<>.and (<>.parses? (<code>.this token)))
        (<>.after (<>.not (<code>.this token)))
        <code>.any)))

(type Alias
  Text)

(def alias
  (Parser Alias)
  (<>.after (<code>.this (' "as")) <code>.local))

(type Field
  [Bit Text (Maybe Alias) Nilable])

(def static!
  (Parser Any)
  (<code>.this (' "static")))

(def field
  (Parser Field)
  (<code>.form (all <>.and
                    (<>.parses? ..static!)
                    <code>.local
                    (<>.maybe ..alias)
                    ..nilable)))

(def constant
  (Parser Field)
  (<code>.form (all <>.and
                    (<>#in true)
                    <code>.local
                    (<>.maybe ..alias)
                    ..nilable)))

(type Common_Method
  (Record
   [#name Text
    #alias (Maybe Alias)
    #inputs (List Nilable)
    #io? Bit
    #try? Bit
    #output Nilable]))

(def common_method
  (Parser Common_Method)
  (all <>.and
       <code>.local
       (<>.maybe ..alias)
       (<code>.tuple (<>.some ..nilable))
       (<>.parses? (<code>.this (' "io")))
       (<>.parses? (<code>.this (' "try")))
       ..nilable))

(def input_variables
  (-> (List Nilable) (List [Bit Code]))
  (|>> list.enumeration
       (list#each (function (_ [idx [nilable? type]])
                    [nilable? (|> idx %.nat code.local)]))))

(def (nilable_type [nilable? type])
  (-> Nilable Code)
  (if nilable?
    (` (.Maybe (, type)))
    type))

(def (with_nil g!temp [nilable? input])
  (-> Code [Bit Code] Code)
  (if nilable?
    (` (when (, input)
         {.#Some (, g!temp)}
         (, g!temp)

         {.#None}
         ("scheme object nil")))
    input))

(def (without_nil g!temp [nilable? outputT] output)
  (-> Code Nilable Code Code)
  (if nilable?
    (` (let [(, g!temp) (, output)]
         (if ("scheme object nil?" (, g!temp))
           {.#None}
           {.#Some (, g!temp)})))
    (` (let [(, g!temp) (, output)]
         (if (not ("scheme object nil?" (, g!temp)))
           (, g!temp)
           (.panic! "Nil is an invalid value!"))))))

(type Import
  (Variant
   {#Function Common_Method}
   {#Constant Field}))

(def import
  (Parser Import)
  (all <>.or
       (<code>.form ..common_method)
       ..constant
       ))

(def (with_io with? without)
  (-> Bit Code Code)
  (if with?
    (` (io.io (, without)))
    without))

(def (io_type io? rawT)
  (-> Bit Code Code)
  (if io?
    (` (io.IO (, rawT)))
    rawT))

(def (with_try with? without_try)
  (-> Bit Code Code)
  (if with?
    (` (..try (, without_try)))
    without_try))

(def (try_type try? rawT)
  (-> Bit Code Code)
  (if try?
    (` (.Either .Text (, rawT)))
    rawT))

(def (make_function g!method g!temp source inputsT io? try? outputT)
  (-> Code Code Code (List Nilable) Bit Bit Nilable Code)
  (let [g!inputs (input_variables inputsT)]
    (` (def ((, g!method)
             [(,* (list#each product.right g!inputs))])
         (-> [(,* (list#each nilable_type inputsT))]
             (, (|> (nilable_type outputT)
                    (try_type try?)
                    (io_type io?))))
         (as_expected
          (, (<| (with_io io?)
                 (with_try try?)
                 (without_nil g!temp outputT)
                 (` ("scheme apply"
                     (as ..Function (, source))
                     (,* (list#each (with_nil g!temp) g!inputs)))))))))))

(def .public import
  (syntax (_ [import ..import])
    (with_symbols [g!temp]
      (when import
        {#Function [name alias inputsT io? try? outputT]}
        (let [imported (` ("scheme constant" (, (code.text name))))]
          (in (list (..make_function (code.local (maybe.else name alias))
                                     g!temp
                                     imported
                                     inputsT
                                     io?
                                     try?
                                     outputT))))

        {#Constant [_ name alias fieldT]}
        (let [imported (` ("scheme constant" (, (code.text name))))
              g!name (code.local (maybe.else name alias))]
          (in (list (` (def (, g!name)
                         (syntax ((, g!name) [])
                           (of meta.monad (,' in)
                               (list (` (.as (, (nilable_type fieldT)) (, imported)))))))))))
        ))))