aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/compiler/default/phase/extension.lux
blob: ba3180500f63a593cc2d24351eaaf89ac00e767c (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
(.module:
  [lux #*
   [control
    [monad (#+ do)]
    ["ex" exception (#+ exception:)]]
   [data
    ["." error (#+ Error)]
    ["." text ("text/." Order<Text>)
     format]
    [collection
     ["." list ("list/." Functor<List>)]
     ["." dictionary (#+ Dictionary)]]]
   ["." function]]
  ["." //])

(type: #export (Extension i)
  [Text (List i)])

(with-expansions [<Bundle> (as-is (Dictionary Text (Handler s i o)))]
  (type: #export (Handler s i o)
    (-> Text
        (//.Phase [<Bundle> s] i o)
        (//.Phase [<Bundle> s] (List i) o)))

  (type: #export (Bundle s i o)
    <Bundle>))

(type: #export (State s i o)
  {#bundle (Bundle s i o)
   #state s})

(type: #export (Operation s i o v)
  (//.Operation (State s i o) v))

(type: #export (Phase s i o)
  (//.Phase (State s i o) i o))

(do-template [<name>]
  [(exception: #export (<name> {name Text})
     (ex.report ["Extension" (%t name)]))]

  [cannot-overwrite]
  [invalid-syntax]
  )

(exception: #export [s i o] (unknown {where Text} {name Text} {bundle (Bundle s i o)})
  (ex.report ["Where" (%t where)]
             ["Extension" (%t name)]
             ["Available" (|> bundle
                              dictionary.keys
                              (list.sort text/<)
                              (list/map (|>> %t (format text.new-line text.tab)))
                              (text.join-with ""))]))

(exception: #export (incorrect-arity {name Text} {arity Nat} {args Nat})
  (ex.report ["Extension" (%t name)]
             ["Expected" (%n arity)]
             ["Actual" (%n args)]))

(def: #export (install name handler)
  (All [s i o]
    (-> Text (Handler s i o) (Operation s i o Any)))
  (function (_ [bundle state])
    (case (dictionary.get name bundle)
      #.None
      (#error.Success [[(dictionary.put name handler bundle) state]
                       []])

      _
      (ex.throw cannot-overwrite name))))

(def: #export (apply where phase [name parameters])
  (All [s i o]
    (-> Text (Phase s i o) (Extension i) (Operation s i o o)))
  (function (_ (^@ stateE [bundle state]))
    (case (dictionary.get name bundle)
      (#.Some handler)
      (((handler name phase) parameters)
       stateE)

      #.None
      (ex.throw unknown [where name bundle]))))

(def: #export (localized get set transform)
  (All [s s' i o v]
    (-> (-> s s') (-> s' s s) (-> s' s')
        (-> (Operation s i o v) (Operation s i o v))))
  (function (_ operation)
    (function (_ [bundle state])
      (let [old (get state)]
        (case (operation [bundle (set (transform old) state)])
          (#error.Success [[bundle' state'] output])
          (#error.Success [[bundle' (set old state')] output])

          (#error.Error error)
          (#error.Error error))))))

(def: #export (temporary transform)
  (All [s i o v]
    (-> (-> s s)
        (-> (Operation s i o v) (Operation s i o v))))
  (function (_ operation)
    (function (_ [bundle state])
      (case (operation [bundle (transform state)])
        (#error.Success [[bundle' state'] output])
        (#error.Success [[bundle' state] output])

        (#error.Error error)
        (#error.Error error)))))

(def: #export (with-state state)
  (All [s i o v]
    (-> s (-> (Operation s i o v) (Operation s i o v))))
  (..temporary (function.constant state)))

(def: #export (read get)
  (All [s i o v]
    (-> (-> s v) (Operation s i o v)))
  (function (_ [bundle state])
    (#error.Success [[bundle state] (get state)])))

(def: #export (update transform)
  (All [s i o]
    (-> (-> s s) (Operation s i o Any)))
  (function (_ [bundle state])
    (#error.Success [[bundle (transform state)] []])))

(def: #export (lift action)
  (All [s i o v]
    (-> (//.Operation s v)
        (//.Operation [(Bundle s i o) s] v)))
  (function (_ [bundle state])
    (case (action state)
      (#error.Success [state' output])
      (#error.Success [[bundle state'] output])

      (#error.Error error)
      (#error.Error error))))