Skip to content

Instantly share code, notes, and snippets.

@meiamsome
Created August 3, 2016 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meiamsome/829315641c1fdf4b33d89e53294fc20f to your computer and use it in GitHub Desktop.
Save meiamsome/829315641c1fdf4b33d89e53294fc20f to your computer and use it in GitHub Desktop.
Brainfuck interpreter in a single Python statement (No eval, double sided 'infinite' tape, no number limit in a cell). First lambda function is the entire interpreter, arguments are program (as a string), input and output (as files, already open)
list(((lambda program, input_stream, output_stream, instruction_stack=[], count=[]:
(lambda instruction_queue=[0], program_stack=[], program_memory=[0], pointer=[0],
instructions=[
(count.append(0) or len(count) - 1, len(instruction_stack), x, instruction_stack.append([len(count)]) or instruction_stack[-1]) if x == '['
else (
(count.append(0) or len(count) - 1, len(instruction_stack), x, instruction_stack[-1].append(len(count)) or [instruction_stack.pop()[0],len(count)])if x == ']'
else (count.append(0) or len(count) - 1, len(instruction_stack), x, [len(count)]*2
)
) for x in program]:
(
(instruction_queue.append(instructions[x][3][program_memory[pointer[0]]==0]) if instructions[x][3][program_memory[pointer[0]]==0] != len(instructions) else None)
or (program_memory.insert(pointer[0], program_memory.pop(pointer[0]) - 1 + ('- +'.index(instructions[x][2]))) if instructions[x][2] in '-+' else None)
or (
(
(program_memory.append(0) if pointer[0] == len(program_memory) - 1 else None)
or pointer.append(pointer.pop() + 1) if instructions[x][2] == '>'
else (program_memory.insert(0,0) if pointer[0] == 0 else pointer.append(pointer.pop() - 1))
) if instructions[x][2] in '<>' else None)
or (program_memory.insert(program_memory.pop(pointer[0]) * 0 + pointer[0], ord(input_stream.read(1))) if instructions[x][2] == ',' else None)
or (output_stream.write(chr(program_memory[pointer[0]])) and output_stream.flush() if instructions[x][2] == '.' else None)
or (pointer[0], program_memory) # output to list the current memory state, for debugging
for x in instruction_queue)
)()
)(list(filter(',.><[]+-'.count,''.join(__import__('fileinput').input()))), __import__('sys').stdin, __import__('sys').stdout)))
list((lambda f, g, h, i=[], j=[]:(lambda a=[0], b=[], c=[0], d=[0], e=[(j.append(0) or len(j) - 1, len(i), x, i.append([len(j)]) or i[-1]) if x == '[' else ((j.append(0) or len(j) - 1, len(i), x, i[-1].append(len(j)) or [i.pop()[0],len(j)]) if x == ']' else (j.append(0) or len(j) - 1, len(i), x, [len(j)]*2)) for x in f]:((a.append(e[x][3][c[d[0]]==0]) if e[x][3][c[d[0]]==0] != len(e) else None) or (c.insert(d[0], c.pop(d[0]) - 1 + ('- +'.index(e[x][2]))) if e[x][2] in '-+' else None) or (((c.append(0) if d[0] == len(c) - 1 else None) or d.append(d.pop() + 1) if e[x][2] == '>' else (c.insert(0,0) if d[0] == 0 else d.append(d.pop() - 1)))if e[x][2] in '<>' else None) or (c.insert(c.pop(d[0]) * 0 + d[0] + 1, ord(g.read(1))) if e[x][2] == ',' else None) or (h.write(chr(c[d[0]])) and h.flush() if e[x][2] == '.' else None) for x in a))())(list(filter(',.><[]+-'.count,''.join(__import__('fileinput').input()))), __import__('sys').stdin, __import__('sys').stdout))
@alexmclarty
Copy link

👍 ⚡ 🍆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment