-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf-readable.lisp
48 lines (42 loc) · 1.3 KB
/
bf-readable.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
(let ((tape (make-array 30000))
(pointer 0)
(source-file (open (cadr *posix-argv*))))
(defun current ()
(aref tape pointer))
(defun read-source ()
(case (read-char source-file nil :eof)
(#\< (cons (lambda ()
(decf pointer))
(read-source)))
(#\> (cons (lambda ()
(incf pointer))
(read-source)))
(#\+ (cons (lambda ()
(incf (aref tape pointer)))
(read-source)))
(#\- (cons (lambda ()
(decf (aref tape pointer)))
(read-source)))
(#\. (cons (lambda ()
(princ (code-char (current))))
(read-source)))
(#\, (cons (lambda ()
(force-output)
(setf (aref tape pointer)
(char-code (read-char nil nil #\Nul))))
(read-source)))
(#\[ (cons (read-source)
(read-source)))
(#\] '())
(:eof (cons #'exit '()))
(t (read-source))))
(defun eval-cmd (cmd)
(if (consp cmd)
(when (> (current) 0)
(eval-bf cmd))
(funcall cmd)))
(defun eval-bf (commands)
(mapc #'eval-cmd commands)
(when (> (current) 0)
(eval-bf commands)))
(eval-bf (read-source)))