#!/usr/bin/python #coding: utf-8 # #Brainfuck Interpreter Maker #Copyright 2011 Sebastian Kaspari (Brainfuck part) import sys import re import MeCab bf_curse = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++." bf_curse2 = ["+++++++++","[",">","++++++++",">","+++++++++++",">","+++++","<<<","-","]",">",".",">","++",".","+++++++","..","+++",".",">","-",".","------------",".","<","++++++++",".","--------",".","+++",".","------",".","--------",".",">","+","."] def execute(filename): words_raw = open(filename).read() tagg = MeCab.Tagger() parse = tagg.parseToNode(words_raw) words = [] while parse: feature = unicode(parse.feature, "utf-8") surface = unicode(parse.surface, "utf-8") if feature.startswith(u"名詞") or feature.startswith(u"感動詞"): words.append(surface) parse = parse.next wDict = {} for word in words: wDict[word] = wDict.get(word, 0) + 1 word_unique = [word for word in words if wDict[word] == 1] if len(word_unique) > len(bf_curse): bf_list = zip(word_unique[:len(bf_curse)], bf_curse) outfilename = filename[:filename.find(".")]+".py" write_script(outfilename, bf_list) print '"'+outfilename+'" was created.' elif len(word_unique) > len(bf_curse2): bf_list = zip(word_unique[:len(bf_curse2)], bf_curse2) outfilename = filename[:filename.find(".")]+".py" write_script(outfilename, bf_list) print '"'+outfilename+'" was created.' else: print "Cannot make brainfuck interpreter. (not enough unique words)" print len(word_unique) print len(bf_curse) exit(-1) def write_script(outfilename, bf_list): fw = open(outfilename, "w") fw.write("""#!/usr/bin/python #coding: utf-8 # # Brainfuck Interpreter # Copyright 2011 Sebastian Kaspari (Original Version) # This code was modified by script named "make_byscript_mecab.py". # # Usage: ./"""+outfilename+""" [FILE] import sys import re import MeCab def execute(filename): words_raw = open(filename).read() tagg = MeCab.Tagger() parse = tagg.parseToNode(words_raw) words = [] while parse: feature = unicode(parse.feature, "utf-8") surface = unicode(parse.surface, "utf-8") if feature.startswith(u"名詞") or feature.startswith(u"感動詞"): words.append(surface) parse = parse.next evaluate(words) def evaluate(code): code = cleanup(code) bracemap = buildbracemap(code) cells, codeptr, cellptr = [0], 0, 0 while codeptr < len(code): command = code[codeptr] if command == ">": cellptr += 1 if cellptr == len(cells): cells.append(0) if command == "<": cellptr = 0 if cellptr <= 0 else cellptr - 1 if command == "+": cells[cellptr] = cells[cellptr] + 1 if cells[cellptr] < 255 else 0 if command == "-": cells[cellptr] = cells[cellptr] - 1 if cells[cellptr] > 0 else 255 if command == "[" and cells[cellptr] == 0: codeptr = bracemap[codeptr] if command == "]" and cells[cellptr] != 0: codeptr = bracemap[codeptr] if command == ".": sys.stdout.write(chr(cells[cellptr])) if command == ",": cells[cellptr] = ord(getch.getch()) codeptr += 1 def cleanup(code): bfDict = { """) for bfword, bfcommand in bf_list: fw.write((u"u'"+bfword+u"':'"+bfcommand+u"',\n").encode("utf-8")) fw.write("""} ans = [bfDict[word] for word in code if (word in bfDict)] tmp = "" for word in ans: tmp = tmp+word return tmp return ans def buildbracemap(code): temp_bracestack, bracemap = [], {} for position, command in enumerate(code): if command == "[": temp_bracestack.append(position) if command == "]": start = temp_bracestack.pop() bracemap[start] = position bracemap[position] = start return bracemap # From http://code.activestate.com/recipes/134892/ # "getch()-like unbuffered character reading from stdin # on both Windows and Unix (Python recipe)" class _Getch: '''Gets a single character from standard input. Does not echo to the screen.''' def __init__(self): try: self.impl = _GetchWindows() except ImportError: self.impl = _GetchUnix() def __call__(self): return self.impl() class _GetchUnix: def __init__(self): import tty, sys def __call__(self): import sys, tty, termios fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch class _GetchWindows: def __init__(self): import msvcrt def __call__(self): import msvcrt return msvcrt.getch() getch = _Getch() #getch() ends here def main(): if len(sys.argv) == 2: execute(sys.argv[1]) else: print "Usage:", sys.argv[0], "filename" if __name__ == "__main__": main() """) def main(): if len(sys.argv) == 2: execute(sys.argv[1]) else: print "Usage:", sys.argv[0], "filename" if __name__ == "__main__": main()