3

This is the code for my interpereter:

program=list(raw_input('Program: '))
array = [0 for i in range(100)]
pointer=0
prgpointer=0
run = True
while run:
    try:
        command = program[prgpointer]
        if command == '>':
            pointer += 1
        if command == '<':
            pointer -= 1
        if command == '+':
            array[pointer]=array[pointer]+1
        if command == '-':
            array[pointer]=array[pointer]-1
        if command == '.':
            print(array[pointer])
        if command == '[' and array[pointer]==0:
            while program[prgpointer]!=']':
                prgpointer+=1
        if command == ']' and array[pointer]!=0:
            while program[prgpointer]!='[':
                prgpointer-=1        
    except:
        run = False  
    prgpointer+=1

When I run this program:

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

I get the result of

-1
-3
4
4
7
0
-2
7
10
4
-4
1
1

This porgram is a functional 'hello world' program in any other bf interpereter. Even when the output is converted to ASCII it is not 'Hello World' Is there any major problem that can be pointed out about my interpereter? Are the commands correct?

T Williams
  • 45
  • 5

1 Answers1

4
  1. To print the char value at your data pointer you need sys.stdout.write(chr(array[pointer])) because otherwise you'll just print numbers -- after all numbers are what's in your list.

  2. The language spec says that [ and ] represent jumps to the matching bracket. You are jumping to the next/previous bracket.

Jason S
  • 13,538
  • 2
  • 37
  • 42
  • Thank you. I understood part 1 in writing but when displaying the ascii characters for my broken results, all I receved was blank space. I fixed my interpereter for part 2 as well – T Williams Feb 22 '16 at 03:01
  • Not all ascii codepoints are displayable. Most of the low numbers are control characters and there aren't any negative ones. – Jason S Feb 22 '16 at 03:02
  • E.g. the letter 'H' is 72. Fix the looping and you should have much larger #s in your data array. – Jason S Feb 22 '16 at 03:03
  • It was also necessary for me to force the array values to act like signed 8-bit numbers, for program compatibility. – T Williams Feb 22 '16 at 15:44