aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/extension.lux
blob: e8121b9b6330166872c4d7155b305ef5eeb5df5a (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
(.module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:])
       (data ["e" error]
             [text]
             (coll [dict #+ Dict]))
       [macro])
  [//]
  (// ["la" analysis]
      ["ls" synthesis]))

(do-template [<name>]
  [(exception: #export (<name> {message Text})
     message)]

  [Unknown-Analysis]
  [Unknown-Synthesis]
  [Unknown-Translation]
  [Unknown-Statement]

  [Cannot-Define-Analysis-More-Than-Once]
  [Cannot-Define-Synthesis-More-Than-Once]
  [Cannot-Define-Translation-More-Than-Once]
  [Cannot-Define-Statement-More-Than-Once]
  )

(type: #export Analysis
  (-> (-> Code (Meta Code))
      (-> Type Code (Meta Top))
      (List Code) (Meta Code)))

(type: #export Synthesis
  (-> (-> la.Analysis ls.Synthesis) (List Code) Code))

(type: #export Syntheses (Dict Text Synthesis))

(type: #export Translation
  (-> (List Code) (Meta Code)))

(type: #export Statement
  (-> (List Code) (Meta Unit)))

(type: #export Extensions
  {#analysis (Dict Text Analysis)
   #synthesis Syntheses
   #translation (Dict Text Translation)
   #statement (Dict Text Statement)})

(def: #export fresh
  Extensions
  {#analysis (dict.new text.Hash<Text>)
   #synthesis (dict.new text.Hash<Text>)
   #translation (dict.new text.Hash<Text>)
   #statement (dict.new text.Hash<Text>)})

(def: get
  (Meta Extensions)
  (function (_ compiler)
    (#e.Success [compiler
                 (|> compiler (get@ #.extensions) (:! Extensions))])))

(def: (set extensions)
  (-> Extensions (Meta Unit))
  (function (_ compiler)
    (#e.Success [(set@ #.extensions (:! Void extensions) compiler)
                 []])))

(do-template [<name> <type> <category> <exception>]
  [(def: #export (<name> name)
     (-> Text (Meta <type>))
     (do macro.Monad<Meta>
       [extensions ..get]
       (case (dict.get name (get@ <category> extensions))
         (#.Some extension)
         (wrap extension)

         #.None
         (//.throw <exception> name))))]

  [find-analysis    Analysis    #analysis    Unknown-Analysis]
  [find-synthesis   Synthesis   #synthesis   Unknown-Synthesis]
  [find-translation Translation #translation Unknown-Translation]
  [find-statement   Statement   #statement   Unknown-Statement]
  )

(do-template [<no> <all> <type> <category> <empty>]
  [(def: #export <no>
     <type>
     <empty>)

   (def: #export <all>
     (Meta <type>)
     (|> ..get
         (:: macro.Monad<Meta> map (get@ <category>))))]

  [no-syntheses all-syntheses Syntheses #synthesis (dict.new text.Hash<Text>)]
  )

(do-template [<name> <type> <category> <exception>]
  [(def: #export (<name> name extension)
     (-> Text <type> (Meta Unit))
     (do macro.Monad<Meta>
       [extensions ..get
        _ (//.assert <exception> name
                     (not (dict.contains? name (get@ <category> extensions))))
        _ (..set (update@ <category> (dict.put name extension) extensions))]
       (wrap [])))]

  [install-analysis    Analysis    #analysis    Cannot-Define-Analysis-More-Than-Once]
  [install-synthesis   Synthesis   #synthesis   Cannot-Define-Synthesis-More-Than-Once]
  [install-translation Translation #translation Cannot-Define-Translation-More-Than-Once]
  [install-statement   Statement   #statement   Cannot-Define-Statement-More-Than-Once]
  )