Skip to content

Commit b5254fc

Browse files
committed
Python Shebang: Initial commit
1 parent 81b8fb7 commit b5254fc

File tree

8 files changed

+879
-0
lines changed

8 files changed

+879
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[build-system]
2+
requires = ["setuptools>=64.0.0", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "brainf"
7+
version = "1.0.0"
8+
9+
[project.scripts]
10+
brainf = "brainf:main"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env brainf
2+
3+
>>>>++>+[
4+
[
5+
>[>>]<[>+>]<<[>->>+<<<-]>+[
6+
[+>>[<<+>>-]>]+[-<<+<]>-[
7+
-[<+>>+<-]++++++[>++++++++<-]+>.[-]<<[
8+
>>>[[<<+>+>-]>>>]<<<<[[>+<-]<-<<]>-
9+
]>>>[
10+
<<-[<<+>>-]<+++++++++<[
11+
>[->+>]>>>[<<[<+>-]>>>+>>[-<]<[>]>+<]<<<<<<-
12+
]>[-]>+>>[<<<+>>>-]>>>
13+
]<<<+[-[+>>]<<<]>[<<<]>
14+
]>>>[<[>>>]<<<[[>>>+<<<-]<<<]>>>>>>>-[<]>>>[<<]<<[>+>]<]<<
15+
]++>>
16+
]<<++++++++.+
17+
]
18+
19+
This program computes the factorials (https://oeis.org/A000142). Because this
20+
sequence is infinitely long, this program doesn't terminate on its own; you will
21+
have to kill it. This program is much faster than my earlier factorial program.
22+
23+
[factorial2.b -- compute factorials
24+
(c) 2019 Daniel B. Cristofani
25+
http://brainfuck.org/]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env brainf
2+
3+
>++++++++++>+>+[
4+
[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[
5+
[-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
6+
[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>
7+
]<<<
8+
]
9+
10+
This program doesn't terminate; you will have to kill it.
11+
Daniel B Cristofani (cristofdathevanetdotcom)
12+
http://www.hevanet.com/cristofd/brainfuck/

python-shebang/interpreter/scripts/hanoi.b

Lines changed: 711 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env brainf
2+
# Hello world taken from Wikipedia.org
3+
4+
++++++++ Set Cell #0 to 8
5+
[
6+
>++++ Add 4 to Cell #1; this will always set Cell #1 to 4
7+
[ as the cell will be cleared by the loop
8+
>++ Add 2 to Cell #2
9+
>+++ Add 3 to Cell #3
10+
>+++ Add 3 to Cell #4
11+
>+ Add 1 to Cell #5
12+
<<<<- Decrement the loop counter in Cell #1
13+
] Loop until Cell #1 is zero; number of iterations is 4
14+
>+ Add 1 to Cell #2
15+
>+ Add 1 to Cell #3
16+
>- Subtract 1 from Cell #4
17+
>>+ Add 1 to Cell #6
18+
[<] Move back to the first zero cell you find; this will
19+
be Cell #1 which was cleared by the previous loop
20+
<- Decrement the loop Counter in Cell #0
21+
] Loop until Cell #0 is zero; number of iterations is 8
22+
23+
The result of this is:
24+
Cell no : 0 1 2 3 4 5 6
25+
Contents: 0 0 72 104 88 32 8
26+
Pointer : ^
27+
28+
>>. Cell #2 has value 72 which is 'H'
29+
>---. Subtract 3 from Cell #3 to get 101 which is 'e'
30+
+++++++..+++. Likewise for 'llo' from Cell #3
31+
>>. Cell #5 is 32 for the space
32+
<-. Subtract 1 from Cell #4 for 87 to give a 'W'
33+
<. Cell #3 was set to 'o' from the end of 'Hello'
34+
+++.------.--------. Cell #3 for 'rl' and 'd'
35+
>>+. Add 1 to Cell #5 gives us an exclamation point
36+
>++. And finally a newline from Cell #6
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env brainf
2+
,
3+
[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
4+
[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
5+
[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
6+
[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
7+
[>++++++++++++++<-
8+
[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
9+
[>>+++++[<----->-]<<-
10+
[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
11+
[>++++++++++++++<-
12+
[>+<-[>+<-[>+<-[>+<-[>+<-
13+
[>++++++++++++++<-
14+
[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
15+
[>>+++++[<----->-]<<-
16+
[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
17+
[>++++++++++++++<-
18+
[>+<-]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
19+
]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]>.[-]<,]
20+
21+
of course any function char f(char) can be made easily on the same principle
22+
23+
[Daniel B Cristofani (cristofdathevanetdotcom)
24+
http://www.hevanet.com/cristofd/brainfuck/]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env brainf
2+
3+
++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[
4+
-<<<[
5+
->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
6+
]>.>+[>>]>+
7+
]
8+
9+
[Shows an ASCII representation of the Sierpinski triangle
10+
(iteration 5).]
11+
12+
[sierpinski.b -- display Sierpinski triangle
13+
(c) 2016 Daniel B. Cristofani
14+
http://brainfuck.org/]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import fileinput
2+
import sys
3+
4+
5+
def main() -> None:
6+
with fileinput.input(encoding="utf-8") as file:
7+
interpret("".join(line for line in file if not line.startswith("#")))
8+
9+
10+
def interpret(code: str, memory_size: int = 2**15) -> None:
11+
memory, jumps = [0] * memory_size, find_matching_brackets(code)
12+
ip = ptr = 0
13+
while ip < len(code):
14+
match code[ip]:
15+
case ">":
16+
ptr += 1
17+
case "<":
18+
ptr -= 1
19+
case "+":
20+
memory[ptr] += 1
21+
case "-":
22+
memory[ptr] -= 1
23+
case ".":
24+
print(chr(memory[ptr]), end="")
25+
case ",":
26+
memory[ptr] = ord(c) if (c := sys.stdin.read(1)) else 0
27+
case "[":
28+
if memory[ptr] == 0:
29+
ip = jumps[ip]
30+
case "]":
31+
if memory[ptr] != 0:
32+
ip = jumps[ip]
33+
ip += 1
34+
35+
36+
def find_matching_brackets(code: str) -> dict[int, int]:
37+
jumps, stack = {}, []
38+
for i, instruction in enumerate(code):
39+
if instruction == "[":
40+
stack.append(i)
41+
elif instruction == "]":
42+
jumps[i], jumps[j] = (j := stack.pop()), i
43+
return jumps
44+
45+
46+
if __name__ == "__main__":
47+
main()

0 commit comments

Comments
 (0)