gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
ant - default taks calls metrics as well
[mjc2wsl.git] / src-wsl / transf-exp.wsl
1 C:"
2 Copyright (C) 2012,2014,2015 Doni Pracner http://perun.dmi.rs/pracner
4 This program is free software; you can redistribute it
5 and/or modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later
8 version.
10 This program is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied
12 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU General Public License for more
14 details.
16 You should have received a copy of the GNU General Public
17 License along with this program. If not, see
18 <http://www.gnu.org/licenses/>.
19 ==========================================================";
21 C:"Automatic transformation tool for simplification of WSL";
22 C:"code automaticaly translated from a low level language.";
23 C:"such as assembly or bytecode generated by our translators.";
25 C:"experimental version";
27 C:"set the following so that Action Systems are not automaticaly treated as regular";
28 Assume_A_S_Regular := 0;
30 MW_PROC @Process_Prog() ==
32 C:"start with a few 'sure' ones, that don't need to be repeated";
34 @Trans(TR_Delete_All_Skips, "");
36 C:"remove all the comments ";
37 FOREACH Statement DO
38 IF @ST(@I) = T_Comment THEN
39 @Delete
40 FI
41 OD;
43 old := < >;
44 anotherrun := 1;
45 C:"main loop that applies transformations while we have changes";
46 WHILE anotherrun > 0 DO
48 C:"remember the previous version of the program";
49 old := @Program;
51 FOREACH Statement DO
52 IF @Trans?(TR_Simplify_Item) THEN
53 @Trans(TR_Simplify_Item,"")
54 FI
55 OD;
57 C:"Convert DO loops into WHILE loops";
58 FOREACH Statement DO
59 IF @Trans?(TR_Floop_To_While) THEN
60 @Trans(TR_Floop_To_While, "");
61 FI
62 OD;
63 FOREACH Statement DO
64 IF @Trans?(TR_Flag_Removal) THEN
65 @Trans(TR_Flag_Removal, "");
66 FI
67 OD;
68 C:"Go back to the start, and remove redundant";
69 @Goto(< >);
70 IF @Trans?(TR_Delete_All_Redundant) THEN
71 @Trans(TR_Delete_All_Redundant, "");
72 FI;
73 IF @Trans?(TR_Constant_Propagation) THEN
74 @Trans(TR_Constant_Propagation, "");
75 FI;
78 FOREACH Statement DO
79 IF @ST(@I) = T_A_S THEN
80 IF @Trans?(TR_Simplify_Action_System) THEN
81 @Trans(TR_Simplify_Action_System, "")
82 FI;
83 IF @Trans?(TR_Collapse_Action_System) THEN
84 @Trans(TR_Collapse_Action_System, "");
85 FI;
86 FI
87 OD;
89 ATEACH Statement DO
90 IF @Trans?(TR_Stack_To_Var) THEN
91 @Trans(TR_Stack_To_Var,"")
92 FI
93 OD;
95 FOREACH Statement DO
96 IF @Trans?(TR_Push_Pop) THEN
97 @Trans(TR_Push_Pop, "");
98 FI
99 OD;
101 C:"test if there were actuall changes from the
102 transformations and decide to go anotherrun";
103 IF @Equal?(old,@Program) THEN
104 anotherrun := 0;
105 ELSE
106 anotherrun := 1;
107 FI;
109 OD;
111 C:"This makes sense after all of the other
112 transformations, there are probably no procedures at
113 the start of the process";
115 C:"start of the program";
116 @Goto(< >);
117 C:"'manual' navigating to find the procedures";
119 FOREACH Statement DO
120 IF @ST(@I) = T_Where
121 THEN @Down_To(2); @Down; C:" to first defn ";
122 DO IF @ST(@I) = T_Proc THEN
123 IF @Trans?(TR_Stack_To_Par) THEN
124 @Trans(TR_Stack_To_Par, "") FI;
125 FI;
126 IF @Right? THEN @Right ELSE EXIT(1) FI OD;
127 @Up; @Up FI OD;
130 IF @Trans?(TR_Remove_All_Redundant_Vars) THEN
131 @Trans(TR_Remove_All_Redundant_Vars, "");
132 FI;
134 SKIP
135 END;
137 MW_PROC @Prog_Stat_Comp(Pro, After VAR)==
138 VAR < ma := 0, mb :=1 > :
139 ma := @McCabe(Pro);
140 mb := @McCabe(After);
141 PRINT ("McCabe ", ma, " ", mb, " ",(mb-ma));
142 ma := @Stat_Count(Pro);
143 mb := @Stat_Count(After);
144 PRINT ("Statem ", ma, " ",mb, " ",(mb-ma));
145 ma := @CFDF_Metric(Pro);
146 mb := @CFDF_Metric(After);
147 PRINT ("CF/DF ", ma," ", mb," ", (mb-ma));
148 ma := @Total_Size(Pro);
149 mb := @Total_Size(After);
150 PRINT ("Size ", ma," ", mb, " ",(mb-ma));
151 ma := @Struct_Metric(Pro);
152 mb := @Struct_Metric(After);
153 PRINT ("Struct ", ma, " ",mb, " ",(mb-ma));
154 SKIP
155 ENDVAR
156 END;
158 MW_PROC @Get_New_Name(VAR str) ==
159 IF @Ends_With?(str, ".wsl") THEN
160 str := SUBSTR(str, 0, SLENGTH(str)-4)
161 FI;
162 str := str ++ "_t.wsl"
163 END;
165 MW_PROC @Process_File(filename) ==
166 IF @File_Exists?(filename) THEN
167 @New_Program(@Parse_File(filename, T_Statements));
168 PRINT("Processing: ", filename);
169 prog := @Program;
170 @Process_Prog();
171 @Get_New_Name(VAR filename);
172 @PP_Item(@Program, 80, filename);
173 PRINT("<Metrics>");
174 PRINT(filename);
175 @Prog_Stat_Comp(prog, @Program);
176 PRINT("</Metrics>")
177 ELSE
178 PRINT("ERROR: File ",filename," not found");
179 FI
180 END;
182 C:"Main program - processes the arguments";
183 VAR< prog := < >, inifilename := "transf.ini",
184 filename:="", file := "", inifile:= "",
185 Argv := ARGV
186 >:
187 C:"First one is the script name that is being executed";
188 Argv := TAIL(Argv);
190 IF Argv = < > THEN
191 PRINT("no arguments passed; using ",inifilename);
192 IF @File_Exists?(inifilename) THEN
193 inifile := @Open_Input_File(inifilename);
194 filename := @Read_Line(inifile);
195 WHILE NOT @EOF?(filename) DO
196 PRINT("");
197 @Process_File(filename);
198 filename := @Read_Line(inifile)
199 OD;
200 @Close_Input_Port(inifile);
201 ELSE
202 PRINT("ini file (",inifilename,") not found.",
203 " it should contain a list of filenames to be converted");
204 PRINT("OR you can give command line arguments to be processed");
205 FI
206 ELSE
207 FOR arg IN Argv DO
208 @Process_File(arg);
209 OD
210 FI
211 ENDVAR
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner