Print debugging statement: Difference between revisions

m
→‎{{header|Phix}}: added syntax colouring the hard way
(Added Wren)
m (→‎{{header|Phix}}: added syntax colouring the hard way)
Line 488:
was also tweaked to convert supplied addresses, which it previously did not. It proved to be quite an easy enhancement
to the language, albeit as yet undocumented.
 
<lang Phix>function _debug_info()
<!--<lang Phix>-->
-- use throw to convert a return address and routine number
<span style="color: #008080;">function</span> <span style="color: #000000;">_debug_info</span><span style="color: #0000FF;">()</span>
-- from the call stack into a proper line number, etc.
<span style="color: #000080;font-style:italic;">-- use throw to convert a return address and routine number
-- (private, not called direct/from outside this file)
-- from the call stack into a proper line number, etc.
integer rtn
-- (private, not called direct/from outside this file)</span>
atom ret_addr
<span style="color: #004080;">integer</span> <span style="color: #000000;">rtn</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">ret_addr</span>
#ilASM{
[32]
mov edx,[ebp+20] -- prev_ebp
mov eax,[edx+28] -- return address
mov edx,[edx+20] -- prev_ebp
lea edi,[ret_addr]
call :%pStoreMint
mov eax,[edx+8] -- calling routine no
mov [rtn],eax
[64]
mov rdx,[rbp+40] -- prev_ebp
mov rax,[rdx+56] -- return address
mov rdx,[rdx+40] -- prev_ebp
lea rdi,[ret_addr]
call :%pStoreMint
mov rax,[rdx+16] -- calling routine no
mov [rtn],rax
[]
}
<span style="color: #008080;">try</span>
try
<span style="color: #008080;">throw</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ret_addr</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rtn</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
throw({1,ret_addr-1,-1,rtn,-1,-1,-1})
<span style="color: #008080;">catch</span> <span style="color: #000000;">e</span>
catch e
<span style="color: #008080;">return</span> <span style="color: #000000;">e</span>
return e
<span style="color: #008080;">end</span> <span style="color: #008080;">try</span>
end try
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function
<span style="color: #000080;font-style:italic;">-- NOTE: following five routines must all use the exact same nesting level.</span>
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">debug_info</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">_debug_info</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">debug_line</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">_debug_info</span><span style="color: #0000FF;">()[</span><span style="color: #000000;">E_LINE</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">debug_rtn</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">_debug_info</span><span style="color: #0000FF;">()[</span><span style="color: #000000;">E_NAME</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">debug_file</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">_debug_info</span><span style="color: #0000FF;">()[</span><span style="color: #000000;">E_FILE</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">debug_path</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">_debug_info</span><span style="color: #0000FF;">()[</span><span style="color: #000000;">E_PATH</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
test()<!--</lang>-->
 
This can be used as follows (0.8.1+)
-- NOTE: following five routines must all use the exact same nesting level.
 
<!--<lang Phix>-->
global function debug_info()
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">reflections</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
return _debug_info()
end function
<span style="color: #0000FF;">?</span><span style="color: #000000;">debug_info</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">debug_line</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">debug_info</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">debug_line</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">debug_file</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"This is line %d in file %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">debug_line</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">debug_file</span><span style="color: #0000FF;">()})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
 
global function debug_line()
return _debug_info()[E_LINE]
end function
 
global function debug_rtn()
return _debug_info()[E_NAME]
end function
 
global function debug_file()
return _debug_info()[E_FILE]
end function
 
global function debug_path()
return _debug_info()[E_PATH]
end function</lang>
This can be used as follows (0.8.1+)
<lang Phix>include builtins/reflections.e
?debug_info()
?debug_line()
procedure test()
?debug_info()
?debug_line()
?debug_file()
printf(1,"This is line %d in file %s\n",{debug_line(),debug_file()})
end procedure
test()</lang>
{{out}}
<pre>
7,805

edits