Reputation: 31
I'm using Sublime Text 2, Package Control, and SublimeREPL to (try) to create and run python code. The code, a simple Brainfuck interpreter, is as follows. However this isn't the problem:
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 "))
I successfully ran this code in IDLE without a hitch, so I'm certain the code itself isn't an issue.
When using SublimeREPL to run it however, I get the following error:
>>> 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
^
And I have no idea where that's coming from. I can replicate this by simply loading the file into Sublime Text, and selecting Tools > SublimeREPL > Eval in REPL > File.
I've tried Tools > SublimeREPL > Python > Python - RUN Current File as well. When doing this, I receive the following in a REPL tab:
C:\Python27\python.EXE: can't open file '$file_basename': [Errno 2] No such file or directory
***Repl Closed***
I can type in the prompt, but it's completely unresponsive. I guessed it was something wrong with my Python installation. I'm not sure what to do about it or how to fix it, or if it's something I should even be worried about.
As far as I know I've installed SublimeREPL properly, using Package Control and copying the SublimeREPL settings from Settings - Default to Settings - User. I've tried reinstalling my Python installation. Any help is appreciated.
Upvotes: 3
Views: 1565
Reputation: 102842
You are missing something critical in your program - a way to run it. Traditionally, that consists of the following at the end of the file:
if __name__ == '__main__':
main()
Unlike C programs, for example, just defining a main()
function doesn't automatically mean it'll be executed when the file is run - it needs to be called explicitly.
Once I added those lines to the program, I was able to execute the following Hello World program (from Wikipedia):
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
by running Tools -> SublimeREPL -> Python -> Python - RUN Current File
. If you want to interact with the program a little more, first make sure you have a Python REPL open, then select Tools -> SublimeREPL -> Eval in REPL -> File
. This will load the brainfuck()
and main()
functions in memory, so you can run them more than once if you wish.
Upvotes: 1