2013-11-24 1 views
3

저는 파이썬 코드를 만들고 실행하기 위해 Sublime Text 2, Package Control 및 SublimeREPL을 사용하고 있습니다. 간단한 Brainfuck 인터프리터 인 코드는 다음과 같습니다. 그러나 이것은 문제가되지 않습니다 :IDLE에서 유용하지 않은 오류를 반환하는 SublimeREPL

import sys 

def brainfuck(arg_bf_string): 

    bf_string = arg_bf_string 
    bf_string_pointer = 0 

    value_array = [0] * 65536 
    value_array_pointer = 0 

    running = True 
    while running: 

     if bf_string[bf_string_pointer] == ">": 
      value_array_pointer += 1 

     if bf_string[bf_string_pointer] == "<": 
      value_array_pointer -= 1 

     if bf_string[bf_string_pointer] == "+": 
      value_array[value_array_pointer] += 1 

     if bf_string[bf_string_pointer] == "-": 
      value_array[value_array_pointer] -= 1 

     if bf_string[bf_string_pointer] == ".": 
      sys.stdout.write(chr(value_array[value_array_pointer])) 

     if bf_string[bf_string_pointer] == ",": 
      value_array[value_array_pointer] = ord(raw_input("INP ")) 

     if bf_string[bf_string_pointer] == "[": 
      if value_array[value_array_pointer] == 0: 

       loop_depth = 0 
       loop_pointer = bf_string_pointer + 1 
       loop_searching = True 

       while loop_searching: 
        if bf_string[loop_pointer] == "[": 
         loop_depth += 1 

        elif bf_string[loop_pointer] == "]" and loop_depth > 0: 
         loop_depth -= 1 

        elif bf_string[loop_pointer] == "]" and loop_depth == 0: 
         bf_string_pointer = loop_pointer 
         loop_searching = False 

        loop_pointer += 1 

     if bf_string[bf_string_pointer] == "]": 
      if value_array[value_array_pointer] != 0: 

       loop_depth = 0 
       loop_pointer = bf_string_pointer - 1 
       loop_searching = True 

       while loop_searching: 
        if bf_string[loop_pointer] == "]": 
         loop_depth += 1 

        elif bf_string[loop_pointer] == "[" and loop_depth > 0: 
         loop_depth -= 1 

        elif bf_string[loop_pointer] == "[" and loop_depth == 0: 
         bf_string_pointer = loop_pointer 
         loop_searching = False 

        loop_pointer -= 1 

     bf_string_pointer += 1 
     if bf_string_pointer > len(bf_string)-1: 
      running = False 

def main(): 
    brainfuck(raw_input("BF ")) 

내가 성공적으로 차질없이 IDLE에서이 코드를 실행, 그래서 나는 코드 자체는 문제가되지 않습니다 확신합니다. 그러나 그것을 실행하는 SublimeREPL를 사용하는 경우

, 나는 다음과 같은 오류 얻을 :

>>> Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<string>", line 1 
    Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32 
      ^

을 그리고 나는 그에서오고 아무 생각이 없습니다. 파일을 Sublime Text로 로딩하고 REPL> File에서 Tools> SublimeREPL> Eval을 선택하면됩니다.

Tools> SublimeREPL> Python> Python - RUN Current File을 시도했습니다.

C:\Python27\python.EXE: can't open file '$file_basename': [Errno 2] No such file or directory 

***Repl Closed*** 

내가 프롬프트에 입력 할 수 있지만, 완전히 응답입니다 :이 일을 할 때, 나는 REPL 탭에서 다음을받을 수 있습니다. 내 파이썬 설치에 문제가 있다고 생각했다. 나는 그것에 대해 어떻게 해야할지, 어떻게 고쳐야할지, 또는 그것이 내가 걱정해야만하는 것이 었는지 확신 할 수 없다.

내가 아는 한 SublimeREPL을 올바르게 설치했으며 패키지 제어를 사용하고 설정 - 기본값에서 설정 - 사용자로 SublimeREPL 설정을 복사합니다. 파이썬 설치를 다시 시도했다. 어떤 도움을 주셔서 감사합니다.

답변

1

프로그램 실행에있어 중요한 요소가 누락되었습니다. 전통적으로, 즉 파일의 끝에 다음과 같이 구성

C 프로그램과는 달리
if __name__ == '__main__': 
    main() 

, 예를 들어, 단지 main() 함수를 정의하면 자동으로이 파일을 실행할 때 실행됩니다 의미하지 않는다 -을 명시 적으로 호출해야합니다. 나는 프로그램 그 라인을 추가하면

, 나는 (위키 백과에서) 다음의 Hello World 프로그램을 실행 할 수 있었다 :

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. 

Tools -> SublimeREPL -> Python -> Python - RUN Current File을 실행하여. 조금 더 프로그램과 상호 작용하려면 먼저 파이썬 REPL이 열려 있는지 확인한 다음 Tools -> SublimeREPL -> Eval in REPL -> File을 선택하십시오. 그러면 메모리에 brainfuck()main() 함수가로드되므로 원하는 경우 두 번 이상 실행할 수 있습니다.

+0

처음으로 작업했습니다. 후속 시도는 'Tools -> SublimeREPL -> Eval in REPL -> File'로 프로그램을 불러오고 같은 에러를 반환합니다 : '>>> 트레이스 백 (최근 호출 마지막) : 파일 "", 줄 1, 파일 "", 줄 1 Python 2.7.6 (기본값, Nov 10 2013, 19:24:18) [MSC v.1500 32 비트 (Intel)] on win32 ^ SyntaxError : 구문이 잘못되었습니다. 'Tools -> SublimeREPL -> Python -> Python - RUN Current File'은 여전히 ​​작동하지 않습니다. –

관련 문제