Stack traces: Difference between revisions

1,519 bytes removed ,  14 years ago
m
Unicon/Icon consistency 2
m (Unicon/Icon consistency 2)
m (Unicon/Icon consistency 2)
Line 760:
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}}
{{omit from|M4}}
 
=={{header|Unicon}}==
 
The code for <tt>buildStackTrace</tt> is taken verbatim from the UniLib package.
If that package is installed, then adding:
<lang Unicon>import Utils</lang> at the start would be sufficient.
<lang Unicon>procedure main()
g()
write()
f()
end
 
procedure f()
g()
end
 
procedure g()
# Using 1 as argument omits the trace of buildStackTrace itself
every write("\t",!buildStackTrace(1))
end
 
#<p>
# Compute the current stack trace. Starting at level <i>n</i> above
# the current procedure. Here, <i>n</i> defaults to 0, which will
# include this procedure in the stack trace.
# <i>ce</i> defaults to &current.
# <i>This only works with newer versions of Unicon!</i>
# <[generates the stacktrace from current call back to first
# in the co-expression]>
#</p>
procedure buildStackTrace(n:0, # starting distance from this call
ce # co-expr to trace stack in [&current]
)
local L
/ce := &current
L := []; n -:= 1
while pName := image(proc(ce, n+:=1)) do {
fName := keyword("&file",ce,n) | "no file name"
fLine := keyword("&line",ce,n) | "no line number"
put(L, pName||" ["||fName||":"||fLine||"]" )
}
return L
end</lang>
 
The output of this example is:
<pre>
procedure g [Stacktrace.icn:13]
procedure main [Stacktrace.icn:2]
 
procedure g [Stacktrace.icn:13]
procedure f [Stacktrace.icn:8]
procedure main [Stacktrace.icn:4]
</pre>
Anonymous user