# Redcode Pretty Printer v0.1 by John Metcalf # Align labels, opcodes, operands, and comments for improved readability. # Usage: $ ruby pretty.rb # Note: support for multiple labels per line not yet implemented opcodes = 'dat|mov|add|sub|mul|div|mod|jmp|jmz|jmn|djn|cmp|seq|sne|slt|spl|nop|ldp|stp|org|equ|end|pin|for|rof' File.foreach(ARGV[0], chomp: true) do |line| line.scrub! line.lstrip! if line =~ /\bequ\b/i line.prepend(' ') if line =~ /^(?:#{opcodes})\b/i match = line.match(/^(\w+:?)? # label \s*([\w.]+)? # opcode \s*([^,;]+)? # a-field (?:,\s*([^;]+))? # b-field \s*(;.*)?$/x) # comment label, opcode, afield, bfield, comment = ((match&.captures || []) + [''] * 5)[0, 5].map(&:to_s) if label.empty? && opcode.empty? puts comment else puts (label.ljust(7) + ' ' + opcode.ljust(7) + [afield.gsub(/\s/, ''), bfield.empty? ? '' : ','].join.ljust(14) + bfield.gsub(/\s/, '').ljust(14) + comment).rstrip end end