;;; Brainfuck for GNU Guile ;; Copyright (C) 2009 Free Software Foundation, Inc. ;; This library is free software; you can redistribute it and/or ;; modify it under the terms of the GNU Lesser General Public ;; License as published by the Free Software Foundation; either ;; version 3 of the License, or (at your option) any later version. ;; ;; This library is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; Lesser General Public License for more details. ;; ;; You should have received a copy of the GNU Lesser General Public ;; License along with this library; if not, write to the Free Software ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ;;; Code: (define-module (language brainfuck compile-scheme) #:export (compile-scheme)) ;; Compilation of Brainfuck to Scheme is pretty straight-forward. For all of ;; brainfuck's instructions, there are basic representations in Scheme we ;; only have to generate. ;; ;; Brainfuck's pointer and data-tape are stored in the variables pointer and ;; tape, where tape is a vector of integer values initially set to zero. Pointer ;; starts out at position 0. ;; Our tape is thus of finite length, with an address range of 0..n for ;; some defined upper bound n depending on the length of our tape. ;; Define the length to use for the tape. (define tape-size 30000) ;; This compiles a whole brainfuck program. This constructs a Scheme code like: ;; (let ((pointer 0) ;; (tape (make-vector tape-size 0))) ;; (begin ;;
;; (write-char #\newline))) ;; ;; So first the pointer and tape variables are set up correctly, then the ;; program's body is executed in this context, and finally we output an ;; additional newline character in case the program does not output one. ;; ;; TODO: Find out and explain the details about env, the three return values and ;; how to use the options. Implement options to set the tape-size, maybe. (define (compile-scheme exp env opts) (values `(let ((pointer 0) (tape (make-vector ,tape-size 0))) ,@(if (not (eq? '