adds and changes
parent
63a88ee103
commit
d03ff769e2
@ -1,2 +1,3 @@
|
||||
#lang reader "bf-reader.rkt"
|
||||
+++++++[>+++++<-]>.
|
||||
Greatest language ever!
|
||||
++++++++[>++++++++<-]>.
|
@ -1,44 +1,36 @@
|
||||
#lang br
|
||||
(provide (rename-out [bf-module-begin #%module-begin])
|
||||
#%top-interaction)
|
||||
|
||||
(define #'(bf-module-begin BF-PARSE-TREE ...)
|
||||
(define #'(bf-module-begin PARSE-TREE ...)
|
||||
#'(#%module-begin
|
||||
BF-PARSE-TREE ...))
|
||||
|
||||
|
||||
;; macros to expand our parse tree into local functions
|
||||
|
||||
(provide bf-program op loop)
|
||||
PARSE-TREE ...))
|
||||
(provide (rename-out [bf-module-begin #%module-begin])
|
||||
#%top-interaction)
|
||||
|
||||
;; bf-program doesn't do anything
|
||||
(define #'(bf-program <op-or-loop> ...)
|
||||
#'(begin <op-or-loop> ...))
|
||||
(define #'(bf-program OP-OR-LOOP ...)
|
||||
#'(begin OP-OR-LOOP ...))
|
||||
(provide bf-program)
|
||||
|
||||
;; op branches. Note that string & number literals are
|
||||
;; matched literally in syntax patterns.
|
||||
(define-cases #'op
|
||||
[#'(_ ">") #'(move-pointer 1)]
|
||||
[#'(_ "<") #'(move-pointer -1)]
|
||||
[#'(_ "+") #'(set-pointer-byte! (add1 (get-pointer-byte)))]
|
||||
[#'(_ "-") #'(set-pointer-byte! (sub1 (get-pointer-byte)))]
|
||||
[#'(_ ".") #'(write-byte (get-pointer-byte))]
|
||||
[#'(_ ",") #'(set-pointer-byte! (read-byte))])
|
||||
|
||||
|
||||
(define #'(loop "[" <op-or-loop> ... "]")
|
||||
#'(until (zero? (get-pointer-byte))
|
||||
<op-or-loop> ...))
|
||||
[#'(op ">") #'(move-pointer 1)]
|
||||
[#'(op "<") #'(move-pointer -1)]
|
||||
[#'(op "+") #'(set-current-byte! (add1 (get-current-byte)))]
|
||||
[#'(op "-") #'(set-current-byte! (sub1 (get-current-byte)))]
|
||||
[#'(op ".") #'(write-byte (get-current-byte))]
|
||||
[#'(op ",") #'(set-current-byte! (read-byte))])
|
||||
(provide op)
|
||||
|
||||
;; bf implementation
|
||||
|
||||
;; state: one vector, one pointer
|
||||
(define bf-vector (make-vector 30000 0))
|
||||
(define bf-pointer 0)
|
||||
|
||||
;; gets and sets
|
||||
(define (get-pointer-byte) (vector-ref bf-vector bf-pointer))
|
||||
(define (set-pointer-byte! val) (vector-set! bf-vector bf-pointer val))
|
||||
(define (move-pointer how-far)
|
||||
(set! bf-pointer (+ bf-pointer how-far)))
|
||||
|
||||
(define (get-current-byte)
|
||||
(vector-ref bf-vector bf-pointer))
|
||||
(define (set-current-byte! val)
|
||||
(vector-set! bf-vector bf-pointer val))
|
||||
|
||||
;; pointer mover
|
||||
(define (move-pointer how-far) (set! bf-pointer (+ bf-pointer how-far)))
|
||||
(define #'(loop "[" OP-OR-LOOP ... "]")
|
||||
#'(until (zero? (get-current-byte))
|
||||
OP-OR-LOOP ...))
|
||||
(provide loop)
|
||||
|
@ -0,0 +1,7 @@
|
||||
#lang reader "bf-reader.rkt"
|
||||
>++++++++++>>>+>+[>>>+[-[<<<<<[+<<<<<]>>[[-]>[<<+>+>-]
|
||||
<[>+<-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
|
||||
[>[-]>>>>+>+<<<<<<-[>+<-]]]]]]]]]]]>[<+>-]+>>>>>]<<<<<
|
||||
[<<<<<]>>>>>>>[>>>>>]++[-<<<<<]>>>>>>-]+>>>>>]<[>++<-]
|
||||
<<<<[<[>+<-]<<<<]>>[->[-]++++++[<++++++++>-]>>>>]<<<<<
|
||||
[<[>+>+<<-]>.<<<<<]>.>>>>]
|
Loading…
Reference in New Issue