require 'stringio'
class HelloWorld
def self.interpret(**kwargs)
new(**kwargs).interpret
end
def initialize(input: STDIN, output: STDOUT)
@accumulator = 1
@stack = []
@input = input
@output = output
end
def interpret(input = @input)
input.each_char do |c|
puts(" => %c: %p (%i)" % [c, @stack, @accumulator]) if ENV['VERBOSE']
case c
when 'c'
@stack.push('' << (@stack.pop || ''))
when 'h'
@stack.push(get_user_input)
when 'e'
@stack.push(@stack.pop.to_i)
when 'l'
@stack.push(@stack.first)
when 'o'
x = @stack.pop
if @looping && x == 0
raise StopIteration
elsif !@looping
@looping = true
branch = input.gets('o')
loop{interpret(StringIO.new(branch))} rescue StopIteration
@looping = false
else
@stack.push(@stack.last)
end
when ','
@stack.push(@accumulator += 1)
when '.'
@stack.push(@accumulator -= 1)
when ' '
raise StopIteration if @stack.first == @accumulator
when 'w'
x = @stack.pop
y = @stack.pop
@stack.push(y % x)
when 'm'
x = @stack.pop
y = @stack.pop
@stack.push(x % y)
when 'r'
@stack.push(@stack.pop == 0 ? 1 : 0)
when 'd'
@stack.push(@stack.pop == 0 ? "false\n" : "true\n")
when '!'
@output.print @stack.pop
return(0) if @stack.pop == 0
when '0'..'9'
x = @stack.pop
if x.is_a?(Numeric) && x != 0
@stack.push(x*10 + c.to_i)
else
@stack.push(x) if x
@stack.push(c.to_i)
end
end
end
unless @looping
raise 'Unexpected end of input. Please end programs with "!"'
end
end
def get_user_input
ARGV.shift || gets.chomp
end
end
input = "hello, worl!"
if input.nil?
loop{HelloWorld.interpret(input: StringIO.new(gets))}
else
HelloWorld.interpret(input: StringIO.new(input))
end
cmVxdWlyZSAnc3RyaW5naW8nCgpjbGFzcyBIZWxsb1dvcmxkCgogIGRlZiBzZWxmLmludGVycHJldCgqKmt3YXJncykKICAgIG5ldygqKmt3YXJncykuaW50ZXJwcmV0CiAgZW5kCgogIGRlZiBpbml0aWFsaXplKGlucHV0OiBTVERJTiwgb3V0cHV0OiBTVERPVVQpCiAgICBAYWNjdW11bGF0b3IgPSAxCiAgICBAc3RhY2sgPSBbXQogICAgQGlucHV0ID0gaW5wdXQKICAgIEBvdXRwdXQgPSBvdXRwdXQKICBlbmQKCiAgZGVmIGludGVycHJldChpbnB1dCA9IEBpbnB1dCkKICAgIGlucHV0LmVhY2hfY2hhciBkbyB8Y3wKICAgICAgcHV0cygiID0+ICVjOiAlcCAoJWkpIiAlIFtjLCBAc3RhY2ssIEBhY2N1bXVsYXRvcl0pIGlmIEVOVlsnVkVSQk9TRSddCiAgICAgIGNhc2UgYwogICAgICB3aGVuICdjJwogICAgICAgIEBzdGFjay5wdXNoKCcnIDw8IChAc3RhY2sucG9wIHx8ICcnKSkKICAgICAgd2hlbiAnaCcKICAgICAgICBAc3RhY2sucHVzaChnZXRfdXNlcl9pbnB1dCkKICAgICAgd2hlbiAnZScKICAgICAgICBAc3RhY2sucHVzaChAc3RhY2sucG9wLnRvX2kpCiAgICAgIHdoZW4gJ2wnCiAgICAgICAgQHN0YWNrLnB1c2goQHN0YWNrLmZpcnN0KQogICAgICB3aGVuICdvJwogICAgICAgIHggPSBAc3RhY2sucG9wCiAgICAgICAgaWYgQGxvb3BpbmcgJiYgeCA9PSAwCiAgICAgICAgICByYWlzZSBTdG9wSXRlcmF0aW9uCiAgICAgICAgZWxzaWYgIUBsb29waW5nCiAgICAgICAgICBAbG9vcGluZyA9IHRydWUKICAgICAgICAgIGJyYW5jaCA9IGlucHV0LmdldHMoJ28nKQogICAgICAgICAgbG9vcHtpbnRlcnByZXQoU3RyaW5nSU8ubmV3KGJyYW5jaCkpfSByZXNjdWUgU3RvcEl0ZXJhdGlvbgogICAgICAgICAgQGxvb3BpbmcgPSBmYWxzZQogICAgICAgIGVsc2UKICAgICAgICAgIEBzdGFjay5wdXNoKEBzdGFjay5sYXN0KQogICAgICAgIGVuZAogICAgICB3aGVuICcsJwogICAgICAgIEBzdGFjay5wdXNoKEBhY2N1bXVsYXRvciArPSAxKQogICAgICB3aGVuICcuJwogICAgICAgIEBzdGFjay5wdXNoKEBhY2N1bXVsYXRvciAtPSAxKQogICAgICB3aGVuICcgJwogICAgICAgIHJhaXNlIFN0b3BJdGVyYXRpb24gaWYgQHN0YWNrLmZpcnN0ID09IEBhY2N1bXVsYXRvcgogICAgICB3aGVuICd3JwogICAgICAgIHggPSBAc3RhY2sucG9wCiAgICAgICAgeSA9IEBzdGFjay5wb3AKICAgICAgICBAc3RhY2sucHVzaCh5ICUgeCkKICAgICAgd2hlbiAnbScKICAgICAgICB4ID0gQHN0YWNrLnBvcAogICAgICAgIHkgPSBAc3RhY2sucG9wCiAgICAgICAgQHN0YWNrLnB1c2goeCAlIHkpCiAgICAgIHdoZW4gJ3InCiAgICAgICAgQHN0YWNrLnB1c2goQHN0YWNrLnBvcCA9PSAwID8gMSA6IDApCiAgICAgIHdoZW4gJ2QnCiAgICAgICAgQHN0YWNrLnB1c2goQHN0YWNrLnBvcCA9PSAwID8gImZhbHNlXG4iIDogInRydWVcbiIpCiAgICAgIHdoZW4gJyEnCiAgICAgICAgQG91dHB1dC5wcmludCBAc3RhY2sucG9wCiAgICAgICAgcmV0dXJuKDApIGlmIEBzdGFjay5wb3AgPT0gMAogICAgICB3aGVuICcwJy4uJzknCiAgICAgICAgeCA9IEBzdGFjay5wb3AKICAgICAgICBpZiB4LmlzX2E/KE51bWVyaWMpICYmIHggIT0gMAogICAgICAgICAgQHN0YWNrLnB1c2goeCoxMCArIGMudG9faSkKICAgICAgICBlbHNlCiAgICAgICAgICBAc3RhY2sucHVzaCh4KSBpZiB4CiAgICAgICAgICBAc3RhY2sucHVzaChjLnRvX2kpCiAgICAgICAgZW5kCiAgICAgIGVuZAogICAgZW5kCiAgICB1bmxlc3MgQGxvb3BpbmcKICAgICAgcmFpc2UgJ1VuZXhwZWN0ZWQgZW5kIG9mIGlucHV0LiBQbGVhc2UgZW5kIHByb2dyYW1zIHdpdGggIiEiJwogICAgZW5kCiAgZW5kCgogIGRlZiBnZXRfdXNlcl9pbnB1dAogICAgQVJHVi5zaGlmdCB8fCBnZXRzLmNob21wCiAgZW5kCgplbmQKCmlucHV0ID0gImhlbGxvLCB3b3JsISIKCmlmIGlucHV0Lm5pbD8KICBsb29we0hlbGxvV29ybGQuaW50ZXJwcmV0KGlucHV0OiBTdHJpbmdJTy5uZXcoZ2V0cykpfQplbHNlCiAgSGVsbG9Xb3JsZC5pbnRlcnByZXQoaW5wdXQ6IFN0cmluZ0lPLm5ldyhpbnB1dCkpCmVuZA==