gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Copyright years
[mjc2wsl.git] / src-wsl / metrics.wsl
1 C:"Doni Pracner (c) 2015";
2 C:"
3 This program is free software; you can redistribute it
4 and/or modify it under the terms of the GNU General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later
7 version.
9 This program is distributed in the hope that it will be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the GNU General Public License for more
13 details.
15 You should have received a copy of the GNU General Public
16 License along with this program. If not, see
17 <http://www.gnu.org/licenses/>.
18 ==========================================================";
20 C:"This program generates metrics for WSL programs,
21 including options for comparing two programs (mainly ment to
22 compare different versions) and output differences as direct
23 numbers and percentages in a CSV type of format.
25 One of the goals was to enable this to be used both in the
26 command line for quick checks but also to be used for
27 automation and creation of CSV files for collections of
28 programs.";
30 C:"usage {option} {filename}";
32 C:"Options";
33 C:" -h or --help - help screen";
34 C:" -H or --header - print header for metrics";
35 C:" -c 'file1' 'file2' - compare two files and print the metrics";
36 C:" -HC print header for comparison";
37 C:" -o 'file' - set output for the print";
38 C:" -s 'separator' - set the separator in outputs (default comma)";
39 C:" otherwise print the metrics of the given file(s)";
41 C:"options are processed in sequence, so you can have
42 for instance multiple outputs during the execution and
43 run it for different programs";
45 C:"examples;
47 'filename' 'file2' 'file3'
48 - just output the metrics all files on the screen,
49 each in it's own row
51 -c 'f1' 'f2'
52 - compare two files and output the differences on the
53 screen
55 -o res.tmp -HC -c 'f1' 'f1a' -c 'f2' 'f2a'
56 - output a header row and two rows comparing the given
57 files in the specified file
58 ";
60 Field_Separator := ",";
62 MW_PROC @Get_New_Name(VAR str) ==
63 IF @Ends_With?(str, ".wsl") THEN
64 str := SUBSTR(str, 0, SLENGTH(str)-4)
65 FI;
66 str := str ++ ".met"
67 END;
69 MW_PROC @Process_File(filename VAR metricslist) ==
70 metricslist := < >;
71 IF @File_Exists?(filename) THEN
72 @New_Program(@Parse_File(filename, T_Statements));
74 C:"add them in reverse to the start of the list";
75 metricslist := < @Struct_Metric(@Program) > ++ metricslist ;
76 metricslist := < @Total_Size(@Program) > ++ metricslist ;
77 metricslist := < @CFDF_Metric(@Program) > ++ metricslist ;
78 metricslist := < @Gen_Type_Count("T_Expression",@Program) > ++ metricslist ;
79 metricslist := < @Stat_Count(@Program) > ++ metricslist ;
80 metricslist := < @Essential(@Program) > ++ metricslist ;
81 metricslist := < @McCabe(@Program) > ++ metricslist
82 ELSE
83 PRINT("ERROR: File ",filename," not found");
84 FI
85 END;
87 MW_PROC @Write_Metrics(metrics VAR) ==
88 FOR met IN metrics DO
89 @WS(Field_Separator);
90 @WN(met)
91 OD
92 END;
94 MW_PROC @Write_Metrics_List() ==
95 @WS("McCabe Cyclo");@WS(Field_Separator);
96 @WS("McCabe Essential");@WS(Field_Separator);
97 @WS("Statements");@WS(Field_Separator);
98 @WS("Expressions");@WS(Field_Separator);
99 @WS("CFDF");@WS(Field_Separator);
100 @WS("Size");@WS(Field_Separator);
101 @WS("Structure")
102 END;
104 MW_PROC @Metrics_Main() ==
105 VAR< prog := < >,
106 filename:="", filename2 := "",
107 metrics := < >, met2 := < >,
108 opened := 0,
109 Argv := @Argv
110 >:
111 C:"First one is the script name that is being executed";
112 Argv := TAIL(Argv);
114 IF Argv = < > THEN
115 PRINT("no arguments passed; supply a filename to make metrics for ");
116 ELSE
117 WHILE Argv <> < > DO
118 POP(filename,Argv);
119 IF filename = "-h" OR filename = "--help" THEN
120 PRINT("HELP - for now in the comments at the start of the script");
121 PRINT("options: --header or -H | -c | -HC | -o | -s");
122 SKIP
123 ELSIF filename = "-H" OR filename = "--header" THEN
124 @WS("filename");@WS(Field_Separator);
125 @Write_Metrics_List();
126 @WL("");
127 ELSIF filename = "-HC" THEN
128 C:"Header for comparison";
129 @WS("filename");@WS(Field_Separator);
130 @Write_Metrics_List();@WS(Field_Separator);
131 @WS("P2 ");
132 @Write_Metrics_List();@WS(Field_Separator);
133 @WS("DIF ");
134 @Write_Metrics_List();@WS(Field_Separator);
135 @WS("% ");
136 @Write_Metrics_List();
137 @WL("");
138 ELSIF filename = "-o" THEN
139 C:"set output";
140 IF Argv = < > THEN
141 PRINT("argument needed after -o")
142 ELSE
143 POP(filename, Argv);
144 opened := opened + 1;
145 @Write_To(filename)
146 FI
147 ELSIF filename = "-s" THEN
148 C:"set separator";
149 IF Argv = < > THEN
150 PRINT("argument needed after -s")
151 ELSE
152 POP(Field_Separator, Argv);
153 FI
154 ELSIF filename = "-c" THEN
155 C:"compare two files and dump the comparison";
156 IF LENGTH(Argv) < 2 THEN
157 PRINT("two arguments needed after -c")
158 ELSE
159 POP(filename, Argv);
161 @Process_File(filename VAR metrics);
162 @WS(filename);
163 @Write_Metrics(metrics);
165 POP(filename2,Argv);
166 @Process_File(filename2 VAR met2);
167 @Write_Metrics(met2);
169 C:"calculate the differences";
170 FOR i := 1 TO LENGTH(metrics) STEP 1 DO
171 met2[i] := metrics[i] - met2[i];
172 IF metrics[i] <> 0 THEN
173 metrics[i] := met2[i] * 100 DIV metrics[i]
174 FI
175 OD;
176 @Write_Metrics(met2);
177 @Write_Metrics(metrics);
179 @WL("");
180 FI;
181 SKIP
182 ELSE
183 @Process_File(filename VAR metrics);
184 @WS(filename);
185 @Write_Metrics(metrics);
186 @WL("");
187 SKIP
188 FI
189 OD;
190 C:"be nice and close all the opened writes";
191 FOR count := 1 TO opened STEP 1 DO
192 @End_Write
193 OD
194 FI
195 ENDVAR
196 END;
198 @Metrics_Main()
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner