PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/module/language/brainfuck/compile-tree-il.scm

#
Scheme | 184 lines | 62 code | 22 blank | 100 comment | 0 complexity | f210ab2938f1e4773d152dd6bf39e8a1 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, GPL-2.0
  1. ;;; Brainfuck for GNU Guile
  2. ;; Copyright (C) 2009, 2011 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. ;; 02110-1301 USA
  17. ;;; Commentary:
  18. ;; Brainfuck is a simple language that mostly mimics the operations of a
  19. ;; Turing machine. This file implements a compiler from Brainfuck to
  20. ;; Guile's Tree-IL.
  21. ;;; Code:
  22. (define-module (language brainfuck compile-tree-il)
  23. #:use-module (system base pmatch)
  24. #:use-module (language tree-il)
  25. #:export (compile-tree-il))
  26. ;; Compilation of Brainfuck is pretty straight-forward. For all of
  27. ;; brainfuck's instructions, there are basic representations in Tree-IL
  28. ;; we only have to generate.
  29. ;;
  30. ;; Brainfuck's pointer and data-tape are stored in the variables pointer and
  31. ;; tape, where tape is a vector of integer values initially set to zero. Pointer
  32. ;; starts out at position 0.
  33. ;; Our tape is thus of finite length, with an address range of 0..n for
  34. ;; some defined upper bound n depending on the length of our tape.
  35. ;; Define the length to use for the tape.
  36. (define tape-size 30000)
  37. ;; This compiles a whole brainfuck program. This constructs a Tree-IL
  38. ;; code equivalent to Scheme code like this:
  39. ;;
  40. ;; (let ((pointer 0)
  41. ;; (tape (make-vector tape-size 0)))
  42. ;; (begin
  43. ;; <body>
  44. ;; (write-char #\newline)))
  45. ;;
  46. ;; So first the pointer and tape variables are set up correctly, then the
  47. ;; program's body is executed in this context, and finally we output an
  48. ;; additional newline character in case the program does not output one.
  49. ;;
  50. ;; The fact that we are compiling to Guile primitives gives this
  51. ;; implementation a number of interesting characteristics. First, the
  52. ;; values of the tape cells do not underflow or overflow. We could make
  53. ;; them do otherwise via compiling calls to "modulo" at certain points.
  54. ;;
  55. ;; In addition, tape overruns or underruns will be detected, and will
  56. ;; throw an error, whereas a number of Brainfuck compilers do not detect
  57. ;; this.
  58. ;;
  59. ;; Note that we're generating the S-expression representation of
  60. ;; Tree-IL, then using parse-tree-il to turn it into the actual Tree-IL
  61. ;; data structures. This makes the compiler more pleasant to look at,
  62. ;; but we do lose is the ability to propagate source information. Since
  63. ;; Brainfuck is so obtuse anyway, this shouldn't matter ;-)
  64. ;;
  65. ;; `compile-tree-il' takes as its input the read expression, the
  66. ;; environment, and some compile options. It returns the compiled
  67. ;; expression, the environment appropriate for the next pass of the
  68. ;; compiler -- in our case, just the environment unchanged -- and the
  69. ;; continuation environment.
  70. ;;
  71. ;; The normal use of a continuation environment is if compiling one
  72. ;; expression changes the environment, and that changed environment
  73. ;; should be passed to the next compiled expression -- for example,
  74. ;; changing the current module. But Brainfuck is incapable of that, so
  75. ;; for us, the continuation environment is just the same environment we
  76. ;; got in.
  77. ;;
  78. ;; FIXME: perhaps use options or the env to set the tape-size?
  79. (define (compile-tree-il exp env opts)
  80. (values
  81. (parse-tree-il
  82. `(let (pointer tape) (pointer tape)
  83. ((const 0)
  84. (call (primitive make-vector) (const ,tape-size) (const 0)))
  85. ,(compile-body exp)))
  86. env
  87. env))
  88. ;; Compile a list of instructions to a Tree-IL expression.
  89. (define (compile-body instructions)
  90. (let lp ((in instructions) (out '()))
  91. (define (emit x)
  92. (lp (cdr in) (cons x out)))
  93. (cond
  94. ((null? in)
  95. ;; No more input, build our output.
  96. (cond
  97. ((null? out) '(void)) ; no output
  98. ((null? (cdr out)) (car out)) ; single expression
  99. (else `(begin ,@(reverse out)))) ; sequence
  100. )
  101. (else
  102. (pmatch (car in)
  103. ;; Pointer moves >< are done simply by something like:
  104. ;; (set! pointer (+ pointer +-1))
  105. ((<bf-move> ,dir)
  106. (emit `(set! (lexical pointer)
  107. (call (primitive +) (lexical pointer) (const ,dir)))))
  108. ;; Cell increment +- is done as:
  109. ;; (vector-set! tape pointer (+ (vector-ref tape pointer) +-1))
  110. ((<bf-increment> ,inc)
  111. (emit `(call (primitive vector-set!) (lexical tape) (lexical pointer)
  112. (call (primitive +)
  113. (call (primitive vector-ref)
  114. (lexical tape) (lexical pointer))
  115. (const ,inc)))))
  116. ;; Output . is done by converting the cell's integer value to a
  117. ;; character first and then printing out this character:
  118. ;; (write-char (integer->char (vector-ref tape pointer)))
  119. ((<bf-print>)
  120. (emit `(call (primitive write-char)
  121. (call (primitive integer->char)
  122. (call (primitive vector-ref)
  123. (lexical tape) (lexical pointer))))))
  124. ;; Input , is done similarly, read in a character, get its ASCII
  125. ;; code and store it into the current cell:
  126. ;; (vector-set! tape pointer (char->integer (read-char)))
  127. ((<bf-read>)
  128. (emit `(call (primitive vector-set!)
  129. (lexical tape) (lexical pointer)
  130. (call (primitive char->integer)
  131. (call (primitive read-char))))))
  132. ;; For loops [...] we use a letrec construction to execute the body until
  133. ;; the current cell gets zero. The body is compiled via a recursive call
  134. ;; back to (compile-body).
  135. ;; (let iterate ()
  136. ;; (if (not (= (vector-ref! tape pointer) 0))
  137. ;; (begin
  138. ;; <body>
  139. ;; (iterate))))
  140. ;;
  141. ;; Indeed, letrec is the only way we have to loop in Tree-IL.
  142. ;; Note that this does not mean that the closure must actually
  143. ;; be created; later passes can compile tail-recursive letrec
  144. ;; calls into inline code with gotos. Admittedly, that part of
  145. ;; the compiler is not yet in place, but it will be, and in the
  146. ;; meantime the code is still reasonably efficient.
  147. ((<bf-loop> . ,body)
  148. (let ((iterate (gensym)))
  149. (emit `(letrec (iterate) (,iterate)
  150. ((lambda ()
  151. (lambda-case
  152. ((() #f #f #f () ())
  153. (if (call (primitive =)
  154. (call (primitive vector-ref)
  155. (lexical tape) (lexical pointer))
  156. (const 0))
  157. (void)
  158. (begin ,(compile-body body)
  159. (call (lexical ,iterate)))))
  160. #f)))
  161. (call (lexical ,iterate))))))
  162. (else (error "unknown brainfuck instruction" (car in))))))))