-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDbasic.txt
1290 lines (977 loc) · 24.1 KB
/
Dbasic.txt
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
--- Starting dbasic ---
You can start dbasic in two ways:
dbasic
dbasic [filename]
Typing dbasic by itself on a line will start the BASIC
interpreter with an empty memory buffer and the prompt
'Ready.' displayed and await your input.
Typing dbasic with a supplied filename:
'dbasic test.bas'
will load the basic source file 'test.bas' into dbasics
memory buffer and begin executition of the BASIC statements
included in the file. When the program terminates dbasic will
exit and you will be returned back to the operating system.
--- Interactive dbasic use ---
Typing dbasic without a filename starts dbasic in the interactive
mode. You will be given a 'Ready.' prompt and dbasic will
await your commands.
Commands are as follows:
quit
exit
This terminates dbasic and returns you to your operating system.
Any files in the dbasic buffer will be lost.
save [filename]
This saves the dbasic buffer to a disk file whose name you
supply in [filename]. The file extension .bas is recommended
but not required. The filename is a path (can include directories).
load [filename]
This loads the disk file [filename] into the dbasic buffer.
Any existing file in the buffer will be overwritten.
The filename is a path (can include directories).
new
The new commands delete everything in the BASIC buffer.
free
The free command shows available memory in the BASIC buffer.
The default is 65536 available bytes, but this can be changed
as a define in the dbasic.h header.
list
The list command displays the BASIC statements in the dbasic buffer.
run
The run command starts execution of the BASIC commands in the
dbasic buffer. Execution will stop at the END command, the
STOP command, or if the program statements do not continue
with a loop such as GOTO or ON..GOTO.
--- Entering BASIC program statements ---
At the Ready. prompt, in addition to the above commands, any BASIC
statement preceeded with a line number will be saved to dbasics
buffer. You can view these statements with the 'list' command.
BASIC statements MUST have a line number. Statements cannot be entered
by themselves.
Example:
10 rem This is a program
20 for n = 1 to 10
30 print n,n*n
40 next n
50 print "Done"
99 end
Line numbers are required. All lines entered or loaded
from a file are converted to lower case (except between
double quotes "" are left alone). All user commands
are lower case. Filenames for save and load can be upper
or lower case or mixed case.
When entering a line, the statements will be saved in numerical order.
This means you can enter the above BASIC program starting at statement 99
and working backwards to line 10.
If you enter a line number of an existing line by itself, the existing
line will be deleted from the buffer. If you enter a different line with
the same line number as an existing line, the original line will be
replaced by the new line.
When you type 'run' the program will be run and the following output
will be generated:
Ready.
run
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
done
Ready.
If you want to save this program, type
save test1.bas
Ready.
save test1.bas
File test1.bas Saved
Ready.
When you run a program, any statements that dbasic doesn't recognize
will stop the program and a SYSTAX ERROR message along with the
offending line number will be displayed:
SYNTAX ERROR: [40 list]
End of buffer reached
list
10 rem
20 rem
30 print
40 list
Ready.
In line 40 above, 'list' is not a valid BASIC statement.
Also, note the 'End of buffer reached' message. This is
dbasics way of telling you that there is no terminating
END or STOP statement to end execution of the BASIC program.
Numeric variables are single letter, a...z
All numeric variables are floating point. Use
the INT() function to change the type.
String variables are single letter, a$...z$
Numeric variables and string variables do not share the
same memory space.
All expressions are algabraic in order:
Unary negation (-1) is highest.
Expressions in parens are next: (a+b)*c
(a+b)*c is different from a+b*c.
Powers have the next highest priority: 2**8
Next is multiplication and division: 3*6/4
Then is addition and subtraction: 4+5-3+9
Lowest priority are logical functions (&, |, ^)
Parenthesises can be nested:
10 let a=(1+2)*(2+(3*4+(43821+5)))
20 print a
99 end
run
131520
Functions can be nested:
10 let d=deg(sin(pi(0)+0.05))
20 print d
99 end
run
-2.8636
Ready.
While running, hitting ctrl-C will stop execution. If the
user is in an input loop, hit ctrl-C then press the enter key.
-------------------------------------
---------- dbasic KEYWORDS ----------
-------------------------------------
All keywords need a surrounding space:
10 let a=5, b=10
20 if a=5 then 10
The following BASIC statements are recognized as
valid keywords by dbasic:
--- REM ---
10 rem Everything after rem until line end is ignored
20 rem This is used to document your program
--- PRINT ---
10 print a,b,c
20 print "a=";a,"b=";b
30 print rnd(), sqr(2)*fna
Semi-colons stop spacing and linefeeds when used at line end
40 print "this is a te";
50 print "st of the print statement"
Commas print 3 spaces, everything between double quotes " "
is printed as is.
Math expressions are evaluated and printed:
10 print 25*sqr(9)*12*(3+4)
NOTE: The PRINT statement will NOT evaluate string
expressions:
10 print left$(a$,1)
will fail.
Instead use:
10 let p$=left$(a$,1)
20 print p$
This is true for left$(), mid$(), right$(), chr$()
The SPC() function will print the number of spaces
between ().
10 print spc(5);"This is printed with leading spaces"
20 print spc(n) "*"
See the SPC() function below for more examples.
--- File Operations ---
If a # and a file number occurs just after the PRINT
statement, all output will be directed to an open file.
10 print #1 a$,n
20 print #5 "This is a test: ";n
The '#' is required. An error will occur is the file number
has not already been opened. See the OPEN command below.
NOTE: When the file is opened, if the file already exists,
reading will start from the BEGINNING of the file. ALL writing
will be APPENDED to the END of the file. Use the
operating system commands to delete the contents of an existing
file if needed.
This is to prevent you from destroying a data file by mistake.
--- INPUT ---
Get user input:
10 rem get 3 numbers from the user
20 input a,b,c
10 rem again, but with a prompt:
20 input "enter a,b,c: ",a,b,c
10 rem get string from the user
20 input "Enter a string: ",a$
30 print "You entered ";a$
Commas are used only to separate variables.
Unlike the PRINT statement, commas and semicolons have no
effect on printing.
When entering a number, an expression can be entered instead
and it will be evaluated before being saved:
10 print "enter n"
20 input n
30 print n
99 end
run
enter n
?354*sin(20) + sqr(12) + 45/5
335.647
Ready.
--- File Operations ---
If a # and a file number occurs just after the INPUT
statement, all input will be read from an open file.
10 input #1 n
For each variable used, one line will be read from the
specified file and it's contents will be saved in that variable.
If there are multiple numbers on a line separated by spaces,
commas, etc, unpredictable results will occur.
Use a string variable (a$...z$) if you need to read an
entire line.
See FILE OPERATIONS below for examples.
NOTE: if end of file is reached, a warning will be printed
and the input operation will exit. Use the function eof(#n)
where n is the file number from 1-7 inclusive to test for an
end of file. EOF() returns 1 on end of file, and 0 if data
is still available.
Example:
10 open test.txt as #1
20 input #1 n
30 if eof(#1) = 1 then 90
40 print n
50 goto 20
90 close #1
100 end
NOTE: eof() will return 1 ONLY after an INPUT statement.
--- LET ---
Use the let keyword to assign variables:
10 let a=5
11 print a
20 let a=5,b=10,c=20
21 print a,b,c
30 let a=rnd(), b=sqr(2)+456*(123+456)
31 print a,b
NOTE: The LET keyword is optional.
10 a=6,b=sqr(a),b$="this is a "+"string"
20 print a,b,b$
99 end
run
6 2.44949 this is a string
Ready.
--- FOR/NEXT ---
10 for n=1 to 10
20 print n
30 next n
10 rem with the step keyword
20 for n=10 to -10 step -1
30 print n*n
40 next n
Do not use spaces in an equasion:
Example that will fail due to spaces in an equasion:
10 for n=(-1 * pi(0)) to (2*pi(0)) step pi(0)/30
Do this instead:
10 for n=(-1*pi(0)) to (2*pi(0)) step pi(0)/30
NOTE: for/next loops CANNOT be nested. This may change
in future releases of dbasic.
--- IF/THEN ---
The IF/THEN statements test both numeric and string
expressions:
-- NUMERIC EXPRESSIONS --
5 rem this program will stop at line 40
10 let a=5
20 if a=10 then 100
30 print a
40 end
100 print "at line 100"
110 end
10 rem examples:
20 if a>5 then 100
30 if a<5 then 200
40 if a>=10 then 300
50 if a<=5 then 400
60 if a!=5 then 500
70 if a<>5 then 500
10 rem with expressions:
20 if (a*54+b) > sqr(c*4) then 200
30 if a&1 != b|9 then 100
NOTE: & is a bitwise logical and
| is a bitwise logical or
^ is a bitwise logical xor
NOTE:
Legal:
10 if a<10 then 20
10 if a < 10 then 20
10 if a>=10 then 20
Not Legal:
10 if a ! = 10 then 20
10 if a! =10 then 20
10 if a<>10 then20
10 ifa>10then 20
Keep the test characters together if more than 1:
<=, >=, !=, <>
The keywords if/then must have a surrounding space.
(Actually all keywords need a surrounding space).
-- STRING EXPRESSIONS --
If a$="abc" and b$="abc" then a$=b$ because both contain the
same strings abc.
If a$="abb" and b$="abc" then, with a string comparison, b$>a$,
a$<b$ and a$ is not equal (!= or <>) to b$.
All string comparisons follow these rules.
10 input "enter name",n$
20 if n$ = "fred" then 100
30 print "not a match"
40 end
100 print "match"
110 end
Note that comparisons between strings and numbers is not allowed.
--- GOTO/GOSUB/RETURN ---
10 input "enter a number: ";a
20 if a=0 then 200
30 gosub 100
40 goto 10
100 print "you entered ";a
110 return
200 end
Variables also work in place of goto/gosub line numbers:
10 let a=100
20 goto a
30 end
100 print "at line 100"
110 end
The gosub/return stack size is limited to 20 levels
of depth. Exceeding that returns an error. (This
is set by a define in dbasic.h and can be changed).
--- ON GOTO ---
10 rem enter 1,2 or 3
20 input "enter 1,2 or 3",a
25 rem NOTE: place a space between line numbers!
30 on a goto 100 200 300 400
40 print "entered ",a
50 end
100 print "at line 100"
110 end
200 print "at line 200"
210 end
300 print "at line 300"
310 end
400 print "at line 400"
410 end
The gosub/return stack size is limited to 20 levels
of depth. Exceeding that returns an error. (This
is set by a define in dbasic.h and can be changed).
--- READ/DATA/RESTORE ---
10 for n=1 to 6
20 read a
30 print a
40 next n
50 end
100 data 100,200,300,400,500,600
10 rem this will fail:
20 for n=1 to 5
30 read a
40 print a
50 next a
60 end
100 data 1,2,3
110 rem note there is not enough data for the read
10 rem this will work
20 let n=1
30 read a
40 print a
50 let n=n+1
60 if n<5 then 30
70 restore
80 if n=10 then 100
90 goto 30
100 end
110 data 100,200,300,400
run
100
200
300
400
100
100
100
100
100
Ready.
The logic of the program does a restore when n>=5 which
is why you get the string of 100's.
--- DEF ---
5 rem Define a user function:
10 def fnf 1/(2*3.14159*sqr(l*c))
20 input "Enter inductance uh: ";l
30 input "Enter capacitance pf: ";c
40 let f=fnf
50 print fnf
60 end
run
Enter inductance uh: ?1.2e-6
Enter capacitance pf: ?220e-12
9.79532e+06
Ready.
This shows that a resonant circuit with an inductance of 1.2 uH
and a capacitance of 220 pf resonates at 9.795 MHz
5 rem functions can be nested:
10 def fna a*2
20 def fnb b*fna
30 input "enter a";a
40 input "enter b";b
50 let c=fnb
60 print c
run
enter a?2
enter b?3
12
Ready.
NOTE: You may get slightly differing results when calculating using
defined functions vs calculating directly. This is due to rounding
errors in the floating point routines of the CLIB libraries.
--- DIM ---
By default, all numeric variables are set up
with arrays of size 11: a(0) thru a(10) thru z(0) thru z(10).
5 rem set up & initialize an array
10 let c=200
20 dim a(c)
30 for n=0 to c-1
40 let a(n)=n
50 next n
55 print "a(150)=";a(150)
60 if a(150) != 150 then 200
70 end
200 print "Bad Array element"
210 end
Arrays only work on numbers a(x)...z(x)
and not strings.
The array subscript is limited only by available
memory.
10 dim a(1e6)
assigns 1 million array elements to a.
Arrays can be re-dimmed upwards. No data loss
will occur.
10 dim a(20)
20 let a(19) = 19
25 print "a(19)=";a(19)
30 dim a(30)
40 let a(29) = 29
50 for n=0 to 29
60 print "a(";n;")=";a(n)
70 next n
99 end
NOTE: you may see unexpected values for uninitalized
array variables. You will need to initialize them
before using them.
10 dim a(100)
20 for n=0 to 99
30 let a(n)=0
40 next n
.
.
.
(rest of program)
--- STOP ---
10 let a=1
20 print a
30 let a=a+1
40 if a<20 then 20
50 stop
The STOP keyword stops program execution. Stop
functions the same as the END keyword, but
prints a message whereas END does not.
--- END ---
Normal execution of a BASIC program stops when
the END statement is reached.
10 print a
20 end
--- CLEAR ---
The clear statement clears all variables. Normally
not used in a BASIC program but included for completeness.
10 let a=5
20 print a
30 clear
40 print a
50 end
run
5
0
Ready.
--- FILE OPERATIONS ---
dbasic supports reading from and writing to files.
The keywords OPEN, CLOSE, REWIND, PRINT # and INPUT #
are used to manipulate files.
--- OPEN ---
Use the keyword 'open' to open a file, with options to
input data from the file, append data to the file or write
data to the file..
The format to open a file:
open [filename] for [mode] as #[file number]
Filename is a path name (includes directories name
if needed).
Mode is input, output and append.
Input means the file is opened for read only. If the
file does not exist, an error will occur.
Output means write only. If the file exists, it will
be overwritten.
Append means open for read with the file pointer at
the start of the file, but any data written to the file
will be appended to the end of the file.
File numbers are 1-7 inclusive.
NOTE: Use quotes around filenames to preserve case. Quotes,
however, are not necessary.
The keyword 'for' is required.
The keyword 'as' is required.
The '#' preceeding the file number is also required.
Example:
10 open test.txt for input as #1
If the filenumber is already used (a file with the filenumber
is currently opened) an error will be reported.
If the file cannot be opened (insufficent space, no permissions
to access the file, etc) an error will be reported.
--- CLOSE ---
Use the keyword 'close' to close an existing file.
Note that files are automatically closed on errors, when
the END or STOP keyword is encountered, when the program
stops, when the end of buffer is reached and when a ctrl-c
is used to stop a running program.
10 close #1
The '#' is required.
An error occurs if the file is already closed.
--- REWIND ---
The keyword 'rewind' points the file position back to
the start of an open file. This is used to re-read an input
file like the RESTORE keyword is used in READ/DATA statements.
10 rewind #1
The '#' is required.
An error occurs if the file is closed.
--- EXAMPLES ---
10 rem create a file, write to it
20 open test.txt for output as #1
30 for n=1 to 10
40 print #1 n
50 next n
60 close #1
99 end
To read the just created file:
10 open test.txt for input as #1
20 for n=1 to 10
30 input #1 a(n)
40 print a(n)
50 next n
60 print "again"
70 rewind #1
80 for n=1 to 10
90 input #1 x
100 print x
110 next n
120 close #1
125 print "results"
130 for n=1 to 10
140 print a(n)
150 next n
160 end
run
1
2
3
4
5
6
7
8
9
10
again
1
2
3
4
5
6
7
8
9
10
results
1
2
3
4
5
6
7
8
9
10
Ready.
NOTE: if end of file is reached, a warning will be printed
but the program will continue to run. Any input statements
will be ignored. Use the function eof(#n) where n is the file
number from 1-7 inclusive to test for an end of file.
EOF() returns 1 on end of file. End of file is only
tested when using the INPUT statement. It is meaningless when
writing to a file (PRINT #).
Example:
10 open test.txt as #1
20 input #1 a
30 if eof(#1) = 1 then 90
40 print a
50 goto 20
90 close #1
99 end
NOTE: eof() will only return 1 AFTER an INPUT statement.
--------------------------------------------------
------------------ FUNCTIONS ---------------------
--------------------------------------------------
--- SIN(), COS(), TAN(), ATN() ---
The functions sin(), cos(), tan(), atn() all return the value
in radians. Use deg() to convert to degrees if needed.
10 let s=sin(12)
20 print "sin(12) in radians = ";s
30 let c=cos(12)
40 print "cos(12) in radians = ";c
50 let t=tan(12)
60 print "tan(12) in radians = ";t
70 let a=atn(12)
80 print "arctan(12) in radians = ";a
99 end
run
sin(12) in radians = -0.536573
cos(12) in radians = 0.843854
tan(12) in radians = -0.63586
arctan(12) in radians = 1.48766
Ready.
--- DEG(), RAD() ---
The function deg() converts the given value
to degrees:
10 let d=deg(1.57)
20 print d
30 end
run
89.9544
Ready.
The function rad() converts the given value
to radians:
10 let r=rad(89.95)
20 print r
30 end
run
1.56992
Ready.
--- PI() ---
Returns the value of pi.
NOTE: A dummy variable is required between ().
An error will be returned without it. The
dummy value is not used.
10 let p=pi(0)
20 print p
--- SQR(), LOG(), LN(), EXP() ---
10 let s=sqr(2)
20 let l=log(25)
30 let n=ln(43)
40 let e=exp(5.25)
50 print "square root of 2 = ";s
60 print "log base 10 log(25) = ";l
70 print "natural log ln(42) = ";n
80 print "exponent exp(5.25), 2.7183^5.25 = ";e
99 end
run
square root of 2 = 1.41421
log base 10 log(25) = 1.39794
natural log ln(42) = 3.7612
exponent exp(5.25), 2.7183^5.25 = 190.566
Ready.
--- INT() ---
10 let x=55.6
20 let i=int(x)
30 print "number ";x,"int(";x;")=";i
99 end
run
number 55.6 int(55.6)=55
Ready.
--- ABS() ---
10 let n=-25.5
20 let a=abs(n)
30 print "number ";n,"absolute value ";a
99 end
run
number -25.5 absolute value 25
Ready.
NOTE: absolute values are an integer.
--- SGN() ---
10 for n=-3 to 3
20 let s=sgn(n)
30 print "number ";n,"sign ";s
40 next n
50 end
run
number -3 sign -1
number -2 sign -1
number -1 sign -1
number 0 sign 0
number 1 sign 1
number 2 sign 1
number 3 sign 1
Ready.
As a test for negative/0/positive numbers:
5 rem test of sgn() and on/goto
10 input "enter a number ";n
20 let s=sgn(n)
30 on s+2 goto 100 200 300
40 print "should never be here"
50 end
100 print "Number ";n;" is negative"
110 end
200 print "Number ";n;" is 0"
210 end
300 print "Number ";n;" is positive"
310 end
--- RND(), RND(-1), RND(n) ---
rnd() and rnd(0) returns a psuedo random number from 0 thru 1.0
with a very high period.
When dbasic is started, a random seed is loaded so that
with each start of dbasic a different sequence of random
numbers will be generated.
rnd(-1) sets a new random seed.
rnd(n) where n is a positive integer > 0 will set a seed,
but the random number sequence will be repeatable (useful
for testing).
10 rem print 5 random numbers
15 print "random at start"
20 for n=1 to 5
30 print rnd()
40 next n
50 rem set a repeatable seed
55 print "repeatable"
60 let r=rnd(1)
70 for n=1 to 5
80 print rnd()
90 next n
100 rem again
105 print "repeatable (again)"
106 let r=rnd(1)
110 for n=1 to 5
120 print rnd()
130 next n
140 rem set a new seed
145 print "new seed"