aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/macro/context.lux
blob: 9db9ef9783a9763562c58f62df0420d69435cc1c (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
(.require
 [library
  [lux (.except def global)
   [abstract
    [monad (.only do)]]
   [control
    ["?" parser]
    ["[0]" exception (.only Exception)]
    ["[0]" maybe]
    [function
     [predicate (.only Predicate)]]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence monoid)]
    [collection
     ["[0]" list (.only)
      ["[0]" property]]]]
   ["[0]" meta (.only)
    ["[0]" symbol (.use "[1]#[0]" codec)]
    ["[0]" code (.only)
     ["?[1]" \\parser]]]]]
 ["[0]" // (.only)
  [syntax (.only syntax)]])

(type .public Stack
  List)

(exception.def (no_definition it)
  (Exception Symbol)
  (exception.report
   (list ["Definition" (symbol#encoded it)])))

(.def (global it)
  (-> Symbol (Meta Any))
  (do meta.monad
    [.let [[@ expected_name] it]
     defs (meta.definitions @)]
    (when (list.one (function (_ [actual_name [exported? type value]])
                      (if (text#= expected_name actual_name)
                        {.#Some value}
                        {.#None}))
                    defs)
      {.#Some it}
      (in it)
      
      {.#None}
      (meta.failure (exception.error ..no_definition [it])))))

(exception.def .public no_active_context)

(.def .public (peek' _ context)
  (All (_ a) (-> (Stack a) Symbol (Meta a)))
  (do meta.monad
    [stack (..global context)]
    (when (|> stack
              (as (Stack Any))
              list.head)
      {.#Some top}
      (in (as_expected top))
      
      {.#None}
      (meta.failure (exception.error ..no_active_context [])))))

(.def .public peek
  (syntax (_ [g!it (at ?.monad each code.symbol ?code.global)])
    (in (list (` (..peek' (, g!it) (.symbol (, g!it))))))))

(exception.def .public no_example)

(.def .public (search' ? _ context)
  (All (_ a) (-> (Predicate a) (Stack a) Symbol (Meta a)))
  (do meta.monad
    [stack (..global context)]
    (when (|> stack
              (as (Stack Any))
              (list.example (as (Predicate Any) ?)))
      {.#Some it}
      (in (as_expected it))
      
      {.#None}
      (meta.failure (exception.error ..no_example [])))))

(.def .public search
  (syntax (_ [g!? ?code.any
              g!context (at ?.monad each code.symbol ?code.global)])
    (in (list (` (..search' (, g!?) (, g!context) (.symbol (, g!context))))))))

(.def (alter on_definition [@ context])
  (-> (-> Definition Definition) Symbol (Meta Any))
  (function (_ lux)
    (let [on_global (is (-> Global Global)
                        (function (_ it)
                          (when it
                            {.#Definition it}
                            {.#Definition (on_definition it)}
                            
                            _
                            it)))
          on_globals (is (-> (property.List Global) (property.List Global))
                         (property.revised context on_global))
          on_module (is (-> Module Module)
                        (revised .#definitions on_globals))]
      {.#Right [(revised .#modules (property.revised @ on_module) lux)
                []]})))

(.def .public (push' top _)
  (All (_ a) (-> a (Stack a) Symbol (Meta Any)))
  (alter (function (_ [exported? type stack])
           (|> stack
               (as (Stack Any))
               {.#Item top}
               (is (Stack Any))
               [exported? type]))))

(.def .public push
  (syntax (_ [g!it ?code.any
              g!context (at ?.monad each code.symbol ?code.global)])
    (in (list (` (..push' (, g!it) (, g!context) (.symbol (, g!context))))))))

(.def .public pop''
  (-> Symbol (Meta Any))
  (alter (function (_ [exported? type value])
           [exported? type (let [value (as (Stack Any) value)]
                             (maybe.else value (list.tail value)))])))

(.def .public pop'
  (syntax (_ [expression? ?code.bit
              context ?code.global])
    (do meta.monad
      [_ (..pop'' context)]
      (in (if expression?
            (list (' []))
            (list))))))

(.def .public pop
  (syntax (_ [g!context (at ?.monad each code.symbol ?code.global)])
    (in (list (` (..pop'' (.symbol (, g!context))))))))

(.def .public def
  (syntax (_ [.let [! ?.monad
                    ?local (at ! each code.local ?code.local)]
              [$ g!expression g!declaration] (?code.tuple (all ?.and ?code.local ?local ?local))
              context_type ?code.any])
    (do [! meta.monad]
      [@ meta.current_module_name
       .let [g!context (code.symbol [@ $])]]
      (//.with_symbols [g!it g!body g!_]
        (in (list (` (.def (, (code.local $))
                       (..Stack (, context_type))
                       (list)))
                  (` (.def ((, g!expression) (, g!it) (, g!body))
                       (-> (, context_type) Code (Meta Code))
                       (do meta.monad
                         [(, g!_) (..push (, g!it) (, g!context))]
                         ((,' in) (` (let [((,' ,') (, g!body)) ((,' ,) (, g!body))
                                           ((,' ,') (, g!_)) (..pop' #1 (, g!context))]
                                       ((,' ,') (, g!body))))))))
                  (` (.def ((, g!declaration) (, g!it) (, g!body))
                       (-> (, context_type) Code (Meta (List Code)))
                       (do meta.monad
                         [(, g!_) (..push (, g!it) (, g!context))]
                         ((,' in) (list (, g!body)
                                        (` (..pop' #0 (, g!context))))))))
                  ))))))