Last active
April 27, 2023 23:20
-
-
Save lennyerik/844732503e0a3d0de37a7b7fe6a2fcf8 to your computer and use it in GitHub Desktop.
Minimal Brainfuck interpreter in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(DescTools) | |
progstr <- ">++++++++[<+++++++++>-]<.>++++[<+++++++>-]<+.+++++++..+++.>>++++++[<+++++++>-]<++.------------.>++++++[<+++++++++>-]<+.<.+++.------.--------.>>>++++[<++++++++>-]<+." | |
prog <- unlist(strsplit(progstr, "")) | |
cells <- rep(0, times=30) | |
pointer <- 1 | |
charIdx <- 1 | |
loopIdxs <- list() | |
while (charIdx <= length(prog)) { | |
char <- prog[charIdx] | |
if (char == "+") { | |
cells[pointer] <- cells[pointer] + 1 | |
} else if (char == "-") { | |
cells[pointer] <- cells[pointer] - 1 | |
} else if (char == ">") { | |
pointer <- pointer + 1 | |
} else if (char == "<") { | |
pointer <- pointer - 1 | |
} else if (char == ".") { | |
cat(AscToChar(cells[pointer])) | |
} else if (char == "[") { | |
loopIdxs <- append(loopIdxs, charIdx) | |
} else if (char == "]") { | |
if (cells[pointer] == 0) { | |
loopIdxs = head(loopIdxs, -1) | |
} else { | |
charIdx = tail(loopIdxs, n=1)[[1]] | |
} | |
} | |
charIdx <- charIdx + 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Standalone version (without dependency on DescTools) | |
asciitable = c("", "", "", "", "", "", "", "", "", "\t", "\n", "\x0b", "\x0c", "\r", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "") | |
progstr <- ">++++++++[<+++++++++>-]<.>++++[<+++++++>-]<+.+++++++..+++.>>++++++[<+++++++>-]<++.------------.>++++++[<+++++++++>-]<+.<.+++.------.--------.>>>++++[<++++++++>-]<+." | |
prog <- unlist(strsplit(progstr, "")) | |
cells <- rep(0, times=30) | |
pointer <- 1 | |
charidx <- 1 | |
loopidxs <- list() | |
while (charidx <= length(prog)) { | |
char <- prog[charidx] | |
if (char == "+") { | |
cells[pointer] <- cells[pointer] + 1 | |
} else if (char == "-") { | |
cells[pointer] <- cells[pointer] - 1 | |
} else if (char == ">") { | |
pointer <- pointer + 1 | |
} else if (char == "<") { | |
pointer <- pointer - 1 | |
} else if (char == ".") { | |
cat(asciitable[cells[pointer]+1]) | |
} else if (char == "[") { | |
loopidxs <- append(loopidxs, charidx) | |
} else if (char == "]") { | |
if (cells[pointer] == 0) { | |
loopidxs = head(loopidxs, -1) | |
} else { | |
charidx = tail(loopidxs, n=1)[[1]] | |
} | |
} | |
charidx <- charidx + 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment