-
Notifications
You must be signed in to change notification settings - Fork 0
/
expander.rkt
46 lines (30 loc) · 1.18 KB
/
expander.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#lang br/quicklang
(define-macro (bf-module-begin PARSE-TREE)
#'(#%module-begin
PARSE-TREE))
(provide (rename-out [bf-module-begin #%module-begin]))
(define-macro (bf-program OP-OR-LOOP-ARG ...)
#'(void OP-OR-LOOP-ARG ...))
(provide bf-program)
(define-macro (bf-loop "[" OP-OR-LOOP-ARG ... "]")
#'(until (zero? (current-byte))
OP-OR-LOOP-ARG ...))
(provide bf-loop)
(define-macro-cases bf-op
[(bf-op ">") #'(next)]
[(bf-op "<") #'(prev)]
[(bf-op "+") #'(inc)]
[(bf-op "-") #'(dec)]
[(bf-op ".") #'(egress)]
[(bf-op ",") #'(ingress)])
(provide bf-op)
(define arr (make-vector 30000 0))
(define ptr 0)
(define (current-byte) (vector-ref arr ptr))
(define (set-current-byte! val) (vector-set! arr ptr val))
(define (next) (set! ptr (+ ptr 1)))
(define (prev) (set! ptr (- ptr 1)))
(define (inc) (set-current-byte! (+ (current-byte) 1)))
(define (dec) (set-current-byte! (- (current-byte) 1)))
(define (egress) (write-byte (current-byte)))
(define (ingress) (set-current-byte! (read-byte)))