BabyLanguage.py

a guest
Nov 4th, 2016
2,964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from random import randint
2. nest=0
3. chars="<>+-,.["
4. def genProgram():
5. global chars
6. global nest
7. output=""
8. while randint(0,6) > 0:
9. nextc=randint(0,6)
10. output+=chars[nextc]
11. if nextc == 6:
12. nest+=1
13. if nest < 20:
14. output+=genProgram()
15. nest-=1
16. output+="]"
17. return output
18. def interpret(bf):
19. tape=0
20. memory=[0]
21. point=0
22. while tape<len(bf):
23. c=bf[tape]
24. if c=="<":
25. if point==0:
26. memory=[0]+memory
27. else:
28. point-=1
29. if c==">":
30. point+=1
31. if point==len(memory):
32. memory+=[0]
33. if c=="+":
34. memory[point]+=1
35. if memory[point]==256:
36. memory[point]=0
37. if c=="-":
38. memory[point]-=1
39. if memory[point]==-1:
40. memory[point]=255
41. if c==".":
42. print(chr(memory[point]), end='')
43. if c==",":
44. get=input()
45. if len(get)==0:
46. memory[point]=0
47. else:
48. memory[point]=ord(get[0])
49. if c=="[":
50. if memory[point]==0:
51. openedCount=0
52. while openedCount>=0 or bf[tape]!="]":
53. tape+=1
54. if bf[tape]=="[":
55. openedCount+=1
56. if bf[tape]=="]":
57. openedCount-=1
58. elif c=="]":
59. if memory[point]!=0:
60. openedCount=0
61. while openedCount>=0 or bf[tape]!="[":
62. tape-=1
63. if bf[tape]=="]":
64. openedCount+=1
65. if bf[tape]=="[":
66. openedCount-=1
67. tape+=1
68. foo=genProgram()
69. interpret(foo)