Some type declarations for sim (performance didn't change much)

This commit is contained in:
2024-08-11 22:17:57 +03:00
parent 9f9956e889
commit e75dd92a1f

View File

@@ -1,72 +1,81 @@
(in-package :cl-forth) (in-package :cl-forth)
(defvar *stack* ()) (defvar *stack* nil)
(defvar *bel* nil) (defvar *bel* nil)
(defmacro push* (item)
`(vector-push ,item *stack*))
(defmacro pop* ()
`(the fixnum (vector-pop *stack*)))
(defun simulate-program (program) (defun simulate-program (program)
(setf *stack* ()) (declare (optimize (speed 3)) (vector program))
(setf *bel* (make-array 1000 :element-type '(unsigned-byte 8))) (let ((*bel* (make-array 1000 :element-type '(unsigned-byte 8)))
(iter (with op = nil) (*stack* (make-array 1000 :element-type 'fixnum :fill-pointer 0)))
(for i below (length program)) (iter (with op = nil)
(setf op (aref program i)) (for i below (the fixnum (length program)))
;; (print op) (declare (fixnum i))
;; (print *stack*) (setf op (aref program i))
(case (car op) ;; (print op)
(:push-int (push (cadr op) *stack*)) ;; (print *stack*)
(:+ (push (+ (pop *stack*) (pop *stack*)) *stack*)) (case (car op)
(:dump (format t "~a~%" (pop *stack*))) (:push-int (push* (cadr op)))
(:= (push (if (= (pop *stack*) (pop *stack*)) 1 0) (:+ (push* (+ (pop*) (pop*))))
*stack*)) (:dump (format t "~a~%" (pop*)))
(: (let ((top (pop *stack*))) (:= (push* (if (= (pop*) (pop*)) 1 0)))
(push top *stack*) (:eş (let ((top (pop*)))
(push top *stack*))) (push* top)
(:ise (when (= 0 (pop *stack*)) (push* top)))
(setf i (cadr op)))) (:ise (when (= 0 (pop*))
(:yoksa (setf i (cadr op))) (setf i (cadr op))))
(:yap (if (= 3 (length op)) (:yoksa (setf i (cadr op)))
(setf i (third op)))) (:yap (if (= 3 (length op))
(:< (let ((top (pop *stack*))) (setf i (third op))))
(push (if (< (pop *stack*) top) 1 0) (:< (let ((top (pop*)))
*stack*))) (push* (if (< (pop*) top) 1 0))))
(:iken (when (= 0 (pop *stack*)) (:iken (when (= 0 (pop*))
(setf i (cadr op)))) (setf i (cadr op))))
(:bel (push 0 *stack*)) (:bel (push* 0))
(:oku (push (aref *bel* (pop *stack*)) *stack*)) (:oku (push* (aref *bel* (pop*))))
(:yaz (let ((top (pop *stack*))) (:yaz (let ((top (pop*)))
(setf (aref *bel* (pop *stack*)) top))) (setf (aref *bel* (pop*)) top)))
(:syscall (let ((fn (pop *stack*))) (:syscall (let ((fn (pop*)))
(case fn (case fn
(1 (let* ((len (pop *stack*)) (1 (let* ((len (pop*))
(start (pop *stack*)) (start (pop*))
(file-desc (pop *stack*))) (file-desc (pop*)))
(declare (ignore file-desc)) (declare (ignore file-desc))
(princ (map 'string #'code-char ;; (princ (map 'string #'code-char
(subseq *bel* start (+ len start)))))) ;; (subseq *bel* start (+ len start))))
(60 (return-from simulate-program))))) (loop for i from start below (+ len start)
(:pipe (let ((top (pop *stack*))) do (write-char (code-char (aref *bel* i))))
(push (logior (pop *stack*) top) *stack*))) (fresh-line)))
(:<< (let ((top (pop *stack*))) (60 (return-from simulate-program)))))
(push (ash (pop *stack*) top) *stack*))) (:pipe (let ((top (pop*)))
(:>> (let ((top (pop *stack*))) (push* (logior (pop*) top))))
(push (ash (pop *stack*) (- top)) *stack*))) (:<< (let ((top (pop*)))
(:& (let ((top (pop *stack*))) (push* (ash (pop*) top))))
(push (logand (pop *stack*) top) *stack*)) ) (:>> (let ((top (pop*)))
(:düş (pop *stack*)) (push* (ash (pop*) (- top)))))
(:değiş (let* ((top1 (pop *stack*)) (:& (let ((top (pop*)))
(top2 (pop *stack*))) (push* (logand (pop*) top))))
(push top1 *stack*) (:düş (pop*))
(push top2 *stack*))) (:değiş (let* ((top1 (pop*))
(:rot (let* ((top1 (pop *stack*)) (top2 (pop*)))
(top2 (pop *stack*)) (push* top1)
(top3 (pop *stack*))) (push* top2)))
(push top2 *stack*) (:rot (let* ((top1 (pop*))
(push top1 *stack*) (top2 (pop*))
(push top3 *stack*))) (top3 (pop*)))
(:üst (let* ((top1 (pop *stack*)) (push* top2)
(top2 (pop *stack*))) (push* top1)
(push top2 *stack*) (push* top3)))
(push top1 *stack*) (:üst (let* ((top1 (pop*))
(push top2 *stack*)))))) (top2 (pop*)))
(push* top2)
(push* top1)
(push* top2)))))))
(defun str->prog (str) (defun str->prog (str)
(parse-tokens (lex-string str))) (parse-tokens (lex-string str)))