Changeset 3244 for lang/ruby/brainfuck

Show
Ignore:
Timestamp:
12/18/07 02:06:40 (5 years ago)
Author:
naoya_t
Message:

r3308@localhost: naochan | 2007-12-18 02:04:53 +0900
supporting / and %; sample program to calculate prime numbers

Location:
lang/ruby/brainfuck/trunk
Files:
1 added
3 removed
6 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/brainfuck/trunk

    • Property svn:ignore set to
      prime2.rb
      prime2.bf
      prime.bf
  • lang/ruby/brainfuck/trunk/README

    r2753 r3244  
    2323  zu2bf : 2次元プログラミング言語「ず」のコンパイラ 
    2424 
    25     「ず」については作者のyharaさんの 
     25    yharaさんの textgraph ライブラリが必要。 
     26    % gem install textgraph 
     27 
     28    プログラミング言語「ず」については作者のyharaさんの 
    2629      http://mono.kmc.gr.jp/~yhara/w/?ZuLanguage 
    27     を参照のこと。textgraphライブラリ(zu-0.1.0.tar.gzに含まれる)が必要。 
     30    を参照のこと。 
    2831 
    29     生成したコードは、8ビットでwrap aroundしない処理系では動きません。 
     32    生成したコードは、今のところ8ビットでwrap aroundしない処理系では動きません。 
    3033 
    3134    % ./zu2bf foo.zu > foo.bf 
    3235 
    3336      「ず」で書かれたプログラム foo.zu をBrainf*ckオブジェクトコードにコンパイル 
     37   デフォルトで80字/行に折り畳み。-n を付ければ折り畳まない (0.1.3) 
    3438 
    3539    % ./zu2bf -r foo.zu  
     
    4347! 開発履歴 
    4448 
     49  * ver 0.1.3 
     50  * ver 0.1.2 
    4551  * ver 0.1.1 : 2007-12-06夜 
    46   * ver 0.1.0 : 2007-12-06朝 
    47   ** 初公開 
     52  * ver 0.1.0 : 2007-12-06朝 初公開 
    4853 
    4954! 作者 
  • lang/ruby/brainfuck/trunk/lib/brainfuck/vm.rb

    r2859 r3244  
    1212      @wrap_around = true 
    1313    end 
    14     attr_reader :wrap_around 
     14    attr_accessor :wrap_around 
    1515 
    1616    def load(code) 
     
    6363      when :show_status 
    6464        printf(" :: ix = %d\n", @ix) 
    65         print  " ::  0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19\n ::" 
    66         (0..9).each{|i| print @st[i] ? sprintf(" %2x", @st[i]) : "  -" } 
    67         printf(" ") 
    68         (10..19).each{|i| print @st[i] ? sprintf(" %2x", @st[i]) : "  -" } 
     65        print  " ::  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19\n ::" 
     66        (0..19).each{|i| print @st[i] ? sprintf(" %2x", @st[i]) : "  -" } 
     67        printf("\n") 
     68        print  " :: 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39\n ::" 
     69        (20..39).each{|i| print @st[i] ? sprintf(" %2x", @st[i]) : "  -" } 
    6970        printf("\n") 
    7071      end 
  • lang/ruby/brainfuck/trunk/lib/brainfuck/writer.rb

    r2871 r3244  
    1 class String 
    2   def optimize 
    3     begin 
    4       last_length = self.length 
    5       self.gsub!(/<>/,"") ; self.gsub!(/></,"") 
    6     end while self.length < last_length 
    7     self.sub!(/[-+<>,]*\Z/, "") 
    8   end 
    9   def fold(per) 
    10     lines = ((length + (per-1)) / per).to_i 
    11     s = "" 
    12     (lines - 1).times{|l| 
    13       s += self[per*l .. per*l + (per-1)] + "\n" 
    14     } 
    15     s += self[per*(lines-1) .. -1] + "\n" if per*(lines-1) < length 
    16     s 
    17   end 
    18 end 
    19  
    201module Brainfuck 
    212 
     
    3011 
    3112      @wrap_around = false 
    32     end 
    33     attr_reader :wrap_around 
     13      @debug_mode = false 
     14      @debug_indent = "\t: " 
     15    end 
     16    attr_accessor :wrap_around, :debug_mode 
     17 
     18    private 
     19    def debug_indent_incr 
     20      @debug_indent += "  " 
     21    end 
     22    def debug_indent_decr 
     23      @debug_indent.sub!(/  \Z/,"") 
     24    end 
     25    def debug_print(s) 
     26      printf("%s%s\n", @debug_indent, s) 
     27    end 
    3428 
    3529    I_NODE     = 0 
     
    4438    I_TMP_MUL  = 9 
    4539    I_TMP_MUL1 = 10 ; I_TMP_MUL2 = 11 
    46      
    47     I_FLAG     = 12 
    48     I_FLAG_AND = I_FLAG_OR = I_FLAG_NOT  = 13 
    49     I_FLAG_BUP = 14 
    50  
    51     I_TMP_YN   = 15 # +3 
    52  
    53     K_         = 18 # +2 
    54     K100       = 20 ; K10 = 21 ; K1 = 22 
     40    I_TMP_DIV  = 12 ; I_TMP_DIV1 = 13 
     41    I_TMP_MOD  = 14 ; I_TMP_MOD1 = 15 
     42 
     43    I_FLAG     = 16 
     44    I_FLAG_AND = I_FLAG_OR = I_FLAG_NOT  = 17 
     45    I_FLAG_BUP = 18 
     46    I_FLAG_BORROW = 19 # 減算で0を下回るときに立つフラグ 
     47 
     48    I_TMP_YN   = 20 # +3 
     49 
     50    K_         = 23 # +2 
     51    K100       = 25 ; K10 = 26 ; K1 = 27 
    5552 
    5653#   TMP        = 19 
    57     VAR_BASE   = 23 
     54    VAR_BASE   = 28 
     55 
     56    private 
     57    def label(pos) 
     58      case pos 
     59      when 0 ; "I_NODE" 
     60      when 1 ; "I_NODE_TMP" 
     61      when 2 ; "I_TMP_SPACE" 
     62      when 3 ; "I_TMP_PUT" 
     63      when 4 ; "I_TMP_PUT + 1" 
     64      when 5 ; "I_TMP_COPY" 
     65      when 6 ; "I_TMP_ZNZ" 
     66      when 7 ; "I_TMP_EQ" 
     67      when 8 ; "I_TMP_ADD" 
     68      when 9 ; "I_TMP_MUL" 
     69      when 10 ; "I_TMP_MUL1D" 
     70      when 11 ; "I_TMP_MUL2" 
     71      when 12 ; "I_TMP_DIV" 
     72      when 13 ; "I_TMP_DIV+1" 
     73      when 14 ; "I_TMP_MOD" 
     74      when 15 ; "I_TMP_MOD+1" 
     75      when 16 ; "I_FLAG" 
     76      when 17 ; "I_FLAG_AND" 
     77      when 18 ; "I_FLAG_BUP" 
     78      when 19 ; "I_FLAG_BORROW" 
     79      when 20 ; "I_TMP_YN" 
     80      when 21 ; "I_TMP_YN+1" 
     81      when 22 ; "I_TMP_YN+2" 
     82      when 23 ; "K_" 
     83      when 24 ; "K_+1" 
     84      when 25 ; "K100" 
     85      when 26 ; "K10" 
     86      when 27 ; "K1" 
     87      when 28 ; "VAR_BASE" 
     88      else "VAR_BASE+#{pos-28}" 
     89      end 
     90    end 
    5891 
    5992    private 
     
    103136    # 絶対移動: ix = pos 
    104137    def move_to(pos) 
     138      if @debug_mode 
     139        debug_print "move_to #{label(pos)}" 
     140        return 
     141      end 
     142 
    105143      return "" if pos == @_ix 
    106144      move(pos - @_ix) 
     
    124162    # while (st[ix]) { ...content... } 
    125163    def bf_loop(&b) 
     164      if @debug_mode 
     165        debug_print "loop {" 
     166        debug_indent_incr 
     167 
     168        b.call 
     169 
     170        debug_indent_decr 
     171        debuf_print "}" 
     172        return 
     173      end 
     174 
    126175      "[" + b.call + "]" 
    127176    end 
    128177    def loop_at(k,&b) 
     178      if @debug_mode 
     179        debug_print "loop_at #{label(k)} {" 
     180        debug_indent_incr 
     181 
     182        # move_to(k) + "[" 
     183        b.call 
     184        # move_to(k) + "]" 
     185        move_to(k) 
     186 
     187        debug_indent_decr 
     188        debug_print "}" 
     189        return 
     190      end 
     191 
    129192      move_to(k) + "[" + b.call + move_to(k) + "]" 
    130193    end 
    131194    def do_once_if(flag_pos,&b) 
     195      if @debug_mode 
     196        debug_print "do_once_if #{label(flag_pos)} {" 
     197        debug_indent_incr 
     198 
     199        # move_to(flag_pos) + "["  
     200        b.call 
     201        # + clr_at(flag_pos) + "]" 
     202 
     203        debug_indent_decr 
     204        debug_print "}" 
     205        return 
     206      end 
     207 
    132208      move_to(flag_pos) + "[" + b.call + clr_at(flag_pos) + "]" 
    133209    end 
     
    135211    # st[ix] = 0 
    136212    def clr 
     213      if @debug_mode 
     214        debug_print "clr" 
     215        return 
     216      end 
     217 
    137218      _set 0 
    138219      "[-]" 
     
    140221 
    141222    def save_ix(&b) 
     223      if @debug_mode 
     224        ix_keep = @_ix 
     225        b.call 
     226        move_to(ix_keep) 
     227        return 
     228      end 
     229 
    142230      ix_keep = @_ix 
    143231      b.call + move_to(ix_keep) 
     
    150238    # st[src_pos] の内容は保持される。 
    151239    def copy_byte(dest_pos, src_pos) 
     240      if @debug_mode 
     241        debug_print "copy_byte #{label(dest_pos)}, #{label(src_pos)}" 
     242        return 
     243      end 
     244 
    152245      return "" if dest_pos == src_pos 
    153246 
    154247      save_ix { 
    155         clr_at(dest_pos) + clr_at(I_TMP_COPY) +  
     248        clr_at(dest_pos) + clr_at(I_TMP_COPY) + 
    156249        repeat(src_pos) { incr_at(dest_pos) + incr_at(I_TMP_COPY) } + 
    157250        repeat(I_TMP_COPY) { incr_at(src_pos) } 
     
    161254    # st[ix] += st[k] 
    162255    def add_byte(k) 
     256      if @debug_mode 
     257        debug_print "add_byte #{label(k)}" 
     258        return 
     259      end 
     260 
    163261      ix_keep = @_ix 
    164262      save_ix { 
     
    167265      } 
    168266    end 
    169     # st[ix] -= st[k] 
     267 
     268    # st[ix] -= st[k], set borrow_flag if result < 0 
    170269    def sub_byte(k) 
     270      if @debug_mode 
     271        debug_print "sub_byte #{label(k)}" 
     272        return 
     273      end 
     274 
     275#      save_ix { 
     276#        copy_byte(I_TMP_SUB, k) + 
     277#        repeat(I_TMP_SUB) { decr_at(ix_keep) } 
     278#      } 
    171279      ix_keep = @_ix 
    172280      save_ix { 
     281        reset_flag_at(I_FLAG_BORROW) + 
    173282        copy_byte(I_TMP_SUB, k) + 
    174         repeat(I_TMP_SUB) { decr_at(ix_keep) } 
     283        repeat(I_TMP_SUB) { 
     284          if_zero_at(ix_keep) { set_flag_at(I_FLAG_BORROW) } + 
     285          decr_at(ix_keep) 
     286        } 
    175287      } 
    176288    end 
     
    179291    # uint8 - 符号なし8ビット整数演算 
    180292    # 
     293 
     294    # st[ix] = val 
    181295    def set_uint8(val) 
    182       # st[ix] = val 
     296      if @debug_mode 
     297        debug_print "set_uint8 #{val}" 
     298        return 
     299      end 
     300 
    183301      clr + add_uint8(val) 
    184302    end 
    185303 
     304    # st[ix] += val 
    186305    def add_uint8(val) 
    187       # st[ix] += val 
     306      if @debug_mode 
     307        debug_print "add_uint8 #{val}" 
     308        return 
     309      end 
     310 
    188311      if val == 0 
    189312        return "" 
     
    212335    end 
    213336 
    214     def sub_uint8(val) ; add_uint8(-val) ; end 
     337    # st[ix] -= val 
     338    def sub_uint8(val) 
     339      if @debug_mode 
     340        debug_print "sub_uint8 #{val}" 
     341        return 
     342      end 
     343 
     344      add_uint8(-val) 
     345    end 
    215346 
    216347    # st[ix] *= val 
    217348    def mul_uint8(val) 
     349      if @debug_mode 
     350        debug_print "mul_uint8 #{val}" 
     351        return 
     352      end 
     353 
    218354      save_ix { 
    219355        copy_byte(I_TMP_MUL1,@_ix) + 
     
    227363      copy_byte(@_ix, I_TMP_MUL) 
    228364    end 
    229     # st[ix] /= val, I_MOD = mod 
     365 
     366    # st[ix] / st[k] = I_TMP_DIV ... I_TMP_MOD 
     367    def div_byte(k) 
     368      if @debug_mode 
     369        debug_print "div_byte #{label(k)}" 
     370        return 
     371      end 
     372 
     373      ix_keep = @_ix 
     374      save_ix { 
     375        copy_byte(I_TMP_MOD,ix_keep) + 
     376        clr_at(I_TMP_DIV) + 
     377        copy_byte(I_TMP_DIV1,k) + 
     378        set_flag_at(I_TMP_MOD1) + # use this addr for flag 
     379        loop_at(I_TMP_MOD1) { 
     380          move_to(I_TMP_MOD) + 
     381          sub_byte(I_TMP_DIV1) + 
     382          incr_at(I_TMP_DIV) + 
     383          if_nonzero_at(I_FLAG_BORROW) { 
     384            move_to(I_TMP_MOD) + 
     385            add_byte(I_TMP_DIV1) + 
     386            decr_at(I_TMP_DIV) + 
     387            reset_flag_at(I_TMP_MOD1) 
     388          } 
     389        }# + 
     390#        copy_byte(ix_keep,I_TMP_MOD) 
     391      } 
     392    end 
     393 
     394    # st[ix] / val = st[ix] ... I_MOD 
    230395    def div_uint8(val) 
    231     end 
    232 #    # st[ix] %= val 
    233 #    def mod_uint8(val) 
    234 #    end 
     396      if @debug_mode 
     397        debug_print "div_uint8 #{val}" 
     398        return 
     399      end 
     400 
     401      ix_keep = @_ix 
     402      save_ix { 
     403        copy_byte(I_TMP_DIV, ix_keep) + 
     404        set_uint8_at(I_TMP_DIV1,val) + 
     405        move_to(I_TMP_DIV) + 
     406        div_byte(I_TMP_DIV1) + 
     407        copy_byte(ix_keep, I_TMP_DIV) 
     408      } 
     409    end 
     410    # st[ix] %= val 
     411    def mod_uint8(val) 
     412      if @debug_mode 
     413        debug_print "mod_uint8 #{val}" 
     414        return 
     415      end 
     416 
     417      ix_keep = @_ix 
     418      save_ix { 
     419        copy_byte(I_TMP_DIV, ix_keep) + 
     420        set_uint8_at(I_TMP_DIV1,val) + 
     421        move_to(I_TMP_DIV) + 
     422        div_byte(I_TMP_DIV1) + 
     423        copy_byte(ix_keep, I_TMP_MOD) 
     424      } 
     425    end 
    235426 
    236427    # boolean 
    237428    # st[ix] &= st[k] 
    238429    def and_bool(k) 
     430      if @debug_mode 
     431        debug_print "and_bool #{label(k)}" 
     432        return 
     433      end 
     434 
    239435      save_ix { 
    240436        if_zero_at(k) { clr_at(@_ix) } 
    241437      } 
    242438    end 
     439 
    243440    # st[ix] |= st[k] 
    244441    def or_bool(k) 
     442      if @debug_mode 
     443        debug_print "or_bool #{label(k)}" 
     444        return 
     445      end 
     446 
    245447      save_ix { 
    246448        if_nonzero_at(k) { copy_byte(@_ix,k) } 
    247449      } 
    248450    end 
     451 
    249452    # st[ix] = not st[k] 
    250453    def bool_not(k) 
     454      if @debug_mode 
     455        debug_print "bool_not #{label(k)}" 
     456        return 
     457      end 
     458 
    251459      save_ix { 
    252460        copy_byte(I_FLAG_NOT, I_FLAG) + 
     
    256464 
    257465    # ***_at(pos) - ※ixはposに留める 
    258     def incr_at(pos) ; move_to(pos) + bf_incr ; end 
    259     def decr_at(pos) ; move_to(pos) + bf_decr ; end 
    260     def read_at(pos) ; move_to(pos) + bf_read ; end 
    261     def clr_at(pos)  ; move_to(pos) + clr ; end 
    262     def set_uint8_at(pos,val) ; move_to(pos) + set_uint8(val) ; end 
    263     def add_uint8_at(pos,val) ; move_to(pos) + add_uint8(val) ; end 
    264     def sub_uint8_at(pos,val) ; move_to(pos) + sub_uint8(val) ; end 
    265     def set_flag_at(pos) ; set_uint8_at(pos,1) ; end 
    266     def reset_flag_at(pos) ; clr_at(pos) ; end 
     466    def incr_at(pos) 
     467      if @debug_mode 
     468        debug_print "incr_at #{label(pos)}" 
     469        return 
     470      end 
     471      move_to(pos) + bf_incr 
     472    end 
     473    def decr_at(pos) 
     474      if @debug_mode 
     475        debug_print "decr_at #{label(pos)}" 
     476        return 
     477      end 
     478      move_to(pos) + bf_decr 
     479    end 
     480    def read_at(pos) 
     481      if @debug_mode 
     482        debug_print "read_at #{label(pos)}" 
     483        return 
     484      end 
     485      move_to(pos) + bf_read 
     486    end 
     487    def clr_at(pos) 
     488      if @debug_mode 
     489        debug_print "clr_at #{label(pos)}" 
     490        return 
     491      end 
     492      move_to(pos) + clr 
     493    end 
     494    def set_uint8_at(pos,val) 
     495      if @debug_mode 
     496        debug_print "set_uint8_at #{label(pos)},#{val}" 
     497        return 
     498      end 
     499      move_to(pos) + set_uint8(val) 
     500    end 
     501    def add_uint8_at(pos,val) 
     502      if @debug_mode 
     503        debug_print "add_uint8_at #{label(pos)},#{val}" 
     504        return 
     505      end 
     506      move_to(pos) + add_uint8(val) 
     507    end 
     508    def sub_uint8_at(pos,val) 
     509      if @debug_mode 
     510        debug_print "sub_uint8_at #{label(pos)},#{val}" 
     511        return 
     512      end 
     513      move_to(pos) + sub_uint8(val) 
     514    end 
     515    def set_flag_at(pos) 
     516      if @debug_mode 
     517        debug_print "set_flag_at #{label(pos)}" 
     518        return 
     519      end 
     520      set_uint8_at(pos,1) 
     521    end 
     522    def reset_flag_at(pos) 
     523      if @debug_mode 
     524        debug_print "reset_flag_at #{label(pos)}" 
     525        return 
     526      end 
     527      clr_at(pos) 
     528    end 
    267529 
    268530    # ブロック 
    269531    def if_nonzero_at(pos, &b) 
     532      if @debug_mode 
     533        debug_print "if_nonzero_at #{label(pos)} {" 
     534        debug_indent_incr 
     535 
     536        b.call 
     537 
     538        debug_indent_decr 
     539        debug_print "}" 
     540        return 
     541      end 
    270542      # st[pos]が 0 でない場合のみブロックの内容を実行。 
    271543      # st[pos]の値は保持される。 
     
    278550    end 
    279551    def repeat(count_pos, &b) 
     552      if @debug_mode 
     553        debug_print "repeat #{label(count_pos)}" 
     554        debug_indent_incr 
     555 
     556        b.call 
     557 
     558        debug_indent_decr 
     559        debug_print "}" 
     560        return 
     561      end 
    280562      # st[count_pos]に指定された回数だけブロックの内容を実行 
    281563      loop_at(count_pos) { b.call + decr_at(count_pos) } 
     
    284566    # test系。ここではtestで始まる名前を付けない 
    285567    def check_if_zero 
     568      if @debug_mode 
     569        debug_print "check_if_zero" 
     570        return 
     571      end 
     572 
    286573      # st[ix] の内容が 0 かどうかを I_FLAG にセットする 
    287574      copy_byte(I_TMP_ZNZ, @_ix) + 
     
    292579      # ix = I_FLAGで戻る 
    293580    end 
     581 
    294582    def check_if_nonzero 
     583      if @debug_mode 
     584        debug_print "check_if_nonzero" 
     585        return 
     586      end 
     587 
    295588      # st[ix] の内容が 0 でないかどうかを I_FLAG にセットする 
    296589      copy_byte(I_TMP_ZNZ, @_ix) + 
     
    301594      # ix = I_FLAGで戻る 
    302595    end 
     596 
    303597    def check_if_n(n) 
     598      if @debug_mode 
     599        debug_print "check_if_n #{n}" 
     600        return 
     601      end 
     602 
    304603      # st[ix] の内容が n に等しいかどうかを I_FLAG にセットする 
    305604      sub_uint8(n) + check_if_zero + add_uint8(n) 
     
    308607    # i/o 
    309608    def write_char(c) 
     609      if @debug_mode 
     610        debug_print "write_char #{c}" 
     611        return 
     612      end 
     613 
    310614      # 1文字表示する。引数cはコード。 
    311615      save_ix { 
     
    314618    end 
    315619    def write_string(s) 
     620      if @debug_mode 
     621        debug_print "write_string \"#{s.gsub(/\n/,"\\n")}\"" 
     622        return 
     623      end 
     624 
    316625      return "" unless s and s.length > 0 
    317626 
     
    345654    end 
    346655    def write_uint8(i) 
     656      if @debug_mode 
     657        debug_print "write_uint8 #{i}" 
     658        return 
     659      end 
     660 
     661#      set_uint8_at(I_TMP_PUT,i) + write_byte_as_uint8(I_TMP_PUT) 
    347662      save_ix { 
    348663        set_uint8_at(I_TMP_PUT,i) + write_curr_as_uint8 
     
    350665    end 
    351666 
     667    def write_byte_as_uint8(k) 
     668      if @debug_mode 
     669        debug_print "write_byte_as_uint8" 
     670        return 
     671      end 
     672 
     673      save_ix { 
     674        move_to(k) + write_curr_as_uint8 
     675      } 
     676    end 
    352677    def write_curr_as_uint8 
     678      if @debug_mode 
     679        debug_print "write_curr_as_uint8" 
     680        return 
     681      end 
    353682#      save_ix { 
    354683        # st[K_] = k 
     
    393722 
    394723    def if_zero_at(pos,&b) 
     724      if @debug_mode 
     725        debug_print "if_zero_at #{label(pos)} {" 
     726        debug_indent_incr 
     727 
     728#        move_to(pos) + check_if_zero + do_once_if(I_FLAG,&b) #if_nonzero_at(I_FLAG,&b) 
     729        b.call 
     730 
     731        debug_indent_decr 
     732        debug_print "}" 
     733        return 
     734      end 
     735 
    395736      # st[pos]=0ならブロックの内容実行 
    396737      save_ix { 
     
    400741 
    401742    def yn_inkey 
     743      if @debug_mode 
     744        debug_print "yn_inkey\t\t# I_TMP_YN <-- 'y' ? 1 : 0" 
     745        return 
     746      end 
    402747      # y か n が入力されるまで入力ループ。 
    403748      # I_TMP_YN に結果が入る。(yなら1,nなら0) 
     
    420765 
    421766    def say(s, next_node=0) 
     767      if @debug_mode 
     768        printf("//   say(\"%s\", %d)\n", s, next_node) 
     769 
     770        write_string(s + "\n") 
     771        set_uint8_at(I_NODE, next_node) 
     772        return 
     773      end 
    422774      # 文字列 s を表示し改行する。 
    423775      write_string(s + "\n") + set_uint8_at(I_NODE, next_node) 
     
    425777 
    426778    def ask(s, n_node, y_node) 
     779      if @debug_mode 
     780        printf("//   ask(\"%s\", %d, %d)\n", s, n_node, y_node) 
     781 
     782        write_string(s + " [y/n]\n> ") 
     783        yn_inkey 
     784        if_nonzero_at(I_TMP_YN) { set_uint8_at(I_NODE, y_node) } 
     785        if_zero_at(I_TMP_YN) { set_uint8_at(I_NODE, n_node) } 
     786        return 
     787      end 
    427788      # 文字列(質問文)s を表示し、y/n の入力を促し、I_NODEを適切なノード番号にセットする 
    428789      # n_node が先なのはZuの仕様に合わせているため 
    429       save_ix { 
    430         write_string(s + " [y/n]\n> ") + 
     790      write_string(s + " [y/n]\n> ") + 
    431791        yn_inkey + 
    432792        if_nonzero_at(I_TMP_YN) { set_uint8_at(I_NODE, y_node) } + 
    433793        if_zero_at(I_TMP_YN) { set_uint8_at(I_NODE, n_node) } 
    434       } 
    435794    end 
    436795 
    437796    def node_switch(table) 
     797      if @debug_mode 
     798        print "// switch (node) {\n" 
     799 
     800        set_flag_at(I_NODE) 
     801        debug_print "loop {" 
     802        debug_indent_incr 
     803        copy_byte(I_NODE_TMP, I_NODE) 
     804 
     805        (1..table.size-1).each {|i| 
     806          decr_at(I_NODE_TMP) 
     807          if table[i] 
     808            print "// case #{i}:\n" 
     809            if_zero_at(I_NODE_TMP) { eval(table[i]) } 
     810          end 
     811        } 
     812        move_to(I_NODE) 
     813        debug_indent_decr 
     814        debug_print "}" 
     815 
     816        print "// }\n" 
     817        return 
     818      end 
     819 
    438820      bf = set_flag_at(I_NODE) + "[" + copy_byte(I_NODE_TMP, I_NODE) 
    439821      (1..table.size-1).each {|i| 
    440822        bf += decr_at(I_NODE_TMP) 
    441         bf += if_zero_at(I_NODE_TMP) { eval(table[i]) } if table[i] 
     823        if table[i] 
     824          bf += if_zero_at(I_NODE_TMP) { eval(table[i]) } 
     825        end 
    442826      } 
    443827      bf + move_to(I_NODE) + "]" 
  • lang/ruby/brainfuck/trunk/mytest.rb

    r2871 r3244  
    77 
    88wr = Brainfuck.writer 
     9wr.debug_mode = false 
    910code = "" 
    1011#code = wr.say("hello.") 
     12#code += wr.say("hello.") 
     13 
     14#code += wr.write_uint8(200) 
     15#code += wr.write_char(0x41) 
     16#code += wr.write_string("\n") 
     17 
     18#code += wr.set_uint8_at(30,5) 
     19#code += wr.mul_uint8(7) 
     20#code += wr.write_byte_as_uint8(30) 
     21#code += wr.write_string("\n") 
     22 
     23code += wr.set_uint8_at(30,50) + wr.set_uint8_at(32,6) 
     24code += wr.move_to(30) + wr.div_byte(32) 
     25code += wr.write_byte_as_uint8(30) + wr.write_string(" / ") + 
     26  wr.write_byte_as_uint8(32) + wr.write_string(" = ") + 
     27  wr.write_byte_as_uint8(12) + wr.write_string(" ... ") + 
     28  wr.write_byte_as_uint8(14) + wr.write_string("\n") 
     29 
     30code += wr.set_uint8_at(30,6) #wr.set_uint8_at(32,6) 
     31 
     32code += wr.write_byte_as_uint8(30) + wr.write_string(" / 2 = ")  
     33code += wr.move_to(30) + wr.div_uint8(2) + 
     34  wr.write_byte_as_uint8(12) + wr.write_string(" ... ") + 
     35  wr.write_byte_as_uint8(14) + wr.write_string("\n") 
    1136 
    1237#256.times{|i| code += wr.write_uint8(i) + wr.write_char(0x20) } 
     
    2247#code += wr.say("goodbye!") 
    2348 
    24 TMP_ADDR = 30 
    25 code += wr.set_uint8_at(TMP_ADDR, 5) + wr.mul_uint8(7) + wr.write_curr_as_uint8 
     49#TMP_ADDR = 30 
     50#code += wr.set_uint8_at(TMP_ADDR, 5) + wr.mul_uint8(7) + wr.write_curr_as_uint8 
     51 
     52#print "code.length = #{code.length}\n" 
     53#print code 
     54#exit 
    2655 
    2756vm = Brainfuck.VM 
    2857vm.load(code) 
    2958vm.run 
    30  
  • lang/ruby/brainfuck/trunk/zu2bf

    r2716 r3244  
    99class Zu2Bf 
    1010  def initialize 
     11    @debug_mode = false 
    1112  end 
     13  attr_accessor :debug_mode 
    1214 
    1315  def compile(src) 
    1416    tg = TextGraph.parse(src) 
    1517    wr = Brainfuck.writer 
     18    wr.debug_mode = @debug_mode 
    1619 
    1720    table = Array.new 
     
    2326      table[1+i] = inst 
    2427    } 
    25     wr.node_switch(table).optimize 
     28    optimize wr.node_switch(table) 
    2629  end 
     30 
     31  def optimize(code) 
     32    return "" unless code and code.length > 0 
     33    begin 
     34      last_length = code.length 
     35      code.gsub!(/<>/,"") 
     36      code.gsub!(/></,"") 
     37    end while code.length < last_length 
     38    code.sub!(/[-+<>,]*\Z/, "") 
     39  end 
     40end 
     41 
     42def fold_string(orig,line_length) 
     43  lines = ((orig.length + (line_length-1)) / line_length).to_i 
     44  s = "" 
     45  (lines - 1).times{|l| 
     46    s += orig[line_length*l .. line_length*l + (line_length-1)] + "\n" 
     47  } 
     48  s += orig[line_length*(lines-1) .. -1] + "\n" if line_length*(lines-1) < orig.length 
     49  s 
    2750end 
    2851 
     
    3457    ARGV.shift 
    3558  end 
     59  options ||= "" 
    3660 
    3761  compiler = Zu2Bf.new 
     62  compiler.debug_mode = true if options =~ /d/ 
    3863 
    3964  bfcode = compiler.compile(ARGF.read) 
    4065 
    41   case options || "" 
     66  case options 
    4267  when /r/ # run 
    4368    bf = Brainfuck.VM 
     
    4873    bf.load bfcode 
    4974    bf.list 
     75  when /d/ # debug 
     76    # nothign 
     77  when /n/ # raw 
     78    print bfcode 
    5079  else 
    51     print bfcode 
     80    print fold_string(bfcode,80) 
    5281  end 
    5382end