<?php
function interpreter(string $code, int $iterations, int $width, int $height): string {
$dataGrid = [];
$output = '';
$codeOffset = -1;
$skip = 0;
$commands = $code;
$pointer = ['x' => 0, 'y' => 0];
$jmps = new SplStack();
for ($row = 0; $row < $width; ++$row) {
for ($col = 0; $col < $height; ++$col) {
$dataGrid[$col][$row] = '0';
}
}
while (strlen($commands) && $iterations) {
//if (!$skip)
//echo "{$commands} ({$pointer['x']}, {$pointer['y']} = {$dataGrid[$pointer['x']][$pointer['y']]})\n";
++$codeOffset;
$command = $commands[0];
$commands = substr($commands, 1);
if ($skip) {
if ($command === ']') {
$jmps->pop();
var_dump("pop");
--$skip;
} elseif ($command === '[') {
++$skip;
}
continue;
}
//var_dump($command);
switch ($command) {
case 'n':
if (--$pointer['x'] === -1) {
$pointer['x'] = $height - 1;
}
break;
case 'e':
if (++$pointer['y'] === $width) {
$pointer['y'] = 0;
}
break;
case 's':
if (++$pointer['x'] === $height) {
$pointer['x'] = 0;
}
break;
case 'w':
if (--$pointer['y'] === -1) {
$pointer['y'] = $width - 1;
}
break;
case '*':
if ($dataGrid[$pointer['x']][$pointer['y']] === '0') {
echo "{$pointer['x']}, {$pointer['y']} = 1\n";
$dataGrid[$pointer['x']][$pointer['y']] = '1';
} else {
echo "{$pointer['x']}, {$pointer['y']} = 0\n";
$dataGrid[$pointer['x']][$pointer['y']] = '0';
}
break;
case '[':
$jmps->push($codeOffset);
var_dump("push");
if ($dataGrid[$pointer['x']][$pointer['y']] === '0') {
$skip = 1;
}
break;
case ']':
$jmpOffset = $jmps->top();
var_dump("pop");
if ($dataGrid[$pointer['x']][$pointer['y']] === '1') {
$commands = substr($code, $jmpOffset + 1);
$codeOffset = $jmpOffset;
// ++$iterations;
}
break;
default:
continue 2;
}
--$iterations;
}
$rows = [];
foreach ($dataGrid as $row) {
$rows[] = implode('', $row);
}
return implode("\r\n", $rows);
}
var_dump(interpreter("*[es*]", 52, 5, 6));
/*
11000
01100
00110
00011
00001
10000*/
// var_dump(interpreter("*[s[e]*]", 9, 5, 5));
/*10000
10000
00000
00000
00000*/
- Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- 0, 0 = 1
string(4) "push"
1, 1 = 1
string(3) "pop"
2, 2 = 1
string(3) "pop"
3, 3 = 1
string(3) "pop"
4, 4 = 1
string(3) "pop"
5, 0 = 1
string(3) "pop"
0, 1 = 1
string(3) "pop"
1, 2 = 1
string(3) "pop"
2, 3 = 1
string(3) "pop"
3, 4 = 1
string(3) "pop"
4, 0 = 1
string(3) "pop"
5, 1 = 1
string(3) "pop"
0, 2 = 1
string(3) "pop"
string(40) "11100
01100
00110
00011
10001
11000"
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
0, 0 = 1
string(4) "push"
1, 1 = 1
string(3) "pop"
2, 2 = 1
string(3) "pop"
3, 3 = 1
string(3) "pop"
4, 4 = 1
string(3) "pop"
5, 0 = 1
string(3) "pop"
0, 1 = 1
string(3) "pop"
1, 2 = 1
string(3) "pop"
2, 3 = 1
string(3) "pop"
3, 4 = 1
string(3) "pop"
4, 0 = 1
string(3) "pop"
5, 1 = 1
string(3) "pop"
0, 2 = 1
string(3) "pop"
string(40) "11100
01100
00110
00011
10001
11000"
- Output for 5.6.0 - 5.6.40
- Parse error: syntax error, unexpected ':', expecting '{' in /in/s5IdF on line 3
Process exited with code 255.
preferences:
212.5 ms | 401 KiB | 288 Q