brainfuck - Better way to implement interpreter in Python -
i attempting implement interpreter brainfuck , of now, using series of if/elif statements.
if(i == ">"): ... elif(i == "<"): ... elif(i == "+"): ... elif(i == "-"): ...
however, seems clunky , un-pythonic me. there better (cleaner/faster/more aesthetically pleasing) way implement this?
i have quick implementation of brainfuck interpreter python in github repo. in nutshell, though, keep dictionary, keys brainfuck characters , values function (or method) objects, , dispatch on that. this:
instructions = { '+': increment, '-': decrement, # other functions } def run(tape): ch = next_token(tape) if ch in instructions: instructions[ch]()
(not actual implementation, quick illustration.)
Comments
Post a Comment