Skip to content

Instantly share code, notes, and snippets.

@sleepnova
Created October 31, 2020 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sleepnova/637bdf1b04df963286f6c0a837d51fc5 to your computer and use it in GitHub Desktop.
Save sleepnova/637bdf1b04df963286f6c0a837d51fc5 to your computer and use it in GitHub Desktop.
#lang bf expander
#lang br/quicklang
(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-ARG ...)
#'(time OP-OR-LOOP-ARG ...))
(provide bf-program)
(define-macro (bf-loop "[" OP-OR-LOOP-ARG ... "]")
#'(until (zero? (current-byte))
OP-OR-LOOP-ARG ...))
(provide bf-loop)
(define-macro-cases bf-op
[(bf-op ">") #'(ptr++)]
[(bf-op "<") #'(ptr--)]
[(bf-op "+") #'(inc)]
[(bf-op "-") #'(dec)]
[(bf-op ".") #'(write)]
[(bf-op ",") #'(read)])
(provide bf-op)
(define arr (make-bytes 30000 0))
(define ptr 0)
(require racket/unsafe/ops)
(define bytes-ref unsafe-bytes-ref)
(define bytes-set! unsafe-bytes-set!)
(define (current-byte) (bytes-ref arr ptr))
(define (set-current-byte! val) (bytes-set! arr ptr val))
(define (add1 n) (unsafe-fx+ n 1))
(define (sub1 n) (unsafe-fx- n 1))
(define (ptr++) (set! ptr (add1 ptr)))
(define (ptr--) (set! ptr (sub1 ptr)))
(define (inc) (set-current-byte! (add1 (current-byte))))
(define (dec) (set-current-byte! (sub1 (current-byte))))
(define (write) (write-byte (current-byte)))
(define (read) (set-current-byte! (read-byte)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment