aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/meta/archive/registry.lux
blob: 0be7c5800d83323b8f2990ec5177f93fb594694b (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
(.using
 [library
  [lux (.full)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" pipe]
    ["[0]" maybe ("[1]#[0]" functor)]
    ["[0]" exception (.only exception:)]
    ["<>" parser
     ["<[0]>" binary (.only Parser)]]]
   [data
    ["[0]" product]
    ["[0]" text
     ["%" format (.only format)]]
    [collection
     [set (.only Set)]
     ["[0]" list]
     ["[0]" sequence (.only Sequence) ("[1]#[0]" functor mix)]
     ["[0]" dictionary (.only Dictionary)]]
    [format
     ["[0]" binary (.only Writer)]]]
   [macro
    ["^" pattern]]
   [type
    [primitive (.full)]]]]
 ["[0]" // "_"
  ["[0]" unit]
  ["[1]" artifact (.only Artifact ID)
   ["[2][0]" category (.only Category)]]])

(primitive: .public Registry
  (Record
   [#artifacts (Sequence [Artifact (Set unit.ID)])
    #resolver (Dictionary Text [ID (Maybe //category.Definition)])])

  (def: .public empty
    Registry
    (abstraction [#artifacts sequence.empty
                  #resolver (dictionary.empty text.hash)]))

  (def: .public artifacts
    (-> Registry (Sequence [Artifact (Set unit.ID)]))
    (|>> representation (the #artifacts)))

  (def: next
    (-> Registry ID)
    (|>> ..artifacts sequence.size))

  (def: .public (resource mandatory? dependencies registry)
    (-> Bit (Set unit.ID) Registry [ID Registry])
    (let [id (..next registry)]
      [id
       (|> registry
           representation
           (revised #artifacts (sequence.suffix [[//.#id id
                                                  //.#category {//category.#Anonymous}
                                                  //.#mandatory? mandatory?]
                                                 dependencies]))
           abstraction)]))

  (template [<tag> <create> <fetch> <type> <name> <+resolver>]
    [(def: .public (<create> it mandatory? dependencies registry)
       (-> <type> Bit (Set unit.ID) Registry [ID Registry])
       (let [id (..next registry)]
         [id
          (|> registry
              representation
              (revised #artifacts (sequence.suffix [[//.#id id
                                                     //.#category {<tag> it}
                                                     //.#mandatory? mandatory?]
                                                    dependencies]))
              (revised #resolver (dictionary.has (<name> it) [id (is (Maybe //category.Definition) <+resolver>)]))
              abstraction)]))

     (def: .public (<fetch> registry)
       (-> Registry (List <type>))
       (|> registry
           representation
           (the #artifacts)
           sequence.list
           (list.all (|>> product.left
                          (the //.#category)
                          (pipe.case
                            {<tag> it} {.#Some it}
                            _ {.#None})))))]

    [//category.#Definition definition definitions //category.Definition
     product.left {.#Some it}]
    [//category.#Analyser analyser analysers Text |> {.#None}]
    [//category.#Synthesizer synthesizer synthesizers Text |> {.#None}]
    [//category.#Generator generator generators Text |> {.#None}]
    [//category.#Directive directive directives Text |> {.#None}]
    [//category.#Custom custom customs Text |> {.#None}]
    )

  (def: .public (find_definition name registry)
    (-> Text Registry (Maybe [ID (Maybe //category.Definition)]))
    (|> (representation registry)
        (the #resolver)
        (dictionary.value name)))

  (def: .public (id name registry)
    (-> Text Registry (Maybe ID))
    (maybe#each product.left (find_definition name registry)))

  (def: .public writer
    (Writer Registry)
    (let [definition (is (Writer //category.Definition)
                         (all binary.and
                              binary.text
                              (binary.maybe
                               (all binary.and
                                    binary.nat
                                    binary.nat
                                    binary.nat
                                    ))
                              ))
          category (is (Writer Category)
                       (function (_ value)
                         (case value
                           (^.template [<nat> <tag> <writer>]
                             [{<tag> value}
                              ((binary.and binary.nat <writer>) [<nat> value])])
                           ([0 //category.#Anonymous binary.any]
                            [1 //category.#Definition definition]
                            [2 //category.#Analyser binary.text]
                            [3 //category.#Synthesizer binary.text]
                            [4 //category.#Generator binary.text] 
                            [5 //category.#Directive binary.text]
                            [6 //category.#Custom binary.text]))))
          mandatory? binary.bit
          dependency (is (Writer unit.ID)
                         (binary.and binary.nat binary.nat))
          dependencies (is (Writer (Set unit.ID))
                           (binary.set dependency))
          artifacts (is (Writer (Sequence [Category Bit (Set unit.ID)]))
                        (binary.sequence_64 (all binary.and category mandatory? dependencies)))]
      (|>> representation
           (the #artifacts)
           (sequence#each (function (_ [it dependencies])
                            [(the //.#category it)
                             (the //.#mandatory? it)
                             dependencies]))
           artifacts)))

  (exception: .public (invalid_category [tag Nat])
    (exception.report
     "Tag" (%.nat tag)))

  (def: .public parser
    (Parser Registry)
    (let [definition (is (Parser //category.Definition)
                         (all <>.and
                              <binary>.text
                              (<binary>.maybe
                               (all <>.and
                                    <binary>.nat
                                    <binary>.nat
                                    <binary>.nat
                                    ))
                              ))
          category (is (Parser Category)
                       (do [! <>.monad]
                         [tag <binary>.nat]
                         (case tag
                           (^.template [<nat> <tag> <parser>]
                             [<nat>
                              (# ! each (|>> {<tag>}) <parser>)])
                           ([0 //category.#Anonymous <binary>.any]
                            [1 //category.#Definition definition]
                            [2 //category.#Analyser <binary>.text]
                            [3 //category.#Synthesizer <binary>.text]
                            [4 //category.#Generator <binary>.text]
                            [5 //category.#Directive <binary>.text]
                            [6 //category.#Custom <binary>.text])
                           
                           _ (<>.failure (exception.error ..invalid_category [tag])))))
          mandatory? <binary>.bit
          dependency (is (Parser unit.ID)
                         (<>.and <binary>.nat <binary>.nat))
          dependencies (is (Parser (Set unit.ID))
                           (<binary>.set unit.hash dependency))]
      (|> (<binary>.sequence_64 (all <>.and category mandatory? dependencies))
          (# <>.monad each (sequence#mix (function (_ [category mandatory? dependencies] registry)
                                           (product.right
                                            (case category
                                              {//category.#Anonymous}
                                              (..resource mandatory? dependencies registry)

                                              (^.template [<tag> <create>]
                                                [{<tag> name}
                                                 (<create> name mandatory? dependencies registry)])
                                              ([//category.#Definition ..definition]
                                               [//category.#Analyser ..analyser]
                                               [//category.#Synthesizer ..synthesizer]
                                               [//category.#Generator ..generator]
                                               [//category.#Directive ..directive]
                                               [//category.#Custom ..custom])
                                              )))
                                         ..empty)))))
  )