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