aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/macro/code.lux
blob: 5de323722699942bd00352b9fce4ac861f313e6e (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
(.module:
  [library
   [lux (#- nat int rev)
    [abstract
     [equivalence (#+ Equivalence)]]
    [data
     ["." product]
     ["." bit]
     ["." name]
     ["." text ("#\." monoid equivalence)]
     [collection
      ["." list ("#\." functor mix)]]]
    [math
     [number
      ["." nat]
      ["." int]
      ["." rev]
      ["." frac]]]
    [meta
     ["." location]]]])

... (type: (Code' w)
...   (#.Bit Bit)
...   (#.Nat Nat)
...   (#.Int Int)
...   (#.Rev Rev)
...   (#.Frac Frac)
...   (#.Text Text)
...   (#.Identifier Name)
...   (#.Tag Name)
...   (#.Form (List (w (Code' w))))
...   (#.Tuple (List (w (Code' w))))
...   (#.Record (List [(w (Code' w)) (w (Code' w))])))

... (type: Code
...   (Ann Location (Code' (Ann Location))))

(template [<name> <type> <tag>]
  [(def: .public (<name> x)
     (-> <type> Code)
     [location.dummy (<tag> x)])]
  
  [bit        Bit                #.Bit]
  [nat        Nat                #.Nat]
  [int        Int                #.Int]
  [rev        Rev                #.Rev]
  [frac       Frac               #.Frac]
  [text       Text               #.Text]
  [identifier Name               #.Identifier]
  [tag        Name               #.Tag]
  [form       (List Code)        #.Form]
  [tuple      (List Code)        #.Tuple]
  [record     (List [Code Code]) #.Record]
  )

(template [<name> <tag>]
  [(def: .public (<name> name)
     (-> Text Code)
     [location.dummy (<tag> ["" name])])]

  [local_identifier #.Identifier]
  [local_tag        #.Tag])

(implementation: .public equivalence
  (Equivalence Code)
  
  (def: (= x y)
    (case [x y]
      (^template [<tag> <eq>]
        [[[_ (<tag> x')] [_ (<tag> y')]]
         (\ <eq> = x' y')])
      ([#.Bit        bit.equivalence]
       [#.Nat        nat.equivalence]
       [#.Int        int.equivalence]
       [#.Rev        rev.equivalence]
       [#.Frac       frac.equivalence]
       [#.Text       text.equivalence]
       [#.Identifier name.equivalence]
       [#.Tag        name.equivalence])

      (^template [<tag>]
        [[[_ (<tag> xs')] [_ (<tag> ys')]]
         (\ (list.equivalence =) = xs' ys')])
      ([#.Form]
       [#.Tuple])

      [[_ (#.Record xs')] [_ (#.Record ys')]]
      (\ (list.equivalence (product.equivalence = =))
         = xs' ys')
      
      _
      false)))

(def: .public (format ast)
  (-> Code Text)
  (case ast
    (^template [<tag> <struct>]
      [[_ (<tag> value)]
       (\ <struct> encoded value)])
    ([#.Bit        bit.codec]
     [#.Nat        nat.decimal]
     [#.Int        int.decimal]
     [#.Rev        rev.decimal]
     [#.Frac       frac.decimal]
     [#.Identifier name.codec])

    [_ (#.Text value)]
    (text.format value)

    [_ (#.Tag name)]
    (text\compose  "#" (\ name.codec encoded name))

    (^template [<tag> <open> <close>]
      [[_ (<tag> members)]
       ($_ text\compose
           <open>
           (list\mix (function (_ next prev)
                       (let [next (format next)]
                         (if (text\= "" prev)
                           next
                           ($_ text\compose prev " " next))))
                     ""
                     members)
           <close>)])
    ([#.Form  "(" ")"]
     [#.Tuple "[" "]"])

    [_ (#.Record pairs)]
    ($_ text\compose
        "{"
        (list\mix (function (_ [left right] prev)
                    (let [next ($_ text\compose (format left) " " (format right))]
                      (if (text\= "" prev)
                        next
                        ($_ text\compose prev " " next))))
                  ""
                  pairs)
        "}")
    ))

(def: .public (replaced original substitute ast)
  (-> Code Code Code Code)
  (if (\ ..equivalence = original ast)
    substitute
    (case ast
      (^template [<tag>]
        [[location (<tag> parts)]
         [location (<tag> (list\map (replaced original substitute) parts))]])
      ([#.Form]
       [#.Tuple])

      [location (#.Record parts)]
      [location (#.Record (list\map (function (_ [left right])
                                      [(replaced original substitute left)
                                       (replaced original substitute right)])
                                    parts))]

      _
      ast)))