-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
instructions.lisp
171 lines (133 loc) · 5.06 KB
/
instructions.lisp
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
166
167
168
169
170
171
(in-package #:org.shirakumo.fraf.speechless)
(defun print-instruction-type (instruction)
(let ((type (string (type-of instruction))))
(if (<= (length type) 6)
(format T "~6a " type)
(format T "~a " (subseq type 0 6)))))
(defclass instruction ()
((index :initarg :index :accessor index)
(label :initarg :label :accessor label)))
(defmethod initialize-instance :after ((instruction instruction) &key)
(unless (slot-boundp instruction 'label)
(setf (label instruction) instruction)))
(defmethod print-object ((instruction instruction) stream)
(print-unreadable-object (instruction stream)
(format stream "~3d ~s~@[ ~a~]"
(index instruction)
(type-of instruction)
(unless (eq instruction (label instruction))
(label instruction)))))
(defmethod disassemble ((instruction instruction))
(print-instruction-type instruction))
(defmethod disassemble :around ((instruction instruction))
(call-next-method)
(when (label instruction)
(format T "~80t[~a]" (label instruction))))
(defclass noop (instruction)
())
(defclass source (instruction)
((name :initarg :name :accessor name)))
(defmethod print-object ((source source) stream)
(print-unreadable-object (source stream)
(format stream "~3d ~s ~s"
(index source)
(type-of source)
(name source))))
(defmethod disassemble :after ((instruction source))
(format T "~a" (name instruction)))
(defclass jump (instruction)
((target :initarg :target :initform (error "TARGET required.") :accessor target)))
(defmethod print-object ((jump jump) stream)
(print-unreadable-object (jump stream)
(format stream "~3d ~s~@[ ~a~] -> ~a"
(index jump)
(type-of jump)
(unless (eq jump (label jump))
(label jump))
(target jump))))
(defmethod disassemble ((instruction jump))
(print-instruction-type instruction)
(format T "~a" (target instruction)))
(defclass conditional (instruction)
((clauses :initarg :clauses :accessor clauses)))
(defmethod print-object ((conditional conditional) stream)
(print-unreadable-object (conditional stream)
(format stream "~3d ~s~@[ ~a~] -> ~{~a~^, ~}"
(index conditional)
(type-of conditional)
(unless (eq conditional (label conditional))
(label conditional))
(mapcar #'cdr (clauses conditional)))))
(defmethod disassemble ((instruction conditional))
(print-instruction-type instruction)
(loop for (func . target) in (clauses instruction)
do (format T "~& ~2d" target)))
(defclass dispatch (instruction)
((func :initarg :func :accessor func)
(targets :initarg :targets :accessor targets)))
(defmethod print-object ((dispatch dispatch) stream)
(print-unreadable-object (dispatch stream)
(format stream "~3d ~s~@[ ~a~] ~s -> ~{~a~^, ~}"
(index dispatch)
(type-of dispatch)
(unless (eq dispatch (label dispatch))
(label dispatch))
(func dispatch)
(targets dispatch))))
(defmethod disassemble ((instruction dispatch))
(print-instruction-type instruction)
(loop for target in (targets instruction)
do (format T "~& ~2d" target)))
(defclass emote (instruction)
((emote :initarg :emote :accessor emote)))
(defmethod disassemble ((instruction emote))
(print-instruction-type instruction)
(format T "~a" (emote instruction)))
(defclass pause (instruction)
((duration :initarg :duration :accessor duration)))
(defmethod disassemble ((instruction pause))
(print-instruction-type instruction)
(format T "~a" (duration instruction)))
(defclass placeholder (instruction)
((func :initarg :func :accessor func)))
(defclass choose (instruction)
())
(defclass commit-choice (jump)
())
(defmethod disassemble ((instruction commit-choice))
(print-instruction-type instruction)
(format T "~a" (target instruction)))
(defclass confirm (instruction)
())
(defclass begin-mark (instruction)
((markup :initarg :markup :accessor markup)
(end :initarg :end :accessor end)))
(defmethod disassemble ((instruction begin-mark))
(print-instruction-type instruction)
(format T "~s" (markup instruction)))
(defclass end-mark (instruction)
((markup :accessor markup)))
(defclass clear (instruction)
())
(defclass text (instruction)
((text :initarg :text :accessor text)))
(defmethod print-object ((text text) stream)
(print-unreadable-object (text stream)
(format stream "~3d ~s ~s"
(index text)
(type-of text)
(text text))))
(defmethod disassemble ((instruction text))
(print-instruction-type instruction)
(format T "~s" (text instruction)))
(defclass eval (instruction)
((func :initarg :func :accessor func)))
(defmethod print-object ((eval eval) stream)
(print-unreadable-object (eval stream)
(format stream "~3d ~s ~s"
(index eval)
(type-of eval)
(func eval))))
(defmethod disassemble ((instruction eval))
(print-instruction-type instruction)
(format T "~s" (func instruction)))