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.
61 lines
1.5 KiB
Racket
61 lines
1.5 KiB
Racket
9 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]))
|
||
9 years ago
|
|
||
8 years ago
|
(define (fold-funcs apl bf-funcs)
|
||
|
(for/fold ([current-apl apl])
|
||
|
([bf-func (in-list bf-funcs)])
|
||
|
(apply bf-func current-apl)))
|
||
|
|
||
8 years ago
|
(define-macro (bf-program OP-OR-LOOP-ARG ...)
|
||
8 years ago
|
#'(begin
|
||
|
(define first-apl (list (make-vector 30000 0) 0))
|
||
|
(void (fold-funcs first-apl (list OP-OR-LOOP-ARG ...)))))
|
||
9 years ago
|
(provide bf-program)
|
||
9 years ago
|
|
||
8 years ago
|
(define-macro (loop "[" OP-OR-LOOP-ARG ... "]")
|
||
|
#'(lambda (arr ptr)
|
||
|
(for/fold ([current-apl (list arr ptr)])
|
||
|
([i (in-naturals)]
|
||
8 years ago
|
#:break (zero? (apply current-byte
|
||
|
current-apl)))
|
||
|
(fold-funcs current-apl (list OP-OR-LOOP-ARG ...)))))
|
||
8 years ago
|
(provide loop)
|
||
|
|
||
8 years ago
|
(define-macro-cases op
|
||
|
[(op ">") #'gt]
|
||
|
[(op "<") #'lt]
|
||
|
[(op "+") #'plus]
|
||
|
[(op "-") #'minus]
|
||
|
[(op ".") #'period]
|
||
|
[(op ",") #'comma])
|
||
|
(provide op)
|
||
|
|
||
8 years ago
|
(define (current-byte arr ptr) (vector-ref arr ptr))
|
||
9 years ago
|
|
||
8 years ago
|
(define (set-current-byte arr ptr val)
|
||
|
(vector-set! arr ptr val)
|
||
|
arr)
|
||
9 years ago
|
|
||
8 years ago
|
(define (gt arr ptr) (list arr (add1 ptr)))
|
||
|
(define (lt arr ptr) (list arr (sub1 ptr)))
|
||
8 years ago
|
|
||
|
(define (plus arr ptr)
|
||
|
(list (set-current-byte arr ptr (add1 (current-byte arr ptr)))
|
||
|
ptr))
|
||
|
|
||
|
(define (minus arr ptr)
|
||
|
(list (set-current-byte arr ptr (sub1 (current-byte arr ptr)))
|
||
|
ptr))
|
||
|
|
||
|
(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))
|
||
8 years ago
|
|