Skip to content

Instantly share code, notes, and snippets.

@mmis1000
Created April 27, 2018 16:05
Show Gist options
  • Select an option

  • Save mmis1000/a1b6960f8274a37b4755ead90679c5f6 to your computer and use it in GitHub Desktop.

Select an option

Save mmis1000/a1b6960f8274a37b4755ead90679c5f6 to your computer and use it in GitHub Desktop.
write brainfuck intepreter with recursion
const recurr = (fn => fn(
(f => f(f))
(self =>
x =>
fn(self(self))(x)
)
))
recurr(
main =>
actions =>
branch =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => {
// console.log(branch, codePtr, code[codePtr], bufferPtr, buffer[bufferPtr], depth);
actions[branch]
(main)(actions)
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(untilDepth)
}
)
({
"code_act": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => {
if (actions[code[codePtr]]) {
main(actions)(code[codePtr])
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(untilDepth)
}
},
">": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => (
main(actions)("code_next")
(buffer)(bufferPtr + 1)
(code)(codePtr)
(depth)(untilDepth)
),
"<": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => (
main(actions)("code_next")
(buffer)(bufferPtr - 1)
(code)(codePtr)
(depth)(untilDepth)
),
"+": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => (
buffer[bufferPtr]++,
main(actions)("code_next")
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(untilDepth)
),
"-": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => (
buffer[bufferPtr]--,
main(actions)("code_next")
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(untilDepth)
),
"[": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => (
buffer[bufferPtr] === 0 ?
main(actions)("code_next_until")
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(depth) :
main(actions)("code_next")
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(depth)
),
"]": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => (
buffer[bufferPtr] !== 0 ?
main(actions)("code_prev_until")
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(depth) :
main(actions)("code_next")
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(depth)
),
".": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => (
process.stdout.write(String.fromCharCode(buffer[bufferPtr])),
main(actions)("code_next")
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(depth)
),
",": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => {
throw new Error('not implement');
buffer[bufferPtr] = prompt('input char').charCodeAt(0);
main(actions)("code_next")
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(depth)
},
"code_next": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => (
main(actions)("code_act")
(buffer)(bufferPtr)
(code)(codePtr + 1)
(depth +
(code[codePtr] === "[") -
(code[codePtr + 1] === "]")
)(untilDepth)
),
"code_next_until": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => {
do {
depth = depth +
(code[codePtr] === "[") -
(code[codePtr + 1] === "]");
codePtr++
} while (depth !== untilDepth)
main(actions)("code_act")
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(untilDepth)
},
"code_prev_until": main =>
actions =>
buffer => bufferPtr =>
code => codePtr =>
depth => untilDepth => {
do {
depth = depth -
(code[codePtr - 1] === "[") +
(code[codePtr] === "]");
codePtr--
} while (depth !== untilDepth)
main(actions)("code_act")
(buffer)(bufferPtr)
(code)(codePtr)
(depth)(untilDepth)
},
})
('code_act')
(new Uint8Array(256))(0)
('++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.')(0)
(0)(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment