Last active
August 29, 2015 14:11
-
-
Save DataKinds/dd704130dc2ea8340309 to your computer and use it in GitHub Desktop.
Bueue fun
This file contains hidden or 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
# Language specification | |
# A @bueue program is a simple number | |
# The @bueue interpreter generates the collatz sequence | |
# of that number and interprets each number in it as | |
# a small subprogramm. | |
# | |
# There is a queue available which can store bits. | |
# Opcodes mod 10: | |
# 0 - enque 0 | |
# 1 - enque 1 | |
# 2 - jump marker | |
# 3 - if bit at front of the queue is not 0 | |
# jump back to the last jump marker | |
# 4 - if bit at end of the queue is not 0 | |
# jump back to the last jump marker | |
# 5 - rotate queue (rotate left) | |
# 6 - flip bit at the front of the queue | |
# 7 - deque bit at the front (that is | |
# drop/remove it) | |
# 8 - print bit | |
# 9 - deque 8 bits (1 byte) convert to char (ascii) and print | |
def collatzMethod(num) | |
ins = [] | |
while num > 1 | |
ins.push num = 3*num + 1 if num.odd? | |
ins.push num = num / 2 if num.even? | |
end | |
return ins | |
end | |
@bueue = [] | |
@jumps = [] | |
ins = collatzMethod(gets.chomp.to_i).join | |
#puts ins | |
index = -1 | |
while index < ins.length | |
index += 1 | |
case ins[index] | |
when ?0 | |
@bueue.push 0 | |
when ?1 | |
@bueue.push 1 | |
when ?2 | |
@jumps.unshift index | |
when ?3 | |
if @jumps != [] | |
if not @bueue.nil? and @bueue[0] != 0 | |
jumpLocation = @jumps.shift | |
index = jumpLocation - 1 #because it will skip ahead an index | |
end | |
end | |
when ?4 | |
if @jumps != [] | |
if not @bueue.nil? and @bueue[-1] != 0 | |
jumpLocation = @jumps.shift | |
index = jumpLocation - 1 #because it will skip ahead an index | |
end | |
end | |
when ?5 | |
@bueue.rotate! | |
when ?6 | |
@bueue[0] = @bueue[0] == 1 ? 0 : 1 | |
when ?7 | |
@bueue.pop | |
when ?8 | |
print @bueue[0] | |
when ?9 | |
bits = @bueue.pop(8).join | |
print [bits].pack("B*") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment