Rebol3 Code Examplex


Stack traces

Print out the current call stack.

Rebol [
    title: "Rosetta code: Stack traces"
    file:  %Stack_traces.r3
    url:   https://rosettacode.org/wiki/Stack_traces
]

stack-info: function[depth [integer!]][
    print [as-yellow "Current stack depth:" stack/depth 0]
    print [as-yellow "Stack bounds:       " stack/limit 0]
    print  as-yellow "Stack functions (with arguments):"
    n: 1
    while [all [n < depth w: stack/word n]][
        print as-green w   ;= Function or object name, if known
        probe stack/args n ;= Block of args
        print ""
        ++ n
    ]
]

fun1: does [stack-info 6]
fun2: does [fun1]
fun2

print-hline
print "Using TRACE function:"
trace on
fun3: does [print "function3"]
fun3
trace off

Output:

Current stack depth: 21
Stack bounds:        16032
Stack functions (with arguments):
all
[[all [n < depth w: stack/word n]] [
    print as-green w
    probe stack/args n
    print ""
    ++ n
]]

while
[6 _ 2 while]

stack-info
[]

fun1
[]

fun2
[[
    stack-info: function [depth [integer!]] [
        print [as-yellow "Current stack depth:" stack/depth 0]
        print [as-yellow "Stack bounds:       " stack/limit 0]
        print as-yellow "Stack functions (with arguments):"
        n: 1
        while [all [n < depth w: stack/word n]] [
            print as-green w
            probe stack/args n
            print ""
            ++ n
        ]
    ]
    fun1: does [stack-info 6]
    fun2: does [fun1]
    fun2
    print-hline
    print "Using TRACE function:"
    trace on
    fun3: does [print "function3"]
    fun3
    trace off
] _ _ _ #(true) _ _]

-----------------------------------------------------------------------------------------------------------------------
Using TRACE function:
   <-- trace == #(unset)
18: fun3:
19: does : function! [body]
20: [print "function3"]
   --> does
    1: make : action! [type spec]
    2: function! : #(function!)
    3: copy/deep
    4: reduce : native! [value /no-set /only words /into out]
    5: [[] body]
      --> reduce
       1: []
       2: body : [print "function3"]
      <-- reduce == [[] [print "function3"]]
      --> copy
      <-- copy == [[] [print "function3"]]
      --> make
      <-- make == make function! [[][print "function3"]]
   <-- does == make function! [[][print "function3"]]
21: fun3 : function! []
   --> fun3
    1: print : native! [value]
    2: "function3"
      --> print
function3
      <-- print == #(unset)
   <-- fun3 == #(unset)
22: trace : native! [mode /back /function]
23: off : #(false)
   --> trace