root/lang/ruby/brainfuck/trunk/zu2bf @ 2753

Revision 2716, 0.9 kB (checked in by naoya_t, 5 years ago)

r2704@localhost: naochan | 2007-12-07 08:37:55 +0900
brainfuck initial import

  • Property svn:executable set to *
Line 
1#!/usr/bin/env ruby
2#
3# zu2bf - Zu compiler
4#
5$LOAD_PATH << File.join(File.dirname(__FILE__), "lib/")
6require 'textgraph'
7require 'brainfuck'
8
9class Zu2Bf
10  def initialize
11  end
12
13  def compile(src)
14    tg = TextGraph.parse(src)
15    wr = Brainfuck.writer
16
17    table = Array.new
18
19    tg.cells.each_with_index {|cell,i|
20      nexts = tg.links.find_all{|from, to| from == i}
21      inst = cell.content.strip
22      nexts.size.times {|t| inst += ", #{1+nexts[t][1]}"}
23      table[1+i] = inst
24    }
25    wr.node_switch(table).optimize
26  end
27end
28
29if ARGV.size==0
30  puts "usage: zu2bf <scriptfile>"
31else
32  if ARGV[0][0] == 0x2D
33    options = ARGV[0][1 .. -1]
34    ARGV.shift
35  end
36
37  compiler = Zu2Bf.new
38
39  bfcode = compiler.compile(ARGF.read)
40
41  case options || ""
42  when /r/ # run
43    bf = Brainfuck.VM
44    bf.load bfcode
45    bf.run
46  when /l/ # list
47    bf = Brainfuck.VM
48    bf.load bfcode
49    bf.list
50  else
51    print bfcode
52  end
53end
Note: See TracBrowser for help on using the browser.