summaryrefslogtreecommitdiff
path: root/bf.prn
blob: c989655999e5cd92eea8fdcf240dcbdbbe33616a (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
900 "TAPELEN" store


##First, read in the source

# ""
# 1 while
# 	getline
# 	dup -1 = if
# 		pop
# 		0
# 	else
# 		"\n" swap + +
# 		1
# 	end
# end
# strlen 1 swap substr swap pop
# #bottom of stack now contains 1 string, the source code
# strlen "len" store
# "code" store

argc 1 = ! if
	"Please give the brainfuck file as a command-line argument." print lf
	exit
end
0 argvget "r" fopen "filehandle" store
""
filehandle fgetc
dup -1 = ! while
	+
	filehandle fgetc
dup -1 = ! end
pop
strlen "len" store
"code" store



##Then, create the tape
TAPELEN
dup 0 > while
	0 swap
1 - dup 0 > end



##Finally, start executing

@defun "execute" {
	#"start of 'execute': " print stackdump

	depth 1 + "depth" gstore

	#"trying to execute " print dup print lf

	"code" store

	0 "i" store
	i len < while
		#"i = " print i print lf
		"code" swapoutvar
		i stridx "c" store
		"code" store
		#"c = " c + print stackdump

		c "#" = if stackdump end
		c "+" = if 1 + 256 % end
		c "-" = if 255 + 256 % end
		c "." = if dup chr print end
		c "," = if
			pop
			getc
			dup -1 = if
				pop 255
			else
				ord 256 %
			end
			#"Input put on stack " print dup print lf
		end
		c ">" = if -1 roll end
		c "<" = if 1 roll end
		c "]" = if
			i "loopend" depth 1 - + gstore
			len "i" store  # == break
		end
		c "[" = if
			i "loopstart" depth + store
			#"loopstart" print depth print " = " print "loopstart" depth + get print lf
			0 "didloop" depth + store
			dup while
				1 "didloop" depth + store
				"code" swapoutvar i 1 + len substr swap "code" store execute
				#stackdump "i = " print i print lf
				"loopstart" depth + get "i" store
			dup end
			"didloop" depth + get ! if
				1 "searchdepth" store
				i 1 + "i" store
				"code" dup swapoutvar
				i len < searchdepth 0 > * while
					i stridx "c" store
					#"Searching index " print i print " (char " print c print ")" print lf
					c "[" = if searchdepth 1 + "searchdepth" store end
					c "]" = if searchdepth 1 - "searchdepth" store end
					i 1 + "i" store
				i len < searchdepth 0 > * end
				#stackdump
				swap store
				searchdepth 0 = ! if
					"No matching ']' found for '[' at index " print loopstart print lf
					exit
				end
				i 1 - "i" store
			else
				"loopend" depth + get 1 + "loopstart" depth + get + "i" store
			end
			#"after loop, i = " print i print lf
		end

		i 1 + "i" store
	i len < end

	depth 1 - "depth" gstore
}

0 "depth" store

"code" swapoutvar
execute  #kaboom

depth 0 = ! if
	"The interpreter exited in depth " depth + "..." + print
end