Re: Classifying variable scope...
- To: mathgroup at smc.vnet.net
- Subject: [mg67623] Re: [mg67583] Classifying variable scope...
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sun, 2 Jul 2006 06:28:15 -0400 (EDT)
- Reply-to: hanlonr at cox.net
- Sender: owner-wri-mathgroup at wolfram.com
Here is a start, It hasn't been tested much. I changed LocalVariablesUsed to LocalVariablesNotUsed since the exceptions seem more likely to be of interest. Clear[ClassifyVariables]; ClassifyVariables[func_Symbol]:=Module[ {const,dv,sym,param,locVar,locVar2,gloVar,hsVar,pat,temp, exc=HoldForm/@Join[Union[Head/@ToExpression[Names[]]], {Head,Constant,Pattern}]}, const=HoldForm/@ToExpression[ Select[Names[],MemberQ[Attributes[#],Constant]&]]; dv=ToExpression["DownValues["<>ToString[func]<>"]"]; sym=Complement[ Cases[dv,x_Symbol:>HoldForm[x],Infinity],const]; param=Union[Cases[DownValues[func][[1,1]], x_Pattern:>ToExpression["HoldForm["<>StringTake[temp=ToString[x], StringPosition[temp,"_"][[1,1]]-1]<>"]"],Infinity]]; locVar=Union[Cases[dv,HoldPattern[Module[vl_List,_]]:> (Map[HoldForm,HoldForm[vl],{2}][[1]]/. HoldPattern[Set[v_,_]]:>v),Infinity][[1]]]; temp=Cases[dv,HoldPattern[Module[_List,b_]]:> HoldForm[b],Infinity][[1]]; locVar2=Complement[locVar,Union[ Cases[temp,x_Symbol:>HoldForm[x],Infinity]]]; pat=Complement[Cases[ Cases[temp,x_Pattern:>HoldForm[x],Infinity], x_Symbol:>HoldForm[x],Infinity],exc]; hsVar=Union[Join[pat, Cases[dv,{s_Symbol,_:1,_?NumericQ}:>HoldForm[s],Infinity]]]; gloVar=Complement[sym, param, locVar, hsVar,const,exc]; {Parameters->param, LocalVariables->locVar, GlobalVariablesUsed->gloVar, LocalVariablesNotUsed->locVar2, HiddenScopeVariables->hsVar}]; Clear[dude]; dude[x_Integer,y_] := Module[{z,w}, Do[Print[i],{i,10}];z+q]; x=1; y=2; q=Pi; i=5; ClassifyVariables[dude]//ColumnForm Parameters -> {HoldForm[x], HoldForm[y]} LocalVariables -> {HoldForm[w], HoldForm[z]} GlobalVariablesUsed -> {HoldForm[q]} LocalVariablesNotUsed -> {HoldForm[w]} HiddenScopeVariables -> {HoldForm[i]} ClassifyVariables[ClassifyVariables]//ColumnForm Parameters -> {HoldForm[func]} LocalVariables -> {HoldForm[const], HoldForm[dv], HoldForm[exc], HoldForm[gloVar], HoldForm[hsVar], HoldForm[locVar], HoldForm[locVar2], HoldForm[param], HoldForm[pat], HoldForm[sym], HoldForm[temp]} GlobalVariablesUsed -> {HoldForm[GlobalVariablesUsed], HoldForm[HiddenScopeVariables], HoldForm[HoldForm], HoldForm[Infinity], HoldForm[LocalVariables], HoldForm[LocalVariablesNotUsed], HoldForm[NumericQ], HoldForm[Parameters]} LocalVariablesNotUsed -> {} HiddenScopeVariables -> {HoldForm[b], HoldForm[s], HoldForm[v], HoldForm[vl], HoldForm[x]} Bob Hanlon ---- MS <mark.stankus at gmail.com> wrote: > Hello, > > I would like to have a program ClassifyVariables so that if > > dude[x_,y_] := Module[{z,w}, Do[Print[i],{i,1,10}];z+q]; > > then ClassifyVariables[dude] would yield > > {Parameters->{x,y}, > LocalVariables->{z,w},GlobalVariablesUsed->q,LocalVariablesUsed->{z}, > HiddenScopeVariables->{i}} > > Is there such a program already available? Any suggestions or pieces of > such > a program available? > > Mark >