You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-demo/bf-demo/expander-imperative.rkt

39 lines
1.0 KiB
Racket

8 years ago
#lang br/quicklang
8 years ago
(define-macro (bf-module-begin PARSE-TREE)
#'(#%module-begin
PARSE-TREE))
(provide (rename-out [bf-module-begin #%module-begin]))
8 years ago
(define-macro (bf-program OP-OR-LOOP-ARG ...)
#'(void OP-OR-LOOP-ARG ...))
8 years ago
(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 ">") #'(gt)]
[(bf-op "<") #'(lt)]
[(bf-op "+") #'(plus)]
[(bf-op "-") #'(minus)]
[(bf-op ".") #'(period)]
[(bf-op ",") #'(comma)])
(provide bf-op)
8 years ago
(define arr (make-vector 30000 0))
(define ptr 0)
(define (current-byte) (vector-ref arr ptr))
(define (set-current-byte! val) (vector-set! arr ptr val))
8 years ago
8 years ago
(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)))
8 years ago