/
exceptions.cljc
62 lines (40 loc) · 1.26 KB
/
exceptions.cljc
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
(ns fif.stdlib.exceptions
(:require
[fif.stack-machine :as stack]
[fif.stack-machine.error-handling :as error-handling]
[fif.stack-machine.mode :as mode]))
(def arg-raise-exception-token 'raise-exception!)
(def arg-on-exception-token 'on-exception)
(def exception-mode-flag :exception-mode)
(defn enter-exception-mode [sm state]
(mode/enter-mode sm exception-mode-flag state))
(defn exit-exception-mode [sm]
(mode/exit-mode sm))
(defmulti exception-mode mode/mode-dispatch-fn)
(defmethod exception-mode
{:op ::raise :op-state ::ignore-code}
[sm]
(let [arg (-> sm stack/get-code first)]
(cond
(= arg arg-on-exception-token)
(-> sm
exit-exception-mode
stack/dequeue-code)
:else
stack/dequeue-code)))
(defn raise-exception [sm]
(-> sm
(enter-exception-mode {:op ::raise :op-state ::ignore-code})))
(defn raise-op [sm]
(-> sm
raise-exception
stack/dequeue-code))
(defn on-exception-op [sm]
(-> sm
stack/dequeue-code
stack/dequeue-code))
(defn import-stdlib-exceptions [sm]
(-> sm
(stack/set-mode exception-mode-flag exception-mode)
(stack/set-word arg-raise-exception-token raise-op)
(stack/set-word arg-on-exception-token on-exception-op)))