-- FIND GLOBALS -- -- Movie Script -- ---------------------------------------------------------------------- -- The FindGlobals() handler is useful for coming to terms with -- someone else's code, especially if there is a plethora of globals. ---------------------------------------------------------------------- on FindGlobals() ----------------------------------------------------- -- ACTION: Prints in the Message window a list of all globals -- declared in this movie, in alphabetical order. If any -- globals are declared but are never given any values, then -- they appear at the beginning. These are followed by all -- globals that are only ever set to VOID. The last group -- are those which are given a non-VOID value, complete with -- the expressions that give them that value. (The same -- expression may be used more than once, but it only -- appears once in the list. -- OUTPUT: Returns the list of globals that receive a non-VOID value -------------------------------------------------------------------- tGlobals = [:] -- [#globalName: ["globalName = someValue", ...],...] tGlobals.sort() the itemDelimiter = "," -- Iterate through members with scripts, looking for global -- declarations and modifications. tLastCast = the number of castLibs repeat with tCastLib = 1 to tLastCast tLastMember = the number of members of castLib(tCastLib) repeat with tMemberNum = 1 to tLastMember tMember = member(tMemberNum, tCastLib) tScriptText = tMember.scriptText if tScriptText <> "" then AddGlobals(tScriptText, tGlobals) FindGlobalValues(tScriptText, tGlobals) end if end repeat end repeat -- Sort the declared variables into (a) those that are never used, -- (b) those that are only ever set to VOID and (c) those which are -- given a non-VOID value. tUnused = [] tVoided = [] i = tGlobals.count repeat while i tSettings = tGlobals[i] case tSettings.count of 0: -- The global is never initialized. Perhaps it is misspelt tGlobal = tGlobals.getPropAt(i) tUnused.addAt(1, tGlobal) tGlobals.deleteAt(i) 1: if tSettings[1] = "= VOID" then tGlobal = tGlobals.getPropAt(i) tVoided.addAt(1, tGlobal&" = VOID") tGlobals.deleteAt(i) end if end case i = i - 1 end repeat -- Prepare the output for the Message window _ = RETURN tOutput = "" if tUnused.count then put _&"UNUSED"&_&"------"&_&ListToString(tUnused)&_ after tOutput end if if tUnused.count then put _&"VOIDED"&_&"------"&_&ListToString(tVoided)&_ after tOutput end if i = tGlobals.count put _&"USED: "&i&_&"----"&ReportProps(tGlobals) after tOutput put tOutput return tGlobals end FindGlobals on AddGlobals(aScriptText, aGlobalList) ------------------------------- -- SOURCE: Sent by FindGlobals() once per script -- INPUT: will be the string scriptText of a Script -- will be a sorted property list with the -- format: -- [: , ...] -- ACTION: Works through each line of aScriptText, looking for -- global declarations and adding the --------------------------------------------------------------------- repeat while TRUE tOffset = offset("global ", aScriptText) if not tOffset then exit repeat end if tLineNo = the number of lines of char 1 to tOffset of aScriptText tLine = line tLineNo of aScriptText delete line 1 to tLineNo of aScriptText if word 1 of tLine <> "global" then -- Ignore commented lines and lines which simply contain "global" next repeat else -- Don't add "global" to aGlobalList delete word 1 of tLine end if tCount = the number of items of tLine repeat with i = 1 to tCount tItem = item i of tLine tGlobal = word 1 of (tItem) case tGlobal of "\", "": -- TO DO: the declaration may continue on next line next repeat end case tGlobal = symbol(tGlobal) if aGlobalList.findPos(tGlobal) then -- tGlobal is already on the list else aGlobalList[tGlobal] = [] end if if word 2 of tItem starts "--" then -- The rest of the line is commented out exit repeat end if end repeat end repeat end AddGlobals on FindGlobalValues(aScriptText, aGlobalList) ------------------------- -- SOURCE: Sent by FindGlobals() once per script -- INPUT: will be the string scriptText of a Script -- will be a sorted property list with the -- format: -- [: , ...] -- ACTION: Works through aScriptText, looking for lines of code where -- each global variable has been set to some value. Adds -- the expression that defines the new value to the linear -- sublist for the given global. --------------------------------------------------------------------- i = aGlobalList.count() repeat while i tGlobal = string(aGlobalList.getPropAt(i)) tList = aGlobalList[i] tScriptText = aScriptText repeat while TRUE tOffset = offset(tGlobal, tScriptText) if not tOffset then exit repeat end if tLineNo = the number of lines of char 1 to tOffset of tScriptText tLine = line tLineNo of tScriptText delete line 1 to tLineNo of tScriptText if tLine contains "=" then -- The value of tGlobal may be set case word 1 of tLine of tGlobal: -- Continue "set": if word 2 of tLine <> tGlobal then next repeat end if otherwise: tOffset = offset(" then ", tLine) if tOffset then -- Conditional statement: may be a single line delete char 1 to (tOffset + 5) of tLine case word 1 of tLine of tGlobal: -- Continue "set": if word 2 of tLine <> tGlobal then next repeat end if otherwise: -- Value of tGlobal not set in conditional clause next repeat end case else -- Not a conditional statement next repeat end if end case tLine = word 2 to (the number of words of tLine) of tLine if not tList.getPos(tLine) then tList.append(tLine) end if end if end repeat i = i - 1 end repeat end FindGlobalValues