BrainFck¶
-
pybrainfuck.
pybrainfuck
()¶ Runs a BrainF*ck Machine with command line arguments
-
pybrainfuck.
BfCommand
(cmd)¶ Decorates the function with
_cmd
attribute taken from the arg
-
class
pybrainfuck.
BrainFck
(**kwargs)¶ BrainF*ck interpreter/virtual machine
- The machine is configurable with following kwargs:
cellsize (default: 8) Size in bits of the numeric type to emulate. This types rollover when crossing the 0 and pow(2, cellsize) boundaries
totalcells (default: 30000) Size in cells in the virtual machine. Literature shows the default value to be expected by many test scripts
Set it to
0
for unbounded sizeextleft (defaults: True): Allow extension to the left. This applies only whilst the array has not reached its full size (if set)
prealloc (default: False) If the number of cells is limited, whether to preallocate them
wrapover (default: False) Wrap over cells boundaries if totalcells is set and all cells have been allocated (by preallocating or because the
totalcells
limit has been reached)debug (default: False) Print the status and command to be evaluated
linemode (default: False) Read the input in lines and interpret each line as a program skipping blank lines
multiline (default: False) In
linemode
join lines until a blank line is seencomments (default: False) In
linemode
skip lines starting withcommentchar
commentchar (default: #) Comment charachter for
comments
inlinemode
breakline (default: False) Print a breakline in between the output of the execution of multiple programs
- Input/Output:
Controlled also through configuration variables
- fin (default: sys.stdin) Stream from which input will be read
- fout (default: sys.stdout) Stream to which input will be printed
- fdebug (default: fout) Stream to print debug messages to
- flushout (default: False) Flush out each write (including debug)
- Cells/Memory:
cells: access to the array of memory cells
The machine checks boundaries and will not go below 0 or above the maximum total number of cells configured
bfnum: class holding the numeric type with the configure bitsize for this machine.
- Command Processing:
- cmd_procs (dict): holds a reference per command to a method to process the command. Automatically filled with methods which have been decorated with BfCommand
- Command index:
- idx (start: -1) Current command index. Increased when a command is read
- Constants:
- maxptr Holds the right limit in cells of the virtual machine
- csize Length of the numeric value (power of 2 of cellsize)
- Status:
- cmd (start: ‘’) Current command being processed
- ptr (start: 0) Current cell for get/set/check operations
- loopskip (start: 0) if > 0, it will skip commands until the matching amount of ‘]’ has been seen. While positive, seeing a ‘[‘ will increase its value by 1
- loops (start: []) Stores index in input of ‘[‘ commands until they must be skipped
- loopback (start: -1) If >= 0, the value indicates a jump to such command index
Commands:
Methods decorated with
BfCommand(cmd)
will be added to a dictionarycmd_procs
usingcmd
as the key.Method retrieval when a command is read (unless commands are being skipped inside a loop) will be done using the dictionary
The commands manipulate the
Status
variablesDecoration allows for easy addition of new commands
Internally
None
is used as a virtual NOP command when the commands inside a loop have to be skippedCommands are deliberately kept as simple as possible in that they don’t do any memory management or command index manipulation. If such an action may be needed it will tackled by the main loop using the information in the status variables modified by the commands
-
__init__
(**kwargs)¶ Initializes the machine (not to confuse with resetting the status)
- It prepares the dictionary of commands
- It configures the “configuration” variables
- It sets the constants for maximum pointer length and cell allocation
- Configures the numeric class
- Prepares the input/output streams
-
reset
()¶ Resets the virtual machine to the default status
-
writeout
(*args)¶
-
debug_out
(*args)¶
-
debug_config
()¶ Prints config debug information
-
debug_status
()¶ Prints status debug information
-
runfiles
(*args)¶ It opens
filenames
(args) in read/binary mode to get an unprocessed stream of bytes and executes the program(s)Parameters: args – paths to files
-
run
(f)¶ Runs a BrainF*ck program
Parameters: f – file (like) object or string If a string is passed it will be internally converted to a file like object In non
linemode
the contents of the file will be executed as a single scriptIf in
linemode
the file will be read line by line (skipping empty lines)If in
multiline
non-empty lines will be joined until a blank line is seenIf, additionally, in
comments
mode, then lines starting with thecommentschar
character will also be skipped
-
execute
(f)¶ Actual execution of the BrainF*ck program
Parameters: f – file (like) object - The machine is “reset” at the beginning and in each loop
Cell (append) memory management is done in dynamic mode (if needed) - To the right if going over the limit - To the left if going below the 0 mark
Jumps in program text are performed if needed
Next command is fetched
On “no command” (EOF) the loop is exited
If a command processor exists for the command it is fetched - In case a loop has to be skipped the command processor is fetched
using the internal NOP command (None)
If any command processor has been fetched is invoked
If internal numerics are in used, do an overflow check on the cell
-
mmu_init
()¶
-
proc_loopskip
()¶ Skips commands until the next closing loop command is found
Loop commands ‘[‘ and ‘]’ seen while looping will be skipped by adding them and substracting them from the loopskip count
-
proc_increment
()¶ Increments by one the value of the current cell
-
proc_decrement
()¶ Decrements by one the value of the current cell
-
proc_whilebegin
()¶ If the current cell is 0, increases the loopskip counter to skip the current loop.
Otherwise it marks the position in the command index seen
-
proc_whileend
()¶ If the current cell is 0, it removes the entry for the loop start to simply carry on
Otherwise it sets loopback to the command index to jump to
-
proc_forward
()¶ Increments cell pointer by one
-
proc_backwards
()¶ Decrements cell pointer by one
-
proc_output
()¶ Outputs in char format the value of the current cell
-
proc_input
()¶ Reads input in char format the value of the current cell