Skip to content

Instantly share code, notes, and snippets.

@kb10uy
Last active August 29, 2015 14:05
Show Gist options
  • Save kb10uy/82f3b8392bc5e0ac5b2c to your computer and use it in GitHub Desktop.
Save kb10uy/82f3b8392bc5e0ac5b2c to your computer and use it in GitHub Desktop.
砂利が口の中に入ってじゃりじゃりする~w
#coding: utf-8
#+-----------------------------------------------------------------------------+
#| Brainfuckをじゃりじゃりにしたやつ |
#| GravelTofu v1.0 |
#| MIT License |
#| |
#| ソースコードファイルの拡張子はjknhjかgim2を適当に推奨 |
#| ([砂]利が[口][の][中]に[入]って[じ]ゃりじゃりする~w と |
#| Gravel In My Mouthから採用) |
#| デフォルトでは出力・入力ともにUTF-8なので必要なら適当に変えて |
#| 所定の文字列以外では空白と改行が許されます |
#| |
#+-----------------------------------------------------------------------------+
require "strscan"
class GravelTofu
def initialize()
@s_inc="砂利"
@s_dec="が"
@s_nxt="口"
@s_prv="の"
@s_jlb="中に"
@s_jmp="入って"
@s_put="じゃりじゃり"
@s_get="する~w"
@code=[]
@code_jump=[]
end
def execute(src)
@code=[]
stack=[]
pc=0
s=StringScanner.new(src)
until s.eos?
case
when s.scan(/#{@s_inc}/)
@code << :increment
pc+=1
when s.scan(/#{@s_dec}/)
@code << :decrement
pc+=1
when s.scan(/#{@s_nxt}/)
@code << :next
pc+=1
when s.scan(/#{@s_prv}/)
@code << :previous
pc+=1
when s.scan(/#{@s_jlb}/)
stack.push pc
@code << :jump_label
pc+=1
when s.scan(/#{@s_jmp}/)
left=stack.pop
@code_jump[pc]=left
@code_jump[left]=pc
#p left.to_s + "and"+pc.to_s
@code << :jump
pc+=1
when s.scan(/#{@s_put}/)
@code << :puts
pc+=1
when s.scan(/#{@s_get}/)
@code << :getc
pc+=1
when s.scan(/\s/)
else
puts "許可されていない文字列があります"
exit
end
end
#@code.each{|i|p i}
run
end
def run
cp=0
mem=[]
ptr=0
until @code[cp].nil?
mem[ptr] ||= 0
case @code[cp]
when :increment
mem[ptr]+=1
when :decrement
mem[ptr]-=1
when :next
ptr+=1
when :previous
ptr-=1
when :jump_label
if mem[ptr]==0 then
cp=@code_jump[cp]
end
when :jump
if mem[ptr]!=0 then
cp=@code_jump[cp]
end
when :puts
print mem[ptr].chr Encoding::UTF_8
when :getc
mem[ptr]=$stdin.getc.ord
end
cp+=1
end
end
end
gc=GravelTofu.new()
gc.execute File.read(ARGV[0],:encoding=>Encoding::UTF_8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment