-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.py
83 lines (77 loc) · 3.21 KB
/
brainfuck.py
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
76
77
78
79
80
81
82
83
# Brainfuck/brainfuck.py
# From Computer Science from Scratch
# Copyright 2024 David Kopec
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pathlib import Path
class Brainfuck:
def __init__(self, file_name: str | Path):
# Open text file and store in instance variable
with open(file_name, "r") as text_file:
self.source_code: str = text_file.read()
def execute(self):
# Setup state
cells: list[int] = [0] * 30000
cell_index = 0
instruction_index = 0
# Keep going as long as there are potential instructions left
while instruction_index < len(self.source_code):
instruction = self.source_code[instruction_index]
match instruction:
case ">":
cell_index += 1
case "<":
cell_index -= 1
case "+":
cells[cell_index] = clamp0_255_wraparound(cells[cell_index] + 1)
case "-":
cells[cell_index] = clamp0_255_wraparound(cells[cell_index] - 1)
case ".":
print(chr(cells[cell_index]), end='', flush=True)
case ",":
cells[cell_index] = clamp0_255_wraparound(int(input()))
case "[":
if cells[cell_index] == 0:
instruction_index = self.find_bracket_match(instruction_index, True)
case "]":
if cells[cell_index] != 0:
instruction_index = self.find_bracket_match(instruction_index, False)
instruction_index += 1
# Find the location of the corresponding bracket to the one at *start*
# If *forward* is true go to the right looking for a matching "]"
# Otherwise do the reverse
def find_bracket_match(self, start: int, forward: bool) -> int:
in_between_brackets = 0
direction = 1 if forward else -1
location = start + direction
start_bracket = "[" if forward else "]"
end_bracket = "]" if forward else "["
while 0 <= location < len(self.source_code):
if self.source_code[location] == end_bracket:
if in_between_brackets == 0:
return location
in_between_brackets -= 1
elif self.source_code[location] == start_bracket:
in_between_brackets += 1
location += direction
# Didn't find a match
print(f"Error: could not find match for {start_bracket} at {start}.")
return start
# Simulate a 1 byte unsigned integer
def clamp0_255_wraparound(num: int) -> int:
if num > 255:
return 0
elif num < 0:
return 255
else:
return num