Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- ThreadFuck interpreter
- ThreadFuck - A multithreaded derivative of Brainfuck
- ThreadFuck commands:
- > Move the active pointer to the right
- < Move the pointer to the left
- + Increment the memory cell at the active pointer
- - Decrement the memory cell at the active pointer
- . Output the character signified by the cell at the active pointer
- , Input a character and store it in the cell at the active pointer
- [ Jump past the matching ] if the cell at the active pointer is 0
- ] Jump back to the matching [ if the cell at the active pointer is nonzero
- ~ Switch active pointer
- ! Create a thread that executes the subprogram selected by the program selector of this thread
- ^ If the program selector is selecting the first subprogram, select the last subprogram, otherwise select the subprogram at the next line
- v If the program selector is selecting the last subprogram, select the first subprogram, otherwise select the subprogram at the previous line
- * Wait until all the other threads terminate
- You can change the code to execute by changeing the co_de variable, examples are given as the
- example_*** variables, you can run examples or you own programs. You can also change the DEBUG
- variable to 0 to disable debug information, and 1 to enable.
- More information see Esolang Wiki: https://esolangs.org/wiki/ThreadFuck
- """
- import sys
- builtin_enumerate=enumerate
- from threading import *
- public_p=0
- public_tape=[0]*1000000
- example_forkbomb='!!' # Fork bomb
- example_simutaneous='''v!!
- ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.''' # Undefined output (Two threads output simutaneously)
- example_threadunsafe='''v!!
- ~++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.''' # Undefined output (Thread unsafe)
- example_deadlock='''v!!
- *''' # Deadlock
- example_lock='''v!*++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
- ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.''' # Lock (Prints "Hello World!<LF>Hello World!<LF>")
- example_lock2='''~+v!v!
- +[>~[]~++++++++[>++++++++<-]>+.+.+.[-]<<~>-<+~]
- +[>~>[]<~++++++++[>++++++++<-]>++++.+.+.[-]<<~>+<-~]''' # Another example of lock (prints "DEFABCDEFABC...")
- co_de=example_lock
- if not co_de:
- raise Exception('Empty program not allowed')
- pcl=co_de.split('\n')
- pc=0
- DEBUG=1
- def tf(code,pc,pcl):
- def inner_tf():
- nonlocal pc,pcl
- s=[]
- matches={}
- ptape=[0]*1000000
- for i,j in builtin_enumerate(code):
- if j=='[':
- s.append(i)
- if j==']':
- m=s.pop()
- matches[m]=i
- matches[i]=m
- cp=0
- pp=0
- priv=True
- tape=ptape
- p=pp
- while cp<len(code):
- if code[cp]=='+':
- tape[p]=(tape[p]+1)%256
- if code[cp]=='-':
- tape[p]=(tape[p]-1)%256
- if code[cp]==',':
- c=sys.stdin.read(1)
- tape[p]=(ord(c) if c else 0)%256
- if code[cp]=='.':
- print(chr(tape[p]),end='')
- sys.stdout.flush()
- if code[cp]=='<':
- p-=1
- if code[cp]=='>':
- p+=1
- if code[cp]=='[':
- if not tape[p]:
- cp=matches[cp]
- if code[cp]==']':
- if tape[p]:
- cp=matches[cp]
- if code[cp]=='^':
- pc=(pc-1)%len(pcl)
- if code[cp]=='v':
- pc=(pc+1)%len(pcl)
- if code[cp]=='~':
- if priv:
- pp=p
- p=public_p
- tape=public_tape
- else:
- p=pp
- tape=ptape
- priv=(not priv)
- if code[cp]=='*':
- while active_count()>2: # The main thread and current thread
- pass
- if code[cp]=='!':
- t=Thread(target=tf(pcl[pc],pc,pcl))
- if DEBUG:
- sys.stderr.write(t.name+' starts\n')
- t.start()
- cp+=1
- if DEBUG:
- sys.stderr.write(current_thread().name+' stops\n')
- return inner_tf
- def interpret_threadfuck():
- t=Thread(target=tf(pcl[pc],pc,pcl),name="Main Thread")
- if DEBUG:
- sys.stderr.write(t.name+' starts\n')
- t.start()
- interpret_threadfuck()
Advertisement
Add Comment
Please, Sign In to add comment