-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSAS.PAS
288 lines (267 loc) · 5.53 KB
/
SAS.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
{ @author: Sylvain Maltais ([email protected])
@created: 2023
@website(https://www.gladir.com/corail)
@abstract(Target: Free Pascal 3.2, Turbo Pascal 7)
}
Program SAS;
Uses Crt,DOS;
Const
CommandList:Array[0..2]of String[20]=(
'DATA','PUT','RUN'
);
MaxLine=1024;
Type
StrPointer=^String;
Var
Terminated:Boolean;
CurrCommand:String;
FileName,CurrLine:String;
PA:Array[1..MaxLine] of StrPointer;
CurrLinePtr,NumberLine:Integer;
CurrNumberLine:Integer;
I:Integer;
CurrPos:Byte;
Function LTrim(S:String):String;
Var
I:Integer;
Begin
I:=1;
While(I<=Length(s)) and (S[I] in [#9,' ']) do Inc(I);
Delete(S,1,I-1);
LTrim:=S;
End;
Function StrToUpper(S:String):String;
Var
I:Byte;
Begin
For I:=1 to Length(S)do Begin
If S[I] in['a'..'z']Then S[I]:=Chr(Ord(S[I])-32);
End;
StrToUpper:=S;
End;
{ Traitement de la liste }
Function AddLine(S:String):Boolean;
Var
P:StrPointer;
Begin
If NumberLine>=MaxLine Then Begin
AddLine:=False;
Exit;
End;
Inc(NumberLine);
GetMem(P,Length(S)+1);
P^:=S;
PA[NumberLine]:=P;
AddLine:=True;
End;
Function IsStringValue:Boolean;Begin
IsStringValue:=False;
If CurrLine[CurrPos]in['''','"']Then Begin
IsStringValue:=True;
End;
End;
Function ExtractCommand:Byte;
Var
I:Byte;
Begin
ExtractCommand:=255;
CurrCommand:='';
For I:=CurrPos to Length(CurrLine)do Begin
If Not(CurrLine[I]in['A'..'Z','a'..'z','_'])Then Begin
CurrCommand:=StrToUpper(Copy(CurrLine,CurrPos,I-CurrPos));
CurrPos:=I;
Break;
End;
End;
If CurrCommand=''Then Begin
CurrCommand:=StrToUpper(Copy(CurrLine,CurrPos,255));
CurrPos:=Length(CurrLine)+1;
End;
For I:=Low(CommandList)to High(CommandList)do Begin
If CurrCommand=CommandList[I]Then Begin
ExtractCommand:=I;
Exit;
End;
End;
End;
Function ReadWord:String;
Var
S:String;
Begin
S:='';
If CurrLine[CurrPos]in['A'..'Z','a'..'z']Then Begin
S:=S+CurrLine[CurrPos];
Inc(CurrPos);
While CurrPos<=Length(CurrLine)do Begin
If Not(CurrLine[CurrPos]in['A'..'Z','a'..'z','0'..'9'])Then Break;
S:=S+CurrLine[CurrPos];
Inc(CurrPos);
End;
End;
ReadWord:=StrToUpper(S);
End;
Function GetStringValue:String;
Var
J:Integer;
_Result:Real;
FunctionFound:Boolean;
S,VarName:String;
Begin
GetStringValue:='';
S:='';
If CurrLine[CurrPos]=''''Then Begin
Inc(CurrPos);
While(CurrLine[CurrPos]<>'''')and(CurrPos<=Length(CurrLine))do Begin
S:=S+CurrLine[CurrPos];
Inc(CurrPos);
End;
If CurrLine[CurrPos]=''''Then Inc(CurrPos);
End
Else
If CurrLine[CurrPos]='"'Then Begin
Inc(CurrPos);
While(CurrLine[CurrPos]<>'"')and(CurrPos<=Length(CurrLine))do Begin
S:=S+CurrLine[CurrPos];
Inc(CurrPos);
End;
If CurrLine[CurrPos]='"'Then Inc(CurrPos);
End;
GetStringValue:=S;
End;
Procedure SkipSpace;Begin
While(CurrLine[CurrPos]in[' '])and(CurrPos<Length(CurrLine))do Inc(CurrPos);
End;
Procedure LoadCommand;
Var
FileLoad:Text;
S:String;
Begin
If FileName<>''Then Begin
{$I-}Assign(FileLoad,FileName);
Reset(FileLoad);{$I+}
If IoResult<>0Then Begin
WriteLn('Fichier introuvable');
Exit;
End;
{NewCommand;}
While Not EOF(FileLoad) do Begin
ReadLn(FileLoad,S);
If Not AddLine(LTrim(S))Then Begin
WriteLn('Manque de m‚moire');
Break;
End;
End;
Close(FileLoad);
End
Else
WriteLn('Nom du fichier absent');
End;
Procedure DataCommand;
Var
CurrDataName:String;
Begin
SkipSpace;
CurrDataName:=ReadWord;
If CurrDataName=''Then Begin
WriteLn('Nom du DATA non sp‚cifi‚.');
Halt;
End;
End;
Procedure PutCommand;Begin
SkipSpace;
If IsStringValue Then Begin
WriteLn(GetStringValue);
End;
End;
Procedure RunCommand;Begin
{ ... }
End;
Function RunLine:Boolean;
Var
R1:Real;
UnknownCommand:Boolean;
NoImplementation:Boolean;
Begin
RunLine:=False;
CurrPos:=1;
Repeat
NoImplementation:=False;
UnknownCommand:=False;
Case ExtractCommand of
0:DataCommand;{DATA}
1:PutCommand;{PUT}
2:RunCommand;{RUN}
Else UnknownCommand:=True;
End;
If(UnknownCommand)Then Begin
WriteLn('Commande non reconnu : ',CurrCommand);
Exit;
End
Else
If(NoImplementation)Then Begin
WriteLn('Cette commande n''a pas ‚t‚ impl‚ment‚');
Exit;
End;
While(CurrLine[CurrPos]in[' ',';'])and(CurrPos<=Length(CurrLine)) do Inc(CurrPos);
If CurrPos>=Length(CurrLine)Then Break;
If Not(CurrLine[CurrPos]in['A'..'Z','a'..'z','_'])Then Begin
WriteLn('Erreur de syntaxe a la position ',CurrPos,' de la ligne ',CurrNumberLine+1);
Exit;
End;
SkipSpace;
Until CurrPos>Length(CurrLine);
RunLine:=True;
End;
Procedure ExecuteCommand;
Var
J:Integer;
Err:Integer;
S1:String;
Begin
If NumberLine>0Then Begin
CurrLinePtr:=1;
While(CurrLinePtr<=NumberLine) do Begin
CurrLine:=PA[CurrLinePtr]^;
CurrNumberLine:=0;
J:=1;
While(J<Length(CurrLine))do Begin
If Not(CurrLine[J]in['0'..'9'])Then Begin
Val(Copy(CurrLine,1,J-1),CurrNumberLine,Err);
Break;
End;
Inc(J);
End;
While J<=Length(CurrLine)do Begin
If CurrLine[J]in[' ',#9]Then Inc(J)
Else Break;
End;
CurrPos:=J;
If Not(RunLine)Then Break;
Inc(CurrLinePtr);
End;
End;
End;
BEGIN
{$IFDEF FPC}
{$IFDEF WINDOWS}
SetUseACP(False);
{$ENDIF}
{$ENDIF}
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin
WriteLn('SAS : Cette commande permet de lancer le langage de programmation SAS');
WriteLn;
WriteLn('Syntaxe : SAS [fichier.SAS]');
End
Else
Begin
FileName:='';
NumberLine:=0;CurrNumberLine:=0;
If ParamCount>0 Then Begin
For I:=1 to ParamCount do Begin
FileName:=ParamStr(I);
End;
LoadCommand;
ExecuteCommand;
End
End;
END.