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

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

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

(def .public with_locals
  (syntax (_ [locals (<code>.tuple (<>.some <code>.local))
              body <code>.any])
    (do [! meta.monad]
      [g!locals (|> locals
                    (list#each //.symbol)
                    (monad.all !))]
      (in (list (` (.with_expansions [(,* (|> (list.zipped_2 locals g!locals)
                                              (list#each (function (_ [name symbol])
                                                           (list (code.local name) symbol)))
                                              list#conjoint))]
                     (, body))))))))

(def (symbol_side module_side? parser)
  (-> Bit (Parser Symbol) (Parser Text))
  (do <>.monad
    [[module short] parser]
    (in (if module_side?
          (when module
            "" short
            _ module)
          short))))

(def (snippet module_side?)
  (-> Bit (Parser Text))
  (.let [full_symbol (..symbol_side module_side? <code>.symbol)]
    (all <>.either
         <code>.text
         (if module_side?
           full_symbol
           (<>.either <code>.local
                      full_symbol))
         (<>#each bit#encoded <code>.bit)
         (<>#each nat#encoded <code>.nat)
         (<>#each int#encoded <code>.int)
         (<>#each rev#encoded <code>.rev)
         (<>#each frac#encoded <code>.frac)
         )))

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

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

(with_template [<name> <simple> <complex>]
  [(def .public <name>
     (syntax (_ [name (<>.or (<>.and (..part true) (..part false))
                             (..part false))])
       (when name
         {.#Left [simple complex]}
         (in (list (<complex> [(text.interposed "" simple)
                               (text.interposed "" complex)])))
         
         {.#Right simple}
         (in (list (|> simple (text.interposed "") <simple>))))))]

  [symbol code.local code.symbol]
  )

(type Environment
  (Dictionary Text Code))

(def (applied env template)
  (-> Environment Code Code)
  (when template
    [_ {.#Symbol "" name}]
    (when (dictionary.value name env)
      {.#Some substitute}
      substitute

      {.#None}
      template)

    (^.with_template [<tag>]
      [[meta {<tag> elems}]
       [meta {<tag> (list#each (applied env) elems)}]])
    ([.#Form]
     [.#Variant]
     [.#Tuple])

    _
    template))

(type Local
  (Record
   [#name Text
    #parameters (List Text)
    #template (List Code)]))

(exception.def .public (irregular_arguments [expected actual])
  (Exception [Nat Nat])
  (exception.report
   (list ["Expected" (of nat.decimal encoded expected)]
         ["Actual" (of nat.decimal encoded actual)])))

(def (macro (open "_[0]"))
  (-> Local Macro)
  (//.macro
    (function (_ inputs compiler)
      (.let [parameters_amount (list.size _#parameters)
             inputs_amount (list.size inputs)]
        (if (nat.= parameters_amount inputs_amount)
          (.let [environment (is Environment
                                 (|> (list.zipped_2 _#parameters inputs)
                                     (dictionary.of_list text.hash)))]
            {.#Right [compiler (list#each (..applied environment) _#template)]})
          (exception.except ..irregular_arguments [parameters_amount inputs_amount]))))))

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

... TODO: Get rid of this (and any local definitions it depends on) once the bootstrapping compiler is gone.
(def .public let
  (syntax (_ [locals (<code>.tuple (<>.some ..local))
              body <code>.any])
    (do meta.monad
      [here_name meta.current_module_name
       expression? (is (Meta Bit)
                       (function (_ lux)
                         {try.#Success [lux (when (the .#expected lux)
                                              {.#None}
                                              false

                                              {.#Some _}
                                              true)]}))
       g!pop (local.push (list#each (function (_ local)
                                      [[here_name (the #name local)]
                                       (..macro local)])
                                    locals))]
      (if expression?
        (//.with_symbols [g!body]
          (in (list (` (.let [(, g!body) (, body)]
                         (exec
                           (, g!pop)
                           (, g!body)))))))
        (in (list body
                  g!pop))))))