aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/target/c++.lux
blob: 952cc0c0b6f064b4be785bba2d9839dbd1d8681f (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
(.require
 [library
  [lux (.except Code Type Global Declaration int as function template local global type)
   [abstract
    [equivalence (.only Equivalence)]]
   [control
    ["|"  pipe]]
   [data
    ["[0]" text (.only \n \t) (.use "[1]#[0]" equivalence)
     ["%" \\format]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor)]]]
   [math
    [number
     ["f" frac]]]
   [meta
    [macro
     ["[0]" template]]
    [type
     ["[0]" nominal]]]]])

(def parameter_separator ", ")
(def term_delimiters ["(" ")"])
(def type_delimiters ["<" ">"])

(nominal.def .public (Code of)
  Text

  (def .public equivalence
    (All (_ of)
      (Equivalence (Code of)))
    (implementation
     (def (= refererence it)
       (text#= (nominal.representation refererence)
               (nominal.representation it)))))

  (def .public code
    (-> (Code Any)
        Text)
    (|>> nominal.representation))

  (with_template [<super> <type>+]
    [(`` (with_template [<type> <parameter>*']
           [(with_expansions [<parameter>* (template.spliced <parameter>*')
                              <brand> (template.symbol [<type> "'"])]
              (nominal.def (<brand> <parameter>*)
                Any)
              
              (.type .public <type>
                (Ex (_ <parameter>*)
                  (<super> (<brand> <parameter>*)))))]

           (,, (template.spliced <type>+))))]

    [Code
     [[Type [of]]
      [Expression [of]]
      [Statement [of]]]]
    
    [Expression
     [[Computation [of]]
      [Reference [of]]]]

    [Type
     [[Type_Name []]]]

    [Computation
     [[Literal []]]]

    [Reference
     [[Local []]
      [Global []]]]

    [Statement
     [[Declaration []]]]
    )

  (def .public bool
    (-> Bit
        Literal)
    (|>> (|.when
           .false "false"
           .true "true")
         nominal.abstraction))

  (def .public double
    (-> Frac
        Literal)
    (|>> (|.cond [(f.= f.positive_infinity)]
                 [(|.new "(+1.0/0.0)" [])]
                 
                 [(f.= f.negative_infinity)]
                 [(|.new "(-1.0/0.0)" [])]
                 
                 [(f.= f.not_a_number)]
                 [(|.new "(0.0/0.0)" [])]

                 ... else
                 [%.frac])
         nominal.abstraction))

  (.type .public Namespace
    Text)

  (def .public standard
    Namespace
    "std")

  (def .public local
    (-> Text
        Local)
    (|>> nominal.abstraction))

  (def instantiation
    (-> (List Type)
        Text)
    (|>> (|.when
           (list)
           ""

           it
           (|> it
               (list#each ..code)
               (text.interposed ..parameter_separator)
               (text.enclosed ..type_delimiters)))))

  (def .public (global [ns name] parameters)
    (-> [Namespace Text] (List Type)
        Global)
    (nominal.abstraction
     (let [instance (%.format name (instantiation parameters))]
       (when ns
         "" instance
         _ (%.format ns "::" instance)))))

  (def .public (type name parameters)
    (-> [Namespace Text] (List Type)
        Type)
    (|> (..global name parameters)
        nominal.transmutation))

  (def .public type_name
    (-> Text
        Type_Name)
    (|>> nominal.abstraction))

  (with_template [<ns> <name>]
    [(def .public <name>
       Type
       (..type [<ns> (template.text [<name>])] (list)))]

    ["" void]
    )

  (def .public *
    (-> Type
        Type)
    (|>> nominal.representation
         (text.suffix "*")
         nominal.abstraction))

  (def .public deref
    (-> Expression
        Expression)
    (|>> nominal.representation
         (text.prefix "*")
         nominal.abstraction))

  (def .public (as type term)
    (-> Type Expression
        Computation)
    (nominal.abstraction
     (%.format "(" (nominal.representation type) ")"
               " " (nominal.representation term))))

  (def .public int
    (-> Int
        Literal)
    (|>> %.int
         nominal.abstraction))

  (def application
    (-> (List Expression)
        Text)
    (|>> (list#each ..code)
         (text.interposed ..parameter_separator)
         (text.enclosed ..term_delimiters)))

  (def .public (on parameters function)
    (-> (List Expression) Expression
        Expression)
    (nominal.abstraction
     (%.format (nominal.representation function)
               (application parameters))))

  (def .public (new of parameters)
    (-> Type (List Expression)
        Expression)
    (nominal.abstraction
     (%.format "new "
               (nominal.representation of)
               (application parameters))))

  (def .public (do method types parameters object)
    (-> Text (List Type) (List Expression) Expression
        Expression)
    (nominal.abstraction
     (%.format (nominal.representation object)
               "." method
               (instantiation types)
               (application parameters))))

  (def .public (<< it to)
    (-> Expression Expression
        Expression)
    (nominal.abstraction
     (%.format (nominal.representation to)
               " << "
               (nominal.representation it))))

  (def .public (include it)
    (-> Text
        Declaration)
    (nominal.abstraction
     (%.format "#include <" it ">")))

  (def .public (then before after)
    (All (_ of)
      (-> (Statement of) (Statement of)
          (Statement of)))
    (nominal.abstraction
     (%.format (nominal.representation before)
               \n (nominal.representation after))))

  (def statement
    (-> Text
        Statement)
    (|>> (text.suffix ";")
         nominal.abstraction))

  (def .public ;
    (-> Expression
        Statement)
    (|>> nominal.representation
         ..statement))

  (def .public delete
    (-> Expression
        Statement)
    (|>> nominal.representation
         (%.format "delete ")
         ..statement))

  (def template
    (-> (List Type_Name)
        Text)
    (|>> (|.when
           (list)
           ""
           
           it
           (%.format "template"
                     " " (|> it
                             (list#each (|>> nominal.representation (%.format "typename ")))
                             (text.interposed ..parameter_separator)
                             (text.enclosed ..type_delimiters))
                     " "))))

  (.type Argument
    [Type Local])

  (def (argument [type it])
    (-> Argument
        Text)
    (%.format (nominal.representation type)
              " " (nominal.representation it)))

  (def arguments
    (-> (List Argument)
        Text)
    (|>> (list#each ..argument)
         (text.interposed ..parameter_separator)
         (text.enclosed ..term_delimiters)))

  (def block
    (-> Statement
        Text)
    (let [\n\t (%.format \n \t)
          <| (%.format "{" \n)
          |> (%.format \n "}")]
      (|>> nominal.representation
           (text.replaced \n \n\t)
           (text.enclosed [<| |>]))))

  (def .public (function name types inputs output body)
    (-> Local (List Type_Name) (List Argument) Type Statement
        Declaration)
    (nominal.abstraction
     (%.format (..template types) (nominal.representation output)
               " " (nominal.representation name)
               (..arguments inputs)
               " " (..block body))))

  (def .public (namespace it body)
    (-> Namespace Declaration
        Declaration)
    (nominal.abstraction
     (%.format "namespace"
               " " it
               " " (..block body))))

  ... https://en.cppreference.com/w/cpp/types/integer
  (with_template [<name>]
    [(def .public (<name> it)
       (-> Expression
           Expression)
       (..on (list it)
             (nominal.abstraction (template.text [<name>]))))]

    [int64_t]
    )

  (def safe
    (-> Text
        Text)
    (let [\\'' (%.format "\" text.\'')]
      (`` (|>> (,, (with_template [<find> <replace>]
                     [(text.replaced <find> <replace>)]

                     ["\" "\\"]
                     [text.\t "\t"]
                     [text.\v "\v"]
                     [text.\0 "\0"]
                     [text.\b "\b"]
                     [text.\f "\f"]
                     [text.\n "\n"]
                     [text.\r "\r"]
                     [text.\'' \\'']
                     ))
               ))))

  ... https://en.cppreference.com/w/cpp/string/basic_string
  (def .public u32string
    (-> Text
        Literal)
    (|>> ..safe
         %.text
         (%.format "U")
         nominal.abstraction))
  )