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-slow.rkt

64 lines
1.6 KiB
Racket

8 years ago
#lang br/quicklang
(define-macro (bf-module-begin PARSE-TREE)
#'(#%module-begin
PARSE-TREE))
(provide (rename-out [bf-module-begin #%module-begin]))
(define (fold-funcs apl bf-funcs)
(for/fold ([current-apl apl])
([bf-func (in-list bf-funcs)])
(apply bf-func current-apl)))
(define-macro (bf-program OP-OR-LOOP-ARG ...)
#'(begin
(define first-apl (list (make-vector 30000 0) 0))
(void (fold-funcs first-apl (list OP-OR-LOOP-ARG ...)))))
(provide bf-program)
(define-macro (bf-loop "[" OP-OR-LOOP-ARG ... "]")
8 years ago
#'(lambda (arr ptr)
(for/fold ([current-apl (list arr ptr)])
([i (in-naturals)]
#:break (zero? (apply current-byte
current-apl)))
(fold-funcs current-apl (list 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 (current-byte arr ptr) (vector-ref arr ptr))
(define (set-current-byte arr ptr val)
(define new-arr (vector-copy arr))
(vector-set! new-arr ptr val)
new-arr)
(define (gt arr ptr) (list arr (add1 ptr)))
(define (lt arr ptr) (list arr (sub1 ptr)))
(define (plus arr ptr)
8 years ago
(list
(set-current-byte arr ptr (add1 (current-byte arr ptr)))
ptr))
8 years ago
(define (minus arr ptr)
8 years ago
(list
(set-current-byte arr ptr (sub1 (current-byte arr ptr)))
ptr))
8 years ago
(define (period arr ptr)
(write-byte (current-byte arr ptr))
(list arr ptr))
(define (comma arr ptr)
(list (set-current-byte arr ptr (read-byte)) ptr))