summaryrefslogtreecommitdiff
path: root/bf.prn
blob: 8d17979342cb422a5df5ab25736f4698f4d5febf (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
10000 "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



##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" store

	#"trying to execute " print dup print lf

	"code" depth + store

	0 "i" store
	i len < while
		#"i = " print i print lf
		"code" depth + dup swapoutvar
		i stridx "c" store
		swap store  #uses the dupped name before the matching swapoutvar
		#"c = " c + print lf

		c "+" = if 1 + 256 % end
		c "-" = if 255 + 256 % end
		c "." = if dup chr print end
		c "," = if pop getc ord 256 % end
		c ">" = if 1 roll end
		c "<" = if -1 roll end
		c "]" = if
			i "loopend" depth 1 - + store
			len "i" store  #essentially, break
		end
		c "[" = if
			i "loopstart" depth + store
			#"loopstart" print depth print " = " print "loopstart" depth + get print lf
			dup while
				"code" depth + swapoutvar i 1 + len substr swap "code" depth + store execute
				#stackdump "i = " print i print lf
				"loopstart" depth + get "i" store
			dup end
			"loopend" depth + get 1 + "loopstart" depth + get + "i" store
			#"after loop, i = " print i print lf
		end

		i 1 + "i" store
	i len < end

	depth 1 - "depth" store
}

0 "depth" store

"code" swapoutvar
execute  #kaboom

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