|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +from character_set import (CHARACTER_SET_SIZE, CHARACTER_TO_VALUE, |
| 6 | + VALUE_TO_CHARACTER) |
| 7 | + |
| 8 | + |
| 9 | +MEMORY_SIZE = 30000 |
| 10 | + |
| 11 | + |
| 12 | +class SegmentationFault(Exception): |
| 13 | + pass |
| 14 | + |
| 15 | + |
| 16 | +class Break(Exception): |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +class Continue(Exception): |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +def _increment_data_pointer(): |
| 25 | + global memory_index |
| 26 | + memory_index += 1 |
| 27 | + if memory_index >= MEMORY_SIZE: |
| 28 | + raise SegmentationFault |
| 29 | + |
| 30 | + |
| 31 | +def _decrement_data_pointer(): |
| 32 | + global memory_index |
| 33 | + memory_index -= 1 |
| 34 | + if memory_index < 0: |
| 35 | + raise SegmentationFault |
| 36 | + |
| 37 | + |
| 38 | +def _increment_val(): |
| 39 | + memory[memory_index] = (memory[memory_index] + 1) % CHARACTER_SET_SIZE |
| 40 | + |
| 41 | + |
| 42 | +def _decrement_val(): |
| 43 | + memory[memory_index] = (memory[memory_index] - 1) % CHARACTER_SET_SIZE |
| 44 | + |
| 45 | + |
| 46 | +def _output_val(): |
| 47 | + sys.stdout.write(VALUE_TO_CHARACTER[memory[memory_index]]) |
| 48 | + |
| 49 | + |
| 50 | +def _input_val(): |
| 51 | + while True: |
| 52 | + user_input = raw_input("Enter a single character:") |
| 53 | + if len(user_input) == 1: |
| 54 | + memory[memory_index] = CHARACTER_TO_VALUE[user_input] |
| 55 | + break |
| 56 | + |
| 57 | + |
| 58 | +def _while_non_zero(): |
| 59 | + if memory[memory_index] == 0: |
| 60 | + raise Break |
| 61 | + |
| 62 | + |
| 63 | +def _end_while(): |
| 64 | + if memory[memory_index] != 0: |
| 65 | + raise Continue |
| 66 | + |
| 67 | + |
| 68 | +COMMANDS = {">": _increment_data_pointer, |
| 69 | + "<": _decrement_data_pointer, |
| 70 | + "+": _increment_val, |
| 71 | + "-": _decrement_val, |
| 72 | + ".": _output_val, |
| 73 | + ",": _input_val, |
| 74 | + "[": _while_non_zero, |
| 75 | + "]": _end_while} |
| 76 | + |
| 77 | + |
| 78 | +def _find_braces(program_string): |
| 79 | + find_opening_brace = {} |
| 80 | + find_closing_brace = {} |
| 81 | + |
| 82 | + opening_braces = [] |
| 83 | + for (position, character) in enumerate(program_string): |
| 84 | + if character == "[": |
| 85 | + opening_braces.append(position) |
| 86 | + |
| 87 | + elif character == "]": |
| 88 | + try: |
| 89 | + opening_brace_position = opening_braces.pop() |
| 90 | + except IndexError: |
| 91 | + opening_brace_position = None |
| 92 | + |
| 93 | + find_opening_brace[position] = opening_brace_position |
| 94 | + |
| 95 | + if opening_brace_position is not None: |
| 96 | + find_closing_brace[opening_brace_position] = position |
| 97 | + |
| 98 | + if len(opening_braces) != 0: |
| 99 | + for opening_brace_position in opening_braces: |
| 100 | + find_closing_brace[opening_brace_position] = None |
| 101 | + |
| 102 | + return find_opening_brace, find_closing_brace |
| 103 | + |
| 104 | + |
| 105 | +def _initialise_memory(): |
| 106 | + global memory, memory_index |
| 107 | + memory = [0] * MEMORY_SIZE |
| 108 | + memory_index = 0 |
| 109 | + |
| 110 | + |
| 111 | +def bf_interpreter(program_string): |
| 112 | + _initialise_memory() |
| 113 | + # Generate mappings to lookup location of matching braces. |
| 114 | + find_opening_brace, find_closing_brace = _find_braces(program_string) |
| 115 | + |
| 116 | + program_position = 0 |
| 117 | + while True: |
| 118 | + try: |
| 119 | + instruction = program_string[program_position] |
| 120 | + except IndexError: |
| 121 | + # We've reached the end of the program. |
| 122 | + break |
| 123 | + |
| 124 | + try: |
| 125 | + COMMANDS[instruction]() |
| 126 | + except KeyError: |
| 127 | + # Brainfuck just ignores any characters that are not in it's operator set. |
| 128 | + pass |
| 129 | + except SegmentationFault: |
| 130 | + # Treat this as a valid way to trigger program exit. |
| 131 | + break |
| 132 | + except Break: |
| 133 | + program_position = find_closing_brace[program_position] |
| 134 | + except Continue: |
| 135 | + program_position = find_opening_brace[program_position] |
| 136 | + |
| 137 | + if program_position is None: |
| 138 | + # i.e. program is syntactically invalid as there is no matching |
| 139 | + # opening/closing brace to go to - exit. |
| 140 | + break |
| 141 | + else: |
| 142 | + program_position += 1 |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + bf_interpreter(sys.argv[1]) |
0 commit comments