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

(nominal.def .public (Code of)
  Text

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

  (with_template [<type> <super>+]
    [(with_expansions [<of> (template.symbol [<type> "'"])]
       (nominal.def (<of> of)
         Any)
       (`` (type .public <type>
             (|> Any <of> (,, (template.spliced <super>+))))))]

    [Type [Code]]
    [Expression [Code]]
    [Computation [Expression' Code]]
    )

  (with_template [<type> <super>+]
    [(with_expansions [<brand> (template.symbol [<type> "'"])]
       (nominal.def <brand> Any)
       (`` (type .public <type> (|> <brand> (,, (template.spliced <super>+))))))]

    [Literal [Computation' Expression' Code]]
    )

  (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))

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

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

  (def .public (on parameters function)
    (-> (List Expression) Expression
        Expression)
    (nominal.abstraction
     (%.format (nominal.representation function)
               "("
               (|> parameters
                   (list#each (|>> nominal.representation))
                   (text.interposed ", "))
               ")")))

  ... 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]
    )

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