-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBasicScanner.pas
411 lines (391 loc) · 9.91 KB
/
BasicScanner.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// TurboriumBasic
//
// Turborium(c) 2021-2024
//
// Source code: https://github.com/turborium/turboriumbasic
unit BasicScanner;
{$SCOPEDENUMS ON}
interface
uses
Classes, SysUtils, BasicClasses;
type
TTokenType = (
// types
Integer, Float, &String, Variable,
// expressions
Plus, Minus, Multiply, Divide, Power,
LeftBracket, RightBracket,
// separators
Comma,// ,
Semicolon,// ;
Colon,// :
// conditions
Equality,// =
LessThanOrEqual,// <=
NotEqual,// <>
LessThan,// <
GreaterThanOrEqual,// >=
GreaterThan,// >
// directives
List, New, Run,
Load, Save,
// operators
Print,
Input,
Cls,
Let,
Rem,
&Goto,
&If,
&Then,
&Else,
&For,
&To,
Step,
Next,
Sleep,
// functions
Sin,
Cos,
Tan,
ArcSin,
ArcCos,
ArcTan,
Sqrt,
Log,
Exp,
Abs,
Sign,
Int,
Frac,
Random,
// terminal
Terminal
);
TScanner = class
private
// code
FCurrentLine: string;
// current position in code line
FPosition: Integer;
// previous position in code line
FPrevPosition: Integer;
// current token
FToken: TTokenType;
// current identifier (function/variable name)
FIdentifier: string;
// current value
FValue: Double;
FStringValue: string;
FIntegerValue: Integer;
procedure Error(const Position: Integer; const Message: string);
function CurentChar: Char; inline;
procedure NextChar; inline;
procedure SkipSpaces; inline;
public
constructor Create;
destructor Destroy; override;
procedure AssignLine(const NewLine: string);
procedure NextToken;
property CurrentLine: string read FCurrentLine;
property Position: Integer read FPosition write FPosition;
property PrevPosition: Integer read FPrevPosition;
property Token: TTokenType read FToken;
property Value: Double read FValue;
property StringValue: string read FStringValue;
property IntegerValue: Integer read FIntegerValue;
property Identifier: string read FIdentifier;
end;
implementation
const
sUnexpectedSymbol = 'Unexpected symbol';
sUnknownFunction = 'Unknown function';
sBadNumber = 'Bad number';
sUnexpectedTerminal = 'Unexpected terminal';
{ TScanner }
constructor TScanner.Create;
begin
AssignLine('');
end;
destructor TScanner.Destroy;
begin
inherited;
end;
procedure TScanner.Error(const Position: Integer; const Message: string);
begin
raise EBasicError.Create(Position, Message);
end;
procedure TScanner.AssignLine(const NewLine: string);
begin
FCurrentLine := NewLine;
FPosition := 0;
NextToken;
end;
function TScanner.CurentChar: Char;
begin
Result := PChar(FCurrentLine)[Position];
end;
procedure TScanner.NextChar;
begin
FPosition := FPosition + 1;
end;
procedure TScanner.NextToken;
var
TokenString: string;
begin
SkipSpaces;
FPrevPosition := Position;
case CurentChar of
'0'..'9':
begin
FToken := TTokenType.Integer;
// for ex: 12.34e+56
// 12
while CharInSet(CurentChar, ['0'..'9']) do
begin
TokenString := TokenString + CurentChar;
NextChar;
end;
// .
if CurentChar = '.' then
begin
FToken := TTokenType.Float;// has '.' => not int
TokenString := TokenString + CurentChar;
NextChar;
end;
// 34
while CharInSet(CurentChar, ['0'..'9']) do
begin
TokenString := TokenString + CurentChar;
NextChar;
end;
// e
if CharInSet(CurentChar, ['e', 'E']) then
begin
FToken := TTokenType.Float;// has 'e' => not int
TokenString := TokenString + CurentChar;
NextChar;
// +/-
if CharInSet(CurentChar, ['-', '+']) then
begin
TokenString := TokenString + CurentChar;
NextChar;
end;
// 56
if not CharInSet(CurentChar, ['0'..'9']) then
Error(Position, sBadNumber);
//raise EBasicError.Create(Position, sBadNumber);// error
while CharInSet(CurentChar, ['0'..'9']) do
begin
TokenString := TokenString + CurentChar;
NextChar;
end;
end;
// convert to IntegerValue if possible
if (FToken = TTokenType.Float) or (not TryStrToIntegerBasic(TokenString, FIntegerValue)) then
begin
// convert to Value if possible
FToken := TTokenType.Float;
if not TryStrToFloatBasic(TokenString, FValue) then
Error(PrevPosition, sBadNumber);
end;
end;
'+':
begin
FToken := TTokenType.Plus;
NextChar;
end;
'-':
begin
FToken := TTokenType.Minus;
NextChar;
end;
'*':
begin
FToken := TTokenType.Multiply;
NextChar;
end;
'/':
begin
FToken := TTokenType.Divide;
NextChar;
end;
'^':
begin
FToken := TTokenType.Power;
NextChar;
end;
'(':
begin
FToken := TTokenType.LeftBracket;
NextChar;
end;
')':
begin
FToken := TTokenType.RightBracket;
NextChar;
end;
'a'..'z', 'A'..'Z':
begin
TokenString := '';
// abc
while CharInSet(CurentChar, ['a'..'z', 'A'..'Z', '0'..'9']) do
begin
TokenString := TokenString + UpCase(CurentChar);
NextChar;
end;
// (
if CurentChar = '(' then
begin
NextChar;
{nop}if TokenString = 'SIN' then
FToken := TTokenType.Sin
else if TokenString = 'COS' then
FToken := TTokenType.Cos
else if TokenString = 'TAN' then
FToken := TTokenType.Tan
else if TokenString = 'ARCSIN' then
FToken := TTokenType.ArcSin
else if TokenString = 'ARCCOS' then
FToken := TTokenType.ArcCos
else if TokenString = 'ARCTAN' then
FToken := TTokenType.ArcTan
else if TokenString = 'SQRT' then
FToken := TTokenType.Sqrt
else if TokenString = 'LOG' then
FToken := TTokenType.Log
else if TokenString = 'EXP' then
FToken := TTokenType.Exp
else if TokenString = 'ABS' then
FToken := TTokenType.Abs
else if TokenString = 'SIGN' then
FToken := TTokenType.Sign
else if TokenString = 'INT' then
FToken := TTokenType.Int
else if TokenString = 'FRAC' then
FToken := TTokenType.Frac
else Error(PrevPosition, sUnknownFunction);
end else
begin
{nop}if TokenString = 'LIST' then
FToken := TTokenType.List
else if TokenString = 'NEW' then
FToken := TTokenType.New
else if TokenString = 'LOAD' then
FToken := TTokenType.Load
else if TokenString = 'SAVE' then
FToken := TTokenType.Save
else if TokenString = 'PRINT' then
FToken := TTokenType.Print
else if TokenString = 'LET' then
FToken := TTokenType.Let
else if TokenString = 'REM' then
FToken := TTokenType.Rem
else if TokenString = 'RUN' then
FToken := TTokenType.Run
else if TokenString = 'GOTO' then
FToken := TTokenType.&Goto
else if TokenString = 'INPUT' then
FToken := TTokenType.Input
else if TokenString = 'CLS' then
FToken := TTokenType.Cls
else if TokenString = 'IF' then
FToken := TTokenType.&If
else if TokenString = 'THEN' then
FToken := TTokenType.&Then
else if TokenString = 'ELSE' then
FToken := TTokenType.&Else
else if TokenString = 'FOR' then
FToken := TTokenType.&For
else if TokenString = 'TO' then
FToken := TTokenType.&To
else if TokenString = 'STEP' then
FToken := TTokenType.Step
else if TokenString = 'NEXT' then
FToken := TTokenType.Next
else if TokenString = 'SLEEP' then
FToken := TTokenType.Sleep
else if TokenString = 'RANDOM' then
FToken := TTokenType.Random
else
begin
FToken := TTokenType.Variable;
FIdentifier := TokenString;
end;
end;
end;
'"':
begin
FToken := TTokenType.&String;
FStringValue := '';
NextChar;
while (CurentChar <> '"') and (CurentChar <> #0) do
begin
FStringValue := FStringValue + CurentChar;
NextChar;
end;
if CurentChar = #0 then
Error(Position, sUnexpectedTerminal);
NextChar;
end;
',':
begin
FToken := TTokenType.Comma;
NextChar;
end;
';':
begin
FToken := TTokenType.Semicolon;
NextChar;
end;
':':
begin
FToken := TTokenType.Colon;
NextChar;
end;
'=':
begin
FToken := TTokenType.Equality;
NextChar;
end;
'<':
begin
NextChar;
if CurentChar = '=' then
begin
FToken := TTokenType.LessThanOrEqual;
NextChar;
end
else if CurentChar = '>' then
begin
FToken := TTokenType.NotEqual;
NextChar;
end else
FToken := TTokenType.LessThan;
end;
'>':
begin
NextChar;
if CurentChar = '=' then
begin
FToken := TTokenType.GreaterThanOrEqual;
NextChar;
end else
FToken := TTokenType.GreaterThan;
end;
#0:
begin
FToken := TTokenType.Terminal;
end;
else
Error(Position, sUnexpectedSymbol);// error
end;
end;
procedure TScanner.SkipSpaces;
begin
while CharInSet(CurentChar, [#9, ' ']) do
begin
NextChar;
end;
end;
end.