Skip to content

Instantly share code, notes, and snippets.

@kb10uy
Created August 31, 2014 12:22
Show Gist options
  • Save kb10uy/36d8712e891c7f8a15f2 to your computer and use it in GitHub Desktop.
Save kb10uy/36d8712e891c7f8a15f2 to your computer and use it in GitHub Desktop.
#coding: utf-8
require "strscan"
class Notepad
def initialize()
@s_inc="メ"
@s_dec="モ"
@s_nxt="note"
@s_prv="pad"
@s_jlb="元に戻す"
@s_jmp="やり直し"
@s_put="帳"
@s_get=".exe"
@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=Notepad.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