aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2024-11-19 09:47:46 -0500
committerGravatar Peter McGoron 2024-11-19 09:47:46 -0500
commit3a716bb7004af068f46b67cfa7b8a033931e151b (patch)
tree5067fea9dae6af0261c7c98dfe079e076611cecc
parentbrainfuck->scheme (diff)
debugging
-rw-r--r--README.md14
-rw-r--r--bf2s.scm11
2 files changed, 20 insertions, 5 deletions
diff --git a/README.md b/README.md
index c507d0c..3036594 100644
--- a/README.md
+++ b/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
diff --git a/bf2s.scm b/bf2s.scm
index 60def3e..b865c70 100644
--- a/bf2s.scm
+++ b/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))))