Created
August 27, 2011 13:05
-
-
Save trishume/1175367 to your computer and use it in GitHub Desktop.
Subleq VM
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
6 7 3 | |
7 -1 9 | |
2 122 0 | |
8 35 -7 |
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
# subleq VM | |
# this version allows dynamic memory allocation (expanding memory by accessing it) | |
# 208 bytes (smaller with no whitespace) | |
# uses loc -1 for ascii output | |
# see http://mazonka.com/subleq/ | |
# pass commands to STDIN or as a filename argument | |
i=0;m = ARGF.read.split(/\s+/).map{|x|x.to_i} | |
while i>=0 | |
m.fill(0,m.length..m[i+1]) if m[i+1]>m.length | |
m[i+1]>0 ? m[m[i+1]]-=m[m[i]] : print(m[m[i]].chr) | |
i=(i > 0 ? m[m[i+1]] : 0) > 0 ? i+1 : m[i+2] | |
end |
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
# subleq VM | |
# neat version | |
# uses loc -1 for ascii output | |
# see http://mazonka.com/subleq/ | |
# pass commands to STDIN or as a filename argument | |
mem = ARGF.read.split(/\s+/).map{|x|x.to_i} | |
i=0 | |
while i>=0 | |
mem[i+1] != -1 ? mem[mem[i+1]] -= mem[mem[i]] : print(mem[mem[i]].chr) | |
if (i > 0 ? mem[mem[i+1]] : 0) > 0 | |
i+=1 | |
else | |
i=mem[i+2] | |
end | |
end |
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
# subleq VM | |
# 162 bytes (could be smaller without lines and tabs) | |
# uses loc -1 for ascii output | |
# see http://mazonka.com/subleq/ | |
# pass commands to STDIN or as a filename argument | |
i,m = 0,ARGF.read.split(/\s+/).map{|x|x.to_i} | |
while i>=0 | |
m[i+1]!=-1 ? m[m[i+1]]-=m[m[i]] : print(m[m[i]].chr) | |
i=(i > 0 ? m[m[i+1]] : 0) > 0 ? i+1 : m[i+2] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The test program only runs with dynamic allocation.