-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf.lisp
165 lines (137 loc) · 3.87 KB
/
bf.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
(defpackage #:bf
(:use #:cl)
(:export #:run
#:run-file
#:main))
(in-package #:bf)
(declaim (ftype function
move-ip-to-]
move-ip-to-[))
(defconstant +data-size+ 30000)
(defstruct bf
(instructions "" :type string)
(ip -1 :type fixnum)
(data (make-array +data-size+
:element-type '(unsigned-byte 8)
:initial-element 0))
(dp 0 :type fixnum))
(defun next-instruction (bf)
(if (>= (bf-ip bf)
(1- (length (bf-instructions bf))))
nil
(progn
(incf (bf-ip bf))
(aref (bf-instructions bf)
(bf-ip bf)))))
(defun prev-instruction (bf)
(if (<= (bf-ip bf) 0)
nil
(progn
(decf (bf-ip bf))
(aref (bf-instructions bf)
(bf-ip bf)))))
(defun read-data (bf)
(aref (bf-data bf)
(bf-dp bf)))
(defun write-data (data bf)
(setf (aref (bf-data bf)
(bf-dp bf))
data))
(defun incr-data (bf)
(let ((new-val (1+ (read-data bf))))
(write-data (mod new-val 256)
bf)))
(defun decr-data (bf)
(let ((new-val (1- (read-data bf))))
(write-data (mod new-val 256)
bf)))
(defun dp-> (bf)
(incf (bf-dp bf)))
(defun <-dp (bf)
(decf (bf-dp bf)))
(defun handle-> (bf)
(dp-> bf))
(defun handle-< (bf)
(<-dp bf))
(defun handle-+ (bf)
(incr-data bf))
(defun handle-- (bf)
(decr-data bf))
(defun handle-. (bf)
(write-char (code-char (read-data bf))))
(defvar *char-buf* nil)
(defun fill-char-buf ()
(setf *char-buf* (nreverse *char-buf*))
(multiple-value-bind (line newline-missing)
(read-line *standard-input* nil :eof)
(if (eql line :eof)
(push :eof *char-buf*)
(progn
(map-into line
#'(lambda (c)
(push c *char-buf*))
line)
(unless newline-missing
(push #\Newline *char-buf*))))
(setf *char-buf* (nreverse *char-buf*))))
(defun read-char-buf ()
(let ((c (pop *char-buf*)))
(or c
(progn
(fill-char-buf)
(pop *char-buf*)))))
(defun handle-comma (bf)
(let ((b (read-char-buf)))
(unless (eql b :eof)
(write-data (char-code b)
bf))))
(defun handle-[ (bf)
(let ((b (read-data bf)))
(if (zerop b)
(move-ip-to-] bf))))
(defun move-ip-to-] (bf)
(let ((i (next-instruction bf)))
(if i
(case i
(#\[ (move-ip-to-] bf)
(move-ip-to-] bf))
(#\] (return-from move-ip-to-]))
(otherwise (move-ip-to-] bf))))))
(defun handle-] (bf)
(let ((b (read-data bf)))
(unless (zerop b)
(move-ip-to-[ bf))))
(defun move-ip-to-[ (bf)
(let ((i (prev-instruction bf)))
(if i
(case i
(#\] (move-ip-to-[ bf)
(move-ip-to-[ bf))
(#\[ (return-from move-ip-to-[))
(otherwise (move-ip-to-[ bf))))))
(defun run (instructions)
(let ((bf (make-bf :instructions instructions)))
(labels ((run-loop (bf)
(let ((i (next-instruction bf)))
(when i
(case i
(#\> (handle-> bf))
(#\< (handle-< bf))
(#\+ (handle-+ bf))
(#\- (handle-- bf))
(#\. (handle-. bf))
(#\, (handle-comma bf))
(#\[ (handle-[ bf))
(#\] (handle-] bf)))
(run-loop bf)))))
(run-loop bf))))
(defun run-file (path)
(let ((program (with-open-file (in path :direction :input)
(let ((pgm (make-string (file-length in))))
(read-sequence pgm in)
pgm))))
(run program)))
(defun main ()
(if (< (length sb-ext:*posix-argv*) 2)
(error "Program file must be provided."))
(run-file (second sb-ext:*posix-argv*)))