Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions brainfuck.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@
import getch

def execute(filename):
"""[summary]
Loads the source code from the file and delivers it to the parser.

Arguments:
filename {[str]} -- [path of the brainfuck source code]
"""

f = open(filename, "r")
evaluate(f.read())
f.close()


def evaluate(code):
"""[summary]
This function is the parser for the brainfuck code.

Arguments:
code {[str]} -- [source code (not analysed)]
"""

code = cleanup(list(code))
bracemap = buildbracemap(code)

Expand Down Expand Up @@ -45,10 +59,30 @@ def evaluate(code):


def cleanup(code):
"""[summary]
Simple scanner for the brainfuck source code.

Arguments:
code {[str]} -- [source code]

Returns:
[str] -- [analysed code]
"""

return ''.join(filter(lambda x: x in ['.', ',', '[', ']', '<', '>', '+', '-'], code))


def buildbracemap(code):
"""[summary]
Generates a map for the start and end addresses of the loops.

Arguments:
code {[str]} -- [analysed code]

Returns:
[dictionary] -- [simple map for the loops.]
"""

temp_bracestack, bracemap = [], {}

for position, command in enumerate(code):
Expand All @@ -61,6 +95,10 @@ def buildbracemap(code):


def main():
"""[summary]
simple main program
"""

if len(sys.argv) == 2: execute(sys.argv[1])
else: print("Usage:", sys.argv[0], "filename")

Expand Down