Created
January 24, 2023 01:10
-
-
Save user21944/cef03086a8284ee943bcc60bb3788494 to your computer and use it in GitHub Desktop.
brainfuck vscript
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
const src = @" | |
>++++++++[-<+++++++++>]<.>>+>-[+]++>++>+++[>[->+++<<+++>]<<]>-----.>-> | |
+++..+++.>-.<<+[>[+>+]>>]<--------------.>>.+++.------.--------.>+.>+. | |
"; | |
const MEMSIZE = 0x7FFF; // using n^2-1 to enable wrapping without modulo | |
const CELLSIZE = 0xFF; // 0xFF 8-bit, 0xFFFF 16-bit, 0xFFFFFFFF 32-bit | |
const INSTRCOUNT = 2048; | |
local mem = array(MEMSIZE + 1, 0); // add one to account for zero index | |
local pc = 0, ptr = 0, instr, bcnt; | |
function execute(instrcount) { | |
for (local i = 0; i < instrcount; ++i) { | |
if (pc >= src.len()) { | |
self.Kill(); | |
break; | |
} | |
instr = src[pc]; | |
if (instr == '>') ptr = (ptr + 1) & MEMSIZE; | |
if (instr == '<') ptr = (ptr - 1) & MEMSIZE; | |
if (instr == '+') mem[ptr] = (mem[ptr] + 1) & CELLSIZE; | |
if (instr == '-') mem[ptr] = (mem[ptr] - 1) & CELLSIZE; | |
if (instr == '[' && mem[ptr] == 0) { | |
bcnt = 1 | |
while (bcnt > 0) { | |
pc += 1 | |
if (src[pc] == '[') bcnt += 1 | |
if (src[pc] == ']') bcnt -= 1 | |
} | |
} | |
if (instr == ']' && mem[ptr] != 0) { | |
bcnt = 1 | |
while (bcnt > 0) { | |
pc -= 1 | |
if (src[pc] == ']') bcnt += 1 | |
if (src[pc] == '[') bcnt -= 1 | |
} | |
} | |
if (instr == '.') put(mem[ptr]); | |
if (instr == ',') mem[ptr] = 0; // unimplemented | |
++pc; | |
} | |
} | |
function bfinit() { | |
local brainfuck = SpawnEntityFromTable("info_target", {targetname = "brainfuck"}) | |
if (brainfuck.ValidateScriptScope()) { | |
brainfuck.GetScriptScope()["Think"] <- function() { | |
execute(INSTRCOUNT); | |
return 0.000001; | |
} | |
} | |
AddThinkToEnt(brainfuck, "Think"); | |
} | |
function put(c) { | |
Msg(c.tochar()); | |
} | |
bfinit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment