Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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