Skip to content

Instantly share code, notes, and snippets.

@dockimbel
Created November 29, 2013 23:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dockimbel/7713170 to your computer and use it in GitHub Desktop.
Save dockimbel/7713170 to your computer and use it in GitHub Desktop.
This is a complete (but not fully tested) implementation of a Brainfuck interpreter in Red language, only using the Parse dialect. The extra verbosity comes from the current lack of built-in reversed input parsing. Once reverse parsing will be implemented in the dialect, the `jump-back` rule should be simplified a lot. Note: this is not meant to…
Red [
Author: "Nenad Rakocevic"
Date: 29/11/2013
]
bf: function [prog [string!]][
size: 30000
cells: make string! size
append/dup cells null size
one-back: [pos: (pos: back pos) :pos]
jump-back: [
one-back
any [
one-back
["]" jump-back "[" | "[" resume: break | skip]
one-back
]
]
cmd: complement charset "[]"
nested: [any cmd | "[" nested "]"]
brainfuck: [
some [
">" (cells: next cells)
| "<" (cells: back cells)
| "+" (cells/1: cells/1 + 1)
| "-" (cells/1: cells/1 - 1)
| "." (prin cells/1)
| "," (cells/1: first input "")
| "[" [if (cells/1 = null) nested "]" | none]
| "]" [pos: if (cells/1 <> null) jump-back :resume | none]
| skip
]
]
parse prog brainfuck
]
; Print Hello World! in brainfuck
bf {
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.
>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment