-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPYTHON.PAS
323 lines (303 loc) · 6.57 KB
/
PYTHON.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
{ @author: Sylvain Maltais ([email protected])
@created: 2022
@website(https://www.gladir.com/linux-0)
@abstract(Target: Free Pascal, Turbo Pascal)
}
Program Python;
Uses Crt,DOS;
Const
ReservedWordList:Array[0..29]of String[10]=(
'and','assert','break','class','continue','def','del',
'elif','else','except','exec','finally','for',
'from','global','if','import','in','is','lambda',
'not','or','pass','print','raise','return','try',
'while','with','yield'
);
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:=Copy(CurrLine,CurrPos,I-CurrPos);
CurrPos:=I;
Break;
End;
End;
If CurrCommand=''Then Begin
CurrCommand:=Copy(CurrLine,CurrPos,255);
CurrPos:=Length(CurrLine)+1;
End;
For I:=Low(ReservedWordList)to High(ReservedWordList)do Begin
If CurrCommand=ReservedWordList[I]Then Begin
ExtractCommand:=I;
Exit;
End;
End;
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;
Function GetSeparator:Char;Begin
If CurrPos>Length(CurrLine)Then Begin
GetSeparator:=#0;
Exit;
End;
SkipSpace;
GetSeparator:=CurrLine[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 PrintCommand;
Var
Paren:Boolean;
Begin
Paren:=False;
SkipSpace;
If GetSeparator='('Then Begin
Inc(CurrPos);
Paren:=True;
End;
If IsStringValue Then Begin
WriteLn(GetStringValue);
End;
If(Paren)Then Begin
If GetSeparator=')'Then Begin
Inc(CurrPos);
End
Else
Begin
WriteLn('ParenthŠse fermeture attendue !');
Halt(2);
End;
End;
End;
Function RunLine:Boolean;
Var
R1:Real;
UnknownCommand:Boolean;
NoImplementation:Boolean;
Begin
RunLine:=False;
CurrPos:=1;
NoImplementation:=False;
UnknownCommand:=False;
Case ExtractCommand of
0:NoImplementation:=True;{and}
1:NoImplementation:=True;{assert}
2:NoImplementation:=True;{break}
3:NoImplementation:=True;{class}
4:NoImplementation:=True;{continue}
5:NoImplementation:=True;{def}
6:NoImplementation:=True;{del}
7:NoImplementation:=True;{elif}
8:NoImplementation:=True;{else}
9:NoImplementation:=True;{except}
10:NoImplementation:=True;{exec}
11:NoImplementation:=True;{finally}
12:NoImplementation:=True;{for}
13:NoImplementation:=True;{from}
14:NoImplementation:=True;{global}
15:NoImplementation:=True;{if}
16:NoImplementation:=True;{import}
17:NoImplementation:=True;{in}
18:NoImplementation:=True;{is}
19:NoImplementation:=True;{lambda}
20:NoImplementation:=True;{not}
21:NoImplementation:=True;{or}
22:NoImplementation:=True;{pass}
23:PrintCommand;{print}
24:NoImplementation:=True;{raise}
25:NoImplementation:=True;{return}
26:NoImplementation:=True;{try}
27:NoImplementation:=True;{while}
28:NoImplementation:=True;{with}
29:NoImplementation:=True;{yield}
Else UnknownCommand:=True;
End;
If(UnknownCommand)Then Begin
WriteLn('Mot r‚serv‚ non reconnu');
Exit;
End
Else
If(NoImplementation)Then Begin
WriteLn('Ce mot r‚serv‚ n''a pas ‚t‚ impl‚ment‚');
Exit;
End;
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;
Procedure RunCommandMode;Begin
Terminated:=False;
Repeat
Write('>>>');
ReadLn(CurrLine);
If CurrLine='quit'Then Exit;
RunLine;
Until Terminated;
End;
BEGIN
{$IFDEF FPC}
{$IFDEF WINDOWS}
SetUseACP(False);
{$ENDIF}
{$ENDIF}
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin
WriteLn('PYTHON : Cette commande permet de lancer le langage de programmation Python');
WriteLn;
WriteLn('Syntaxe : PYTHON [fichier.PY]');
End
Else
If ParamStr(1)='--version'Then Begin
WriteLn('PYTHON 0.1 - Clone Pascal de Python pour Corail, Linux-0 et PYTHON-0');
WriteLn('Licence MIT');
WriteLn;
WriteLn('crit par Sylvain Maltais');
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
Else
RunCommandMode;
End;
END.