bf: group ops

~13s mandelbrot
This commit is contained in:
mehbark 2024-12-17 16:27:04 -05:00
parent f4f57a6857
commit 704ff740e0
2 changed files with 42 additions and 35 deletions

76
bf.lisp
View file

@ -1,46 +1,52 @@
(load "utils.lisp") (load "utils.lisp")
; i've done this before
(deftype cell () '(mod 256))
; TODO: group ops
(defun comp-part (&optional (port *standard-input*)) (defun comp-part (&optional (port *standard-input*))
(begin (begin
(loop with out (= out nil)
for c = (read-char port nil nil) (= op 'inc)
(= val 0)
(=f put (&optional (next 'inc))
(unless (zerop val) (push (list op val) out))
(setf op next)
(setf val 0))
(loop for c = (read-char port nil nil)
while (and c (not (eql c #\]))) while (and c (not (eql c #\])))
when (in c #\+ #\- #\> #\< #\. #\, #\[) when (in c #\+ #\- #\> #\< #\. #\, #\[)
do (push do (ecase c
(ecase c ((#\+ #\-)
(#\+ '(inc)) (unless (eq op 'inc) (put 'inc))
(#\- '(dec)) (incf val (if (eql c #\+) 1 -1)))
(#\> '(minc))
(#\< '(mdec)) ((#\> #\<)
; TODO: fix (unless (eq op 'minc) (put 'minc))
(#\. '(put-byte)) (incf val (if (eql c #\>) 1 -1)))
(#\, '(get-byte))
(#\[ `(loop until (zerop cell) do (progn ,@(comp-part port))))) (#\. (put) (push '(put-byte) out))
out) (#\, (put) (push '(get-byte) out))
finally (return (nreverse out))))) (#\[ (put) (push `(loop until (zerop cell) do (progn ,@(comp-part port))) out)))
finally (progn
(put)
(return (nreverse out))))))
(defun comp (&optional (port *standard-input*)) (defun comp (&optional (port *standard-input*))
`(begin (eval
(declare (optimize (speed 3) (safety 0))) `(lambda (&key (in *standard-input*) (out *standard-output*))
(= mem (make-array '(65536) :element-type 'cell)) (begin
(= mp 0) (declare (optimize (speed 3) (safety 0)))
(=sm cell (aref mem mp)) (= mem (make-array '(65536) :element-type '(unsigned-byte 8)))
(= mp 0)
(=sm cell (aref mem mp))
(=f inc (&optional (n 1)) (setf cell (mod (+ cell n) 256))) (=f inc (&optional (n 1)) (setf cell (mod (+ cell n) 256)))
(=f dec () (inc -1)) (=f minc (&optional (n 1)) (setf mp (mod (+ mp n) 65536)))
(=f minc (&optional (n 1)) (setf mp (mod (+ mp n) 65536))) ; TODO: fix
(=f mdec () (minc -1)) (=f get-byte () (setf cell (mod (char-code (read-char in nil #\null)) 256)))
(=f put-byte () (write-char (code-char cell) out))
(=f get-byte () (setf cell (mod (char-code (read-char *standard-input* nil #\null)) 256))) (=m bf-loop (&body body)
(=f put-byte () (write-char (code-char cell))) `(loop until (zerop cell)
do (progn ,@body)))
(=m bf-loop (&body body) ,@(comp-part port)
`(loop until (zerop cell) t))))
do (progn ,@body)))
,@(comp-part port)
t))

View file

@ -690,6 +690,7 @@
`(let-match1 ,pattern (progn ,@body) `(let-match1 ,pattern (progn ,@body)
(begin ,@rest))) (begin ,@rest)))
;; consider LABELS
((structure ('=f name args . body)) ((structure ('=f name args . body))
`(flet ((,name ,args (begin ,@body))) `(flet ((,name ,args (begin ,@body)))
(begin ,@rest))) (begin ,@rest)))