-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseAndRecognise.~pas
More file actions
102 lines (81 loc) · 2.18 KB
/
Copy pathParseAndRecognise.~pas
File metadata and controls
102 lines (81 loc) · 2.18 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
unit ParseAndRecognise;
interface
uses
UsesUnit, Classes;
function AmountOfOperations(const s: string): Integer; //êîë-âî îïåðàöèé â ñòðîêå
function RecogniseOperation(const Input: string): TConstructionType;
implementation
function ConvertStrToType(const s: string): TConstructionType; //ñòàâèò â ñîîòâåòñòâèå 'if'-ñòðîêå òèï C_if
begin
if s = 'if' then
begin
Result := c_if;
Exit;
end;
if s = 'switch' then
begin
Result := c_switch;
Exit;
end;
if (s = 'while') or (s = 'for') then
begin
Result := c_precycle;
Exit;
end;
if (s = 'do') then
begin
Result := c_postcycle;
Exit;
end;
if (s = 'break') or (s = 'continue') then
begin
Result := c_go;
Exit;
end;
end;
function RecogniseOperation;
var
i, FoundPos: Integer;
NextSymbolAfterID: Char; // ñèìâîë ïîñëå êëþ÷åâîãî ñëîâà, êîòðîå ìû èùåì
begin
Result := C_other; //<-åñëè íè÷åãî íå íàéä¸ì
for i := Low(CIDsOfOperations) to High(CIDsOfOperations) do
begin
FoundPos := Pos(CIDsOfOperations[i], Input);
if FoundPos <> 0 then
NextSymbolAfterID := Input[FoundPos + Length(CIDsOfOperations[i])];
if (FoundPos <> 0) and ((FoundPos + Length(CIDsOfOperations[i]) - 1 = Length(Input)) or (NextSymbolAfterID = ' ') or (NextSymbolAfterID = '(') or (NextSymbolAfterID = ';')) and ((FoundPos = 1) or (Input[FoundPos - 1] = ' ')) then
begin
Result := ConvertStrToType(CIDsOfOperations[i]);
Exit;
end;
end;
//åñëè íè÷åãî íå íàøëè, òî èùåì îïåðàöèþ
for i := Low(COperationsUsed) to High(COperationsUsed) do
begin
FoundPos := Pos(COperationsUsed[i], Input);
if (FoundPos <> 0) then
begin
Result := c_operation;
Exit;
end;
end;
end;
function AmountOfOperations;
var
i, Oper: Integer;
UnParsedString: string;
begin
Result := 0;
UnParsedString := s;
for Oper := 0 to High(COperationsUsed) do
repeat
i := Pos(COperationsUsed[Oper], UnParsedString);
if i <> 0 then //åñëè íàøëè
begin
Inc(Result);
Delete(UnParsedString, i, Length(COperationsUsed[Oper]));
end
until i = 0
end;
end.