User:JayCampbell/bbf2bf.rb


 * 1) !/usr/bin/ruby
 * 2) bbf2bf.rb
 * 3) Convert ehird's "brainfuck to B Nomic platonic autoaction english" to brainfuck
 * 4) Jay Campbell, November 2008
 * 1) Jay Campbell, November 2008

$stdout.sync = $stderr.sync = true

if (file = ARGV.pop) == '-v' then verbose = true; file = ARGV.pop else verbose = false end

filelines = '' File.open(file) do |f| filelines << f.gets while not f.eof end

filelines.gsub!(/[\r\n]+/, ' ') filelines.gsub!(/\./, ".\n") filelines.gsub!(/\- /, '-') filelines.gsub!(/ +/, ' ') filelines.gsub!(/^ /, '')

@lines = filelines.split /[\r\n]+/

@instructions = Array.new @lines.each do |line|

if line =~ /When a party's step is (\d+),?(.*)/i instruction_step = $1.to_i to_eol = $2 if to_eol =~ /and the Contents of the Current Cell is (0|not 0),?(.*)/i condition = $1.downcase clause = $2 else clause = to_eol end

if clause =~ /they must (inc|dec)rease the value of the Interpreter's Pointer attribute by 1 and set their Step to (\d+)/i if $1.downcase == 'inc' @instructions[instruction_step] = '>' else @instructions[instruction_step] = '<' end elsif clause =~ /they must BF- ?(Inc|Dec)rement the Current Cell and set their Step to (\d+)/i if $1.downcase == 'inc' @instructions[instruction_step] = '+' else @instructions[instruction_step] = '-' end elsif clause =~ /they must set their Step to (\d+)/i if condition == 'not 0' @instructions[instruction_step] = ']' @instructions[$1.to_i] = '[' end elsif clause =~ /they must BF- ?(Output|Input) (to )?the Current Cell and set their Step to (\d+)/i if $1.downcase == 'input' @instructions[instruction_step] = ',' else @instructions[instruction_step] = '.' end else end else puts "Malformed line: #{line}" if verbose end end

puts @instructions.join

-- User:JayCampbell 2008