aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/macro/ast.lux
blob: 4fd9c8b2cfde76ddc241595277cd798345a73998 (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
(;module:
  lux
  (lux (control eq)
       (data bool
             number
             [char]
             [text #+ Eq<Text> "Text/" Monoid<Text>]
             ident
             (coll [list #* "" Functor<List> Fold<List>])
             )))

## [Types]
## (type: (AST' w)
##   (#;BoolS Bool)
##   (#;NatS Nat)
##   (#;IntS Int)
##   (#;RealS Real)
##   (#;CharS Char)
##   (#;TextS Text)
##   (#;SymbolS Text Text)
##   (#;TagS Text Text)
##   (#;FormS (List (w (AST' w))))
##   (#;TupleS (List (w (AST' w))))
##   (#;RecordS (List [(w (AST' w)) (w (AST' w))])))

## (type: AST
##   (Meta Cursor (AST' (Meta Cursor))))

## [Utils]
(def: _cursor Cursor ["" +0 +0])

## [Functions]
(do-template [<name> <type> <tag>]
  [(def: #export (<name> x)
     (-> <type> AST)
     [_cursor (<tag> x)])]
  
  [bool   Bool             #;BoolS]
  [nat    Nat              #;NatS]
  [int    Int              #;IntS]
  [deg   Deg             #;DegS]
  [real   Real             #;RealS]
  [char   Char             #;CharS]
  [text   Text             #;TextS]
  [symbol Ident            #;SymbolS]
  [tag    Ident            #;TagS]
  [form   (List AST)       #;FormS]
  [tuple  (List AST)       #;TupleS]
  [record (List [AST AST]) #;RecordS]
  )

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

  [local-symbol #;SymbolS "Produces a local symbol (a symbol with no module prefix)."]
  [local-tag    #;TagS    "Produces a local tag (a tag with no module prefix)."])

## [Structures]
(struct: #export _ (Eq AST)
  (def: (= x y)
    (case [x y]
      (^template [<tag> <eq>]
        [[_ (<tag> x')] [_ (<tag> y')]]
        (:: <eq> = x' y'))
      ([#;BoolS   Eq<Bool>]
       [#;NatS    Eq<Nat>]
       [#;IntS    Eq<Int>]
       [#;DegS    Eq<Deg>]
       [#;RealS   Eq<Real>]
       [#;CharS   char;Eq<Char>]
       [#;TextS   Eq<Text>]
       [#;SymbolS Eq<Ident>]
       [#;TagS    Eq<Ident>])

      (^template [<tag>]
        [[_ (<tag> xs')] [_ (<tag> ys')]]
        (and (:: Eq<Nat> = (size xs') (size ys'))
             (fold (function [[x' y'] old]
                     (and old (= x' y')))
                   true
                   (zip2 xs' ys'))))
      ([#;FormS]
       [#;TupleS])

      [[_ (#;RecordS xs')] [_ (#;RecordS ys')]]
      (and (:: Eq<Nat> = (size xs') (size ys'))
           (fold (function [[[xl' xr'] [yl' yr']] old]
                   (and old (= xl' yl') (= xr' yr')))
                 true
                 (zip2 xs' ys')))
      
      _
      false)))

## [Values]
(def: #export (to-text ast)
  (-> AST Text)
  (case ast
    (^template [<tag> <struct>]
      [_ (<tag> value)]
      (:: <struct> encode value))
    ([#;BoolS   Codec<Text,Bool>]
     [#;NatS    Codec<Text,Nat>]
     [#;IntS    Codec<Text,Int>]
     [#;DegS    Codec<Text,Deg>]
     [#;RealS   Codec<Text,Real>]
     [#;CharS   char;Codec<Text,Char>]
     [#;TextS   text;Codec<Text,Text>]
     [#;SymbolS Codec<Text,Ident>])

    [_ (#;TagS ident)]
    (Text/append  "#" (:: Codec<Text,Ident> encode ident))

    (^template [<tag> <open> <close>]
      [_ (<tag> members)]
      ($_ Text/append <open> (|> members (map to-text) (interpose " ") (text;join-with "")) <close>))
    ([#;FormS  "(" ")"]
     [#;TupleS "[" "]"])

    [_ (#;RecordS pairs)]
    ($_ Text/append "{" (|> pairs (map (function [[left right]] ($_ Text/append (to-text left) " " (to-text right)))) (interpose " ") (text;join-with "")) "}")
    ))

(def: #export (replace original substitute ast)
  {#;doc "Replaces all ASTs that look like 'original' with the 'substitute' in the given AST."}
  (-> AST AST AST AST)
  (if (:: Eq<AST> = original ast)
    substitute
    (case ast
      (^template [<tag>]
        [cursor (<tag> parts)]
        [cursor (<tag> (map (replace original substitute) parts))])
      ([#;FormS]
       [#;TupleS])

      [cursor (#;RecordS parts)]
      [cursor (#;RecordS (map (function [[left right]]
                                [(replace original substitute left)
                                 (replace original substitute right)])
                              parts))]

      _
      ast)))