Created
December 21, 2021 08:24
-
-
Save shirok/69c6e054336f3f43e337e846425d7aa9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use util.match) | |
(use data.sparse) | |
(define-macro (make-dumper . labels) | |
`(lambda (memory) | |
(list ,@(map (^l `(list ',l (~ memory ,l))) labels)))) | |
(define (run program memory dumper) | |
(define (DJN pc) | |
(match-let1 (dest ptr) (~ program pc) | |
(if (zero? (rlet1 v (~ memory ptr) | |
(set! (~ memory ptr) (modulo (- v 1) 256)))) | |
(+ pc 1) | |
(+ pc dest)))) | |
(let loop ((pc 0)) | |
(format #t "~3d: ~s\n" pc (dumper memory)) | |
(if (>= pc (vector-length program)) | |
memory | |
(loop (DJN pc))))) | |
;; COPY A to B | |
(let* ((memory (make-sparse-vector 'u8 :default 0)) | |
(TEMP 0) | |
(A 1) | |
(B 2) | |
(program `#((0 ,TEMP) | |
(0 ,B) | |
(1 ,TEMP) | |
(-1 ,A) | |
(1 ,A) | |
(1 ,B) | |
(-2 ,TEMP)))) | |
(set! (~ memory TEMP) 0) | |
(set! (~ memory A) 10) | |
(set! (~ memory B) 0) | |
(run program memory (make-dumper TEMP A B)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment