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

(type: #export ID
  Nat)

(type: #export Category
  #Anonymous
  (#Definition Text)
  (#Analyser Text)
  (#Synthesizer Text)
  (#Generator Text)
  (#Directive Text))

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

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

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

  (def: #export artifacts
    (-> Registry (Row Artifact))
    (|>> :representation (get@ #artifacts)))

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

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

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

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

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

  (def: #export (remember name registry)
    (-> Text Registry (Maybe ID))
    (|> (:representation registry)
        (get@ #resolver)
        (dictionary.get name)))

  (def: #export 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]))))
          artifacts (: (Writer (Row Category))
                       (binary.row/64 category))]
      (|>> :representation
           (get@ #artifacts)
           (row\map (get@ #category))
           artifacts)))

  (exception: #export (invalid_category {tag Nat})
    (exception.report
     ["Tag" (%.nat tag)]))

  (def: #export parser
    (Parser Registry)
    (let [category (: (Parser Category)
                      (do {! <>.monad}
                        [tag <b>.nat]
                        (case tag
                          0 (\ ! map (|>> #Anonymous) <b>.any)
                          1 (\ ! map (|>> #Definition) <b>.text)
                          2 (\ ! map (|>> #Analyser) <b>.text)
                          3 (\ ! map (|>> #Synthesizer) <b>.text)
                          4 (\ ! map (|>> #Generator) <b>.text)
                          5 (\ ! map (|>> #Directive) <b>.text)
                          _ (<>.fail (exception.construct ..invalid_category [tag])))))]
      (|> (<b>.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])
                                         )))
                                    ..empty)))))
  )