Changeset 3244 for lang/ruby/brainfuck
- Timestamp:
- 12/18/07 02:06:40 (5 years ago)
- Location:
- lang/ruby/brainfuck/trunk
- Files:
-
- 1 added
- 3 removed
- 6 modified
-
. (modified) (1 prop)
-
README (modified) (2 diffs)
-
lib/brainfuck/vm.rb (modified) (2 diffs)
-
lib/brainfuck/writer.rb (modified) (26 diffs)
-
lib/textgraph (deleted)
-
lib/textgraph.rb (deleted)
-
lib/zu+textgraph.README (deleted)
-
mytest.rb (modified) (2 diffs)
-
prime.rb (added)
-
zu2bf (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/brainfuck/trunk
-
Property
svn:ignore set
to
prime2.rb
prime2.bf
prime.bf
-
Property
svn:ignore set
to
-
lang/ruby/brainfuck/trunk/README
r2753 r3244 23 23 zu2bf : 2次元プログラミング言語「ず」のコンパイラ 24 24 25 「ず」については作者のyharaさんの 25 yharaさんの textgraph ライブラリが必要。 26 % gem install textgraph 27 28 プログラミング言語「ず」については作者のyharaさんの 26 29 http://mono.kmc.gr.jp/~yhara/w/?ZuLanguage 27 を参照のこと。 textgraphライブラリ(zu-0.1.0.tar.gzに含まれる)が必要。30 を参照のこと。 28 31 29 生成したコードは、 8ビットでwrap aroundしない処理系では動きません。32 生成したコードは、今のところ8ビットでwrap aroundしない処理系では動きません。 30 33 31 34 % ./zu2bf foo.zu > foo.bf 32 35 33 36 「ず」で書かれたプログラム foo.zu をBrainf*ckオブジェクトコードにコンパイル 37 デフォルトで80字/行に折り畳み。-n を付ければ折り畳まない (0.1.3) 34 38 35 39 % ./zu2bf -r foo.zu … … 43 47 ! 開発履歴 44 48 49 * ver 0.1.3 50 * ver 0.1.2 45 51 * ver 0.1.1 : 2007-12-06夜 46 * ver 0.1.0 : 2007-12-06朝 47 ** 初公開 52 * ver 0.1.0 : 2007-12-06朝 初公開 48 53 49 54 ! 作者 -
lang/ruby/brainfuck/trunk/lib/brainfuck/vm.rb
r2859 r3244 12 12 @wrap_around = true 13 13 end 14 attr_ reader :wrap_around14 attr_accessor :wrap_around 15 15 16 16 def load(code) … … 63 63 when :show_status 64 64 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]) : " -" } 69 70 printf("\n") 70 71 end -
lang/ruby/brainfuck/trunk/lib/brainfuck/writer.rb
r2871 r3244 1 class String2 def optimize3 begin4 last_length = self.length5 self.gsub!(/<>/,"") ; self.gsub!(/></,"")6 end while self.length < last_length7 self.sub!(/[-+<>,]*\Z/, "")8 end9 def fold(per)10 lines = ((length + (per-1)) / per).to_i11 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) < length16 s17 end18 end19 20 1 module Brainfuck 21 2 … … 30 11 31 12 @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 34 28 35 29 I_NODE = 0 … … 44 38 I_TMP_MUL = 9 45 39 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 55 52 56 53 # 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 58 91 59 92 private … … 103 136 # 絶対移動: ix = pos 104 137 def move_to(pos) 138 if @debug_mode 139 debug_print "move_to #{label(pos)}" 140 return 141 end 142 105 143 return "" if pos == @_ix 106 144 move(pos - @_ix) … … 124 162 # while (st[ix]) { ...content... } 125 163 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 126 175 "[" + b.call + "]" 127 176 end 128 177 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 129 192 move_to(k) + "[" + b.call + move_to(k) + "]" 130 193 end 131 194 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 132 208 move_to(flag_pos) + "[" + b.call + clr_at(flag_pos) + "]" 133 209 end … … 135 211 # st[ix] = 0 136 212 def clr 213 if @debug_mode 214 debug_print "clr" 215 return 216 end 217 137 218 _set 0 138 219 "[-]" … … 140 221 141 222 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 142 230 ix_keep = @_ix 143 231 b.call + move_to(ix_keep) … … 150 238 # st[src_pos] の内容は保持される。 151 239 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 152 245 return "" if dest_pos == src_pos 153 246 154 247 save_ix { 155 clr_at(dest_pos) + clr_at(I_TMP_COPY) + 248 clr_at(dest_pos) + clr_at(I_TMP_COPY) + 156 249 repeat(src_pos) { incr_at(dest_pos) + incr_at(I_TMP_COPY) } + 157 250 repeat(I_TMP_COPY) { incr_at(src_pos) } … … 161 254 # st[ix] += st[k] 162 255 def add_byte(k) 256 if @debug_mode 257 debug_print "add_byte #{label(k)}" 258 return 259 end 260 163 261 ix_keep = @_ix 164 262 save_ix { … … 167 265 } 168 266 end 169 # st[ix] -= st[k] 267 268 # st[ix] -= st[k], set borrow_flag if result < 0 170 269 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 # } 171 279 ix_keep = @_ix 172 280 save_ix { 281 reset_flag_at(I_FLAG_BORROW) + 173 282 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 } 175 287 } 176 288 end … … 179 291 # uint8 - 符号なし8ビット整数演算 180 292 # 293 294 # st[ix] = val 181 295 def set_uint8(val) 182 # st[ix] = val 296 if @debug_mode 297 debug_print "set_uint8 #{val}" 298 return 299 end 300 183 301 clr + add_uint8(val) 184 302 end 185 303 304 # st[ix] += val 186 305 def add_uint8(val) 187 # st[ix] += val 306 if @debug_mode 307 debug_print "add_uint8 #{val}" 308 return 309 end 310 188 311 if val == 0 189 312 return "" … … 212 335 end 213 336 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 215 346 216 347 # st[ix] *= val 217 348 def mul_uint8(val) 349 if @debug_mode 350 debug_print "mul_uint8 #{val}" 351 return 352 end 353 218 354 save_ix { 219 355 copy_byte(I_TMP_MUL1,@_ix) + … … 227 363 copy_byte(@_ix, I_TMP_MUL) 228 364 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 230 395 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 235 426 236 427 # boolean 237 428 # st[ix] &= st[k] 238 429 def and_bool(k) 430 if @debug_mode 431 debug_print "and_bool #{label(k)}" 432 return 433 end 434 239 435 save_ix { 240 436 if_zero_at(k) { clr_at(@_ix) } 241 437 } 242 438 end 439 243 440 # st[ix] |= st[k] 244 441 def or_bool(k) 442 if @debug_mode 443 debug_print "or_bool #{label(k)}" 444 return 445 end 446 245 447 save_ix { 246 448 if_nonzero_at(k) { copy_byte(@_ix,k) } 247 449 } 248 450 end 451 249 452 # st[ix] = not st[k] 250 453 def bool_not(k) 454 if @debug_mode 455 debug_print "bool_not #{label(k)}" 456 return 457 end 458 251 459 save_ix { 252 460 copy_byte(I_FLAG_NOT, I_FLAG) + … … 256 464 257 465 # ***_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 267 529 268 530 # ブロック 269 531 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 270 542 # st[pos]が 0 でない場合のみブロックの内容を実行。 271 543 # st[pos]の値は保持される。 … … 278 550 end 279 551 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 280 562 # st[count_pos]に指定された回数だけブロックの内容を実行 281 563 loop_at(count_pos) { b.call + decr_at(count_pos) } … … 284 566 # test系。ここではtestで始まる名前を付けない 285 567 def check_if_zero 568 if @debug_mode 569 debug_print "check_if_zero" 570 return 571 end 572 286 573 # st[ix] の内容が 0 かどうかを I_FLAG にセットする 287 574 copy_byte(I_TMP_ZNZ, @_ix) + … … 292 579 # ix = I_FLAGで戻る 293 580 end 581 294 582 def check_if_nonzero 583 if @debug_mode 584 debug_print "check_if_nonzero" 585 return 586 end 587 295 588 # st[ix] の内容が 0 でないかどうかを I_FLAG にセットする 296 589 copy_byte(I_TMP_ZNZ, @_ix) + … … 301 594 # ix = I_FLAGで戻る 302 595 end 596 303 597 def check_if_n(n) 598 if @debug_mode 599 debug_print "check_if_n #{n}" 600 return 601 end 602 304 603 # st[ix] の内容が n に等しいかどうかを I_FLAG にセットする 305 604 sub_uint8(n) + check_if_zero + add_uint8(n) … … 308 607 # i/o 309 608 def write_char(c) 609 if @debug_mode 610 debug_print "write_char #{c}" 611 return 612 end 613 310 614 # 1文字表示する。引数cはコード。 311 615 save_ix { … … 314 618 end 315 619 def write_string(s) 620 if @debug_mode 621 debug_print "write_string \"#{s.gsub(/\n/,"\\n")}\"" 622 return 623 end 624 316 625 return "" unless s and s.length > 0 317 626 … … 345 654 end 346 655 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) 347 662 save_ix { 348 663 set_uint8_at(I_TMP_PUT,i) + write_curr_as_uint8 … … 350 665 end 351 666 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 352 677 def write_curr_as_uint8 678 if @debug_mode 679 debug_print "write_curr_as_uint8" 680 return 681 end 353 682 # save_ix { 354 683 # st[K_] = k … … 393 722 394 723 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 395 736 # st[pos]=0ならブロックの内容実行 396 737 save_ix { … … 400 741 401 742 def yn_inkey 743 if @debug_mode 744 debug_print "yn_inkey\t\t# I_TMP_YN <-- 'y' ? 1 : 0" 745 return 746 end 402 747 # y か n が入力されるまで入力ループ。 403 748 # I_TMP_YN に結果が入る。(yなら1,nなら0) … … 420 765 421 766 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 422 774 # 文字列 s を表示し改行する。 423 775 write_string(s + "\n") + set_uint8_at(I_NODE, next_node) … … 425 777 426 778 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 427 788 # 文字列(質問文)s を表示し、y/n の入力を促し、I_NODEを適切なノード番号にセットする 428 789 # n_node が先なのはZuの仕様に合わせているため 429 save_ix { 430 write_string(s + " [y/n]\n> ") + 790 write_string(s + " [y/n]\n> ") + 431 791 yn_inkey + 432 792 if_nonzero_at(I_TMP_YN) { set_uint8_at(I_NODE, y_node) } + 433 793 if_zero_at(I_TMP_YN) { set_uint8_at(I_NODE, n_node) } 434 }435 794 end 436 795 437 796 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 438 820 bf = set_flag_at(I_NODE) + "[" + copy_byte(I_NODE_TMP, I_NODE) 439 821 (1..table.size-1).each {|i| 440 822 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 442 826 } 443 827 bf + move_to(I_NODE) + "]" -
lang/ruby/brainfuck/trunk/mytest.rb
r2871 r3244 7 7 8 8 wr = Brainfuck.writer 9 wr.debug_mode = false 9 10 code = "" 10 11 #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 23 code += wr.set_uint8_at(30,50) + wr.set_uint8_at(32,6) 24 code += wr.move_to(30) + wr.div_byte(32) 25 code += 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 30 code += wr.set_uint8_at(30,6) #wr.set_uint8_at(32,6) 31 32 code += wr.write_byte_as_uint8(30) + wr.write_string(" / 2 = ") 33 code += 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") 11 36 12 37 #256.times{|i| code += wr.write_uint8(i) + wr.write_char(0x20) } … … 22 47 #code += wr.say("goodbye!") 23 48 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 26 55 27 56 vm = Brainfuck.VM 28 57 vm.load(code) 29 58 vm.run 30 -
lang/ruby/brainfuck/trunk/zu2bf
r2716 r3244 9 9 class Zu2Bf 10 10 def initialize 11 @debug_mode = false 11 12 end 13 attr_accessor :debug_mode 12 14 13 15 def compile(src) 14 16 tg = TextGraph.parse(src) 15 17 wr = Brainfuck.writer 18 wr.debug_mode = @debug_mode 16 19 17 20 table = Array.new … … 23 26 table[1+i] = inst 24 27 } 25 wr.node_switch(table).optimize28 optimize wr.node_switch(table) 26 29 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 40 end 41 42 def 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 27 50 end 28 51 … … 34 57 ARGV.shift 35 58 end 59 options ||= "" 36 60 37 61 compiler = Zu2Bf.new 62 compiler.debug_mode = true if options =~ /d/ 38 63 39 64 bfcode = compiler.compile(ARGF.read) 40 65 41 case options || ""66 case options 42 67 when /r/ # run 43 68 bf = Brainfuck.VM … … 48 73 bf.load bfcode 49 74 bf.list 75 when /d/ # debug 76 # nothign 77 when /n/ # raw 78 print bfcode 50 79 else 51 print bfcode80 print fold_string(bfcode,80) 52 81 end 53 82 end
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)