aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/macro/code.lux
blob: b986c02f42a8b3b13268aa94b9266fa49eb11d57 (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
(.module:
  [lux (#- nat int rev)
   [control
    ["." equivalence (#+ Equivalence)]]
   [data
    ["." bit]
    ["." name]
    [number
     ["." nat]
     ["." int]
     ["." rev]
     ["." frac]]
    ["." text ("#;." monoid)]
    [collection
     ["." list ("#;." functor fold)]]]])

## (type: (Code' w)
##   (#.Bit Bit)
##   (#.Nat Nat)
##   (#.Int Int)
##   (#.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 Cursor (Code' (Ann Cursor))))

(def: _cursor Cursor ["" 0 0])

(do-template [<name> <type> <tag>]
  [(def: #export (<name> x)
     (-> <type> Code)
     [_cursor (<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]
  )

(do-template [<name> <tag> <doc>]
  [(def: #export (<name> name)
     {#.doc <doc>}
     (-> Text Code)
     [_cursor (<tag> ["" name])])]

  [local-identifier #.Identifier "Produces a local identifier (an identifier with no module prefix)."]
  [local-tag        #.Tag        "Produces a local tag (a tag with no module prefix)."])

(structure: #export 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 (equivalence.product = =))
          = xs' ys')
      
      _
      #0)))

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

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

    [_ (#.Tag name)]
    (text;compose  "#" (:: name.codec encode name))

    (^template [<tag> <open> <close>]
      [_ (<tag> members)]
      ($_ text;compose
          <open>
          (|> members
              (list;map to-text)
              (list.interpose " ")
              (text.join-with ""))
          <close>))
    ([#.Form  "(" ")"]
     [#.Tuple "[" "]"])

    [_ (#.Record pairs)]
    ($_ text;compose
        "{"
        (|> pairs
            (list;map (function (_ [left right])
                        ($_ text;compose (to-text left) " " (to-text right))))
            (list.interpose " ")
            (text.join-with ""))
        "}")
    ))

(def: #export (replace original substitute ast)
  {#.doc "Replaces all code that looks like the 'original' with the 'substitute'."}
  (-> Code Code Code Code)
  (if (:: ..equivalence = original ast)
    substitute
    (case ast
      (^template [<tag>]
        [cursor (<tag> parts)]
        [cursor (<tag> (list;map (replace original substitute) parts))])
      ([#.Form]
       [#.Tuple])

      [cursor (#.Record parts)]
      [cursor (#.Record (list;map (function (_ [left right])
                                    [(replace original substitute left)
                                     (replace original substitute right)])
                                  parts))]

      _
      ast)))