closer
parent
1b1aecbb84
commit
7e367b3d8d
@ -1,29 +1,38 @@
|
||||
#lang br/quicklang
|
||||
|
||||
(provide #%module-begin)
|
||||
(define-macro (bf-module-begin PARSE-TREE)
|
||||
#'(#%module-begin
|
||||
PARSE-TREE))
|
||||
(provide (rename-out [bf-module-begin #%module-begin]))
|
||||
|
||||
(define-macro (bf-program OP-OR-LOOP ...)
|
||||
#'(begin OP-OR-LOOP ...))
|
||||
(define-macro (bf-program PROGRAM-ARG ...)
|
||||
#'(void PROGRAM-ARG ...))
|
||||
(provide bf-program)
|
||||
|
||||
(define-macro-cases op
|
||||
[(op ">") #'(move-pointer 1)]
|
||||
[(op "<") #'(move-pointer -1)]
|
||||
[(op "+") #'(set-current-byte! (add1 (get-current-byte)))]
|
||||
[(op "-") #'(set-current-byte! (sub1 (get-current-byte)))]
|
||||
[(op ".") #'(write-byte (get-current-byte))]
|
||||
[(op ",") #'(set-current-byte! (read-byte))])
|
||||
(provide op)
|
||||
(define arr (make-vector 30000 0))
|
||||
(define ptr 0)
|
||||
|
||||
(define (current-byte) (vector-ref arr ptr))
|
||||
|
||||
(define bf-vector (make-vector 30000 0))
|
||||
(define bf-pointer 0)
|
||||
(define (set-current-byte! val) (vector-set! arr ptr val))
|
||||
|
||||
(define (move-pointer how-far) (set! bf-pointer (+ bf-pointer how-far)))
|
||||
(define (gt) (set! ptr (add1 ptr)))
|
||||
(define (lt) (set! ptr (sub1 ptr)))
|
||||
(define (plus) (set-current-byte! (add1 (current-byte))))
|
||||
(define (minus) (set-current-byte! (sub1 (current-byte))))
|
||||
(define (period) (write-byte (current-byte)))
|
||||
(define (comma) (set-current-byte! (read-byte)))
|
||||
|
||||
(define (get-current-byte) (vector-ref bf-vector bf-pointer))
|
||||
(define (set-current-byte! val) (vector-set! bf-vector bf-pointer val))
|
||||
(define-macro-cases op
|
||||
[(op ">") #'(gt)]
|
||||
[(op "<") #'(lt)]
|
||||
[(op "+") #'(plus)]
|
||||
[(op "-") #'(minus)]
|
||||
[(op ".") #'(period)]
|
||||
[(op ",") #'(comma)])
|
||||
(provide op)
|
||||
|
||||
(define-macro (loop LOOP-ARG ...)
|
||||
#'(until (zero? (get-current-byte))
|
||||
#'(until (zero? (current-byte))
|
||||
LOOP-ARG ...))
|
||||
(provide loop)
|
||||
|
Loading…
Reference in New Issue