debugging
This commit is contained in:
parent
9cf01c72c8
commit
3a716bb700
14
README.md
14
README.md
|
@ -1,13 +1,22 @@
|
|||
# Brainfuck2Scheme
|
||||
|
||||
A simple compiler from Brainfuck to R5RS. The compiler will output a Scheme
|
||||
list that is a lambda with two arguments:
|
||||
list that is a lambda with three arguments:
|
||||
|
||||
* `data`: The data vector.
|
||||
* `dptr`: The initial data pointer.
|
||||
* `debugger`: Debugger function
|
||||
|
||||
To turn the list into an executable Scheme function, just give it to
|
||||
`eval`.
|
||||
`eval`. `(execute scheme len)` will run the Scheme code in `scheme`
|
||||
with a data vector of length `len`.
|
||||
|
||||
This dialect of Brainfuck supports `#` to call the `debugger` procedure
|
||||
with `data` and `dptr` as arguments.
|
||||
|
||||
Since Brainfuck programs become Scheme procedures, you can modularize
|
||||
Brainfuck code and (ab)use the debugger for things like procedure calls
|
||||
and foreign libraries.
|
||||
|
||||
## How It Works
|
||||
|
||||
|
@ -24,6 +33,7 @@ Data access brainfuck instructions are translated like
|
|||
* `<` -> `(set! dptr (- dptr 1))`
|
||||
* `.` -> `(display (vector-ref data dptr))`
|
||||
* `,` -> `(vector-set! data dptr (read-char))`
|
||||
* `#` -> `(debugger data dptr)`
|
||||
|
||||
Branches are trickier. Basically, all code that will be executed in a block
|
||||
is in a lambda. Given `[code...]`, the `code...` will be compiled to a
|
||||
|
|
11
bf2s.scm
11
bf2s.scm
|
@ -44,6 +44,8 @@
|
|||
((#\,) (compile (cdr lst)
|
||||
(cons '(vector-set! data dptr
|
||||
(char->integer (read-char))) ins)))
|
||||
((#\#) (compile (cdr lst)
|
||||
(cons '(debugger data dptr) ins)))
|
||||
((#\[) (let ((rest (compile (cdr lst) '())))
|
||||
(if (not (pair? rest))
|
||||
(error "unmatched [")
|
||||
|
@ -62,7 +64,7 @@
|
|||
(else (compile (cdr lst) ins)))))
|
||||
|
||||
(define (brainfuck->scheme str)
|
||||
`(lambda (data dptr)
|
||||
`(lambda (data dptr debugger)
|
||||
(,(compile (string->list str) '()))))
|
||||
|
||||
(define (brainfuck->scheme-from-file filename)
|
||||
|
@ -74,5 +76,8 @@
|
|||
str
|
||||
(loop (string-append str (read-line port)))))))))
|
||||
|
||||
(define (execute scheme)
|
||||
((eval scheme) (make-vector 4096) 0))
|
||||
(define (execute scheme len)
|
||||
((eval scheme) (make-vector len) 0
|
||||
(lambda (data dptr)
|
||||
(display (list data dptr))
|
||||
(newline))))
|
||||
|
|
Loading…
Reference in New Issue