Tuesday, November 18, 2014

Brainf*ck interpreter

I found in my archives a brainf*ck interpreter written five years ago.

Brainf*ck is a 8 (eight) instructions set programming language:

  • < and > walks through a set of pointers (0 -> ...)
  • + and - increases or decreases the current pointer (0 <->255)
  • [ jumps to the related ']' if the current pointer is 0
  • ] jumps back to the related '[' if the current pointer is > 0
  • . displays the character equivalent to the ascii rank defined by the current pointer (65 is 'A')
  • , gets a character and puts it in the current pointer (in this app: waits a key with raw_input!)

This application is on debugging mode: it vertically displays any change of pointer, value and output!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#! /usr/bin/python
 
# Brainf*ck interpreter
# Jean-Christophe Beumier - GPL2 license
# Version 1.00 2009.07.28 -
 
i=0; ptr=0; bytes=[0]; depth=0
 
prg="Capitalizes a minuscule: >>,<<++++[>++[>----<-]<-]>>." # here comes the bf script
end=len(prg)
 
while (i<end):
  char=prg[i] # spells the program string
 
  if (char=='>'):
    ptr=ptr+1
    if ptr>len(bytes)-1: # needs to add a cell to the pointer list
      bytes+=[0]
    print "% 6.f#% 7.f% 4.f  %s" %(i, ptr, bytes[ptr], "pter +1")
 
  elif (char=='<'):
    ptr=ptr-1;
    print "% 6.f#% 7.f% 4.f  %s" %(i, ptr, bytes[ptr], "pter -1")
    if (ptr<0):
      print "*** Negative pointer doesn't work ***"
 
  elif (char=='+'):
    bytes[ptr]=bytes[ptr]+1;
    if bytes[ptr]==256: bytes[ptr]=0
    print "% 6.f#% 7.f% 4.f  %s" %(i, ptr, bytes[ptr],"byte +1")
 
  elif (char=='-'):
    bytes[ptr]=bytes[ptr]-1 ;
    print "% 6.f#% 7.f% 4.f  %s" %(i, ptr, bytes[ptr],"byte -1")
    if bytes[ptr]==-1: bytes[ptr]=255
 
  elif (char=="["):
    if (bytes[ptr]==0):
      print "% 6.f#% 7.f% 4.f  %s" %(i, ptr, bytes[ptr],"cancelled loop!")
      depth=1
      while(depth>0):
        i+=1
        if prg[i] =="[":
          depth+=1
        if prg[i] =="]":
          depth-=1
    else:
      print "% 6.f#% 7.f% 4.f  %s" %(i, ptr, bytes[ptr],"loop in")
 
  elif (char=="]"):
    if (bytes[ptr]>0):
      print "% 6.f#% 7.f% 4.f  %s" %(i, ptr, bytes[ptr],"loop on")
      depth=1
      while(depth>0):
        i=i-1
        if prg[i] =="]":
          depth+=1
        if prg[i] =="[":
          depth-=1
    else:
      print "% 6.f#% 7.f% 4.f  %s" %(i, ptr, bytes[ptr],"loop out ")
 
  elif (char=='.'):
    print "% 6.f#% 7.f% 4.f  %s" %(i, ptr, bytes[ptr],"output: "), chr(bytes[ptr])
 
  elif (char==','):
    print "% 6.f#% 7.f% 4.f  %s" %(i, ptr, bytes[ptr],"input:  "),
    key=raw_input()
    bytes[ptr]=ord(key)
 
  else:
    print "% 6.f#% 7.f% 4.f  Comment: %s" %(i, ptr, bytes[ptr], char)
  i=i+1
 
raw_input("\n###  End of program. Strike [Enter]  ###")

You can try the canonical Hello world!

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

 

 

Add comment

Add comment

authimage