summaryrefslogtreecommitdiff
path: root/bf.prn
diff options
context:
space:
mode:
Diffstat (limited to 'bf.prn')
-rw-r--r--bf.prn86
1 files changed, 86 insertions, 0 deletions
diff --git a/bf.prn b/bf.prn
new file mode 100644
index 0000000..11f0e69
--- /dev/null
+++ b/bf.prn
@@ -0,0 +1,86 @@
+20 "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 stackdump
+
+ 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 + get 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