-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBasicInterpreter.pas
1416 lines (1324 loc) · 32.2 KB
/
BasicInterpreter.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TurboriumBasic
//
// Turborium(c) 2021-2024
//
// Source code: https://github.com/turborium/turboriumbasic
unit BasicInterpreter;
{$SCOPEDENUMS ON}
interface
uses
BasicClasses, SysUtils, Generics.Collections,
Generics.Defaults, BasicScanner;
type
TInterpreter = class
private type
TRecursiveCall = function: Double of object;
private const
ColumnWidth = 16;
private
Console: IConsole;
// BASIC Mem
InstantLine: string;// index = -1
Lines: TLineList;
Variables: TVarList;
ForStack: TForStack;
// Interpreter
Scanner: TScanner;
LineIndex: Integer;
IsRunning: Boolean;
ExpressionStackLevel: Integer;
NeedSkipElse: Boolean;
procedure RequireToken(const TokenType: TTokenType);
function RecursiveCall(const Func: TRecursiveCall): Double;
procedure Error(const Position: Integer; const Message: string);
// Expression
function BasicFunction: Double;
function Primitive: Double;
function Pow: Double;
function MulAndDiv: Double;
function AddAndSub: Double;
function Condition: Double;
function Expression: Double;
// BASIC
procedure NextLine;
function IsExpression: Boolean;
procedure RunLine;
procedure PrepareLine(const NewLineIndex: Integer);
procedure PrepareInstantLine(const Line: string);
procedure PerformUserInput(const Input: string);
// --------------------------------------------------------------------------------------
// NEW
procedure ExecuteNew;
// LIST / LIST <linenumber> / LIST <linenumber1>-<linenumber2>
procedure ExecuteList;
// RUN / RUN <linenumber>
procedure ExecuteRun;
// SAVE "filename"
procedure ExecuteSave;
// LOAD "filename"
procedure ExecuteLoad;
// --------------------------------------------------------------------------------------
// GOTO <linenumber>
procedure ExecuteGoto;
// LET <var> = <expression>
procedure ExecuteLet;
// PRINT <var1>/"string" ,/; <var2>/"string" ...
procedure ExecutePrint;
// INPUT <var1>; <var2> ... / INPUT "<question>"; <var1>; <var2> ...
procedure ExecuteInput;
// IF <condition> THEN ...
procedure ExecuteIf;
// ELSE ...
procedure ExecuteElse;
// FOR <var> = <number1> TO <number2> / FOR <var> = <number1> TO <number2> STEP <number3>
procedure ExecuteFor;
// NEXT / NEXT <var>
procedure ExecuteNext;
procedure ExecuteSleep;
// --------------------------------------------------------------------------------------
public
constructor Create(const Console: IConsole);
destructor Destroy; override;
procedure Run;
end;
implementation
uses
Math, StrUtils;
const
MaxExpressionStackLevel = 32;
const
sInvite = 'Turborium Basic v0.3';
sReinput = 'Bad input, reinput:';
sClosingParenthesisExpected = 'Closing parenthesis expected';
sExpressionExpected = 'Expression expected';
sUnexpectedSymbol = 'Unexpected symbol';
sBadNumber = 'Bad number';
sDivisionByZero = 'Division by zero';
sBadFunctionArgument = 'Bad function argument';
sOverflow = 'Overflow';
sInternalError = 'Internal error! Please submit issue to https://github.com/turborium/turboriumbasic/';
sStackOverflow = 'Stack overflow';
sVariableNotFound = 'Variable not found';
sExpected1But2Found = 'Expected "%s" but "%s" found';
sExpectedCommaOrSemicolon = 'Expected "," or ";" separator';
sUndefinedLineNumber = 'Undefined line number';
sLineIndexOutOfRange = 'Line index out of range';
sNextWithoutFor = 'Next without For';
sFileCanNotLoadWithMessage = 'File can''t load with message "%s"';
sFileCanNotSaveWithMessage = 'File can''t save with message "%s"';
sBreakByUser = 'Break by user';
function TokenTypeToString(const Token: TTokenType): string;
begin
case Token of
// types
TTokenType.Integer:
Result := 'Integer';
TTokenType.Float:
Result := 'Float';
TTokenType.&String:
Result := 'String';
TTokenType.Variable:
Result := 'Variable';
// expressions
TTokenType.Plus:
Result := '+';
TTokenType.Minus:
Result := '-';
TTokenType.Multiply:
Result := '*';
TTokenType.Divide:
Result := '/';
TTokenType.Power:
Result := '^';
TTokenType.LeftBracket:
Result := '(';
TTokenType.RightBracket:
Result := ')';
// separators
TTokenType.Comma:
Result := ',';
TTokenType.Semicolon:
Result := ';';
TTokenType.Colon:
Result := ':';
// conditions
TTokenType.Equality:
Result := '=';
TTokenType.LessThanOrEqual:
Result := '<=';
TTokenType.NotEqual:
Result := '<>';
TTokenType.LessThan:
Result := '<';
TTokenType.GreaterThanOrEqual:
Result := '>=';
TTokenType.GreaterThan:
Result := '>';
// directives
TTokenType.List:
Result := 'List';
TTokenType.New:
Result := 'New';
TTokenType.Run:
Result := 'Run';
TTokenType.Load:
Result := 'Load';
TTokenType.Save:
Result := 'Save';
// operators
TTokenType.Print:
Result := 'Print';
TTokenType.Input:
Result := 'Input';
TTokenType.Cls:
Result := 'Cls';
TTokenType.Let:
Result := 'Let';
TTokenType.Rem:
Result := 'Rem';
TTokenType.&Goto:
Result := 'Goto';
TTokenType.&If:
Result := 'If';
TTokenType.&Then:
Result := 'Then';
TTokenType.&Else:
Result := 'Else';
TTokenType.&For:
Result := 'For';
TTokenType.&To:
Result := 'To';
TTokenType.Step:
Result := 'Step';
TTokenType.Next:
Result := 'Next';
TTokenType.Sleep:
Result := 'Sleep';
// functions
TTokenType.Sin:
Result := 'Sin(';
TTokenType.Cos:
Result := 'Cos(';
TTokenType.Tan:
Result := 'Tan(';
TTokenType.ArcSin:
Result := 'ArcSin(';
TTokenType.ArcCos:
Result := 'ArcCos(';
TTokenType.ArcTan:
Result := 'ArcTan(';
TTokenType.Sqrt:
Result := 'Sqrt(';
TTokenType.Log:
Result := 'Log(';
TTokenType.Exp:
Result := 'Sin(';
TTokenType.Abs:
Result := 'Abs(';
TTokenType.Sign:
Result := 'Sign(';
TTokenType.Int:
Result := 'Int(';
TTokenType.Frac:
Result := 'Frac(';
TTokenType.Random:
Result := 'Random';
// terminal
TTokenType.Terminal:
Result := 'EOF';
else
Result := 'WHAT?' + IntToStr(Integer(Token));
end;
end;
constructor TInterpreter.Create(const Console: IConsole);
begin
inherited Create;
Self.Console := Console;
Scanner := TScanner.Create;
Lines := TLineList.Create;
Variables := TVarList.Create;
ForStack := TForStack.Create;
end;
destructor TInterpreter.Destroy;
begin
Lines.Free;
Scanner.Free;
Variables.Free;
ForStack.Free;
inherited;
end;
function TInterpreter.RecursiveCall(const Func: TRecursiveCall): Double;
begin
ExpressionStackLevel := ExpressionStackLevel + 1;
if ExpressionStackLevel > MaxExpressionStackLevel then
Error(Scanner.PrevPosition, sStackOverflow);
Result := Func();
ExpressionStackLevel := ExpressionStackLevel - 1;
end;
procedure TInterpreter.RequireToken(const TokenType: TTokenType);
begin
if Scanner.Token <> TokenType then
Error(Scanner.PrevPosition, Format(sExpected1But2Found,
[TokenTypeToString(TokenType), TokenTypeToString(Scanner.Token)]));
end;
procedure TInterpreter.Error(const Position: Integer; const Message: string);
begin
if LineIndex = -1 then
raise EBasicError.Create(Position, Message)
else
raise EBasicError.Create(Lines.Numbers[LineIndex], Position, Message)
end;
procedure TInterpreter.PrepareInstantLine(const Line: string);
begin
LineIndex := -1;
IsRunning := False;
InstantLine := Line;
PrepareLine(LineIndex);
end;
procedure TInterpreter.PrepareLine(const NewLineIndex: Integer);
begin
if NewLineIndex <> -1 then
begin
LineIndex := NewLineIndex;
Scanner.AssignLine(Lines.Lines[NewLineIndex]);
IsRunning := True;
end else
Scanner.AssignLine(InstantLine);
NeedSkipElse := True;
end;
function TInterpreter.BasicFunction: Double;
var
FunctionPos: Integer;
FunctionToken: TTokenType;
X: Double;
begin
FunctionToken := Scanner.Token;
FunctionPos := Scanner.PrevPosition;
Scanner.NextToken;
X := RecursiveCall(Condition);
RequireToken(TTokenType.RightBracket);
Scanner.NextToken;
if IsNan(X) then
Exit(NaN);
Result := NaN;// for compiler paranoia
case FunctionToken of
// SIN
TTokenType.Sin:
Result := Sin(X);
// COS
TTokenType.Cos:
Result := Cos(X);
// TAN
TTokenType.Tan:
Result := Tan(X);
// ARCSIN
TTokenType.ArcSin:
if (X >= 1.0) and (X <= 1.0) then
Result := ArcSin(X)
else
Error(FunctionPos, sBadFunctionArgument);
// ARCCOS
TTokenType.ArcCos:
if (X >= 1.0) and (X <= 1.0) then
Result := ArcCos(X)
else
Error(FunctionPos, sBadFunctionArgument);
// ARCTAN
TTokenType.ArcTan:
Result := ArcTan(X);
// SQRT
TTokenType.Sqrt:
if X >= 0 then
Result := Sqrt(X)
else
Error(FunctionPos, sBadFunctionArgument);
// LOG
TTokenType.Log:
if (X > 0) then
Result := Log10(X)
else
Error(FunctionPos, sBadFunctionArgument);
// EXP
TTokenType.Exp:
Result := Exp(X);
// ABS
TTokenType.Abs:
Result := Abs(X);
// SIGN
TTokenType.Sign:
Result := Sign(X);
// INT
TTokenType.Int:
Result := Int(X);
// FRAC
TTokenType.Frac:
Result := Frac(X);
else
Error(FunctionPos, sInternalError);
end;
end;
function TInterpreter.Primitive: Double;
begin
case Scanner.Token of
// Unary operators +/-
TTokenType.Plus:
begin
Scanner.NextToken;
Result := RecursiveCall(Primitive);
end;
TTokenType.Minus:
begin
Scanner.NextToken;
Result := -RecursiveCall(Primitive);
end;
// Primitives
TTokenType.Float:
begin
Scanner.NextToken;
Result := Scanner.Value;
end;
TTokenType.Integer:
begin
Scanner.NextToken;
Result := Scanner.IntegerValue;
end;
TTokenType.LeftBracket:
begin
Scanner.NextToken;
Result := RecursiveCall(Condition);
RequireToken(TTokenType.RightBracket);
Scanner.NextToken;
end;
TTokenType.Variable:
begin
if not Variables.TryGetVar(Scanner.Identifier, Result) then
Error(Scanner.PrevPosition, sVariableNotFound);// error
Scanner.NextToken;
end;
TTokenType.Random:
begin
Scanner.NextToken;
Result := Random;
end;
// Functions
TTokenType.Sin,
TTokenType.Cos,
TTokenType.Tan,
TTokenType.ArcSin,
TTokenType.ArcCos,
TTokenType.ArcTan,
TTokenType.Sqrt,
TTokenType.Log,
TTokenType.Exp,
TTokenType.Abs,
TTokenType.Sign,
TTokenType.Int,
TTokenType.Frac:
begin
Result := BasicFunction();
end;
else
Error(Scanner.PrevPosition, sExpressionExpected);// error
end;
end;
function TInterpreter.IsExpression: Boolean;
begin
Result := Scanner.Token in
[
TTokenType.Plus,
TTokenType.Minus,
TTokenType.Float,
TTokenType.Integer,
TTokenType.LeftBracket,
TTokenType.Variable,
// func
TTokenType.Sin,
TTokenType.Cos,
TTokenType.Tan,
TTokenType.ArcSin,
TTokenType.ArcCos,
TTokenType.ArcTan,
TTokenType.Sqrt,
TTokenType.Log,
TTokenType.Exp,
TTokenType.Abs,
TTokenType.Sign,
TTokenType.Int,
TTokenType.Frac,
TTokenType.Random
];
end;
function TInterpreter.Pow: Double;
begin
Result := Primitive;
while True do
begin
case Scanner.Token of
// ^
TTokenType.Power:
begin
Scanner.NextToken;
Result := Power(Result, RecursiveCall(Pow));
end;
else
break;
end;
end;
end;
function TInterpreter.MulAndDiv: Double;
var
RightValue: Double;
begin
Result := Pow;
while True do
begin
case Scanner.Token of
// *
TTokenType.Multiply:
begin
Scanner.NextToken;
Result := Result * Pow;
end;
// /
TTokenType.Divide:
begin
Scanner.NextToken;
RightValue := Pow;
if RightValue = 0.0 then
Error(Scanner.Position, sDivisionByZero);
Result := Result / RightValue;
end;
else
break;
end;
end;
end;
function TInterpreter.AddAndSub: Double;
begin
Result := MulAndDiv;
while True do
begin
case Scanner.Token of
// +
TTokenType.Plus:
begin
Scanner.NextToken;
Result := Result + MulAndDiv;
end;
// -
TTokenType.Minus:
begin
Scanner.NextToken;
Result := Result - MulAndDiv;
end;
else
break;
end;
end;
end;
function TInterpreter.Condition: Double;
begin
Result := AddAndSub;
while True do
begin
case Scanner.Token of
// =
TTokenType.Equality:
begin
Scanner.NextToken;
if Result = AddAndSub then
Result := 1
else
Result := 0;
end;
// <=
TTokenType.LessThanOrEqual:
begin
Scanner.NextToken;
if Result <= AddAndSub then
Result := 1
else
Result := 0;
end;
// <
TTokenType.LessThan:
begin
Scanner.NextToken;
if Result < AddAndSub then
Result := 1
else
Result := 0;
end;
// <>
TTokenType.NotEqual:
begin
Scanner.NextToken;
if Result <> AddAndSub then
Result := 1
else
Result := 0;
end;
// >=
TTokenType.GreaterThanOrEqual:
begin
Scanner.NextToken;
if Result >= AddAndSub then
Result := 1
else
Result := 0;
end;
// >
TTokenType.GreaterThan:
begin
Scanner.NextToken;
if Result > AddAndSub then
Result := 1
else
Result := 0;
end;
else
break;
end;
end;
end;
function TInterpreter.Expression: Double;
begin
ExpressionStackLevel := 0;
Result := Condition;
if IsInfinite(Result) or IsNaN(Result) then
Error(Scanner.PrevPosition, sOverflow);
end;
procedure TInterpreter.ExecuteList;
var
I: Integer;
BeginNumber: Integer;
BeginIndex, EndIndex: Integer;
begin
// LIST
Scanner.NextToken;
// default
BeginIndex := 0;
EndIndex := Lines.Count - 1;
// <number>?
if Scanner.Token = TTokenType.Integer then
begin
Scanner.NextToken;
BeginNumber := Scanner.IntegerValue;
// -?
if Scanner.Token = TTokenType.Minus then
begin
// -
Scanner.NextToken;
// <number>
RequireToken(TTokenType.Integer);
Scanner.NextToken;
BeginIndex := Lines.SearchIndexByNumber(BeginNumber);
EndIndex := Lines.SearchIndexByNumber(Scanner.IntegerValue, True);
end else
begin
BeginIndex := Lines.IndexByNumber(BeginNumber);
EndIndex := BeginIndex;
end;
end;
// END
RequireToken(TTokenType.Terminal);
if BeginIndex <> -1 then
for I := BeginIndex to EndIndex do
begin
Console.WriteNewLine(IntToStr(Lines.Numbers[I]) + ' ' + Lines.Lines[I]);
end;
end;
procedure TInterpreter.ExecuteLoad;
var
CommandPos: Integer;
begin
// LOAD
CommandPos := Scanner.PrevPosition;
Scanner.NextToken;
// <filename>
RequireToken(TTokenType.&String);
try
Lines.LoadFromFile(ExtractFilePath(ParamStr(0)) + Scanner.StringValue + '.bas');// hack
except
on E: Exception do
Error(CommandPos, Format(sFileCanNotLoadWithMessage, [E.ToString]));
end;
Scanner.NextToken;
RequireToken(TTokenType.Terminal);
end;
procedure TInterpreter.ExecuteNew;
begin
// NEW
Scanner.NextToken;
RequireToken(TTokenType.Terminal);
// work
Lines.Clear;
Variables.Clear;
end;
// ETO PROSTO AD!
// BUT ETO OK
procedure TInterpreter.ExecuteInput;
function TryInput: Boolean;
var
// UserQuestion: string;
InputLine: string;
InputLinePos: Integer;
InputString: string;
InputValue: Double;
begin
// INPUT
Scanner.NextToken;
// <string>? (user question or ?)
if Scanner.Token = TTokenType.String then
begin
// "string"
Scanner.NextToken;
// ;
RequireToken(TTokenType.Semicolon);
Scanner.NextToken;
// print
Console.Write(Scanner.StringValue);
end else
Console.Write('? ');
// read input
Console.Read(InputLine);
InputLinePos := 0;
while True do
begin
// <var>
RequireToken(TTokenType.Variable);
Scanner.NextToken;
// parse var
InputString := '';
while not CharInSet(PChar(InputLine)[InputLinePos], [',', #0]) do
begin
InputString := InputString + PChar(InputLine)[InputLinePos];
InputLinePos := InputLinePos + 1;
end;
if not TryStrToFloatBasic(InputString, InputValue) then
Exit(False);// reinput needed
// save var
Variables.DefineVar(Scanner.Identifier, InputValue);
// need next var?
if Scanner.Token <> TTokenType.Comma then
Break;
// prepare for parse next var (skip ",")
if PChar(InputLine)[InputLinePos] <> ',' then
Exit(False);// reinput needed
InputLinePos := InputLinePos + 1;
Scanner.NextToken;
end;
// check terminal
if PChar(InputLine)[InputLinePos] <> #0 then
Exit(False);
Result := True;
end;
var
InitialPosition: Integer;
begin
// save scanner pos
InitialPosition := Scanner.Position;
while True do
begin
if TryInput then
Break;// ok
Scanner.Position := InitialPosition;
Console.WriteNewLine(sReinput);
end;
end;
procedure TInterpreter.ExecutePrint;
function NextColumnPos(Pos: Integer): Integer;
begin
result := ((Pos + ColumnWidth {- 1}) div ColumnWidth) * ColumnWidth;// for PRODAVLIVANIE delete "- 1"
end;
var
OutputValue: string;
ConsolePos, NewConsolePos: Integer;
IsNeedSeparator, IsNeedReturn: Boolean;
begin
// PRINT
Scanner.NextToken;
IsNeedSeparator := False;
IsNeedReturn := True;
ConsolePos := 0;
while True do
begin
case Scanner.Token of
// ","
TTokenType.Comma:
begin
// ","
Scanner.NextToken;
// write
NewConsolePos := NextColumnPos(ConsolePos);
Console.Write(StrUtils.DupeString(' ', NewConsolePos - ConsolePos));
ConsolePos := NewConsolePos;
// setup
IsNeedSeparator := False;
IsNeedReturn := False;
end;
// ";"
TTokenType.Semicolon:
begin
// ";"
Scanner.NextToken;
// setup
IsNeedSeparator := False;
IsNeedReturn := False;
end;
// expr?
else
begin
// end
if (not IsExpression) and (Scanner.Token <> TTokenType.&String) then
Break;
// get value
if IsNeedSeparator then
Error(Scanner.PrevPosition, sExpectedCommaOrSemicolon);
if Scanner.Token = TTokenType.String then
begin
// <string>
OutputValue := Scanner.StringValue;
Scanner.NextToken;
end else
OutputValue := FloatToStrBasic(Expression, ColumnWidth - 1);
// write
Console.Write(OutputValue);
ConsolePos := ConsolePos + Length(OutputValue);
// setup
IsNeedSeparator := True;
IsNeedReturn := True;
end;
end;
end;
if IsNeedReturn then
Console.Write(#10);
end;
(*procedure TInterpreter.ExecutePrint;
function NextColumnPos(Pos: Integer): Integer;
begin
result := ((Pos + ColumnWidth {- 1}) div ColumnWidth) * ColumnWidth;// for PRODAVLIVANIE delete "- 1"
end;
var
OutputValue: string;
ConsolePos, NewConsolePos: Integer;
ReturnNeeded: Boolean;
begin
// PRINT
Scanner.NextToken;
ReturnNeeded := True;
ConsolePos := 0;
while True do
begin
// try get value
if not (Scanner.Token in [TTokenType.Comma, TTokenType.Semicolon]) then
begin
// <string> or <expression>
if Scanner.Token = TTokenType.String then
begin
// <string>
Scanner.NextToken;
OutputValue := Scanner.StringValue;
end
else
OutputValue := FloatToStrBasic(Expression, ColumnWidth - 1);
// write value
Console.Write(OutputValue);
ReturnNeeded := True;
end else
OutputValue := '';
// increment virtual cursor
ConsolePos := ConsolePos + Length(OutputValue);
// [<,>,<;>]
if Scanner.Token = TTokenType.Comma then
begin
// ","
Scanner.NextToken;
// write spaces for align
NewConsolePos := NextColumnPos(ConsolePos);
Console.Write(StrUtils.DupeString(' ', NewConsolePos - ConsolePos));
ConsolePos := NewConsolePos;
ReturnNeeded := True;
end
else if Scanner.Token = TTokenType.Semicolon then
begin
// ";"
Scanner.NextToken;
ReturnNeeded := False;
end
else
break;
end;
if ReturnNeeded then
Console.Write(#10);
end;*)
procedure TInterpreter.ExecuteLet;
var
VarName: string;
Result: Double;
begin
if Scanner.Token = TTokenType.Let then
begin
// LET
Scanner.NextToken;
end;
// get <var name>
RequireToken(TTokenType.Variable);
VarName := Scanner.Identifier;
Scanner.NextToken;
// skip =
RequireToken(TTokenType.Equality);
Scanner.NextToken;
// get expression result
Result := Expression;
// set var
Variables.DefineVar(VarName, Result);
end;
procedure TInterpreter.ExecuteRun;
begin
// RUN
Scanner.NextToken;
// <line number>?
if Scanner.Token = TTokenType.Integer then
begin
LineIndex := Lines.IndexByNumber(Scanner.IntegerValue);
if LineIndex = -1 then
Error(Scanner.PrevPosition, sUndefinedLineNumber);
// #0
Scanner.NextToken;
RequireToken(TTokenType.Terminal);
end else
begin
// #0
RequireToken(TTokenType.Terminal);
LineIndex := Lines.SearchIndexByNumber(0);
if LineIndex = -1 then
Exit;
end;
// clear
Variables.Clear;
// work
PrepareLine(LineIndex);
RunLine;
end;
procedure TInterpreter.ExecuteSave;
var
CommandPos: Integer;
begin
// SAVE
CommandPos := Scanner.PrevPosition;
Scanner.NextToken;
// <filename>
RequireToken(TTokenType.&String);
try
Lines.SaveToFile(ExtractFilePath(ParamStr(0)) + Scanner.StringValue + '.bas');// hack
except
on E: Exception do
Error(CommandPos, Format(sFileCanNotSaveWithMessage, [E.ToString]));
end;
Scanner.NextToken;
RequireToken(TTokenType.Terminal);
end;
procedure TInterpreter.ExecuteGoto;
begin
// GOTO
Scanner.NextToken;
// <line number>
RequireToken(TTokenType.Integer);
// work
LineIndex := Lines.IndexByNumber(Scanner.IntegerValue);
if LineIndex = -1 then
Error(Scanner.PrevPosition, sUndefinedLineNumber);
PrepareLine(LineIndex);
end;
procedure TInterpreter.ExecuteSleep;
var
Start: TDateTime;
begin
// SLEEP
Scanner.NextToken;