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