Skip to content

Instantly share code, notes, and snippets.

@trishume
Created August 27, 2011 13:05
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 trishume/1175367 to your computer and use it in GitHub Desktop.
Save trishume/1175367 to your computer and use it in GitHub Desktop.
Subleq VM
6 7 3
7 -1 9
2 122 0
8 35 -7
# 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
# 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
# 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
@trishume
Copy link
Author

The test program only runs with dynamic allocation.

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