aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/meta/archive/registry.lux
blob: 7af5c105b0fabdc8f97386736ac2060817a41ce5 (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
(.using
 [library
  [lux "*"
   [abstract
    [monad {"+" do}]]
   [control
    [pipe {"+" case>}]
    ["[0]" exception {"+" exception:}]
    ["<>" parser
     ["<[0]>" binary {"+" Parser}]]]
   [data
    ["[0]" product]
    ["[0]" text
     ["%" format {"+" format}]]
    [collection
     [set {"+" Set}]
     ["[0]" list]
     ["[0]" sequence {"+" Sequence} ("[1]#[0]" functor mix)]
     ["[0]" dictionary {"+" Dictionary}]]
    [format
     ["[0]" binary {"+" Writer}]]]
   [type
    abstract]]]
 ["[0]" // "_"
  ["[0]" unit]
  ["[1]" artifact {"+" Artifact ID}
   ["[2][0]" category {"+" Category}]]])

(abstract: .public Registry
  (Record
   [#artifacts (Sequence [Artifact (Set unit.ID)])
    #resolver (Dictionary Text ID)])

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

  (def: .public artifacts
    (-> Registry (Sequence [Artifact (Set unit.ID)]))
    (|>> :representation (value@ #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>]
    [(def: .public (<create> name mandatory? dependencies registry)
       (-> Text Bit (Set unit.ID) Registry [ID Registry])
       (let [id (..next registry)]
         [id
          (|> registry
              :representation
              (revised@ #artifacts (sequence.suffix [[//.#id id
                                                      //.#category {<tag> name}
                                                      //.#mandatory? mandatory?]
                                                     dependencies]))
              (revised@ #resolver (dictionary.has name id))
              :abstraction)]))

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

    [//category.#Definition definition definitions]
    [//category.#Analyser analyser analysers]
    [//category.#Synthesizer synthesizer synthesizers]
    [//category.#Generator generator generators]
    [//category.#Directive directive directives]
    [//category.#Custom custom customs]
    )

  (def: .public (id name registry)
    (-> Text Registry (Maybe ID))
    (|> (:representation registry)
        (value@ #resolver)
        (dictionary.value name)))

  (def: .public writer
    (Writer Registry)
    (let [category (: (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 binary.text]
                           [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 (: (Writer unit.ID)
                        (binary.and binary.nat binary.nat))
          dependencies (: (Writer (Set unit.ID))
                          (binary.set dependency))
          artifacts (: (Writer (Sequence [Category Bit (Set unit.ID)]))
                       (binary.sequence/64 ($_ binary.and category mandatory? dependencies)))]
      (|>> :representation
           (value@ #artifacts)
           (sequence#each (function (_ [it dependencies])
                            [(value@ //.#category it)
                             (value@ //.#mandatory? it)
                             dependencies]))
           artifacts)))

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

  (def: .public parser
    (Parser Registry)
    (let [category (: (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 <binary>.text]
                           [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 (: (Parser unit.ID)
                        (<>.and <binary>.nat <binary>.nat))
          dependencies (: (Parser (Set unit.ID))
                          (<binary>.set unit.hash dependency))]
      (|> (<binary>.sequence/64 ($_ <>.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)))))
  )