Skip to content

Instantly share code, notes, and snippets.

@suna-pan
Created May 11, 2013 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suna-pan/5559546 to your computer and use it in GitHub Desktop.
Save suna-pan/5559546 to your computer and use it in GitHub Desktop.
NULL_CHOLL FUCK
class NullcholFuck
def initialize(prog)
@program = []
until prog.empty?
/^(あっ|えー|あの|えっと|その|…|、|。)(.*)/ =~ prog
raise "文法に誤りがあります." unless $1
@program << $1
prog = $2
end
end
def exec
pc = pt = 0
ary = [0]
while @program.size > pc
case @program[pc]
when "あっ"
ary[pt+=1] ||= 0
when "えー"
pt -= 1 if pt > 0
when "あの"
ary[pt] += 1
when "えっと"
ary[pt] -= 1
when "その"
print ary[pt].chr
when "…"
ary[pt] = $stdin.getc.bytes.to_a[0]
when "、"
if ary[pt] == 0
jmp = 1
until jmp==0
pc += 1
jmp += 1 if @program[pc] == "、"
jmp -= 1 if @program[pc] == "。"
end
end
when "。"
if ary[pt] != 0
jmp = 1
until jmp==0
pc -= 1
jmp += 1 if @program[pc] == "。"
jmp -= 1 if @program[pc] == "、"
end
end
else
raise "文法に誤りがあります."
end
pc += 1
end
ary[pc]
end
end
begin
file = File.open(ARGV[0])
p = file.read.gsub(/[\n\r\s]/, '')
file.close
rescue
raise "ファイルが開けませんでした."
end
nf = NullcholFuck.new(p)
nf.exec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment