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

(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>)))))]

  [symbol code.local code.symbol]
  )

(type: Environment
  (Dictionary Text Code))

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

      {.#None}
      template)

    (^.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: .public (irregular_arguments [expected Nat
                                          actual Nat])
  (exception.report
   "Expected" (# nat.decimal encoded expected)
   "Actual" (# nat.decimal encoded actual)))

(def: (macro (open "_[0]"))
  (-> 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 (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])))

(syntax: .public (let [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 (case (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)))))