-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToBrainfuck.php
More file actions
75 lines (66 loc) · 2.54 KB
/
StringToBrainfuck.php
File metadata and controls
75 lines (66 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* StringToBrainfuck.php
*
* Converts a string to a Brainfuck script that will output said string.
* usage: php StringToBrainfuck.php your string here
*
* @author Maxamilian Demian <max@maxdemian.com>
* @link https://www.maxodev.org
* @link https://github.com/Maxoplata/StringToBrainfuck
*/
// if we have arguments passed to the script
if (count($argv) > 1) {
$inputString = implode(' ', array_splice($argv, 1));
// the Brainfuck code we will output in the end
$bfCode = '';
/**
* our current location on the "tape" (pointer 1).
* we use pointer 0 as a multiplier for pointer 1 to shorten the output script.
*
* e.g.
* A(65) = ++++++[>++++++++++<-]>+++++.
* ++++++ = add 6 to current pointer value (pointer 0)
* [ = while current pointer (pointer 0)'s value > 0
* > = move pointer ahead one (to pointer 1)
* ++++++++++ = add 10 to current pointer value (pointer 1)
* < = move pointer back one (to pointer 0)
* - = subtract 1 from current pointervalue (pointer 0)
* ] = end while loop
* > = move pointer ahead one (to pointer 1)
* +++++ = add 5 to current pointer value (pointer 1)
* . = print out character at current pointer value (pointer 1, value 65, char 'A')
*
* instead of:
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
*/
$currentLocation = 0;
// iterate through each character in the string
for ($i = 0; $i < strlen($inputString); $i++) {
// get the Unicode code for the current character
$charVal = ord($inputString[$i]);
if ($charVal > $currentLocation) {
// move ahead on the "tape" to build the character
$bfCode .= str_repeat('+', floor(($charVal - $currentLocation) / 10));
$bfCode .= '[>++++++++++<-]>';
$bfCode .= str_repeat('+', ($charVal - $currentLocation) % 10);
} elseif ($charVal < $currentLocation) {
// move backwards on the "tape" to build the character
$bfCode .= str_repeat('+', floor(($currentLocation - $charVal) / 10));
$bfCode .= '[>----------<-]>';
$bfCode .= str_repeat('-', ($currentLocation - $charVal) % 10);
} else {
// delete the "<" from the previous command as we are on the same character
// and we will want to print it out again
$bfCode = substr($bfCode, 0, -1);
}
// print out the current character
$bfCode .= '.';
// if we are not on the last letter of the string, move pointer position back to 0
if ($i < (strlen($inputString) - 1)) {
$bfCode .= '<';
}
$currentLocation = $charVal;
}
print $bfCode . PHP_EOL;
}