4 This plugin allows you to use the toy programming language known
5 as "Brainfuck", which is turing complete with only 8 commands:
7 > increment pointer into data array
8 < decrement the data pointer
9 + increment the byte at the pointer
10 - decrement the data byte
11 . output the byte at the pointer
12 , input a byte, store it into the data array
13 [ starts a code block, only run if current data byte is zero
14 ] loop back to the opening bracket
18 ++>++++++++++[-<+++++++>]<.+>+++++++[-<++++>]<.++>++
19 +++[-<+>]<..+++.------>+++++++++[-<-------->]<.
22 <?plugin-input BrainFuck ....code.... ?>
24 You must first create a symlink in the mpi/ main directory to
25 here, else this can't be used (disabled because it could slow
30 $ewiki_plugins["mpi"]["brainfuck"] = "ewiki_mpi_brainfuck";
31 function ewiki_mpi_brainfuck($action, $args, &$iii, &$s)
35 if ($action == "input") {
36 return('<form action="'.ewiki_script("", $ewiki_id).'">'
37 .'<input name="i" type="text"> '
38 .'<input type="hidden" name="id" value="'.htmlentities($ewiki_id).'">'
39 .'<input type="submit"></form>'
44 foreach ($_REQUEST as $i=>$str) {
49 return(bf_exec($args["_"], $input));
54 function bf_exec(&$code, $in)
59 $in_len = strlen($in);
61 $code_len = strlen($code);
64 while ($pc < $code_len) {
65 switch ($c=$code{$pc}) {
67 case ">": // increment data pointer
69 if ($p >= count($data)) {
74 case "<": // decrement data pointer
80 case "+": // inc data byte
84 case "-": // dec data byte
89 $out .= chr($data[$p]);
93 if ($in_p < $in_len) {
94 $data[$p] = ord($in[$in_p++]);
101 case "[": // skip loop, if data byte is zero
102 if ($data[$p] == 0) {
138 $out .= " (PARSING ERROR '$c' at position #$pc)";