-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglgen.lisp
147 lines (139 loc) · 2.75 KB
/
glgen.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
(defun instr-to-gl (instr)
(ecase (first instr)
(add "a")
(bring-to-top "b")
(copy-top "c")
(divide "d")
(end-loop "e")
(flip "f")
(greater "g")
(execute-instr "h")
(input "i")
(jump-forward "j")
(kill-stack "k")
(start-loop "l")
(multiply "m")
(push (make-string (second instr) :initial-element #\n))
(output-num "o")
(output-asc "p")
(noop "q")
(remainder "r")
(subtract "s")
(terminate "t")
(unbring-to-top "u")
(push-ascii (format nil "v~%~a" (code-char (second instr))))
(push-100 "w")
(pop-top "x")
(pop-at (make-string (second instr) :initial-element #\y))
(negate "z")))
(defun translate (stmts)
(format nil "~{~a~%~}" (mapcar #'instr-to-gl stmts)))
;; -0, +1
(defconstant +read-one-number+
'((push-ascii 65)
(push-ascii 113)
(subtract)
(input)
(subtract)
(push 10)
(multiply)
(push-ascii 65)
(push-ascii 113)
(subtract)
(input)
(subtract)
(add)
(input)
(pop-top)))
;; -0, +1
(defconstant +push-zero+
`((push-100)
(push-100)
(subtract)))
;; -0, +1
(defconstant +push-one+
`((push-ascii 99)
(push-100)
(subtract)))
;; -2, +1
(defconstant +maximum+
`((copy-top)
(unbring-to-top)
(flip)
(copy-top)
(unbring-to-top)
(greater)
(bring-to-top)
(flip)
(bring-to-top)
(flip)
(negate)
(start-loop)
(pop-top)
(flip)
,@+push-zero+
(end-loop)
(pop-top)
(pop-top)))
(defconstant +program+
`(,@+push-zero+
(copy-top)
(push-100)
;; Main outer loop
(start-loop)
;; When we get here, the "current" row is at the bottom of the
;; stack, padded on both sides with zeroes. On top is the number
;; of iterations remaining.
(copy-top)
(push-100)
(subtract)
,@+push-one+
(add)
;; Inner loop
(start-loop)
;; Now, the row is on the bottom, padded with zeroes. On top of
;; that is the number of big iterations remaining, and on top of
;; that is the number of small iterations remaining.
,@+read-one-number+
(copy-top)
(bring-to-top)
(add)
(flip)
(bring-to-top)
(copy-top)
(unbring-to-top)
(add)
,@+maximum+
(flip)
(unbring-to-top)
(flip)
(bring-to-top)
,@+push-one+
(flip)
(subtract)
;; End of inner loop
(end-loop)
(flip)
,@+push-one+
(flip)
(subtract)
;; End of outer loop
(end-loop)
(pop-top)
(push-100)
;; Fold loop
(start-loop)
(unbring-to-top)
,@+maximum+
(bring-to-top)
,@+push-one+
(flip)
(subtract)
;; End fold loop
(end-loop)
(pop-top)
(output-num)
(push 10)
(output-asc)
(terminate)))
(format t "~a~%" (translate +program+))