[comp.lang.icon] example of function tracing...

sbw@TURING.CSE.NAU.EDU (Steve Wampler) (11/10/90)

A recent Icon Newsletter showed a neat way to trace a builtin
function.  The following little package shows how this might
be used conveniently.  Briefly, the procedure 'trace_option()'
is passed the argument list to 'main'.  The argument '-trace'
turns on general tracing.  Arguments of the form '-Ffunction'
turn on tracing of the builtin function 'function', if possible.

I think the comments explain everything else.

Right now, the package only traces functions that I frequently
want to trace, though it is easy to add tracing of others.

---- snip 'traceset.icn' ----
procedure trace_options(args)
   local nextarg, arg

	#
	# Check arguments for tracing parameters
	#
	#    can trace some builtin functions, e.g.:
	#
	#        -Ftab
	# 
	#    will trace tab()
	#

   every arg := !args do {
      if map(arg) == "-trace" then
         &trace := -1
      else if match("-F",arg) then {	# trace a built-in function
         set_trace(arg[3:0])
         }
      }

   return
end

procedure set_trace(vf)
   local traceset, vp

	#
	# trace the builtin function 'vf', if possible
	#

   if not find("Version 8",&version) then {
      write(&errout,"You are running ",&version,", which doesn't support")
      write(&errout,"   this package.  You need version 8.")
      stop()
      }
  
	# so far, can only trace these (easy to add more)
   traceset := set(["any","bal","copy","find","many","move","tab","upto"])
    
   if member(traceset,vf) then {
      &trace := -1		# have to also trace all procedures!
      vp := vf
      vp[1] := map(vp[1],&lcase,&ucase)
      variable(vp) :=: variable(vf)
      write(&errout, "Calls of '",vf,"' will be traced as calls of '",vp,"'")
      }
   else
      write(&errout, vf, " is not a function that can be traced!")

   return
end

procedure Any(cs,s,i,j)
   return Any(cs,s,i,j)
end

procedure Bal(cs,cl,cr,s,i,j)
   suspend Bal(cs,cl,cr,s,i,j)
end

procedure Copy(cs)
   return Copy(cs)
end

procedure Find(s1,s,i,j)
   suspend Find(s1,s,i,j)
end

procedure Many(cs,s,i,j)
   return Many(cs,s,i,j)
end

procedure Match(s1,s,i,j)
   return Match(s1,s,i,j)
end

procedure Move(cs)
   suspend Move(cs)
end

procedure Tab(i)
   suspend Tab(i)
end

procedure Upto(cs,s,i,j)
   suspend Upto(cs)
end
---- snip 'traceset.icn' end ----

-- 
	Steve Wampler
	{....!arizona!naucse!sbw}
	{sbw@turing.cse.nau.edu}