User:JayCampbell/smatiny.rb

smanity.rb - An interpreter by User:JayCampbell for iHope's SMATINY esoteric programming language


 * 1) !/usr/bin/ruby
 * 2) smatiny.rb
 * 3) An interpreter by JayCampbell 2008
 * 4)   for iHope's SMATINY esoteric programming language
 * 1) An interpreter by JayCampbell 2008
 * 2)   for iHope's SMATINY esoteric programming language

filename = ARGV[0] memory = Array.new pointer = 0

File.open(filename) do |f| while line = f.gets
 * 1) parse supplied file

# remove comments line.gsub!(/#.*/, '')

# spaces and puntuations are optional, case-insensitive if line =~ /(\d+)\.?\s*swap\s*(\d+)\s*with\s*(\d+)\s*\.?/i memory[$1.to_i] = [ $2.to_i, $3.to_i ]

elsif line =~ /(\d+)\.?\s*do\s*nothing\s*\.?/i # don't really need this except if a strange program overwrites its own input memory[$1.to_i] = nil

elsif line =~ /(\d+)\.?\s*output\s*this\s*block's\s*position\s*\.?/i memory[$1.to_i] = 'print'

elsif line =~ /[\d\w]/ $stderr.puts "Bad line: #{line}" end end end

k=0; memory.each {|m| if m != nil then $stderr.print "#{k}=#{m.inspect} " end; k += 1}; puts $stderr.puts "Max cell: #{memory.size - 1}" # starting at 1, not 0 $stderr.puts "Non-NOP: #{memory.select{ |m| m != nil }.size}"

while pointer += 1
 * 1) run it

instruction = memory[pointer]

if instruction == 'print' putc pointer

elsif instruction.class == Array x = pointer y = instruction[0] z = instruction[1]

memory[y], memory[z] = memory[z], memory[y]

if x == y then pointer = z    elsif x == z then pointer = y end

end

break if pointer > memory.size

# very verbose - uncomment for step-by-step dump # print "\nP=#{pointer} "; k=0; memory.each {|m| if m != nil then print "#{k}=#{m.inspect} " end; k += 1}; puts

end