Advertisement
babusha

Untitled

Jul 20th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.86 KB | None | 0 0
  1. #!/usr/bin/ruby -Ku
  2. require 'bindata'
  3.  
  4. class Brainfuck
  5.     def self.read_file file
  6.         if not File.exist? file
  7.             puts "File " + file + " doesn't exist!"
  8.             exit 1
  9.         end
  10.         @new_name = file.gsub(/.bf/,'')
  11.         @@code = File.open(file).readlines.map { |line| line.chomp }.join.split('')
  12.     end
  13.    
  14.     def self.validate
  15.         @a = 0
  16.         @@code.each do |item|
  17.             if item == '['
  18.                 @a += 1
  19.             end
  20.             if item == ']'
  21.                 @a -= 1
  22.             end
  23.         end
  24.         if @a > 0
  25.             puts "Unexpected [ or ]"
  26.             exit 1
  27.         end
  28.     end
  29.    
  30.     def self.compile
  31.         $cells = Array.new
  32.         $cell  = 0
  33.         @fun = Proc.new do |command,file|
  34.             if $cells[$cell].nil?
  35.                     $cells[$cell] = 0
  36.             end
  37.                
  38.             case command
  39.             when '+'
  40.                 BinData::Int8be.new(0x1).write(file)
  41.                 $cells[$cell] += 1
  42.             when '-'
  43.                 BinData::Int8be.new(0x2).write(file)
  44.                 $cells[$cell] -= 1
  45.             when '>'
  46.                 BinData::Int8be.new(0x3).write(file)
  47.                 $cell += 1
  48.             when '<'
  49.                 BinData::Int8be.new(0x4).write(file)
  50.                 $cell -= 1
  51.             when ','
  52.                 BinData::Int8be.new(0x5).write(file)
  53.             when '.'
  54.                 BinData::Int8be.new(0x6).write(file)
  55.             end
  56.         end
  57.  
  58.         brackets = 0
  59.         loop_code = Array.new
  60.         File.open(@new_name,'wb') do |file|
  61.             @@code.each_with_index do |item,index|
  62.                 if $cells[$cell].nil?
  63.                     $cells[$cell] = 0
  64.                 end
  65.                
  66.                 if item == '['
  67.                     brackets += 1
  68.                     next
  69.                 end
  70.                
  71.                 if brackets > 0 and item != ']'
  72.                     loop_code << item
  73.                     next
  74.                 end
  75.                    
  76.                
  77.                 if item == ']'
  78.                     brackets -= 1
  79.                     while $cells[$cell] != 0
  80.                         loop_code.each do |c|
  81.                             @fun.call c,file
  82.                             break if $cells[$cell] == 0
  83.                         end
  84.                     end
  85.                     next
  86.                 end
  87.                 @fun.call item,file
  88.             end
  89.         end
  90.     end
  91. end
  92.  
  93. ARGV.each do |file_name|
  94.     if not File.exist? file_name
  95.         puts "File #{file_name} doesn't exist!"
  96.         exit 1
  97.     end
  98.     Brainfuck.read_file file_name
  99.     Brainfuck.validate
  100.     Brainfuck.compile
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement