-
Notifications
You must be signed in to change notification settings - Fork 2
/
PAR.PAS
338 lines (312 loc) · 10.5 KB
/
PAR.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
{/////////////////////////////////////////////////////////////////////////
//
// Dos Navigator Version 1.51 Copyright (C) 1991-99 RIT Research Labs
//
// This programs is free for commercial and non-commercial use as long as
// the following conditions are aheared to.
//
// Copyright remains RIT Research Labs, and as such any Copyright notices
// in the code are not to be removed. If this package is used in a
// product, RIT Research Labs should be given attribution as the RIT Research
// Labs of the parts of the library used. This can be in the form of a textual
// message at program startup or in documentation (online or textual)
// provided with the package.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// "Based on Dos Navigator by RIT Research Labs."
//
// THIS SOFTWARE IS PROVIDED BY RIT RESEARCH LABS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The licence and distribution terms for any publically available
// version or derivative of this code cannot be changed. i.e. this code
// cannot simply be copied and put under another distribution licence
// (including the GNU Public Licence).
//
//////////////////////////////////////////////////////////////////////////}
unit Par;
interface
var ErrOcc : Boolean;
ErrStr : String;
CellOccured: Boolean;
type
FPtr = ^FRec;
FArray = Array[1..50] of FPtr;
PFArray = ^FArray;
OpType = (opCell,opFormula,opValue,opFunc,opSign,opSFunc);
FRec = Record
Owner: FPtr;
Name: String;
Tp: OpType;
case OpType of
opCell: (Left, Right: FPtr);
opFunc: (ParamsNum: Byte; Params: PFArray);
end;
OpString = String[2];
Const
Signs : Set of Char = ['+','-','*','/','^','=','|','\','&','%','#','~','>','<',':'];
FSigns : Set of Char = ['+','-','*','/','^',':'];
UnarySigns: Set of Char = ['+','-','~'];
MaxOperations = 26;
Prior : Array [1..MaxOperations] of OpString =
('||','&&','^^','~','=','<','>','==','>=','=>','<=','=<',
'<>','><','\','|','+','-','^','>>','<<','&','%','*','/',':');
{procedure GetToken( St : String; Start : Byte; var O : String;var Tp : OpType);
}procedure Error( Message : String);
function IsPrior (c1,c2 : String) : Boolean;
function GetFormulaTree(St : String) : FPtr;
function GetFormula(const S: String): FPtr;
implementation
uses Advance;
procedure GetToken( St : String; var Start : Integer; var O : String;var Tp : OpType);
var I,L : Integer;
S,S1 : String;
B: Boolean;
begin
UpStr(St);
O:=Copy(St,Start,250);
if O[1]='(' then Tp:=opFormula
else if (O[1]>='0') and (O[1]<='9') or (O[1] = '$') or (O[1] = '.')
then Tp:=opValue
else if (O[1]>='A') and (O[1]<='Z') or (O[1] = '@')
then Tp:=opCell
else if O[1] in signs
then
begin
S:=O[1];
if (O[2] in signs) and not (O[1] in UnarySigns)
and not (O[2] in UnarySigns)
then S:=S+O[2];
O:=S;
Tp:=opSign;
Inc(Start, Length(S));
Exit;
end
else
begin
Error('Invalid expression was occurred');
Exit;
end;
L:=1;
S:='';
Case Tp of
opFormula : begin
i:=1;
S:=O[1];
repeat
Inc(L);
AddStr(S, O[L]);
Inc(i,Byte(O[L]='(')-Byte(O[L]=')'));
until ((i=0) and (O[L]=')')) or (L>Byte(O[0]));
Inc(Start, Length(S));
end;
opValue : begin
B := On;
repeat
AddStr(S, O[L]); B := B and (O[L] in ['0'..'9','E','.']);
if B and (L < Length(O)) and (O[L] = 'E') and (O[L+1] in ['+','-'])
and (S[1] >= '0') and (S[1] <= '9') then
begin S := S + O[L+1]; Inc(L); end;
Inc(L);
until not (O[L] in ['0'..'9','X','A'..'F','H','O','.']) or (L>Byte(O[0]));
Inc(Start, Length(S));
if S[1] = '.' then Insert('0', S, 1);
if PosChar('.', S) = 0 then
if S[Length(S)] = 'H' then
begin
S := '@H'+Copy(S, 1, Length(S)-1);
tp := opCell;
end else
if S[1] = '$' then
begin
S := '@H'+Copy(S, 2, Length(S)-1);
tp := opCell;
end else
if (S[Length(S)] = 'B') and (S[2] in ['0'..'1']) then
begin
S := '@B'+Copy(S, 1, Length(S)-1);
tp := opCell;
end else
if S[Length(S)] = 'O' then
begin
S := '@O'+Copy(S, 1, Length(S)-1);
tp := opCell;
end else
if (S[1] = '0') and (S[0] > #1) then
begin
tp := opCell;
case S[2] of
'X': S := '@H'+Copy(S,3,255);
'B': S := '@B'+Copy(S,3,255);
else S := '@O'+Copy(S,2,255);
end;
end
end;
opCell : begin
repeat
AddStr(S, O[L]);
Inc(L);
if (O[L]='(') and (L <= Length(O)) then
begin
I := L;
GetToken(O,L,S1,Tp);
L := I;
S:=S+S1;
Tp:=opFunc;
end;
until (O[L] in Signs) or (Tp=opFunc) or (L>Byte(O[0]));
Inc(Start, Length(S));
end;
end;
O:=S;
ErrOcc:=False;
end;
procedure Error(Message : String);
begin
ErrStr:=Message;
ErrOcc:=True
end;
function IsPrior;
var i : Byte;
begin
i:=1;
While (c1<>Prior[i]) and (c2<>Prior[i]) and (i<=MaxOperations) do Inc(i);
IsPrior:=c1=Prior[i];
end;
function GetFormulaTree(St : String) : FPtr;
var LastOp,Root,p,
CurPtr : FPtr;
S,S1 : String;
Tp : OpType;
Count : Integer;
Procedure NewOperand( S : String);
var Q, Q1: String;
A,I: Integer;
begin
if tp=opFormula then
begin
DelFC(s);Dec(Byte(s[0]));
p:=GetFormulaTree(s);
end
else
if tp=opFunc then
begin
New(p);
p^.Owner := LastOp;
p^.Name := Copy(s,1,Pos('(',s)-1);
p^.Tp := Tp;
New(p^.Params);
p^.ParamsNum := 0; p^.Params^[1] := nil;
Q := Copy(s,Pos('(',s) + 1,200); Dec(Q[0]); I := 1; A := 0;
While (I <= Length(Q)) do
begin
Q1 := '';
While (I <= Length(Q)) and (Q[I] <> ',') do
begin
Q1 := Q1 + Q[I];
if Q[I] = '(' then Inc(A) else if Q[I] = ')' then Dec(A);
if A < 0 then begin ErrOcc := True; Exit end;
Inc(I);
end;
if A <> 0 then begin ErrOcc := True; Exit end;
Inc(p^.ParamsNum); Inc(I);
p^.Params^[p^.ParamsNum] := GetFormulaTree(Q1);
end;
end
else
begin
New(p);
p^.Owner:=LastOp;
p^.Left:=Nil;
p^.Right:=Nil;
p^.Name:=s;
p^.Tp:=Tp; CellOccured := CellOccured or (Tp = opCell);
end;
if LastOP<>Nil then LastOp^.Right:=p;
CurPtr:=p;
end;
procedure NewSign(S : String);
Label Loop;
var q : FPtr;
begin
New(p);
p^.Name:=S;
p^.Tp:=opSign;
q:=LastOp;
Loop:
if q=Nil then
begin
Root:=p;
p^.Left:=CurPtr;
p^.Right:=Nil;
p^.Owner:=Nil;
end
else
begin
if isPrior(s,q^.Name) then
begin
While (q<>Nil) and isPrior(s,q^.Name) do q:=q^.Owner;
CurPtr:=q;
if q=Nil then CurPtr:=Root;
GoTo Loop;
end
else
begin
p^.Owner:=q;
p^.Left:=q^.Right;
p^.Right:=Nil;
q^.Right:=p;
end
end;
LastOp:=p;
CurPtr:=p;
end;
begin
if ErrOcc then Exit;
LastOp:=Nil;Root:=Nil;CurPtr:=Nil;GetFormulaTree:=Nil;
Count:=1;
repeat
GetToken(St,Count,S,Tp);
{Inc(Count,Byte(s[0]));}
if not ErrOcc then
if Tp=opSign then NewSign(S) else NewOperand(S);
GetFormulaTree:=Root;
until (Count>Byte(St[0])) or ErrOcc;
if (Root=Nil) and not ErrOcc then GetFormulaTree:=CurPtr;
end;
function GetFormula(const S: String): FPtr;
var A1, A2, I: Integer;
begin
CellOccured := False; GetFormula := nil;
ErrOcc := False; A1 := 0; A2 := 0;
For I := 1 to Length(S) do
begin
if S[I] = '(' then Inc(A1) else
if S[I] = ')' then Dec(A1);
if A1 < 0 then
begin ErrOcc := True; Exit end;
end;
if A1 > 0 then
begin ErrOcc := True; Exit end;
GetFormula := GetFormulaTree(S);
end;
end.