-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainForm 2.pas
More file actions
219 lines (189 loc) · 5.55 KB
/
mainForm 2.pas
File metadata and controls
219 lines (189 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
unit mainForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, RegExpr,
Vcl.ComCtrls;
type
TfrmMain = class(TForm)
menu: TMainMenu;
menuFile: TMenuItem;
fileOpen: TMenuItem;
fileSpace: TMenuItem;
fileExit: TMenuItem;
gbContent: TGroupBox;
openDialog: TOpenDialog;
gbMetr: TGroupBox;
lblLPC: TLabel;
edtLPC: TEdit;
lblAPC: TLabel;
edtAPC: TEdit;
lblMI: TLabel;
edtMaxInter: TEdit;
richEdt: TRichEdit;
procedure fileExitClick(Sender: TObject);
procedure fileOpenClick(Sender: TObject);
procedure richEdtChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.fileExitClick(Sender: TObject);
begin
Close;
end;
procedure TfrmMain.fileOpenClick(Sender: TObject);
begin
openDialog.Execute;
if FileExists(openDialog.FileName) then
richEdt.Lines.LoadFromFile(openDialog.FileName);
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
richEdt.WantTabs := true;
end;
procedure TfrmMain.richEdtChange(Sender: TObject);
type
TArray = array of integer;
const
OPER_EXPR_QUANTITY = 10;
OPERATOR_EXPR: array [0..OPER_EXPR_QUANTITY-1] of string = (
'(\.eq(ua)?l\?)|(\bdefined\?)|(\bnot\b)|(\band\b)|(\bor\b)',
'(\[)|(\{)|(::)',
'[\^\/\%\+\-]\=?', // ^ / % + - self and with =
'([\&\|])(\1|\=)?', //& && &= | || |=
'\*\*?\=?', // * ** *= **=
'(\<\>)|(\=\>\>?)|(\<\<\=)|((\<|\>)(\5|\=)?)',//grate or less sign
'[^\-\+\*\/\%\.\&\|\^\<\>\!\=]\={1,3}([^\>\=\~$])', // = == ===
'[\=]?~', // ~ self and with =
'(\.){1,3}',
'(![\~\=]?)' // ! self and with ~ or =
);
CUT_EXPR = '(?m)((\"|\'').*?\2)|((\/\/|\#).*?$)|((=begin).*?(=end))';
IF_UNLESS_EXPR = '((if)|(unless))';
ELSIF_EXPR = '(\b(elsif)\b)';
TERN_OP_EXPR = '(\?.+?\:)';
TERN_QSTN_EXPR = '(\s*?\?\\s*?)';
TERN_TWODOT_EXPR = '(\b\:\b)';
END_WORD_EXPR = '(end)';
THEN_WORD_EXPR = '(then)';
END_BLOCK_EXPR = '(\b(while)\b)|(\b(until)\b)|(\b(begin)\b)|(\b(def)\b)|'+
'(\b(class)\b)';
var
i, quantityOfOperators,quantityOfConditionals, maxInterior: Integer;
rubyCodeStr: String;
function QuantityOfSubStr(str, subStrExp: String): Integer;
var
regExp: TRegExpr;
quantity: Integer;
begin
regExp := TRegExpr.Create;
regExp.Expression := subStrExp;
quantity := 0;
if (regExp.Exec(str)) then
repeat
inc(quantity);
until not (regExp.ExecNext);
regExp.Free;
Result := quantity;
end;
function MaxOfArray(arrayOfLegths: TArray):Integer;
var
i,maxLength: Integer;
begin
maxLength:=0;
for i := 0 to Length(arrayOfLegths)-1 do
if arrayOfLegths[i] > maxLength then
maxLength := arrayOfLegths[i];
Result := maxLength;
end;
function StrReplace(strForRep, beforeRep, afterRep: String): String;
var
regExp: TRegExpr;
begin
regExp := TRegExpr.Create;
regExp.Expression := beforeRep;
strForRep := regExp.Replace(strForRep, afterRep, true);
regExp.Free;
Result := strForRep;
end;
function FindIfInterior(str: String): TArray;
type
TStack = record
maxOfStack: Integer;
level: Integer;
end;
var
regExp: TRegExpr;
interOfIf: TArray;
stack: TStack;
procedure UpdateStack(match: String ; var stack: TStack);
var
ifIsExists, unlessIsExists: Boolean;
begin
match:=match.Trim;
if (match = 'end')and(stack.level <> 0) then
dec(stack.level)
else begin
ifIsExists := pos('if',match)<>0;
unlessIsExists := pos('unless',match)<>0;
if not ((ifIsExists) and (match.Length <> 'if'.Length))or
((unlessIsExists) and (match.Length <> 'unless'.Length)) then
inc(stack.level);
if ( ifIsExists )or(match = 'elsif')or( unlessIsExists ) then
inc(stack.maxOfStack);
end;
end;
begin
regExp := TRegExpr.Create;
regExp.Expression := IF_UNLESS_EXPR+'|'+ELSIF_EXPR+'|'+THEN_WORD_EXPR+'|'+
END_WORD_EXPR;
stack.level := 0;
stack.maxOfStack := 0;
if (regExp.Exec(str)) then
repeat
UpdateStack(regExp.Match[0], stack);
if stack.level = 0 then
begin
SetLength(interOfIf, Length(interOfIf) + 1);
interOfIf[Length(interOfIf) - 1] := stack.maxOfStack;
stack.maxOfStack := 0;
end;
until not (regExp.ExecNext);
Result := interOfIf;
regExp.Free;
end;
function MaxInteriorCounter(str: String):Integer;
var
interiors: TArray;
begin
interiors := FindIfInterior(str);
if (Length(interiors) <> 0) then
Result := maxOfArray(interiors) - 1
else
Result := 0;
end;
procedure UpdateMetrics(quantCondOper, quantAllOper, maxInter: Integer);
begin
edtLPC.Text := quantCondOper.ToString();
inc(quantAllOper, quantCondOper);
edtAPC.Text := edtLPC.Text+'/' + quantAllOper.ToString();
edtMaxInter.Text := maxInter.ToString();
end;
begin
rubyCodeStr := StrReplace(richEdt.Lines.Text, CUT_EXPR, '');
quantityOfOperators:=0;
for i := 0 to OPER_EXPR_QUANTITY - 1 do
inc(quantityOfOperators, QuantityOfSubStr(rubyCodeStr,OPERATOR_EXPR[i]));
quantityOfConditionals := QuantityOfSubStr(rubyCodeStr,
TERN_OP_EXPR+'|'+IF_UNLESS_EXPR+'|'+ELSIF_EXPR);
maxInterior := MaxInteriorCounter(rubyCodeStr);
UpdateMetrics(quantityOfConditionals,quantityOfOperators,maxInterior);
end;
end.