Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
subleq/subleq.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
35 lines (34 sloc)
763 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Example program | |
prog = [ 21, -1, -2, #0 #Print loc 21 | |
19, 20, -1, #3 #Subtract 1 from pos 20, quit when 0 | |
18, 0, -2, #6 #Add one to pos 0 | |
17, 17, 0, #9 #Jump to pos 0 | |
0, 0, 0, #12 | |
0, 0, 0, #15 | |
-1, 1, 13, #18 Pos 20 is string length | |
72, 101, 108, #21 #Data begins here | |
108, 111, 32, | |
87, 111, 114, | |
108, 100, 33, | |
10] | |
pc=0 | |
c=0 | |
out="" | |
while True: | |
c+=1 | |
a,b,c=prog[pc:pc+3] | |
if c==-2: | |
c=pc+3 | |
pc+=3 | |
if b>=0: | |
prog[b]-=prog[a] | |
result=prog[b] | |
else: | |
result=-1 | |
if b==-1: | |
out+= chr(prog[a]) | |
if result<=0: | |
pc=c | |
if pc<0: | |
break | |
print(out) |