Working on GC.
[scheme.forth.jl.git] / debugging.4th
diff --git a/debugging.4th b/debugging.4th
new file mode 100644 (file)
index 0000000..54e17a5
--- /dev/null
@@ -0,0 +1,40 @@
+\ Words useful for debugging
+
+: return-depth
+    RSP@ RSP0 - 1- ;
+
+: stack-trace
+    RSP@ RSP0 1+ do
+        i 0> if
+            i @ 1- dup 0> if
+                @ dup 0> if
+                    >name dup 0> if
+                        cr .name
+                    else
+                        cr ." ***"
+                        drop
+                    then
+                else
+                    cr ." ***"
+                    drop
+                then
+            else
+                cr ." ***"
+                drop
+            then
+        else
+            cr ." ***"
+            drop
+        then
+                
+        loop ;
+
+: trace
+    cr ." ---" cr
+    ." Return stack depth:" return-depth . cr
+    ." Stack trace:"
+    stack-trace
+    cr ." ---" cr
+
+    trace
+;