-
Notifications
You must be signed in to change notification settings - Fork 6
/
BasicClasses.pas
440 lines (378 loc) · 10.4 KB
/
BasicClasses.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// TurboriumBasic
//
// Turborium(c) 2021-2024
//
// Source code: https://github.com/turborium/turboriumbasic
unit BasicClasses;
{$SCOPEDENUMS ON}
interface
uses
SysUtils, Generics.Collections, Generics.Defaults;
type
IConsole = interface
procedure ReadNewLine(out Line: string);
procedure Read(out Str: string);
procedure WriteNewLine(const Line: string);
procedure Write(const Str: string);
procedure Clear();
function CheckBreak(): Boolean;
end;
EBasicError = class(Exception)
private
FLineNumber: Integer;
FPosition: Integer;
public
constructor Create(const Position: Integer; const Message: string); overload;
constructor Create(const LineNumber, Position: Integer; const Message: string); overload;
property Position: Integer read FPosition;
property LineNumber: Integer read FLineNumber;
end;
TLineList = class
private type
TLine = record
private
FNumber: Integer;
FCode: string;
public
constructor Create(const Number: Integer; const Code: string);
function ToString: string;
property Number: Integer read FNumber;
property Code: string read FCode;
end;
TLineComparer = class(TComparer<TLine>)
public
function Compare(const Left, Right: TLine): Integer; override;
end;
private
List: TList<TLine>;
function GetLine(Index: Integer): string;
function GetLineCount: Integer;
function GetNumber(Index: Integer): Integer;
public const
MaxLineNumber = 100000;
public
constructor Create;
destructor Destroy; override;
function SearchIndexByNumber(const Number: Integer; const Upward: Boolean = False): Integer;
function IndexByNumber(const Number: Integer): Integer;
property Lines[Index: Integer]: string read GetLine;
property Numbers[Index: Integer]: Integer read GetNumber;
property Count: Integer read GetLineCount;
procedure SetLine(const Number: Integer; const Code: string);
procedure DeleteLine(const Number: Integer);
procedure Clear;
procedure SaveToFile(const FileName: string);
procedure LoadFromFile(const FileName: string);
end;
TVarList = class
private
Variables: TDictionary<string, Double>;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure DefineVar(const Name: string; const Value: Double);
function TryGetVar(const Name: string; out Value: Double): Boolean;
end;
TForItem = record
Target: string;
LineIndex: Integer;
Position: Integer;
Value: Double;
EndValue: Double;
Step: Double;
end;
// fast coded shit
TForStack = class
private
FItems: TList<TForItem>;
function GetItem(Index: Integer): TForItem;
procedure SetItem(Index: Integer; const Value: TForItem);
public
constructor Create;
destructor Destroy; override;
function IndexByTarget(const Target: string): Integer;
procedure Add(const Item: TForItem);
function LastIndex: Integer;
procedure Delete(const Index: Integer);
property Items[Index: Integer]: TForItem read GetItem write SetItem; default;
end;
function TryStrToIntegerBasic(const Str: string; out Value: Integer): Boolean;
function TryStrToFloatBasic(const Str: string; out Value: Double): Boolean;
function FloatToStrBasic(const Value: Double; const MaxWidth: Integer): string;
implementation
uses
Classes;
function TryStrToIntegerBasic(const Str: string; out Value: Integer): Boolean;
var
Code: Integer;
begin
Val(Trim(Str), Value, Code);
Result := Code = 0;
end;
function TryStrToFloatBasic(const Str: string; out Value: Double): Boolean;
var
Code: Integer;
begin
Val(Trim(Str), Value, Code);
Result := Code = 0;
end;
function FloatToStrBasic(const Value: Double; const MaxWidth: Integer): string;
var
MaxDigits: Integer;
begin
// minmal width: Length("-1e-222") = 7
Assert(MaxWidth >= 7);
MaxDigits := MaxWidth;
// 1 pass
Result := FloatToStrF(Value, TFloatFormat.ffGeneral, MaxDigits, 0, TFormatSettings.Invariant);
// shrink passes (2 or 3)
while Length(Result) > MaxWidth do
begin
MaxDigits := MaxDigits - (Length(Result) - MaxWidth);
Result := FloatToStrF(Value, TFloatFormat.ffGeneral, MaxDigits,
0, TFormatSettings.Invariant);
end;
end;
{ EBasicError }
constructor EBasicError.Create(const Position: Integer; const Message: string);
begin
inherited Create('Error: ' + Message + ' at ' + IntToStr(Position));
FLineNumber := -1;
FPosition := Position;
end;
constructor EBasicError.Create(const LineNumber, Position: Integer; const Message: string);
begin
inherited Create('Error: ' + Message + ' at ' + IntToStr(LineNumber) + ', ' + IntToStr(Position));
FLineNumber := LineNumber;
FPosition := Position;
end;
{ TLineList.TCodelineComparer }
function TLineList.TLineComparer.Compare(const Left, Right: TLine): Integer;
begin
Result := Left.Number - Right.Number;
end;
{ TLineList.TLine }
constructor TLineList.TLine.Create(const Number: Integer; const Code: string);
begin
FNumber := Number;
FCode := Code;
end;
function TLineList.TLine.ToString: string;
begin
Result := IntToStr(FNumber) + ' ' + FCode;
end;
{ TLineList }
constructor TLineList.Create;
begin
inherited Create;
List := TList<TLine>.Create(TLineComparer.Create);
end;
destructor TLineList.Destroy;
begin
List.Free;
inherited;
end;
procedure TLineList.SaveToFile(const FileName: string);
var
Line: TLine;
Strings: TStringList;
begin
Strings := TStringList.Create;
try
for Line in List do
begin
Strings.Add(Line.ToString);
end;
Strings.SaveToFile(FileName);
finally
Strings.Free;
end;
end;
procedure TLineList.LoadFromFile(const FileName: string);
var
Line: TLine;
Strings: TStringList;
I, Position, Number, CodePosition: Integer;
NumberString, Code: string;
begin
List.Clear;
Strings := TStringList.Create;
try
Strings.LoadFromFile(FileName);
for I := 0 to Strings.Count - 1 do
begin
Position := 0;
if CharInSet(PChar(Strings[I])[Position], ['0'..'9']) then
begin
// get number
NumberString := '';
while CharInSet(PChar(Strings[I])[Position], ['0'..'9']) do
begin
NumberString := NumberString + PChar(Strings[I])[Position];
Position := Position + 1;
end;
if not TryStrToIntegerBasic(NumberString, Number) or
(Number < 0) or (Number > MaxLineNumber) or (IndexByNumber(Number) <> -1) then
raise Exception.Create('Bad line number at ' + IntToStr(I) + ' line');
// skip one space
if PChar(Strings[I])[Position] = ' ' then
CodePosition := Position + 1
else
CodePosition := Position;
while PChar(Strings[I])[Position] = ' ' do
begin
Position := Position + 1;
end;
// editor
if PChar(Strings[I])[Position] <> #0 then
begin
// set line
Code := '';
while PChar(Strings[I])[CodePosition] <> #0 do
begin
Code := Code + PChar(Strings[I])[CodePosition];
CodePosition := CodePosition + 1;
end;
Line := Line.Create(Number, Code);
List.Add(Line);
end else
raise Exception.Create('Bad line (' + IntToStr(I) + ')');
end;
end;
finally
Strings.Free;
end;
end;
function TLineList.SearchIndexByNumber(const Number: Integer; const Upward: Boolean): Integer;
var
I: Integer;
begin
if Upward then
begin
for I := List.Count - 1 downto 0 do
if List[I].Number <= Number then
Exit(I);
end else
begin
for I := 0 to List.Count - 1 do
if List[I].Number >= Number then
Exit(I);
end;
Result := -1;
end;
procedure TLineList.SetLine(const Number: Integer; const Code: string);
var
Index: Integer;
begin
Index := IndexByNumber(Number);
if Index < 0 then
List.Add(TLine.Create(Number, Code))
else
List[Index] := TLine.Create(Number, Code);
List.Sort;
end;
procedure TLineList.Clear;
begin
List.Clear;
end;
procedure TLineList.DeleteLine(const Number: Integer);
var
Index: Integer;
begin
Index := IndexByNumber(Number);
if Index >= 0 then
List.Delete(Index);
end;
function TLineList.GetLine(Index: Integer): string;
begin
Result := List[Index].Code;
end;
function TLineList.GetLineCount: Integer;
begin
Result := List.Count;
end;
function TLineList.GetNumber(Index: Integer): Integer;
begin
Result := List[Index].Number;
end;
function TLineList.IndexByNumber(const Number: Integer): Integer;
var
I: Integer;
begin
for I := 0 to List.Count - 1 do
if List[I].Number = Number then
Exit(I);
Result := -1;
end;
{ TVarList }
procedure TVarList.Clear;
begin
Variables.Clear;
end;
constructor TVarList.Create;
begin
Variables := TDictionary<string, Double>.Create;
end;
procedure TVarList.DefineVar(const Name: string; const Value: Double);
begin
Variables.AddOrSetValue(UpperCase(Name), Value);
end;
destructor TVarList.Destroy;
begin
Variables.Free;
inherited;
end;
function TVarList.TryGetVar(const Name: string; out Value: Double): Boolean;
begin
Result := Variables.TryGetValue(UpperCase(Name), Value);
end;
{ TForStack }
procedure TForStack.Add(const Item: TForItem);
var
ItemIndex: Integer;
begin
// Remove old?
ItemIndex := IndexByTarget(Item.Target);
if ItemIndex <> -1 then
FItems.Delete(ItemIndex);
// add
FItems.Add(Item);
end;
constructor TForStack.Create;
begin
FItems := TList<TForItem>.Create;
end;
procedure TForStack.Delete(const Index: Integer);
begin
FItems.Delete(Index);
end;
destructor TForStack.Destroy;
begin
FItems.Free;
inherited;
end;
function TForStack.GetItem(Index: Integer): TForItem;
begin
Result := FItems[Index];
end;
function TForStack.IndexByTarget(const Target: string): Integer;
var
I: Integer;
begin
for I := 0 to FItems.Count - 1 do
if UpperCase(Target) = FItems[I].Target then
begin
Exit(I);
end;
Result := -1;
end;
function TForStack.LastIndex: Integer;
begin
Result := FItems.Count - 1;
end;
procedure TForStack.SetItem(Index: Integer; const Value: TForItem);
begin
FItems[Index] := Value;
end;
end.