aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/macro/local.lux
blob: d1e9c9475d8cac6b0b99b796e339ebcc67e30046 (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
(.require
 [library
  [lux (.except with let)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["<>" parser]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only Exception)]]
   [data
    ["[0]" product]
    ["[0]" text]
    [collection
     ["[0]" list (.use "[1]#[0]" functor)
      ["[0]" property]]]]
   ["[0]" meta (.only)
    ["[0]" code (.only)
     ["<[1]>" \\parser]]]]]
 ["[0]" // (.only)
  [syntax (.only syntax)]])

(exception.def .public (unknown_module module)
  (Exception Text)
  (exception.report
   (list ["Module" (text.format module)])))

(with_template [<name>]
  [(exception.def .public (<name> [module definition])
     (Exception [Text Text])
     (exception.report
      (list ["Module" (text.format module)]
            ["Definition" (text.format definition)])))]

  [cannot_shadow_definition]
  [unknown_definition]
  )

(def (with_module name body)
  (All (_ a) (-> Text (-> Module (Try [Module a])) (Meta a)))
  (function (_ compiler)
    (when (|> compiler (the .#modules) (property.value name))
      {.#Some module}
      (when (body module)
        {try.#Success [module' output]}
        {try.#Success [(revised .#modules (property.has name module') compiler)
                       output]}
        
        {try.#Failure error}
        {try.#Failure error})

      {.#None}
      (exception.except ..unknown_module [name]))))

(def (push_one [name macro])
  (-> [Symbol Macro] (Meta Any))
  (do meta.monad
    [[module_name definition_name] (meta.normal name)
     .let [definition (is Global {.#Definition [false .Macro macro]})
           add_macro! (is (-> (property.List Global) (property.List Global))
                          (property.has definition_name definition))]]
    (..with_module module_name
      (function (_ module)
        (when (|> module (the .#definitions) (property.value definition_name))
          {.#None}
          {try.#Success [(revised .#definitions add_macro! module)
                         []]}
          
          {.#Some _}
          (exception.except ..cannot_shadow_definition [module_name definition_name]))))))

(def (pop_one name)
  (-> Symbol (Meta Any))
  (do meta.monad
    [[module_name definition_name] (meta.normal name)
     .let [lacks_macro! (is (-> (property.List Global) (property.List Global))
                            (property.lacks definition_name))]]
    (..with_module module_name
      (function (_ module)
        (when (|> module (the .#definitions) (property.value definition_name))
          {.#Some _}
          {try.#Success [(revised .#definitions lacks_macro! module)
                         []]}

          {.#None}
          (exception.except ..unknown_definition [module_name definition_name]))))))

(def (pop_all macros self)
  (-> (List Symbol) Symbol Macro)
  (//.macro
    (function (_ _)
      (do [! meta.monad]
        [_ (monad.each ! ..pop_one macros)
         _ (..pop_one self)
         compiler meta.compiler_state]
        (in (when (the .#expected compiler)
              {.#Some _}
              (list (' []))
              
              {.#None}
              (list)))))))

(def .public (push macros)
  (-> (List [Symbol Macro]) (Meta Code))
  (do meta.monad
    [_ (monad.each meta.monad ..push_one macros)
     seed meta.seed
     g!pop (//.symbol "pop")
     _ (.let [g!pop (is Symbol
                        ["" (code.format g!pop)])]
         (..push_one [g!pop (..pop_all (list#each product.left macros) g!pop)]))]
    (in (` ((, g!pop))))))

(def .public (with macros expression? body)
  (-> (List [Symbol Macro]) Bit Code (Meta (List Code)))
  (do [! meta.monad]
    [g!pop (..push macros)]
    (.if expression?
      (//.with_symbols [g!body]
        (in (list (` (.let [(, g!body) (, body)]
                       (exec
                         (, g!pop)
                         (, g!body)))))))
      (in (list body
                g!pop)))))

(def .public let
  (syntax (_ [locals (<code>.tuple (<>.some (<>.and <code>.local <code>.any)))
              body <code>.any])
    (do [! meta.monad]
      [here_name meta.current_module_name
       locals (monad.each ! (function (_ [name value])
                              (|> value
                                  (meta.eval .Macro)
                                  (at ! each (|>> (as .Macro)
                                                  [[here_name name]]))))
                          locals)
       expression? (is (Meta Bit)
                       (function (_ lux)
                         {try.#Success [lux (when (the .#expected lux)
                                              {.#None}
                                              false

                                              {.#Some _}
                                              true)]}))]
      (..with locals expression? body))))