aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/meta/archive/artifact.lux
blob: e4cc633defd161aa6b27184ab268583881f01562 (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
(.module:
  [library
   [lux #*
    [abstract
     [monad (#+ do)]]
    [control
     [pipe (#+ case>)]
     ["." exception (#+ exception:)]
     ["<>" parser
      ["<.>" binary (#+ Parser)]]]
    [data
     ["." product]
     ["." text
      ["%" format (#+ format)]]
     [collection
      ["." list]
      ["." row (#+ Row) ("#\." functor fold)]
      ["." dictionary (#+ Dictionary)]]
     [format
      ["." binary (#+ Writer)]]]
    [type
     abstract]]])

(type: .public ID
  Nat)

(type: .public Category
  #Anonymous
  (#Definition Text)
  (#Analyser Text)
  (#Synthesizer Text)
  (#Generator Text)
  (#Directive Text)
  (#Custom Text))

(type: .public Artifact
  {#id ID
   #category Category})

(abstract: .public Registry
  {}
  
  {#artifacts (Row Artifact)
   #resolver (Dictionary Text ID)}

  (def: .public empty
    Registry
    (:abstraction {#artifacts row.empty
                   #resolver (dictionary.empty text.hash)}))

  (def: .public artifacts
    (-> Registry (Row Artifact))
    (|>> :representation (value@ #artifacts)))

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

  (def: .public (resource registry)
    (-> Registry [ID Registry])
    (let [id (..next registry)]
      [id
       (|> registry
           :representation
           (revised@ #artifacts (row.suffix {#id id
                                             #category #Anonymous}))
           :abstraction)]))

  (template [<tag> <create> <fetch>]
    [(def: .public (<create> name registry)
       (-> Text Registry [ID Registry])
       (let [id (..next registry)]
         [id
          (|> registry
              :representation
              (revised@ #artifacts (row.suffix {#id id
                                                #category (<tag> name)}))
              (revised@ #resolver (dictionary.has name id))
              :abstraction)]))

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

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

  (def: .public (remember 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 #Anonymous binary.any]
                           [1 #Definition binary.text]
                           [2 #Analyser binary.text]
                           [3 #Synthesizer binary.text]
                           [4 #Generator binary.text] 
                           [5 #Directive binary.text]
                           [6 #Custom binary.text]))))
          artifacts (: (Writer (Row Category))
                       (binary.row/64 category))]
      (|>> :representation
           (value@ #artifacts)
           (row\map (value@ #category))
           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> (\ ! map (|>> <tag>) <parser>)])
                          ([0 #Anonymous <binary>.any]
                           [1 #Definition <binary>.text]
                           [2 #Analyser <binary>.text]
                           [3 #Synthesizer <binary>.text]
                           [4 #Generator <binary>.text]
                           [5 #Directive <binary>.text]
                           [6 #Custom <binary>.text])
                          
                          _ (<>.failure (exception.error ..invalid_category [tag])))))]
      (|> (<binary>.row/64 category)
          (\ <>.monad map (row\fold (function (_ artifact registry)
                                      (product.right
                                       (case artifact
                                         #Anonymous
                                         (..resource registry)

                                         (^template [<tag> <create>]
                                           [(<tag> name)
                                            (<create> name registry)])
                                         ([#Definition ..definition]
                                          [#Analyser ..analyser]
                                          [#Synthesizer ..synthesizer]
                                          [#Generator ..generator]
                                          [#Directive ..directive]
                                          [#Custom ..custom])
                                         )))
                                    ..empty)))))
  )