how to check for unnecessary local declarations?

Mark Wieder mwieder at ahsoftware.net
Fri Jul 3 02:36:10 EDT 2015


On 07/02/2015 08:20 PM, Kay C Lan wrote:
> Which proves the point that it isn't as easy as you first made out. As you
> say, cracking the GLX2 code is probably the easiest route to take and I'd
> be surprise if that code wasn't developed over a long period of time of
> 'discovering' odd edge cases cropping up here and there, that Jerry never
> envisage in a single session of coding.

Well, here's the easy part from glx2:

-- gather the script local variables
function getLocals pScript
   local tTemp, tLocals

   filter pScript with "local *"
   repeat for each line tLine in pScript
     put word 2 to -1 of tLine & cr after tTemp
   end repeat
   -- allow for multiple declarations in one line
   replace "," with cr in tTemp
   repeat for each line tLine in tTemp
     put word 1 of tLine & cr after tLocals
   end repeat
   chomp tLocals -- chomp is in the frontscript, removes trailing cr
   set the tLocals of this card to tLocals
end getLocals

-- here's the part that looks at usage (watch the lineWrap)

   --theHandler here could be the entire script of a single handler
   repeat for each line theLine in theHandler
     if word 1 to 3 of theLine is "repeat for each" then
       put word 5 of theLine into theWord
       if theTemps is empty then
         put theWord into theTemps
       else
         if theWord is not among the lines of theTemps then
           put cr & theWord after theTemps
         end if
       end if
       next repeat
     end if
     if "into" is among the words of theLine or "after" is among the 
words of theLine or "before" is among the words of theLine or word 1 of 
theLine is "repeat then
       put 0 into tWordNum
       repeat for each word theWord in theLine
         add 1 to tWordNum
         if char 1 of theWord is "#" or char 1 to 2 of theWord is "--" then
           exit repeat
         end if
         if char 1 of theWord is quote then
           if theTemps is empty then
             put theWord into theTemps
           else
             if theWord is not among the lines of theTemps then
               put cr & theWord after theTemps
             end if
           end if
           next repeat
         end if
         if theWord is among the items of "each,with,into,after,before" then
	  put word tWordNum+1 of theLine into theNextWord
           if "(" is in theNextWord then
             next repeat
           end if
           put token 1 of theNextWord into theNextWord
           if theNextWord is not among the lines of theTokens and 
theNExtWord is not empty then
             if "[" is in theNextWord then
               replace "[" with cr in theNextWord
               replace "]" with "" in theNextWord
             end if
             if theTemps is empty then
               put theNextWord into theTemps
             else
               if theNextWord is not among the lines of theTemps then
                 put cr & theNextWord after theTemps
               end if
             end if
           end if
         end if
       end repeat
     end if
   end repeat

-- 
  Mark Wieder
  ahsoftware at gmail.com




More information about the use-livecode mailing list