-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemotegen.lisp
423 lines (383 loc) Β· 15.2 KB
/
emotegen.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
;; Emotinomicon code generation helper
(defparameter +default-push-num-length+ 13)
(defclass label ()
((label-name :type symbol :accessor label-name :initarg :label-name)))
(defclass goto ()
((label-name :type symbol :accessor label-name :initarg :label-name)))
;; Condition must have stack effect ( -- x) and must NOT depend on the
;; current top of the stack. If condition is true, then we pop it and
;; jump to the label. If condition is false, we proceed.
;;
;; If inverted-condition is true, then we jump on a false condition
;; and proceed on a true one. The default for inverted-condition is
;; false.
(defclass goto-maybe ()
((branch-condition :type list :accessor branch-condition :initarg :branch-condition)
(label-name :type symbol :accessor label-name :initarg :label-name)
(inverted-condition :type boolean :accessor inverted-condition :initarg :inverted-condition :initform nil)))
(defun label (label-name)
(make-instance 'label :label-name label-name))
(defun goto (label-name)
(make-instance 'goto :label-name label-name))
(defun goto-maybe (branch-condition label-name &key (inverted-condition nil))
(make-instance 'goto-maybe
:branch-condition branch-condition
:label-name label-name
:inverted-condition inverted-condition))
(defgeneric parse (cmd index labels-hash)
(:documentation "Takes the command, the current position in the
source code, and a hash table of known labels. The last argument is
mutable. Returns the new position after executing the given
command. We need to pre-process the input so we know the source
index of labels for goto statements, since that's our only
nontrivial conditional capability."))
(defmethod parse ((cmd list) index labels-hash)
;; List commands are always of length 1 in the resulting source
;; code.
(1+ index))
(defmethod parse ((cmd label) index labels-hash)
;; Labels do not produce any code, but they modify the labels hash.
(when (gethash (label-name cmd) labels-hash)
(error "Duplicate label ~A" (label-name cmd)))
(setf (gethash (label-name cmd) labels-hash) index)
index)
(defmethod parse ((cmd string) index labels-hash)
(+ index (length cmd)))
(defmethod parse ((cmd goto) index labels-hash)
(+ index (1+ +default-push-num-length+)))
(defmethod parse ((cmd goto-maybe) index labels-hash)
(let* ((index1 (+ index +default-push-num-length+))
(index2 (parse-seq (branch-condition cmd) index1 labels-hash))
(index3 (+ index2 3)))
index3))
(defun parse-seq (cmds starting-index labels-hash)
(loop with index = starting-index
for cmd in cmds
do (setf index (parse cmd index labels-hash))
finally (return index)))
(defgeneric translate (cmd labels-hash)
(:documentation "Takes the command and produces a string. The string must be of the length
promised by #'parse."))
(defmethod translate ((cmd label) labels-hash)
"")
(defmethod translate ((cmd goto) labels-hash)
(let ((destination-index (gethash (label-name cmd) labels-hash)))
(unless destination-index
(error "No such label ~A" (label-name cmd)))
(unless (< destination-index 10000)
(error "Label index ~A too big, increase the limit :(" destination-index))
(format nil "~{~A~}"
(append (mapcar (lambda (c) (translate c labels-hash)) (push-num-fixed-length destination-index))
(list (translate '(pop-and-goto) labels-hash))))))
(defmethod translate ((cmd goto-maybe) labels-hash)
(let ((destination-index (gethash (label-name cmd) labels-hash)))
(unless destination-index
(error "No such label ~A" (label-name cmd)))
(unless (< destination-index 10000)
(error "Label index ~A too big, increase the limit :(" destination-index))
(format nil "~{~A~}"
(append (mapcar (lambda (c) (translate c labels-hash)) (push-num-fixed-length destination-index))
(mapcar (lambda (c) (translate c labels-hash)) (branch-condition cmd))
(list (translate (if (inverted-condition cmd) '(skip-if-true) '(skip-if-false)) labels-hash)
(translate '(pop-and-goto) labels-hash)
(translate '(drop) labels-hash))))))
(defmethod translate ((cmd string) labels-hash)
cmd)
(defmethod translate ((cmd list) labels-hash)
(ecase (first cmd)
(push0 "π")
(push1 "π
")
(push2 "π")
(push3 "π")
(push4 "π")
(push5 "π")
(push6 "π")
(push7 "π‘")
(push8 "π")
(push9 "π")
(push10 "π")
(push100 "π―")
(add "β")
(sub "β")
(div "β")
(mul "β")
(input-char "β«")
(output-char "β¬")
(open-loop "βͺ")
(close-loop "β©")
(dup "π")
(drop "π")
(reverse-stack "π")
(reverse-n "π") ; DOESN'T WORK IN THE REFERENCE IMPLEMENTATION !!
(pow "π")
(log "π")
(negate "π’")
(abs "π")
(2n "π")
(3n "βΊοΈ")
(4n "π")
(n/2 "π")
(n/3 "π")
(n/4 "π")
(n^2 "π£")
(n^3 "π")
(n^4 "π")
(sqrt "π")
(cbrt "π")
(4rt "π")
(floor "π")
(ceil "π")
(round "π€")
(n+1 "π")
(n-1 "πΏ")
(mod "π")
(2^n "π")
(3^n "π")
(4^n "π")
(fibo "π ")
(lucas "π₯")
(pi "π¦")
(e "π«")
(phi "π°")
(ln "π΅")
(log10 "πΊ")
(e^n "πΏ")
(10^n "π§")
(pop-and-goto "π¬")
(pop-and-modify-code "π±")
(pop-and-get-code "πΆ")
(n%2 "π»")
(rand "π")
(output-num "π¨")
(begin-quote "π")
(end-quote "π²")
(factorial "οΈβ")
(if-then-else "οΈβ")
(skip "β")
(skip-if-true "β")
(double-factorial "βΌοΈ")
(skip-if-false "βοΈ")))
(defmethod translate-all (cmds)
(let ((labels-hash (make-hash-table :test 'equal)))
(parse-seq cmds 0 labels-hash)
(mapcar (lambda (cmd) (translate cmd labels-hash)) cmds)))
(defun lit-string (body)
`((begin-string)
,@body
(end-string)))
(defun do-loop (body)
`((open-loop)
,@body
(close-loop)))
(defun do-if (condition body &key (label (gensym)) (inverted-condition t))
"condition shall have stack effect ( -- x) and body can have arbitrary
stack effect."
`(,(goto-maybe condition label :inverted-condition inverted-condition)
,@body
,(label label)))
;; Only supports numbers from 0 to 100
(defun push-num (n)
(case n
(0 '((push0)))
(1 '((push1)))
(2 '((push2)))
(3 '((push3)))
(4 '((push4)))
(5 '((push5)))
(6 '((push6)))
(7 '((push7)))
(8 '((push8)))
(9 '((push9)))
(10 '((push10)))
(100 '((push100)))
(t (multiple-value-bind (quotient remainder) (floor n 10)
`((push10) ,@(push-num quotient) (mul) ,@(push-num remainder) (add))))))
;; I think the moon phases are pretty, so I'm going to use them as
;; padding :) We don't have to use different characters (we could just
;; spam 'new moon', but I consider myself something of an artist)
(defparameter *padding*
#("π" "π" "π" "π" "π" "π" "π" "π"))
(defparameter *padding-position*
0)
(defun generate-padding-list (n)
(if (<= n 0)
nil
(cons (elt *padding* *padding-position*)
(progn (setf *padding-position* (mod (1+ *padding-position*) (length *padding*)))
(generate-padding-list (1- n))))))
(defun pad-list (list desired-length)
(append list (generate-padding-list (- desired-length (length list)))))
(defun push-num-fixed-length (n &optional (desired-length +default-push-num-length+))
"As push-num but padding to a desired length. The default
desired-length of 13 assumes that n is less than 10,000, as every
number less than that can be represented in 13 characters or less."
(let ((num-instructions (push-num n)))
(pad-list num-instructions desired-length)))
;; Using repeated multiplication, we do better than naive push-num
;; since we'll be using this number A LOT.
(defparameter *65536*
'((push10) (push6) (add) (dup) (mul) (dup) (mul)))
;; Copies the Nth element on the stack
;;
;; (copy-nth 0) is equivalent to (dup)
;; (defun copy-nth (n)
;; `(,@(push-num n)
;; (reverse-n)
;; (dup)
;; ,@(push-num (1+ n))
;; (reverse-n)
;; ,@(push-num n)
;; (reverse-n)
;; ,@(push-num (1+ n))
;; (reverse-n)))
(defun padding (n)
(loop for i from 1 to n
collect '(reverse-stack)))
;; Each variable occupies two cells, so make sure to use even numbers
;; here. We store the higher order bits in the given position N and
;; the lower order bits in N+1.
(defparameter *var-numerator* 0)
(defparameter *var-denominator* 2)
(defparameter *var-numbers-count* 4)
(defparameter *var-temporary* 6)
(defparameter *var-whole-part* 8)
(defparameter *var-new-numerator* 10)
(defun save-var (n body)
"Save the top of the stack to the variable. Supports up to 32
bits (unsigned). Note that this evaluates the body twice, since
there's no reliable way to swap stack positions.
Note that the stack effect of `body` must be exactly ( -- x), and
that value will be pushed onto the stack."
`(,@(push-num n)
,@body
,@*65536*
(n-1)
(n+1)
(div)
(floor)
(pop-and-modify-code)
,@(push-num (1+ n))
,@*65536*
,@body
(n-1)
(n+1)
(mod)
(pop-and-modify-code)))
(defun get-var (n)
`(,@(push-num n)
(pop-and-get-code)
,@*65536*
(mul)
,@(push-num (1+ n))
(pop-and-get-code)
(add)))
(format t "~{~A~}~%"
(translate-all `(,@(padding 20) ; Space used for read-write variables
,@(save-var *var-numerator*
`(,@(push-num 987654321)))
,@(save-var *var-denominator*
`(,@(push-num 123456789)))
,@(save-var *var-numbers-count*
`(,@(push-num 0)))
(push1) ; Loop sentinel
,@(do-loop
`((drop)
,@(save-var *var-whole-part*
`(,@(get-var *var-numerator*)
,@(get-var *var-denominator*)
(div)
(floor)))
;; We have the whole part; leave it on
;; the stack for later and just keep
;; going.
,@(get-var *var-whole-part*)
,@(save-var *var-new-numerator*
`(,@(get-var *var-numerator*)
,@(get-var *var-whole-part*)
,@(get-var *var-denominator*)
(mul)
(sub)))
,@(save-var *var-numerator*
`(,@(get-var *var-denominator*)))
,@(save-var *var-denominator*
`(,@(get-var *var-new-numerator*)))
,@(save-var *var-temporary*
`(,@(get-var *var-numbers-count*)
,@(push-num 1)
(add)))
,@(save-var *var-numbers-count*
`(,@(get-var *var-temporary*)))
,@(get-var *var-numerator*)
,@(get-var *var-denominator*)
(mul)
;; Most of the arithmetic operations in
;; the language operate on bignums that
;; don't work correctly when taken for
;; truthiness. But fortunately, n-1 and
;; n+1 coerce back to regular numbers
;; that are correctly falsy at zero.
(n-1)
(n+1)))
(drop) ; Drop loop sentinel value
,@(do-if
`((push1)
,@(get-var *var-numbers-count*)
(n%2)
(sub)
(n-1)
(n+1))
`(,@(do-if
`(,@(get-var *var-whole-part*)
(n-1))
`(;; Case 1: We need to correct, but the top of the stack is not 1.
(push1)
(sub)
(push1)
,@(save-var *var-temporary*
`(,@(get-var *var-numbers-count*)))
,@(save-var *var-numbers-count*
`(,@(get-var *var-temporary*)
(n-1)
(n+1)
(n+1)))))
,@(do-if
`((push1)
,@(get-var *var-numbers-count*)
(n%2)
(sub)
(n-1)
(n+1))
`(;; Case 2: We need to correct, top of the stack equals 1.
(drop)
(n-1)
(n+1)
(n+1)))))
(push1) ; Loop sentinel
,@(do-loop
`((drop)
(output-num)
,@(push-num 44) ; ASCII value of comma
(output-char)
,@(save-var *var-temporary*
`(,@(get-var *var-numbers-count*)))
,@(save-var *var-numbers-count*
`(,@(get-var *var-temporary*)
(n-1)))
,@(get-var *var-numbers-count*)
(n-1)
(n+1)))
(drop)))) ; Drop loop sentinel
;; (format t "~{~A~}~%"
;; (translate-all `(,@(push-num 10)
;; ,@(push-num 20)
;; ,@(push-num 30)
;; ,@(push-num 40)
;; ,@(push-num 50)
;; ,@(push-num 60)
;; ,@(copy-nth 2)
;; (output-num)
;; (output-num)
;; (output-num)
;; (output-num)
;; (output-num)
;; (output-num))))