refactoring
parent
4e0e306777
commit
fc826f9269
@ -1,36 +1,34 @@
|
|||||||
#lang br
|
#lang br
|
||||||
|
|
||||||
(define #'(bf-module-begin _PARSE-TREE ...)
|
(define-macro (bf-module-begin SRC-EXPR ...)
|
||||||
#'(#%module-begin
|
#'(#%module-begin
|
||||||
_PARSE-TREE ...))
|
SRC-EXPR ...))
|
||||||
(provide (rename-out [bf-module-begin #%module-begin])
|
(provide (rename-out [bf-module-begin #%module-begin]))
|
||||||
#%top-interaction)
|
|
||||||
|
|
||||||
(define #'(bf-program _OP-OR-LOOP ...)
|
(define-macro (bf-program OP-OR-LOOP ...)
|
||||||
#'(begin _OP-OR-LOOP ...))
|
#'(begin OP-OR-LOOP ...))
|
||||||
(provide bf-program)
|
(provide bf-program)
|
||||||
|
|
||||||
(define-cases #'op
|
(define-macro op
|
||||||
[#'(op ">") #'(move-pointer 1)]
|
[(op ">") #'(move-pointer 1)]
|
||||||
[#'(op "<") #'(move-pointer -1)]
|
[(op "<") #'(move-pointer -1)]
|
||||||
[#'(op "+") #'(set-current-byte! (add1 (get-current-byte)))]
|
[(op "+") #'(set-current-byte! (add1 (get-current-byte)))]
|
||||||
[#'(op "-") #'(set-current-byte! (sub1 (get-current-byte)))]
|
[(op "-") #'(set-current-byte! (sub1 (get-current-byte)))]
|
||||||
[#'(op ".") #'(write-byte (get-current-byte))]
|
[(op ".") #'(write-byte (get-current-byte))]
|
||||||
[#'(op ",") #'(set-current-byte! (read-byte))])
|
[(op ",") #'(set-current-byte! (read-byte))])
|
||||||
(provide op)
|
(provide op)
|
||||||
|
|
||||||
(define bf-vector (make-vector 30000 0))
|
(define bf-vector (make-vector 30000 0))
|
||||||
(define bf-pointer 0)
|
(define bf-pointer 0)
|
||||||
|
|
||||||
(define (move-pointer how-far)
|
(define (move-pointer how-far) (set! bf-pointer (+ bf-pointer how-far)))
|
||||||
(set! bf-pointer (+ bf-pointer how-far)))
|
|
||||||
|
|
||||||
(define (get-current-byte)
|
(define (get-current-byte) (vector-ref bf-vector bf-pointer))
|
||||||
(vector-ref bf-vector bf-pointer))
|
(define (set-current-byte! val) (vector-set! bf-vector bf-pointer val))
|
||||||
(define (set-current-byte! val)
|
|
||||||
(vector-set! bf-vector bf-pointer val))
|
|
||||||
|
|
||||||
(define #'(loop "[" _OP-OR-LOOP ... "]")
|
(define-macro (loop LOOP-ARG ...)
|
||||||
#'(until (zero? (get-current-byte))
|
#'(until (zero? (get-current-byte))
|
||||||
_OP-OR-LOOP ...))
|
LOOP-ARG ...))
|
||||||
(provide loop)
|
(provide loop)
|
||||||
|
|
||||||
|
(provide #%top-interaction)
|
@ -1,4 +1,4 @@
|
|||||||
#lang brag
|
#lang brag
|
||||||
bf-program : (op | loop)*
|
bf-program : (op | loop)*
|
||||||
op : ">" | "<" | "+" | "-" | "." | ","
|
op : ">" | "<" | "+" | "-" | "." | ","
|
||||||
loop : "[" (op | loop)* "]"
|
loop : /"[" (op | loop)* /"]"
|
@ -0,0 +1,6 @@
|
|||||||
|
#lang reader br/demo/stacker
|
||||||
|
push 4
|
||||||
|
push 8
|
||||||
|
+
|
||||||
|
push 3
|
||||||
|
*
|
@ -1,33 +1,29 @@
|
|||||||
#lang br
|
#lang br
|
||||||
|
|
||||||
(define (read-syntax source-path input-port)
|
(define (read-syntax source-path input-port)
|
||||||
(define src-strs (remove-blank-lines (port->lines input-port)))
|
(define src-strs (remove-blank-lines (port->lines input-port)))
|
||||||
(define (make-datum str) (format-datum '(dispatch ~a) str))
|
(define (make-datum str) (format-datum '(dispatch ~a) str))
|
||||||
(define src-exprs (map make-datum src-strs))
|
(define src-exprs (map make-datum src-strs))
|
||||||
(strip-context
|
(strip-context
|
||||||
(inject-syntax ([#'(_SRC-EXPR ...) src-exprs])
|
(with-pattern ([(SRC-EXPR ...) (map make-datum src-strs)])
|
||||||
#'(module stacker-mod br/demo/stacker
|
#'(module stacker-mod br/demo/stacker
|
||||||
_SRC-EXPR ...))))
|
SRC-EXPR ...))))
|
||||||
(provide read-syntax)
|
(provide read-syntax)
|
||||||
|
|
||||||
(define #'(stacker-module-begin _READER-LINE ...)
|
(define-macro (stacker-module-begin READER-LINE ...)
|
||||||
#'(#%module-begin
|
#'(#%module-begin
|
||||||
_READER-LINE ...
|
READER-LINE ...
|
||||||
(display (first stack))))
|
(display (first stack))))
|
||||||
(provide (rename-out [stacker-module-begin #%module-begin]))
|
(provide (rename-out [stacker-module-begin #%module-begin]))
|
||||||
(provide #%top-interaction)
|
|
||||||
|
|
||||||
(define stack empty)
|
(define stack empty)
|
||||||
(define (push num) (set! stack (cons num stack)))
|
(define (push num) (set! stack (cons num stack)))
|
||||||
(provide push)
|
(provide push)
|
||||||
|
|
||||||
(define (dispatch arg-1 [arg-2 #f])
|
(define-cases dispatch
|
||||||
(cond
|
[(_ push num) (push num)]
|
||||||
[(number? arg-2) (push arg-2)]
|
[(_ op) (define op-result (op (first stack) (second stack)))
|
||||||
[else
|
(set! stack (cons op-result (drop stack 2)))])
|
||||||
(define op arg-1)
|
|
||||||
(define op-result (op (first stack) (second stack)))
|
|
||||||
(set! stack (cons op-result (drop stack 2)))]))
|
|
||||||
(provide dispatch)
|
(provide dispatch)
|
||||||
|
|
||||||
(provide + *)
|
(provide + * #%app #%datum #%top-interaction)
|
||||||
(provide #%app #%datum)
|
|
Loading…
Reference in New Issue