aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/macro/template.lux
blob: aea5bfb5c4b08af66d394a492c544a2dbd0c8c5d (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
(.module:
  [library
   [lux (#- let local)
    ["." meta]
    [abstract
     ["." monad (#+ do)]]
    [control
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]
     ["<>" parser ("#\." functor)
      ["<.>" code (#+ Parser)]]]
    [data
     ["." bit ("#\." codec)]
     ["." text]
     [collection
      ["." list ("#\." monad)]
      ["." dictionary (#+ Dictionary)]]]
    [math
     [number
      ["." nat ("#\." decimal)]
      ["." int ("#\." decimal)]
      ["." rev ("#\." decimal)]
      ["." frac ("#\." decimal)]]]]]
  ["." //
   [syntax (#+ syntax:)]
   ["." code]
   ["." local]])

(syntax: .public (spliced [parts (<code>.tuple (<>.some <code>.any))])
  (in parts))

(syntax: .public (amount [parts (<code>.tuple (<>.some <code>.any))])
  (in (list (code.nat (list.size parts)))))

(syntax: .public (with_locals [locals (<code>.tuple (<>.some <code>.local_identifier))
                               body <code>.any])
  (do {! meta.monad}
    [g!locals (|> locals
                  (list\map //.identifier)
                  (monad.all !))]
    (in (list (` (.with_expansions [(~+ (|> (list.zipped/2 locals g!locals)
                                            (list\map (function (_ [name identifier])
                                                        (list (code.local_identifier name) (as_is identifier))))
                                            list\join))]
                   (~ body)))))))

(def: (name_side module_side? parser)
  (-> Bit (Parser Name) (Parser Text))
  (do <>.monad
    [[module short] parser]
    (in (if module_side?
          (case module
            "" short
            _ module)
          short))))

(def: (snippet module_side?)
  (-> Bit (Parser Text))
  (.let [full_identifier (..name_side module_side? <code>.identifier)
         full_tag (..name_side module_side? <code>.tag)]
    ($_ <>.either
        <code>.text
        (if module_side?
          full_identifier
          (<>.either <code>.local_identifier
                     full_identifier))
        (if module_side?
          full_tag
          (<>.either <code>.local_tag
                     full_tag))
        (<>\map bit\encoded <code>.bit)
        (<>\map nat\encoded <code>.nat)
        (<>\map int\encoded <code>.int)
        (<>\map rev\encoded <code>.rev)
        (<>\map frac\encoded <code>.frac)
        )))

(def: (part module_side?)
  (-> Bit (Parser (List Text)))
  (<code>.tuple (<>.many (..snippet module_side?))))

(syntax: .public (text [simple (..part false)])
  (in (list (|> simple (text.interposed "") code.text))))

(template [<name> <simple> <complex>]
  [(syntax: .public (<name> [name (<>.or (<>.and (..part true) (..part false))
                                         (..part false))])
     (case name
       (#.Left [simple complex])
       (in (list (<complex> [(text.interposed "" simple)
                             (text.interposed "" complex)])))
       
       (#.Right simple)
       (in (list (|> simple (text.interposed "") <simple>)))))]

  [identifier code.local_identifier code.identifier]
  [tag code.local_tag code.tag]
  )

(type: Environment
  (Dictionary Text Code))

(def: (applied env template)
  (-> Environment Code Code)
  (case template
    [_ (#.Identifier "" name)]
    (case (dictionary.value name env)
      (#.Some substitute)
      substitute

      #.None
      template)

    (^template [<tag>]
      [[meta (<tag> elems)]
       [meta (<tag> (list\map (applied env) elems))]])
    ([#.Tuple]
     [#.Form])

    [meta (#.Record members)]
    [meta (#.Record (list\map (: (-> [Code Code] [Code Code])
                                 (function (_ [key value])
                                   [(applied env key)
                                    (applied env value)]))
                              members))]

    _
    template))

(type: Local
  {#name Text
   #parameters (List Text)
   #template (List Code)})

(exception: .public (irregular_arguments {expected Nat} {actual Nat})
  (exception.report
   ["Expected" (\ nat.decimal encoded expected)]
   ["Actual" (\ nat.decimal encoded actual)]))

(def: (macro (^slots [#parameters #template]))
  (-> Local Macro)
  ("lux macro"
   (function (_ inputs compiler)
     (.let [parameters_amount (list.size parameters)
            inputs_amount (list.size inputs)]
       (if (nat.= parameters_amount inputs_amount)
         (.let [environment (: Environment
                               (|> (list.zipped/2 parameters inputs)
                                   (dictionary.of_list text.hash)))]
           (#.Right [compiler (list\map (..applied environment) template)]))
         (exception.except ..irregular_arguments [parameters_amount inputs_amount]))))))

(def: local
  (Parser Local)
  (do <>.monad
    [[name parameters] (<code>.form (<>.and <code>.local_identifier
                                            (<>.many <code>.local_identifier)))
     template (<code>.tuple (<>.some <code>.any))]
    (in {#name name
         #parameters parameters
         #template template})))

(syntax: .public (let [locals (<code>.tuple (<>.some ..local))
                       body <code>.any])
  (do meta.monad
    [here_name meta.current_module_name
     expression? (: (Meta Bit)
                    (function (_ lux)
                      (#try.Success [lux (case (value@ #.expected lux)
                                           #.None
                                           false

                                           (#.Some _)
                                           true)])))
     g!pop (local.push (list\map (function (_ local)
                                   [[here_name (value@ #name local)]
                                    (..macro local)])
                                 locals))]
    (if expression?
      (//.with_identifiers [g!body]
        (in (list (` (.let [(~ g!body) (~ body)]
                       (exec (~ g!pop)
                         (~ g!body)))))))
      (in (list body
                g!pop)))))