-
Notifications
You must be signed in to change notification settings - Fork 1
/
sbaprogcontrlru.pas
291 lines (248 loc) · 7.92 KB
/
sbaprogcontrlru.pas
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
unit SBAProgContrlrU;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, ComCtrls, Math;
const
cSBACtrlrSignatr='-- /SBA: Controller';
cSBALblSignatr='-- /L:';
cSBAStartProgDetails='-- /SBA: Program Details';
cSBAEndProgDetails='-- /SBA: End';
cSBAStartUSignals='-- /SBA: User Signals';
cSBAEndUSignals='-- /SBA: End';
cSBAStartUProc='-- /SBA: User Procedures';
cSBAEndUProc='-- /SBA: End';
cSBAStartProgUReg='-- /SBA: User Registers';
cSBAEndProgUReg='-- /SBA: End';
cSBAStartProgLabels='-- /SBA: Label constants';
cSBAEndProgLabels='-- /SBA: End';
cSBAStartUserProg='-- /SBA: User Program';
cSBAEndUserProg='-- /SBA: End';
cSBAStartUStatements='-- /SBA: User Statements';
cSBAEndUStatements='-- /SBA: End';
cSBASTPTypedef='subtype STP_type';
type
{ TSBAContrlrProg }
TSBAContrlrProg = class(TObject)
FFileName : string;
STPCnt : integer;
private
procedure SetFilename(AValue: String);
public
constructor Create;
function DetectSBAContrlr(Src:TStrings):boolean;
function ExtractSBALbls(Prog:TStrings; Labels:TListItems): boolean;
function CpyUBlock(Prog,Src:TStrings;BStart,BEnd:String):boolean;
function CpySrc2Prog(Src,Prog:TStrings):boolean;
function CpyProgDetails(Prog,Src:TStrings):boolean;
function CpyUSignals(Prog,Src:TStrings):boolean;
function CpyUProcedures(Prog, Src: TStrings): boolean;
function CpyProgUReg(Prog,Src:TStrings):boolean;
function GenLblandProgFormat(Prog:TStrings; Labels:TListItems ): boolean;
function CpyProgLabels(Labels:TListItems;Src:TStrings):boolean;
function CpyUserProg(Prog,Src:TStrings):boolean;
function CpyUStatements(Prog, Src: TStrings): boolean;
property Filename : String read FFilename write SetFilename;
end;
implementation
uses SBAProgramU, ConfigFormU, UtilsU, DebugU;
{ TSBAContrlrProg }
function TSBAContrlrProg.DetectSBAContrlr(Src: TStrings): boolean;
var I:integer;
begin
Result:=false;
for I:=0 to Min(Src.Count-1,9) do
begin
Result:=(Pos(cSBACtrlrSignatr,Src[i])<>0);
if Result then break;
end;
end;
function TSBAContrlrProg.ExtractSBALbls(Prog: TStrings; Labels: TListItems
): boolean;
var
i,iPos,sblock:integer;
begin
Result:=false;
Labels.Clear;
sblock:=GetPosList(cSBAStartUserProg,Prog);
if sblock=-1 then exit;
for i:=sblock to Prog.Count-1 do
begin
iPos := Pos(cSBALblSignatr, Prog[i]);
if (iPos<>0) then Labels.Add.Caption:=Copy(Prog[i],iPos+length(cSBALblSignatr),100);
if (pos(cSBAEndUserProg,Prog[i])<>0) then break;
end;
Result:=pos(cSBAEndUserProg,Prog[i])<>0;
end;
function TSBAContrlrProg.CpySrc2Prog(Src, Prog: TStrings): boolean;
var
i,startpos,iPos:integer;
function CopyS2P(BStart,BEnd:string):boolean;
var i:integer;
begin
result:=false;
startpos:=GetPosList(BStart,Src);
if startpos>=0 then
begin
For i:=startpos to Src.Count-1 do
begin
Prog.Append(Src[i]);
if (pos(BEnd,Src[i])<>0) then break;
end;
if (pos(BEnd,Src[i])=0) then exit;
Prog.Append('');
result:=true;
end;
end;
begin
Result:=false;
Prog.Clear;
// Program Details
if not CopyS2P(cSBAStartProgDetails,cSBAEndProgDetails) then exit;
// User Signals (Optional)
if CtrlAdvMode then CopyS2P(cSBAStartUSignals,cSBAEndUSignals);
// User Main process Procedures and Functions (Optional)
if CtrlAdvMode then CopyS2P(cSBAStartUProc,cSBAEndUProc);
// Program User Registers and Constants
if not CopyS2P(cSBAStartProgUReg,cSBAEndProgUReg) then exit;
//Check for labels block
startpos:=GetPosList(cSBAStartProgLabels,Src);
iPos:=GetPosList(cSBAEndProgLabels,Src,startpos);
startpos:=GetPosList(cSBAStartUserProg,Src,iPos);
if startpos=-1 then exit;
// SBA User Program
startpos:=GetPosList(cSBAStartUserProg,Src);
if startpos>=0 then
begin
iPos:=0;
For i:=startpos to Src.Count-1 do
begin
iPos := Pos('=>', Src[i]);
if (iPos<>0) then break;
end;
if (iPos=0) or (iPos>20) then exit;
For i:=startpos to Src.Count-1 do
begin
if LeftStr(Src[i],2)='--' then
Prog.Append(Src[i])
else
Prog.Append(Copy(Src[i],iPos,1000));
if (pos(cSBAEndUserProg,Src[i])<>0) then break;
end;
if (pos(cSBAEndUserProg,Src[i])=0) then exit;
Prog.Append('');
end;
// User Statements (Optional)
if CtrlAdvMode then CopyS2P(cSBAStartUStatements,cSBAEndUStatements);
Result:=true;
end;
// Copy User block code from Prog to Src
// BStart and BEnd are start and end block signature constants
function TSBAContrlrProg.CpyUBlock(Prog, Src: TStrings; BStart, BEnd: String
): boolean;
var
i,sblock,eblock,iPos:integer;
begin
Result:=false;
iPos:=GetPosList(BStart,Src);
if iPos=-1 then
begin
Result:=GetPosList(BStart,Prog)=-1;
exit;
end;
while (pos(BEnd,Src[iPos])=0) and (Src.Count>iPos) do Src.Delete(iPos);
if (pos(BEnd,Src[iPos])=0) then exit else Src.Delete(iPos);
sblock:=GetPosList(BStart,Prog);
eblock:=GetPosList(BEnd,Prog,sblock);
if (sblock=-1) or (eblock=-1) then exit;
For i:=eblock downto sblock do Src.Insert(iPos,Prog[i]);
Result:=true;
end;
// Copy Program Details
function TSBAContrlrProg.CpyProgDetails(Prog, Src: TStrings): boolean;
begin
Result:=CpyUBlock(Prog,Src,cSBAStartProgDetails,cSBAEndProgDetails);
end;
// Copy User signals and type definitions
function TSBAContrlrProg.CpyUSignals(Prog, Src: TStrings): boolean;
begin
Result:=CpyUBlock(Prog,Src,cSBAStartUSignals,cSBAEndUSignals);
end;
// Copy Main process user prodecures and functions
function TSBAContrlrProg.CpyUProcedures(Prog, Src: TStrings): boolean;
begin
Result:=CpyUBlock(Prog,Src,cSBAStartUProc,cSBAEndUProc);
end;
//Copy User registers and constants
function TSBAContrlrProg.CpyProgUReg(Prog, Src: TStrings): boolean;
begin
Result:=CpyUBlock(Prog,Src,cSBAStartProgUReg,cSBAEndProgUReg);
end;
// Copy User program
function TSBAContrlrProg.CpyUserProg(Prog, Src: TStrings): boolean;
begin
Result:=CpyUBlock(Prog,Src,cSBAStartUserProg,cSBAEndUserProg);
end;
//Copy User Statements
function TSBAContrlrProg.CpyUStatements(Prog, Src: TStrings): boolean;
begin
Result:=CpyUBlock(Prog,Src,cSBAStartUStatements,cSBAEndUStatements);
end;
// Set Controller file name
procedure TSBAContrlrProg.SetFilename(AValue: String);
begin
if FFilename=AValue then Exit;
FFilename:=AValue;
end;
constructor TSBAContrlrProg.Create;
begin
FFileName:=ProgramsDir+cSBADefaultPrgName;
end;
// Extract Labels and complete steps numbers
function TSBAContrlrProg.GenLblandProgFormat(Prog: TStrings; Labels: TListItems
): boolean;
const
sizestep = 3; //Number of digits for the step
var
i,sblock:integer;
iPos,cnt:integer;
s:String;
begin
Result:=false;
Labels.Clear;
cnt:=1;
sblock:=GetPosList(cSBAStartUserProg,Prog);
if sblock=-1 then exit;
for i:=sblock to Prog.Count-1 do
begin
iPos := Pos(cSBALblSignatr, Prog[i]);
if (iPos<>0) then Labels.Add.Caption:=' constant '+Copy(Prog[i],iPos+length(cSBALblSignatr),100)+': integer := '+Format('%.*d;', [sizestep,cnt]);
iPos := Pos('=>', Prog[i]);
if (iPos=1) then
begin
s:=' When '+Format('%.*d', [sizestep,cnt]);
inc(cnt);
end else S:= Format('%*s',[13+sizestep,' ']);
if LeftStr(Prog[i],2)<>'--' then Prog[i]:=S+Prog[i];
if (pos(cSBAEndUserProg,Prog[i])<>0) then break;
end;
STPCnt:=cnt;
Result:=pos(cSBAEndUserProg,Prog[i])<>0;
end;
// Copy program labels
function TSBAContrlrProg.CpyProgLabels(Labels: TListItems; Src: TStrings
): boolean;
var i,iPos:integer;
begin
Result:=false;
iPos:=GetPosList(cSBAStartProgLabels,Src);
if iPos=-1 then exit;
Inc(iPos);
while (pos(cSBAEndProgLabels,Src[iPos])=0) and (Src.Count>iPos) do Src.Delete(iPos);
if (pos(cSBAEndProgLabels,Src[iPos])=0) then exit;
For i:=Labels.Count-1 downto 0 do Src.Insert(iPos,Labels[i].Caption);
iPos:=GetPosList(cSBASTPTypedef,Src);
if iPos<>-1 then Src[iPos]:=' subtype STP_type is integer range 0 to '+inttostr(STPCnt)+';';
Result:=true;
end;
end.