fuckery.vm module¶
pyFuckery - vm.py Created on 2/12/17.
VM Definition to execute brainfuck programs which have been parsed into lark.Tree objects.
-
class
fuckery.vm.
VirtualMachine
(memory_size: int = 30000, loop_detection: bool = False)[source]¶ Bases:
object
This is the brainfuck VM. You can drop this into programs that need a brainfuck VM, such as a module you don’t want a coworker to ever easily maintain, or a really cruel programming based game.
Init function for the VM.
Parameters: - memory_size – Number of memory cells to instantiate.
- loop_detection – Enables loop detection if this evaluates to True. This is very costly from a
computation perspective, so use it wisely.
-
current_value
¶ Property which represents the data value the current memory address points too.
Returns:
-
dec_data_value
() → None[source]¶ Decrements the value pointed to by the data pointer by 1. This wraps at zero, back to 255.
Returns: None
-
inc_data_value
() → None[source]¶ Increments the value pointed to by the data pointer by 1. This wraps at 255, back to zero.
Returns: None
-
io_input
() → None[source]¶ Reads a single character from self.stream_in.
If self.stream_in is sys.stdin (default value), it will prompt the user for a string and record the FIRST byte of that string. Otherwise, it will attempt to read 1 byte from the stream_in buffer.
Empty inputs have no effect on the state of the system.
Returns: None
-
io_output
() → None[source]¶ Writes the current value, after casting it via chr(), to self.stream_out.
Returns: None
-
parse_and_run
(program: str) → None[source]¶ Parse and run a brainfuck program.
Parameters: program – String representing a brainfuck program. Returns: None
-
run
(tree: lark.tree.Tree) → None[source]¶ Walk a Brainfuck AST and execute the program contained in the AST.
This function is recursive, so its possible for a deeply nested program to hit the Python interpreter recursion limit, but if your brainfuck does that, kudos to you.
Parameters: tree – Parsed brainfuck program. Returns:
-
state_hash
¶ MD5 representing the state of the system. It is a hash of the memory and the current data pointer.
Note - Computing this frequently can be expensive to do with a large memory section, as the memory section is serialized via msgpack.dumps() prior to hashing.
Returns: