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