Created
May 11, 2013 10:26
-
-
Save suna-pan/5559546 to your computer and use it in GitHub Desktop.
NULL_CHOLL FUCK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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