- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathguide.html
1648 lines (1646 loc) · 119 KB
/
guide.html
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
<!DOCTYPE html>
<htmllang="en">
<head>
<metacharset="utf-8">
<metahttp-equiv="X-UA-Compatible" content="IE=edge">
<metaname="viewport" content="width=device-width, initial-scale=1">
<title>SmallBASIC | guide</title>
<metaname="description" content="SmallBASIC | One more basic">
<linkrel="canonical" href="/guide.html">
<linkrel="keywords" href="guide">
<linkrel="stylesheet" href="/css/style.css">
<linkrel="icon" type="image/png" href="/images/sb-desktop-32x32.png">
<scriptsrc="/clipboard.js"></script>
</head>
<body>
<buttononclick="topFunction()" id="BackToTopBtn" title="Go to top">⯅</button>
<scriptsrc="/backtotop.js"></script>
<divclass="wrapAll clearfix">
<navclass="navigation">
<divclass="logo">
<ahref="/"><imgsrc='/images/sb-logo.png?v=2' alt="logo"></a>
</div>
<divclass="navlinks">
<ahref="/pages/download.html">Download</a>
<ahref="/pages/news.html">News</a>
<ahref="/pages/community.html">Community</a>
<ahref="/pages/articles.html">Resources</a>
<ahref="/pages/reference.html">Language Reference</a>
<aclass='active' href="/pages/guide.html">SmallBASIC Manual</a>
</div>
</nav>
<divclass="mainsection">
<divclass="tabs clearfix">
<divclass="tabsRight">
<atarget="_github" href="https://github.com/smallbasic/smallbasic.github.io/blob/master/_build/pages/guide.markdown">Edit</a>
<atarget="_github" href="https://github.com/smallbasic/smallbasic.github.io/commits/master/_build/pages/guide.markdown">History</a>
</div>
</div>
<divclass="article">
<h1id="smallbasic-user-guide">SmallBASIC User Guide</h1>
<blockquote>
<p>SmallBASIC is a fast and easy to learn BASIC language interpreter
ideal for everyday calculations, scripts and prototypes. SmallBASIC
includes trigonometric, matrices and algebra functions, a built in IDE,
a powerful string library, system, sound, and graphic commands along
with structured programming syntax.</p>
</blockquote>
<divclass="articleRight">
<divclass="articleRightInner">
<p>Contents</p>
</div>
<ul>
<li><ahref="#GettingStarted">Getting Started</a>
<ul>
<li><ahref="#SmallbasicVersions">SmallBASIC Versions</a></li>
<li><ahref="#Windows">Windows</a></li>
<li><ahref="#Linux">Linux</a></li>
<li><ahref="#Android">Android</a></li>
<li><ahref="#BuildFromSource">Build from Source</a></li>
<li><ahref="#UsingtheIntegratedDevelopmentEnvironment">Using the
IDE</a></li>
</ul></li>
<li><ahref="#SourceCodeFormat">Source Code Format</a></li>
<li><ahref="#Comments">Comments</a></li>
<li><ahref="#NumberAndStrings">Numbers and Srings</a>
<ul>
<li><ahref="#Integers">Integers</a></li>
<li><ahref="#Reals">Reals</a></li>
<li><ahref="#Strings">String Literals</a></li>
</ul></li>
<li><ahref="#ConstantsVariables">Variables and Constants</a>
<ul>
<li><ahref="#Variable">Variable names</a></li>
<li><ahref="#AssigningValuesToVariables">Assigning Values to
Variables</a></li>
<li><ahref="#Constants">Constants</a></li>
</ul></li>
<li><ahref="#Arrays%7D">Arrays</a>
<ul>
<li><ahref="#CreatingArrays">Creating Arrays</a></li>
<li><ahref="#AccessingElementsOfAnArray">Accessing Elements of an
Array</a></li>
<li><ahref="#NestedArrays">Nested Arrays</a></li>
<li><ahref="#ArrayOperations">Array Operations</a></li>
</ul></li>
<li><ahref="#Matrices">Matrices</a>
<ul>
<li><ahref="#Creating2DMatrices">Creating 2D Matrices</a></li>
<li><ahref="#AccessingElementsOfA2DMatrix">Accessing Elements of a 2D
Matrix</a></li>
<li><ahref="#MatrixOperations">2D Matrix Operations</a></li>
<li><ahref="#HigherDimensionalMatrices">Higher Dimensional
Matrices</a></li>
<li><ahref="#RowAndColumnVectors">Row and Column Vectors</a></li>
</ul></li>
<li><ahref="#Maps">Maps</a>
<ul>
<li><ahref="#CreatingAMapVariable">Creating a Map Variable</a></li>
<li><ahref="#AccessingElementsOfAMapVariable">Accessing Elements of a
Map Variable</a></li>
<li><ahref="#AddKeyValuePairs">Add Key-Value Pairs</a></li>
<li><ahref="#KeyValuePairsWithReferences">Key-Value Pairs with
References</a></li>
<li><ahref="#MapsAsPseudoObjectForOOP">Maps as Pseudo Objects for
OOP</a></li>
</ul></li>
<li><ahref="#Operators">Operators</a>
<ul>
<li><ahref="#pseudo">Pseudo-operators</a></li>
</ul></li>
<li><ahref="#Expressions">Expressions</a></li>
<li><ahref="#SubroutinesAndFunctions">Subroutines and Functions</a>
<ul>
<li><ahref="#SubroutinesAndFunctionsNames">Names</a></li>
<li><ahref="#DeclarationOfSubroutines">Declaration of
Subroutines</a></li>
<li><ahref="#DeclarationOfFunctions">Declaration of Functions</a></li>
<li><ahref="#Parameters">Parameters</a></li>
<li><ahref="#ExitAFunctionOrSubroutine">Exit a function or
subroutine</a></li>
<li><ahref="#SingleLineFunctions">Single-line Functions</a></li>
<li><ahref="#UsingLocalVariables">Using Local Variables</a></li>
<li><ahref="#NestedRoutines">Nested Routines</a></li>
<li><ahref="#DeclarationsInPalmOS">Declarations in PalmOS</a></li>
</ul></li>
<li><ahref="#Conditions">Conditions</a>
<ul>
<li><ahref="#IfThenElseifEndif">IF-THEN-ELSIF-ELSE</a></li>
<li><ahref="#SingleLineIfThenElse">Single-line IF-THEN-ELSE</a></li>
<li><ahref="#InlineVersionOfIf">Inline Version of IF</a></li>
<li><ahref="#SelectCase">SELECT CASE</a></li>
</ul></li>
<li><ahref="#Loops">Loops</a>
<ul>
<li><ahref="#ForToNextLoop">FOR-TO-NEXT Loop</a></li>
<li><ahref="#ForInNextLoop">FOR-IN-NEXT Loop</a></li>
<li><ahref="#WhileWendLoop">WHILE-WEND Loop</a></li>
<li><ahref="#RepeatUntilLoop">REPEAT-UNTIL Loop</a></li>
<li><ahref="#TheDoKeyword">The DO Keyword</a></li>
</ul></li>
<li><ahref="#Units">Units</a>
<ul>
<li><ahref="#UnitsDeclaration">Declaration</a></li>
<li><ahref="#UnitsImport">Import</a></li>
</ul></li>
<li><ahref="#InputAndOutput">Input and Output</a>
<ul>
<li><ahref="#PrintOnScreen">Print on Screen</a></li>
<li><ahref="#ReadInputFromKeyboard">Read Input from the
Keyboard</a></li>
</ul></li>
<li><ahref="#TheUseKeyword">The USE Keyword</a></li>
<li><ahref="#Statement1">OPTION</a>
<ul>
<li><ahref="#Statement2">OPTION BASE</a></li>
<li><ahref="#Statement3">OPTION MATCH</a></li>
<li><ahref="#Statement4">OPTION PREDEF</a></li>
</ul></li>
<li><ahref="#Meta">Meta Commands</a></li>
<li><ahref="#ExceptionHandling">Exception Handling</a></li>
</ul>
</div>
<h2id="GettingStarted">Getting Started</h2>
<p>SmallBASIC is available for various operating systems, including
Windows, Linux, and Android. It can be built from source to run on many
other POSIX-compliant systems, such as Raspberry Pi OS. With its
built-in Integrated Development Environment (IDE), you can write
programs on your Android-powered tablet or mobile phone and run them
instantly.</p>
<h3id="SmallbasicVersions">SmallBASIC Versions</h3>
<p>SmallBASIC comes in three different flavors for the desktop. The SDL
version offers a simple but efficient IDE and is perfect for working
with graphics. The FLTK version has a great IDE. The console version
doesn’t offer an IDE and is text-only but works perfect with plugins
like raylib or nuklear. The Android version is similar to the SDL
version and comes with an integrated IDE, too.</p>
<h3id="Windows">Windows</h3>
<p>Download the <ahref="/pages/download.html">latest release of
SmallBASIC</a>. The different versions of SmallBASIC are included in the
zip-file. Extract the zip-file to a location of your choice. Open the
SmallBASIC folder and start one of the following programs:</p>
<ul>
<li>sbasicg.exe: SDL</li>
<li>sbasici.exe: FLTK</li>
<li>sbasic.exe: Console</li>
</ul>
<h3id="Linux">Linux</h3>
<p>Download the <ahref="pages/download.html">latest release of
SmallBASIC</a>. The different versions of SmallBASIC are provided as
separate AppImages. Download an AppImage and copy it to a directory of
your choice. Execute the AppImage. Depending of the Linux version you
have to make the AppImage executable:
<code>chmod u+x AppImageFile</code>, where <code>AppImageFile</code> is
the filename of the AppImage.</p>
<h3id="Android">Android</h3>
<p>Download and install SmallBASIC for Android using <a
href="https://play.google.com/store/apps/details?id=net.sourceforge.smallbasic">Google
Play</a>. Files are stored in <code>/InternalMemory/SmallBASIC</code> or
in case of an old Android version in
<code>/InternalMemory/Android/data/net.sourceforge.smallbasic/files</code>.
For easy file transfer between Android and desktop, please read <a
href="/pages/android_file_transfer.html">SmallBASIC file
transfer</a></p>
<h3id="BuildFromSource">Build from Source</h3>
<p>Using Linux it is quite easy to build SmallBASIC from source. This
has the advantage, that you don’t need to use AppImages and you can use
SmallBASIC on other devices like Raspberry Pi. Please follow the
instructions on <a
href="https://github.com/smallbasic/SmallBASIC">Github</a>.</p>
<h3id="UsingtheIntegratedDevelopmentEnvironment">Using the Integrated
Development Environment</h3>
<p>Please read the separate articles for the different versions of
SmallBASIC:</p>
<ul>
<li><ahref="/pages/sdl.html">SDL</a></li>
<li><ahref="/pages/android.html">Android</a></li>
<li><ahref="/pages/fltk.html">FLTK</a></li>
</ul>
<h2id="SourceCodeFormat">Source Code Format</h2>
<p>SmallBASIC files are plain text files in ASCII or UTF-8. A program
consists of at least one line of text. Every line is terminated with a
<code>CR</code> or <code>CR/LF</code>. Each line consist of one or more
commands. Multiple commands in a line are separated by a colon
<code>:</code>. If <code>&</code> is the last character of a line,
SmallBASIC will continue with the command in the next line.</p>
<p>Spaces and tabs are ignored as long as the syntax is valid. Inside a
string literal spaces and tabs are retained.</p>
<p>A line can start with a line number. Line numbers can be in any
order. Multiple lines can have the same line number. Commands like
<code>GOTO</code> will go to the last line with the specified line
number.</p>
<h2id="Comments">Comments</h2>
<p>A comment is a piece of code which is ignored by SmallBASIC but gives
the programmer helpful information about the source code. Comments can
be created by placing text after a quotation mark <code>'</code> or
after the command <code>REM</code>. Comments can be placed anywhere in
the code but not inside a string. Starting with <code>'</code> or
<code>REM</code> the rest of the line will be a comment.</p>
<p>When using <code>REM</code> be aware that <code>REM</code> is a build
in command and multiple commands need to be separated with a colon
<code>:</code>.</p>
<divclass="sourceCode" id="cb1"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb1-1"><ahref="#cb1-1" aria-hidden="true" tabindex="-1"></a><spanclass="co">' This is a comment</span></span>
<spanid="cb1-2"><ahref="#cb1-2" aria-hidden="true" tabindex="-1"></a><spanclass="co">REM This is a comment</span></span>
<spanid="cb1-3"><ahref="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb1-4"><ahref="#cb1-4" aria-hidden="true" tabindex="-1"></a>a = <spanclass="dv">1</span><spanclass="co">' This is a comment</span></span>
<spanid="cb1-5"><ahref="#cb1-5" aria-hidden="true" tabindex="-1"></a>a = <spanclass="dv">1</span> : <spanclass="co">REM This is a comment</span></span></code></pre></div>
<h2id="NumberAndStrings">Numbers and Strings</h2>
<h3id="Integers">Integers</h3>
<p>Integers are used to store numeric values without any fractional
part. Integer is the default data type. You can declare integers in
decimal, hexadecimal, octal and binary form. For hexadecimal the number
starts with <code>0x</code> or <code>&x</code>, for octal with
<code>0o</code> or <code>&o</code>, and for binary with
<code>0b</code> or <code>&b</code>.</p>
<divclass="sourceCode" id="cb2"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb2-1"><ahref="#cb2-1" aria-hidden="true" tabindex="-1"></a><spanclass="dv">256</span></span>
<spanid="cb2-2"><ahref="#cb2-2" aria-hidden="true" tabindex="-1"></a><spanclass="dv">0x100</span><spanclass="co">' Hexadecimal form 1</span></span>
<spanid="cb2-3"><ahref="#cb2-3" aria-hidden="true" tabindex="-1"></a><spanclass="dv">&h100</span><spanclass="co">' Hexadecimal form 2</span></span>
<spanid="cb2-4"><ahref="#cb2-4" aria-hidden="true" tabindex="-1"></a><spanclass="dv">0o400</span><spanclass="co">' Octal form 1</span></span>
<spanid="cb2-5"><ahref="#cb2-5" aria-hidden="true" tabindex="-1"></a><spanclass="dv">&o400</span><spanclass="co">' Octal form 2</span></span>
<spanid="cb2-6"><ahref="#cb2-6" aria-hidden="true" tabindex="-1"></a><spanclass="dv">0b111</span><spanclass="co">' Binary form 1</span></span>
<spanid="cb2-7"><ahref="#cb2-7" aria-hidden="true" tabindex="-1"></a><spanclass="dv">&b111</span><spanclass="co">' Binary form 2</span></span></code></pre></div>
<h3id="Reals">Reals</h3>
<p>Floating point values are used to store numeric data which may have a
fractional part. Any number which out-bounds the limits or an integer or
has decimal digits will be converted automatically to real. Reals can be
written in scientific notation.</p>
<divclass="sourceCode" id="cb3"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb3-1"><ahref="#cb3-1" aria-hidden="true" tabindex="-1"></a><spanclass="dv">1.2</span></span>
<spanid="cb3-2"><ahref="#cb3-2" aria-hidden="true" tabindex="-1"></a><spanclass="dv">.25</span></span>
<spanid="cb3-3"><ahref="#cb3-3" aria-hidden="true" tabindex="-1"></a><spanclass="dv">1E2</span></span>
<spanid="cb3-4"><ahref="#cb3-4" aria-hidden="true" tabindex="-1"></a><spanclass="dv">1.2E-2</span></span>
<spanid="cb3-5"><ahref="#cb3-5" aria-hidden="true" tabindex="-1"></a><spanclass="dv">2.6E-0.25</span></span></code></pre></div>
<h3id="Strings">String Literals</h3>
<p>Strings are used to store sequences of characters. The characters of
a string are enclosed with <code>"</code>. If a string spans more than
one line, enclose the string with <code>"""</code>. The end of each line
will be a line break in the string. Strings may be appended to one
another using the <code>+</code> operator.</p>
<divclass="sourceCode" id="cb4"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb4-1"><ahref="#cb4-1" aria-hidden="true" tabindex="-1"></a><spanclass="st">"This is a string"</span></span>
<spanid="cb4-2"><ahref="#cb4-2" aria-hidden="true" tabindex="-1"></a><spanclass="st">"Hello, "</span> + <spanclass="st">"world!"</span></span>
<spanid="cb4-3"><ahref="#cb4-3" aria-hidden="true" tabindex="-1"></a><spanclass="st">"""This is</span></span>
<spanid="cb4-4"><ahref="#cb4-4" aria-hidden="true" tabindex="-1"></a>a <spanclass="fu">string</span></span>
<spanid="cb4-5"><ahref="#cb4-5" aria-hidden="true" tabindex="-1"></a>over several lines</span>
<spanid="cb4-6"><ahref="#cb4-6" aria-hidden="true" tabindex="-1"></a>with <spanclass="fu">line</span> breaks<spanclass="st">"""</span></span></code></pre></div>
<p>Strings can contain escape sequences. Escape sequences always start
with the <code>\</code> character. The following escape sequences are
supported:</p>
<table>
<colgroup>
<colstyle="width: 13%" />
<colstyle="width: 86%" />
</colgroup>
<thead>
<trclass="header">
<th>Code</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<trclass="odd">
<td><code>\a</code></td>
<td>beep</td>
</tr>
<trclass="even">
<td><code>\t</code></td>
<td>tab (20 px)</td>
</tr>
<trclass="odd">
<td><code>\r</code></td>
<td>return</td>
</tr>
<trclass="even">
<td><code>\n</code></td>
<td>next line</td>
</tr>
<trclass="odd">
<td><code>\"</code></td>
<td>quote <code>"</code></td>
</tr>
<trclass="even">
<td><code>\\</code></td>
<td>Backslash <code>\</code></td>
</tr>
<trclass="odd">
<td><code>\e[K</code></td>
<td>clear to end of line</td>
</tr>
<trclass="even">
<td><code>\e[nG</code></td>
<td>move to column <code>n</code></td>
</tr>
<trclass="odd">
<td><code>\e[s</code></td>
<td>save cursor position</td>
</tr>
<trclass="even">
<td><code>\e[u</code></td>
<td>restore cursor position</td>
</tr>
<trclass="odd">
<td><code>\e[0m</code></td>
<td>reset all attributes to their defaults</td>
</tr>
<trclass="even">
<td><code>\e[1m</code></td>
<td>set bold on</td>
</tr>
<trclass="odd">
<td><code>\e[3m</code></td>
<td>set italic on</td>
</tr>
<trclass="even">
<td><code>\e[4m</code></td>
<td>set underline on</td>
</tr>
<trclass="odd">
<td><code>\e[7m</code></td>
<td>reverse video</td>
</tr>
<trclass="even">
<td><code>\e[21m</code></td>
<td>set bold off</td>
</tr>
<trclass="odd">
<td><code>\e[23m</code></td>
<td>set italic off</td>
</tr>
<trclass="even">
<td><code>\e[24m</code></td>
<td>set underline off</td>
</tr>
<trclass="odd">
<td><code>\e[27m</code></td>
<td>set reverse off</td>
</tr>
<trclass="even">
<td><code>\e[nm</code></td>
<td><code>n</code>: foreground color 30 to 37, <code>m</code>:
background color 40 to 47</td>
</tr>
</tbody>
</table>
<p>Example:</p>
<divclass="sourceCode" id="cb5"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb5-1"><ahref="#cb5-1" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span><spanclass="st">"String with line\nbreak"</span></span>
<spanid="cb5-2"><ahref="#cb5-2" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span><spanclass="st">"\e[32m\e[47mGreen text on white background"</span></span></code></pre></div>
<p>In the console version of SmallBASIC (sbasic.exe or sbasic) most of
the escape codes, for example <a
href="https://en.wikipedia.org/wiki/ANSI_escape_code">ANSI Codes at
wikipedia</a>, can be used in version 12.25 or later. Support of escape
codes depends on the operating system and the terminal you are
using.</p>
<h2id="ConstantsVariables">Variables and Constants</h2>
<p>SmallBASIC uses, internally, 4 data-types:</p>
<ul>
<li>Integer (64bit)</li>
<li>Real (64bit)</li>
<li>String (> 2GB)</li>
<li>Array (> 50M elements)</li>
</ul>
<p>However, all user variables (include arrays) are ‘Variant’. That
means the data-type is invisible to users and might change during
runtime. Arrays are always dynamic (size and number of elements), even
if you had declared their size. Conversions between those types are
performed internally. In any case there are functions for the user to do
it manually.</p>
<h3id="Variable">Variable Names</h3>
<p>Variable names can use any alphanumeric characters, extended
characters (ASCII codes 128 - 255 for non-English languages), the symbol
<code>_</code>, and the symbol <code>$</code>. The first character of
the name cannot be a digit nor <code>$</code>.</p>
<p>The symbol <code>$</code> is supported for compatibility. The dollar
in variable names will be count as part of the name, that means
<code>v</code> and <code>v$</code> are two different variables. It can
be used only as the last character of the name, and only one is
allowed.</p>
<p>Variable names are case-insensitive.</p>
<pre><code>abc, a_c, _bc, ab2c, abc$ -> valid names
1cd, a$b, $abc -> invalid names</code></pre>
<h3id="AssigningValuesToVariables">Assigning Values to Variables</h3>
<p>Variables do not need to be declared. A variable will be initialized
with <code>0</code> when used for the first time.</p>
<p>Values are assigned to a variable using the <code>=</code> character.
The value at the right of <code>=</code> will be assigned to the
variable on the left side. Additionally the right side of <code>=</code>
can be an expression or a function.</p>
<divclass="sourceCode" id="cb7"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb7-1"><ahref="#cb7-1" aria-hidden="true" tabindex="-1"></a>a = <spanclass="dv">256</span></span>
<spanid="cb7-2"><ahref="#cb7-2" aria-hidden="true" tabindex="-1"></a>b = <spanclass="dv">0x100</span></span>
<spanid="cb7-3"><ahref="#cb7-3" aria-hidden="true" tabindex="-1"></a>c = <spanclass="dv">0o400</span></span>
<spanid="cb7-4"><ahref="#cb7-4" aria-hidden="true" tabindex="-1"></a>d = <spanclass="dv">0b111</span></span>
<spanid="cb7-5"><ahref="#cb7-5" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb7-6"><ahref="#cb7-6" aria-hidden="true" tabindex="-1"></a>e = <spanclass="st">"This is a string"</span></span>
<spanid="cb7-7"><ahref="#cb7-7" aria-hidden="true" tabindex="-1"></a>g = <spanclass="st">"""This is</span></span>
<spanid="cb7-8"><ahref="#cb7-8" aria-hidden="true" tabindex="-1"></a>a <spanclass="fu">string</span></span>
<spanid="cb7-9"><ahref="#cb7-9" aria-hidden="true" tabindex="-1"></a>over several lines</span>
<spanid="cb7-10"><ahref="#cb7-10" aria-hidden="true" tabindex="-1"></a>with <spanclass="fu">line</span> breaks<spanclass="st">"""</span></span>
<spanid="cb7-11"><ahref="#cb7-11" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb7-12"><ahref="#cb7-12" aria-hidden="true" tabindex="-1"></a>h = <spanclass="dv">1</span> + <spanclass="dv">1</span></span>
<spanid="cb7-13"><ahref="#cb7-13" aria-hidden="true" tabindex="-1"></a>i = h</span>
<spanid="cb7-14"><ahref="#cb7-14" aria-hidden="true" tabindex="-1"></a>j = <spanclass="dv">1</span> + h</span>
<spanid="cb7-15"><ahref="#cb7-15" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb7-16"><ahref="#cb7-16" aria-hidden="true" tabindex="-1"></a>k = <spanclass="st">"Hello, "</span> + <spanclass="st">"world"</span></span>
<spanid="cb7-17"><ahref="#cb7-17" aria-hidden="true" tabindex="-1"></a>l = k + <spanclass="st">"!"</span></span>
<spanid="cb7-18"><ahref="#cb7-18" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb7-19"><ahref="#cb7-19" aria-hidden="true" tabindex="-1"></a>m = j > h</span>
<spanid="cb7-20"><ahref="#cb7-20" aria-hidden="true" tabindex="-1"></a>n = h == j</span>
<spanid="cb7-21"><ahref="#cb7-21" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb7-22"><ahref="#cb7-22" aria-hidden="true" tabindex="-1"></a>o = <spanclass="fu">sin</span>(<spanclass="dv">0.5</span>)</span></code></pre></div>
<p>For compatibility reasons, SmallBASIC support the assignment of
values to a variable using the <code>LET</code> command.</p>
<divclass="sourceCode" id="cb8"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb8-1"><ahref="#cb8-1" aria-hidden="true" tabindex="-1"></a><spanclass="dt">let</span> x = <spanclass="dv">20</span></span></code></pre></div>
<h3id="Constants">Constants</h3>
<p>Constants store values that don’t change during the execution of the
program. Constant variables can be declared by using the keyword
<code>CONST</code>. Assigning a value to a constant a second time will
result in an error.</p>
<divclass="sourceCode" id="cb9"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb9-1"><ahref="#cb9-1" aria-hidden="true" tabindex="-1"></a><spanclass="dt">CONST</span> my_pi = <spanclass="dv">3.14</span></span></code></pre></div>
<p>SmallBASIC predefines the following constants:</p>
<table>
<thead>
<trclass="header">
<thstyle="text-align: center;">Constant</th>
<thstyle="text-align: center;">Value</th>
</tr>
</thead>
<tbody>
<trclass="odd">
<tdstyle="text-align: center;">PI</td>
<tdstyle="text-align: center;">3.14..</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;">TRUE</td>
<tdstyle="text-align: center;">1</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;">FALSE</td>
<tdstyle="text-align: center;">0</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;">NIL</td>
<tdstyle="text-align: center;">nil (not set)</td>
</tr>
</tbody>
</table>
<h2id="Arrays">Arrays</h2>
<p>Arrays are used to store sequences of elements. Arrays are always
dynamic. The allowed data-types of the elements can be integers, reals
and strings and maps Data-types can be mixed in an array. Nested arrays
are allowed.</p>
<p>Arrays are a subclass of Matrices (see <a
href="#Matrices">Matrices</a>). They are equivalent to a row vector.</p>
<h3id="CreatingArrays">Creating Arrays</h3>
<p>Arrays can be created by assignment using the <code>=</code>
operator, with the command <code>DIM</code>, or with
<code>[]</code>.</p>
<p><strong>By Assignment</strong></p>
<p>To assign an array to a variable, the array elements are enclosed by
square brackets <code>[</code> and <code>]</code>. The array elements
are separated by a comma <code>,</code>. If no value for a element is
given, SmallBASIC will set this element to <code>0</code>.</p>
<divclass="sourceCode" id="cb10"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb10-1"><ahref="#cb10-1" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">1</span>,<spanclass="dv">2</span>,<spanclass="dv">3</span>,<spanclass="dv">4</span>]</span>
<spanid="cb10-2"><ahref="#cb10-2" aria-hidden="true" tabindex="-1"></a>A = [,<spanclass="dv">2</span>,,<spanclass="dv">4</span>] <spanclass="co">' Equal to A = [0,2,0,4]</span></span>
<spanid="cb10-3"><ahref="#cb10-3" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">1</span>,<spanclass="st">"A"</span>, <spanclass="dv">2.2</span>] <spanclass="co">' Array with elements of different data-type</span></span>
<spanid="cb10-4"><ahref="#cb10-4" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">1</span>,<spanclass="st">"A"</span>, [<spanclass="dv">2</span>,<spanclass="dv">3</span>]] <spanclass="co">' Creates an nested array</span></span>
<spanid="cb10-5"><ahref="#cb10-5" aria-hidden="true" tabindex="-1"></a>S = [<spanclass="dv">1</span>, <spanclass="st">"Small"</span>, <spanclass="st">"BASIC"</span>] <spanclass="co">' Numbers and strings</span></span>
<spanid="cb10-6"><ahref="#cb10-6" aria-hidden="true" tabindex="-1"></a>M = [{x: <spanclass="dv">1</span>, y: <spanclass="dv">2</span>}, {x: <spanclass="dv">4</span>, y: <spanclass="dv">7</span>}] <spanclass="co">' Array of maps</span></span></code></pre></div>
<p><strong>By DIM</strong></p>
<p>Using the command <code>DIM A(n)</code> or <code>DIM A[n]</code>, an
array <code>A</code> with <code>n + 1</code> elements will be created.
Every element is set to <code>0</code>.</p>
<divclass="sourceCode" id="cb11"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb11-1"><ahref="#cb11-1" aria-hidden="true" tabindex="-1"></a><spanclass="dt">DIM</span> A(<spanclass="dv">3</span>) <spanclass="co">' Array A with 4 elements</span></span>
<spanid="cb11-2"><ahref="#cb11-2" aria-hidden="true" tabindex="-1"></a><spanclass="dt">DIM</span> A[<spanclass="dv">3</span>]</span></code></pre></div>
<p>When using the command <code>DIM A(lower TO upper)</code> it is also
possible, to set the <code>lower</code> and <code>upper</code> element
index of an array.</p>
<divclass="sourceCode" id="cb12"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb12-1"><ahref="#cb12-1" aria-hidden="true" tabindex="-1"></a><spanclass="co">' One dimensional array of 7 elements, starting from 3</span></span>
<spanid="cb12-2"><ahref="#cb12-2" aria-hidden="true" tabindex="-1"></a><spanclass="co">' with elements A(3), A(4), ... , A(9)</span></span>
<spanid="cb12-3"><ahref="#cb12-3" aria-hidden="true" tabindex="-1"></a><spanclass="dt">DIM</span> A(<spanclass="dv">3</span><spanclass="kw">TO</span><spanclass="dv">9</span>)</span></code></pre></div>
<p><strong>Empty Array</strong></p>
<p>The following example will create an empty array variable. If the
variable already exist, it will be deleted and reinitialized as an
array.</p>
<divclass="sourceCode" id="cb13"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb13-1"><ahref="#cb13-1" aria-hidden="true" tabindex="-1"></a>A = []</span></code></pre></div>
<h3id="AccessingElementsOfAnArray">Accessing Elements of an Array</h3>
<p>To access an element of an array, the array name is followed by
<code>(i)</code> or <code>[i]</code>, where <code>i</code> is the
element index. In SmallBASIC using default configuration, the first
element of an array has the index <code>0</code>.</p>
<p>Examples:</p>
<divclass="sourceCode" id="cb14"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb14-1"><ahref="#cb14-1" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">1</span>,<spanclass="dv">2</span>,<spanclass="dv">3</span>,<spanclass="dv">4</span>]</span>
<spanid="cb14-2"><ahref="#cb14-2" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A(<spanclass="dv">1</span>) <spanclass="co">' Output: 2</span></span>
<spanid="cb14-3"><ahref="#cb14-3" aria-hidden="true" tabindex="-1"></a>A[<spanclass="dv">1</span>] = <spanclass="dv">5</span><spanclass="co">' A = [1,5,3,4]</span></span>
<spanid="cb14-4"><ahref="#cb14-4" aria-hidden="true" tabindex="-1"></a>A(<spanclass="dv">0</span>) = A(<spanclass="dv">3</span>) <spanclass="co">' A = [4,5,3,4]</span></span></code></pre></div>
<p>Accessing an element outside the bounds of the array will result in
an error.</p>
<h3id="NestedArrays">Nested Arrays</h3>
<p>In a nested array, one or more elements of this array are arrays.
Nested arrays can be created by assignment with the <code>=</code>
operator. There is no limit to the depth of nesting.</p>
<divclass="sourceCode" id="cb15"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb15-1"><ahref="#cb15-1" aria-hidden="true" tabindex="-1"></a>A = [[<spanclass="dv">1</span>,<spanclass="dv">2</span>] , [<spanclass="dv">3</span>,<spanclass="dv">4</span>]]</span>
<spanid="cb15-2"><ahref="#cb15-2" aria-hidden="true" tabindex="-1"></a>A = [[<spanclass="dv">1</span>,<spanclass="dv">2</span>] , [<spanclass="dv">3</span>, [<spanclass="dv">4</span>, <spanclass="dv">5</span>]]]</span>
<spanid="cb15-3"><ahref="#cb15-3" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb15-4"><ahref="#cb15-4" aria-hidden="true" tabindex="-1"></a>B = [<spanclass="dv">1</span>, <spanclass="dv">2</span>, <spanclass="dv">3</span>]</span>
<spanid="cb15-5"><ahref="#cb15-5" aria-hidden="true" tabindex="-1"></a>C = [<spanclass="dv">4</span>, <spanclass="dv">5</span>]</span>
<spanid="cb15-6"><ahref="#cb15-6" aria-hidden="true" tabindex="-1"></a>B(<spanclass="dv">2</span>) = C <spanclass="co">' -> B = [1, 2, [4, 5]]</span></span></code></pre></div>
<p>To access an element of a nested array, a second (or third, etc) pair
of parenthesis or square brackets can be used.</p>
<divclass="sourceCode" id="cb16"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb16-1"><ahref="#cb16-1" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">1</span>, <spanclass="dv">2</span>, [<spanclass="dv">4</span>, <spanclass="dv">5</span>]]</span>
<spanid="cb16-2"><ahref="#cb16-2" aria-hidden="true" tabindex="-1"></a><spanclass="kw">PRINT</span> A[<spanclass="dv">2</span>][<spanclass="dv">0</span>] <spanclass="co">' Output: 4</span></span>
<spanid="cb16-3"><ahref="#cb16-3" aria-hidden="true" tabindex="-1"></a><spanclass="kw">PRINT</span> A(<spanclass="dv">2</span>)(<spanclass="dv">1</span>) <spanclass="co">' Output: 5</span></span></code></pre></div>
<h3id="ArrayOperations">Array Operations</h3>
<p>SmallBASIC supports basic array operations using the standard
operators:</p>
<p><strong>Element-wise Addition and Subtraction</strong></p>
<divclass="sourceCode" id="cb17"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb17-1"><ahref="#cb17-1" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">1</span>, <spanclass="dv">2</span>, <spanclass="dv">3</span>, <spanclass="dv">4</span>]</span>
<spanid="cb17-2"><ahref="#cb17-2" aria-hidden="true" tabindex="-1"></a>B = [<spanclass="dv">5</span>, <spanclass="dv">6</span>, <spanclass="dv">7</span>, <spanclass="dv">8</span>]</span>
<spanid="cb17-3"><ahref="#cb17-3" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb17-4"><ahref="#cb17-4" aria-hidden="true" tabindex="-1"></a>C = B + A</span>
<spanid="cb17-5"><ahref="#cb17-5" aria-hidden="true" tabindex="-1"></a>D = A - B</span></code></pre></div>
<p><strong>Element-wise Multiplication</strong></p>
<divclass="sourceCode" id="cb18"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb18-1"><ahref="#cb18-1" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">1</span>, <spanclass="dv">2</span>, <spanclass="dv">3</span>, <spanclass="dv">4</span>]</span>
<spanid="cb18-2"><ahref="#cb18-2" aria-hidden="true" tabindex="-1"></a>B = [<spanclass="dv">5</span>, <spanclass="dv">6</span>, <spanclass="dv">7</span>, <spanclass="dv">8</span>]</span>
<spanid="cb18-3"><ahref="#cb18-3" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb18-4"><ahref="#cb18-4" aria-hidden="true" tabindex="-1"></a>C = A * B</span>
<spanid="cb18-5"><ahref="#cb18-5" aria-hidden="true" tabindex="-1"></a>D = <spanclass="dv">0.8</span> * A</span></code></pre></div>
<p><strong>Equality</strong></p>
<divclass="sourceCode" id="cb19"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb19-1"><ahref="#cb19-1" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">1</span>, <spanclass="dv">2</span>, <spanclass="dv">3</span>, <spanclass="dv">4</span>]</span>
<spanid="cb19-2"><ahref="#cb19-2" aria-hidden="true" tabindex="-1"></a>B = [<spanclass="dv">5</span>, <spanclass="dv">6</span>, <spanclass="dv">7</span>, <spanclass="dv">8</span>]</span>
<spanid="cb19-3"><ahref="#cb19-3" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb19-4"><ahref="#cb19-4" aria-hidden="true" tabindex="-1"></a>e = A==B</span></code></pre></div>
<p><strong>Unary</strong></p>
<divclass="sourceCode" id="cb20"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb20-1"><ahref="#cb20-1" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">5</span>, <spanclass="dv">6</span>; <spanclass="dv">7</span>, <spanclass="dv">8</span>]</span>
<spanid="cb20-2"><ahref="#cb20-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb20-3"><ahref="#cb20-3" aria-hidden="true" tabindex="-1"></a>B = -A</span></code></pre></div>
<p><strong>Append Elements</strong></p>
<p>The pseudo-operator <code><<</code> can be used to append
elements to an array.</p>
<divclass="sourceCode" id="cb21"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb21-1"><ahref="#cb21-1" aria-hidden="true" tabindex="-1"></a>A << <spanclass="dv">1</span></span>
<spanid="cb21-2"><ahref="#cb21-2" aria-hidden="true" tabindex="-1"></a>A << <spanclass="dv">2</span></span>
<spanid="cb21-3"><ahref="#cb21-3" aria-hidden="true" tabindex="-1"></a>A << <spanclass="dv">3</span></span>
<spanid="cb21-4"><ahref="#cb21-4" aria-hidden="true" tabindex="-1"></a><spanclass="kw">PRINT</span> A(<spanclass="dv">1</span>) <spanclass="co">' Output: 2</span></span></code></pre></div>
<h2id="Matrices">Matrices</h2>
<h3id="Creating2DMatrices">Creating 2D Matrices</h3>
<p>A 2D matrix can be created by assignment using the <code>=</code>
operator or with the command <code>DIM</code>.</p>
<p><strong>By Assignment</strong></p>
<p>To assign a matrix to a variable, the matrix elements are enclosed by
square brackets <code>[</code> and <code>]</code>. The matrix elements
of one row are separated by <code>,</code> and rows are separated by a
semicolon <code>;</code>. If no value for a element is given, SmallBASIC
will set this element to <code>0</code>.</p>
<p>Examples 1:</p>
<divclass="sourceCode" id="cb22"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb22-1"><ahref="#cb22-1" aria-hidden="true" tabindex="-1"></a>M = [<spanclass="dv">11</span>, <spanclass="dv">12</span>; <spanclass="dv">21</span>, <spanclass="dv">22</span>; <spanclass="dv">31</span>, <spanclass="dv">32</span>]</span>
<spanid="cb22-2"><ahref="#cb22-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb22-3"><ahref="#cb22-3" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Creates the 3x2 matrix</span></span>
<spanid="cb22-4"><ahref="#cb22-4" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 11 12</span></span>
<spanid="cb22-5"><ahref="#cb22-5" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 21 22</span></span>
<spanid="cb22-6"><ahref="#cb22-6" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 31 32</span></span></code></pre></div>
<p>Example 2:</p>
<pre><code>M = [ ; ; 1, 2 ; 3, 4, 5]
' Creates the 3x3 matrix
' 0 0 0
' 1 2 0
' 3 4 5</code></pre>
<p><strong>By DIM</strong></p>
<p>Using the command <code>DIM M(n,m)</code> or <code>DIM M[n,m]</code>,
a matrix <code>M</code> with <code>n + 1</code> rows and
<code>m + 1</code> elements per row will be created. Every element is
set to <code>0</code>.</p>
<p>Example:</p>
<divclass="sourceCode" id="cb24"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb24-1"><ahref="#cb24-1" aria-hidden="true" tabindex="-1"></a><spanclass="dt">DIM</span> M(<spanclass="dv">3</span>,<spanclass="dv">2</span>)</span>
<spanid="cb24-2"><ahref="#cb24-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb24-3"><ahref="#cb24-3" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Creates the 3x4 matrix</span></span>
<spanid="cb24-4"><ahref="#cb24-4" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0 0 0</span></span>
<spanid="cb24-5"><ahref="#cb24-5" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0 0 0</span></span>
<spanid="cb24-6"><ahref="#cb24-6" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0 0 0</span></span>
<spanid="cb24-7"><ahref="#cb24-7" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0 0 0</span></span></code></pre></div>
<p>When using the command
<code>DIM A(rlower TO rupper, elower TO eupper)</code> it is also
possible, to set the <code>rlower</code> and <code>rupper</code> row
index and <code>elower</code> and <code>eupper</code> element index of a
matrix.</p>
<p>Example:</p>
<divclass="sourceCode" id="cb25"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb25-1"><ahref="#cb25-1" aria-hidden="true" tabindex="-1"></a><spanclass="dt">DIM</span> M(<spanclass="dv">2</span><spanclass="kw">to</span><spanclass="dv">4</span>, <spanclass="dv">3</span><spanclass="kw">to</span><spanclass="dv">7</span>)</span>
<spanid="cb25-2"><ahref="#cb25-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb25-3"><ahref="#cb25-3" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Creates the 5x3 matrix</span></span>
<spanid="cb25-4"><ahref="#cb25-4" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0 0 0 0 0</span></span>
<spanid="cb25-5"><ahref="#cb25-5" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0 0 0 0 0</span></span>
<spanid="cb25-6"><ahref="#cb25-6" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0 0 0 0 0</span></span>
<spanid="cb25-7"><ahref="#cb25-7" aria-hidden="true" tabindex="-1"></a><spanclass="co">' With elements M(2,3) to M(2,7), M(3,3) to M(3,7) and M(4,3) to M(4,7)</span></span></code></pre></div>
<h3id="AccessingElementsOfA2DMatrix">Accessing Elements of a 2D
Matrix</h3>
<p>To access an element of a matrix, the matrix name is followed by
<code>(row, column)</code> or <code>[row, column]</code>, where
<code>column</code> and <code>row</code> are the coordinates of the
matrix element. In SmallBASIC using default configuration, the first
element of a matrix has the index <code>(0, 0)</code>.</p>
<p>Examples:</p>
<divclass="sourceCode" id="cb26"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb26-1"><ahref="#cb26-1" aria-hidden="true" tabindex="-1"></a>M = [<spanclass="dv">1</span>,<spanclass="dv">2</span>;<spanclass="dv">3</span>,<spanclass="dv">4</span>]</span>
<spanid="cb26-2"><ahref="#cb26-2" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> M(<spanclass="dv">0</span>,<spanclass="dv">0</span>) <spanclass="co">' Output: 1</span></span>
<spanid="cb26-3"><ahref="#cb26-3" aria-hidden="true" tabindex="-1"></a>M[<spanclass="dv">1</span>,<spanclass="dv">0</span>] = <spanclass="dv">5</span><spanclass="co">' M = [1,2;5,4]</span></span>
<spanid="cb26-4"><ahref="#cb26-4" aria-hidden="true" tabindex="-1"></a>M(<spanclass="dv">0</span>,<spanclass="dv">1</span>) = M(<spanclass="dv">0</span>,<spanclass="dv">0</span>) <spanclass="co">' M = [1,1,5,4]</span></span></code></pre></div>
<p>Accessing an element outside the bounds of the matrix will result in
an error.</p>
<h3id="MatrixOperations">2D Matrix Operations</h3>
<p>SmallBASIC supports basic matrix operations using the standard
operators:</p>
<p><strong>Addition and Subtraction</strong></p>
<divclass="sourceCode" id="cb27"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb27-1"><ahref="#cb27-1" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">1</span>, <spanclass="dv">2</span>; <spanclass="dv">3</span>, <spanclass="dv">4</span>]</span>
<spanid="cb27-2"><ahref="#cb27-2" aria-hidden="true" tabindex="-1"></a>B = [<spanclass="dv">5</span>, <spanclass="dv">6</span>; <spanclass="dv">7</span>, <spanclass="dv">8</span>]</span>
<spanid="cb27-3"><ahref="#cb27-3" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb27-4"><ahref="#cb27-4" aria-hidden="true" tabindex="-1"></a>C = B + A</span>
<spanid="cb27-5"><ahref="#cb27-5" aria-hidden="true" tabindex="-1"></a>D = A - B</span></code></pre></div>
<p><strong>Multiplication</strong></p>
<pre><code>A = [1, 2; 3, 4]
B = [5 ; 6]
C = A * B
D = 0.8 * A</code></pre>
<p><strong>Equality</strong></p>
<divclass="sourceCode" id="cb29"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb29-1"><ahref="#cb29-1" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">1</span>, <spanclass="dv">2</span>; <spanclass="dv">3</span>, <spanclass="dv">4</span>]</span>
<spanid="cb29-2"><ahref="#cb29-2" aria-hidden="true" tabindex="-1"></a>B = [<spanclass="dv">5</span>, <spanclass="dv">6</span>; <spanclass="dv">7</span>, <spanclass="dv">8</span>]</span>
<spanid="cb29-3"><ahref="#cb29-3" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb29-4"><ahref="#cb29-4" aria-hidden="true" tabindex="-1"></a>e = A==B</span></code></pre></div>
<p><strong>Unary</strong></p>
<divclass="sourceCode" id="cb30"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb30-1"><ahref="#cb30-1" aria-hidden="true" tabindex="-1"></a>A = [<spanclass="dv">5</span>, <spanclass="dv">6</span>; <spanclass="dv">7</span>, <spanclass="dv">8</span>]</span>
<spanid="cb30-2"><ahref="#cb30-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb30-3"><ahref="#cb30-3" aria-hidden="true" tabindex="-1"></a>B = -A</span></code></pre></div>
<h3id="HigherDimensionalMatrices">Higher Dimensional Matrices</h3>
<p>Matrices with a dimension higher than two can be created with the
command <code>DIM M(n1, n2, ..., ni)</code>, where <code>n1 + 1</code>
to <code>ni + 1</code> are the number of elements of each dimension. To
access an element, give the coordinates of that element in parenthesis
or square brackets.</p>
<p>Example:</p>
<divclass="sourceCode" id="cb31"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb31-1"><ahref="#cb31-1" aria-hidden="true" tabindex="-1"></a><spanclass="dt">DIM</span> M(<spanclass="dv">2</span>,<spanclass="dv">2</span>,<spanclass="dv">3</span>) <spanclass="co">' Creates a 3x3x4 3D Matrix</span></span>
<spanid="cb31-2"><ahref="#cb31-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb31-3"><ahref="#cb31-3" aria-hidden="true" tabindex="-1"></a>M[<spanclass="dv">0</span>,<spanclass="dv">0</span>,<spanclass="dv">2</span>] = <spanclass="dv">5</span></span></code></pre></div>
<p>Equality is the only valid matrix operating for higher dimensional
matrices.</p>
<h3id="RowAndColumnVectors">Row and Column Vectors</h3>
<p><strong>By Assignment</strong></p>
<p>To create a column vector, separate the elements by
<code>;</code>.</p>
<p>Example:</p>
<divclass="sourceCode" id="cb32"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb32-1"><ahref="#cb32-1" aria-hidden="true" tabindex="-1"></a>v = [<spanclass="dv">1</span>;<spanclass="dv">2</span>;<spanclass="dv">3</span>]</span>
<spanid="cb32-2"><ahref="#cb32-2" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Creates the column vector</span></span>
<spanid="cb32-3"><ahref="#cb32-3" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 1</span></span>
<spanid="cb32-4"><ahref="#cb32-4" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 2</span></span>
<spanid="cb32-5"><ahref="#cb32-5" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 3</span></span>
<spanid="cb32-6"><ahref="#cb32-6" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb32-7"><ahref="#cb32-7" aria-hidden="true" tabindex="-1"></a><spanclass="kw">PRINT</span> v(<spanclass="dv">1</span>,<spanclass="dv">0</span>) <spanclass="co">' Prints the value of the element in row 2 -> 2</span></span></code></pre></div>
<p><strong>By DIM</strong></p>
<p>The command <code>DIM A(n)</code> creates a row vector with
<code>n+1</code> elements. To create a column vector, use
<code>DIM A(n, 0)</code>. The elements of the column vector can be
access by <code>A(n,0)</code>, where <code>n</code> is the column index
of the element.</p>
<divclass="sourceCode" id="cb33"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb33-1"><ahref="#cb33-1" aria-hidden="true" tabindex="-1"></a><spanclass="dt">DIM</span> A(<spanclass="dv">2</span>)</span>
<spanid="cb33-2"><ahref="#cb33-2" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Creates a row vector</span></span>
<spanid="cb33-3"><ahref="#cb33-3" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0 0 0</span></span>
<spanid="cb33-4"><ahref="#cb33-4" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb33-5"><ahref="#cb33-5" aria-hidden="true" tabindex="-1"></a><spanclass="dt">DIM</span> B(<spanclass="dv">2</span>, <spanclass="dv">0</span>)</span>
<spanid="cb33-6"><ahref="#cb33-6" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Creates a column vector</span></span>
<spanid="cb33-7"><ahref="#cb33-7" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0</span></span>
<spanid="cb33-8"><ahref="#cb33-8" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0</span></span>
<spanid="cb33-9"><ahref="#cb33-9" aria-hidden="true" tabindex="-1"></a><spanclass="co">' 0</span></span>
<spanid="cb33-10"><ahref="#cb33-10" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb33-11"><ahref="#cb33-11" aria-hidden="true" tabindex="-1"></a><spanclass="kw">PRINT</span> B(<spanclass="dv">1</span>,<spanclass="dv">0</span>) <spanclass="co">' Prints the value of the element in row 2</span></span></code></pre></div>
<h2id="Maps">Maps</h2>
<p>Maps are complex data structures with key-value pairs. Elements of a
map are addressed by keys. Allowed data-types are integers, reals,
strings, arrays, maps and references. Elements of different data-types
can be mixed in a map.</p>
<h3id="CreatingAMapVariable">Creating a Map Variable</h3>
<p>Maps can be created by assignment using the <code>=</code> operator,
with the command <code>ARRAY</code> and with <code>{}</code>.</p>
<p><strong>By Assignment</strong></p>
<p>To assign a map to a variable, key-value pairs are enclosed by curly
brackets <code>{</code> and <code>}</code>. The key-value pairs are
separated by comma <code>,</code>. A key-value pair consist of a key and
a value. Key and value are separated by a colon <code>:</code>. The key
is a string. It is not necessary to enclose the string with quotes. The
value can be any data-type, array, map or reference. The order of
key-value pairs is not relevant for maps.</p>
<p>Additionally, a map variable can be created with the assignment
<code>name.key = value</code>, where <code>name</code> is the name of
the map variable, <code>key</code> is the name of the key and value any
datatype supported by SmallBASIC.</p>
<p>Examples of valid map assignments:</p>
<divclass="sourceCode" id="cb34"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb34-1"><ahref="#cb34-1" aria-hidden="true" tabindex="-1"></a>A = {x:<spanclass="dv">1</span>, y:<spanclass="dv">2</span>, z:<spanclass="dv">3</span>}</span>
<spanid="cb34-2"><ahref="#cb34-2" aria-hidden="true" tabindex="-1"></a>B = {<spanclass="st">"x"</span>:<spanclass="dv">1</span>, <spanclass="st">"y"</span>:<spanclass="dv">2</span>, <spanclass="st">"z"</span>:<spanclass="dv">3</span>}</span>
<spanid="cb34-3"><ahref="#cb34-3" aria-hidden="true" tabindex="-1"></a>C = {<spanclass="dv">1</span>:<spanclass="dv">10</span>, <spanclass="dv">2</span>:<spanclass="dv">5</span>, Name: <spanclass="st">"SmallBASIC"</span>}</span>
<spanid="cb34-4"><ahref="#cb34-4" aria-hidden="true" tabindex="-1"></a>D = {ArrayData:[<spanclass="dv">1</span>,<spanclass="dv">2</span>,<spanclass="dv">3</span>], MapData:{node:<spanclass="dv">1</span>, value:<spanclass="dv">5</span>}}</span>
<spanid="cb34-5"><ahref="#cb34-5" aria-hidden="true" tabindex="-1"></a>E.x = <spanclass="dv">1</span></span></code></pre></div>
<p><strong>By ARRAY</strong></p>
<p>Using the command <code>M = ARRAY(JSONString)</code> will create a
map variable from the string <code>JSONString</code>.</p>
<divclass="sourceCode" id="cb35"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb35-1"><ahref="#cb35-1" aria-hidden="true" tabindex="-1"></a>M = <spanclass="kw">ARRAY</span>(<spanclass="st">"{x:1, y:2, z:3}"</span>)</span>
<spanid="cb35-2"><ahref="#cb35-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb35-3"><ahref="#cb35-3" aria-hidden="true" tabindex="-1"></a>s = <spanclass="st">"{1:10, 2:5, Name:\"</span>SmallBASIC\<spanclass="st">"}"</span><spanclass="co">' The quotation marks of the string SmallBASIC are escaped with \"</span></span>
<spanid="cb35-4"><ahref="#cb35-4" aria-hidden="true" tabindex="-1"></a>M = <spanclass="kw">ARRAY</span>(s)</span></code></pre></div>
<p><strong>Empty Map</strong></p>
<p>The following example will create an empty map variable. If the
variable already exist, it will be deleted and reinitialized as a
map.</p>
<divclass="sourceCode" id="cb36"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb36-1"><ahref="#cb36-1" aria-hidden="true" tabindex="-1"></a>A = {}</span></code></pre></div>
<h3id="AccessingElementsOfAMapVariable">Accessing Elements of a Map
Variable</h3>
<p>An element of a map variable can be accessed in the following three
ways:</p>
<oltype="1">
<li><code>name.key</code></li>
<li><code>name("key")</code> or <code>name["key"]</code></li>
<li><code>s = "key" : name(s)</code> or
<code>s = "key" : name[s]</code></li>
</ol>
<p>where <code>name</code> is the name of the map variable and
<code>key</code> is the key of the key-value pair. The third option
offers the possibility to access an element using a string-variable as
the key.</p>
<divclass="sourceCode" id="cb37"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb37-1"><ahref="#cb37-1" aria-hidden="true" tabindex="-1"></a>A = {x:<spanclass="dv">1</span>, y:<spanclass="dv">2</span>, z:<spanclass="dv">3</span>}</span>
<spanid="cb37-2"><ahref="#cb37-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb37-3"><ahref="#cb37-3" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A.x</span>
<spanid="cb37-4"><ahref="#cb37-4" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A(<spanclass="st">"x"</span>)</span>
<spanid="cb37-5"><ahref="#cb37-5" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A[<spanclass="st">"x"</span>]</span>
<spanid="cb37-6"><ahref="#cb37-6" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb37-7"><ahref="#cb37-7" aria-hidden="true" tabindex="-1"></a>A.y = <spanclass="dv">3</span></span>
<spanid="cb37-8"><ahref="#cb37-8" aria-hidden="true" tabindex="-1"></a>A(<spanclass="st">"y"</span>) = <spanclass="dv">3</span></span>
<spanid="cb37-9"><ahref="#cb37-9" aria-hidden="true" tabindex="-1"></a>A[<spanclass="st">"y"</span>] = <spanclass="dv">3</span></span></code></pre></div>
<p>To access elements of an array or a matrix inside a map, use
additional parentheses <code>()</code> or square
brackets<code>[]</code>.</p>
<divclass="sourceCode" id="cb38"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb38-1"><ahref="#cb38-1" aria-hidden="true" tabindex="-1"></a>A = {x:[<spanclass="dv">2</span>,<spanclass="dv">3</span>], y:[<spanclass="dv">1</span>,<spanclass="dv">2</span>;<spanclass="dv">3</span>,<spanclass="dv">4</span>]}</span>
<spanid="cb38-2"><ahref="#cb38-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb38-3"><ahref="#cb38-3" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A.x(<spanclass="dv">0</span>) <spanclass="co">' Output: 2</span></span>
<spanid="cb38-4"><ahref="#cb38-4" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A.x[<spanclass="dv">1</span>] <spanclass="co">' Output: 3</span></span>
<spanid="cb38-5"><ahref="#cb38-5" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A.y[<spanclass="dv">1</span>,<spanclass="dv">1</span>] <spanclass="co">' Output: 4</span></span>
<spanid="cb38-6"><ahref="#cb38-6" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb38-7"><ahref="#cb38-7" aria-hidden="true" tabindex="-1"></a>A.x(<spanclass="dv">0</span>) = <spanclass="dv">5</span></span>
<spanid="cb38-8"><ahref="#cb38-8" aria-hidden="true" tabindex="-1"></a>A.x[<spanclass="dv">1</span>] = <spanclass="dv">6</span></span>
<spanid="cb38-9"><ahref="#cb38-9" aria-hidden="true" tabindex="-1"></a>A.y[<spanclass="dv">1</span>,<spanclass="dv">1</span>] = <spanclass="dv">7</span></span>
<spanid="cb38-10"><ahref="#cb38-10" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A <spanclass="co">' Output: {"y":[1,2;3,7],"x":[5,6]}</span></span></code></pre></div>
<p>To access a nested map variable, use a further <code>.</code> or
<code>("key")</code>. Both notations can be mixed.</p>
<divclass="sourceCode" id="cb39"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb39-1"><ahref="#cb39-1" aria-hidden="true" tabindex="-1"></a>A = {x:{a:<spanclass="dv">1</span>, b:{c:<spanclass="dv">2</span>}}}</span>
<spanid="cb39-2"><ahref="#cb39-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb39-3"><ahref="#cb39-3" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A.x.a <spanclass="co">' Output: 1</span></span>
<spanid="cb39-4"><ahref="#cb39-4" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A.x.b.c <spanclass="co">' Output: 2</span></span>
<spanid="cb39-5"><ahref="#cb39-5" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A(<spanclass="st">"x"</span>)(<spanclass="st">"a"</span>) <spanclass="co">' Output: 1</span></span>
<spanid="cb39-6"><ahref="#cb39-6" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A(<spanclass="st">"x"</span>)(<spanclass="st">"b"</span>)(<spanclass="st">"c"</span>) <spanclass="co">' Output: 2</span></span>
<spanid="cb39-7"><ahref="#cb39-7" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A(<spanclass="st">"x"</span>)(<spanclass="st">"b"</span>).c <spanclass="co">' Output: 2</span></span>
<spanid="cb39-8"><ahref="#cb39-8" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb39-9"><ahref="#cb39-9" aria-hidden="true" tabindex="-1"></a>A.x.a = <spanclass="dv">3</span></span>
<spanid="cb39-10"><ahref="#cb39-10" aria-hidden="true" tabindex="-1"></a>A.x.b.c = <spanclass="dv">4</span></span>
<spanid="cb39-11"><ahref="#cb39-11" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A <spanclass="co">' Output: {"x":{"a":3,"b":{"c":4</span><spanclass="re">}}}</span></span>
<spanid="cb39-12"><ahref="#cb39-12" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb39-13"><ahref="#cb39-13" aria-hidden="true" tabindex="-1"></a>A(<spanclass="st">"x"</span>)(<spanclass="st">"a"</span>) = <spanclass="dv">5</span></span>
<spanid="cb39-14"><ahref="#cb39-14" aria-hidden="true" tabindex="-1"></a>A(<spanclass="st">"x"</span>)(<spanclass="st">"b"</span>)(<spanclass="st">"c"</span>) = <spanclass="dv">6</span></span>
<spanid="cb39-15"><ahref="#cb39-15" aria-hidden="true" tabindex="-1"></a>A(<spanclass="st">"x"</span>)(<spanclass="st">"b"</span>).c = <spanclass="dv">7</span></span>
<spanid="cb39-16"><ahref="#cb39-16" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A <spanclass="co">' Output: {"x":{"a":5,"b":{"c":7</span><spanclass="re">}}}</span><spanclass="co"></span></span></code></pre></div>
<p>Reading a non-existing element of a map will return <code>0</code>.
Writing to a non-existing element will create a new key-value pair.</p>
<h3id="AddKeyValuePairs">Add Key-Value Pairs</h3>
<p>Use dot-notation or parenthesis-notation with the <code>=</code>
operator to add a new key-value pair to a map variable.</p>
<divclass="sourceCode" id="cb40"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb40-1"><ahref="#cb40-1" aria-hidden="true" tabindex="-1"></a>A = {a:<spanclass="dv">1</span>, b:<spanclass="dv">2</span>}</span>
<spanid="cb40-2"><ahref="#cb40-2" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb40-3"><ahref="#cb40-3" aria-hidden="true" tabindex="-1"></a>A.c = <spanclass="dv">3</span></span>
<spanid="cb40-4"><ahref="#cb40-4" aria-hidden="true" tabindex="-1"></a>A(<spanclass="st">"d"</span>) = <spanclass="dv">4</span></span>
<spanid="cb40-5"><ahref="#cb40-5" aria-hidden="true" tabindex="-1"></a>A.e = {x:<spanclass="dv">5</span>, y:<spanclass="dv">6</span>}</span></code></pre></div>
<h3id="KeyValuePairsWithReferences">Key-Value Pairs with
References</h3>
<p>The value of a key-value pair can be a reference to a function or
subroutine.</p>
<divclass="sourceCode" id="cb41"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb41-1"><ahref="#cb41-1" aria-hidden="true" tabindex="-1"></a>A = {}</span>
<spanid="cb41-2"><ahref="#cb41-2" aria-hidden="true" tabindex="-1"></a>A.x = <spanclass="dv">1</span></span>
<spanid="cb41-3"><ahref="#cb41-3" aria-hidden="true" tabindex="-1"></a>A.Add = @Add</span>
<spanid="cb41-4"><ahref="#cb41-4" aria-hidden="true" tabindex="-1"></a>A.PrintBold = @PrintBold</span>
<spanid="cb41-5"><ahref="#cb41-5" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb41-6"><ahref="#cb41-6" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A <spanclass="co">' Output: {"x":1,"Add":func,"PrintBold":func}</span></span>
<spanid="cb41-7"><ahref="#cb41-7" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb41-8"><ahref="#cb41-8" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Execute function and subroutine</span></span>
<spanid="cb41-9"><ahref="#cb41-9" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A.Add(<spanclass="dv">1</span>, <spanclass="dv">2</span>)</span>
<spanid="cb41-10"><ahref="#cb41-10" aria-hidden="true" tabindex="-1"></a>A.PrintBold(<spanclass="st">"Test"</span>)</span>
<spanid="cb41-11"><ahref="#cb41-11" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb41-12"><ahref="#cb41-12" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Define function and subroutine</span></span>
<spanid="cb41-13"><ahref="#cb41-13" aria-hidden="true" tabindex="-1"></a><spanclass="kw">func </span>Add(a,b)</span>
<spanid="cb41-14"><ahref="#cb41-14" aria-hidden="true" tabindex="-1"></a><spanclass="kw">return</span> a + b</span>
<spanid="cb41-15"><ahref="#cb41-15" aria-hidden="true" tabindex="-1"></a><spanclass="kw">end</span></span>
<spanid="cb41-16"><ahref="#cb41-16" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb41-17"><ahref="#cb41-17" aria-hidden="true" tabindex="-1"></a><spanclass="kw">sub </span>PrintBold(s)</span>
<spanid="cb41-18"><ahref="#cb41-18" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span><spanclass="st">"\e[1m"</span>; s; <spanclass="st">"\e[21m"</span></span>
<spanid="cb41-19"><ahref="#cb41-19" aria-hidden="true" tabindex="-1"></a><spanclass="kw">end</span></span></code></pre></div>
<h3id="MapsAsPseudoObjectForOOP">Maps as Pseudo Objects for OOP</h3>
<p>Maps in combination with references can be used to create a pseudo
object similar to object-oriented programming.</p>
<divclass="sourceCode" id="cb42"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb42-1"><ahref="#cb42-1" aria-hidden="true" tabindex="-1"></a>A = MyAddClass(<spanclass="dv">3</span>,<spanclass="dv">4</span>)</span>
<spanid="cb42-2"><ahref="#cb42-2" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span> A.Add()</span>
<spanid="cb42-3"><ahref="#cb42-3" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb42-4"><ahref="#cb42-4" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Definitions</span></span>
<spanid="cb42-5"><ahref="#cb42-5" aria-hidden="true" tabindex="-1"></a><spanclass="kw">func </span>MyAddClass(x,y)</span>
<spanid="cb42-6"><ahref="#cb42-6" aria-hidden="true" tabindex="-1"></a><spanclass="dt">local</span> TempMap</span>
<spanid="cb42-7"><ahref="#cb42-7" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb42-8"><ahref="#cb42-8" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Properties</span></span>
<spanid="cb42-9"><ahref="#cb42-9" aria-hidden="true" tabindex="-1"></a> TempMap.x = x</span>
<spanid="cb42-10"><ahref="#cb42-10" aria-hidden="true" tabindex="-1"></a> TempMap.y = y</span>
<spanid="cb42-11"><ahref="#cb42-11" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb42-12"><ahref="#cb42-12" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Methods</span></span>
<spanid="cb42-13"><ahref="#cb42-13" aria-hidden="true" tabindex="-1"></a> TempMap.Add = @AddFunction</span>
<spanid="cb42-14"><ahref="#cb42-14" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb42-15"><ahref="#cb42-15" aria-hidden="true" tabindex="-1"></a><spanclass="co">' Functions and subroutines</span></span>
<spanid="cb42-16"><ahref="#cb42-16" aria-hidden="true" tabindex="-1"></a><spanclass="kw">func </span>AddFunction()</span>
<spanid="cb42-17"><ahref="#cb42-17" aria-hidden="true" tabindex="-1"></a><spanclass="kw">return</span><spanclass="dt">self</span>.x + <spanclass="dt">self</span>.y</span>
<spanid="cb42-18"><ahref="#cb42-18" aria-hidden="true" tabindex="-1"></a><spanclass="kw">end</span></span>
<spanid="cb42-19"><ahref="#cb42-19" aria-hidden="true" tabindex="-1"></a></span>
<spanid="cb42-20"><ahref="#cb42-20" aria-hidden="true" tabindex="-1"></a><spanclass="kw">return</span> TempMap</span>
<spanid="cb42-21"><ahref="#cb42-21" aria-hidden="true" tabindex="-1"></a><spanclass="kw">end</span></span></code></pre></div>
<h2id="Operators">Operators</h2>
<p>SmallBASIC supports the following operators, sorted by priority:</p>
<table>
<colgroup>
<colstyle="width: 18%" />
<colstyle="width: 81%" />
</colgroup>
<thead>
<trclass="header">
<thstyle="text-align: center;">Operator</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<trclass="odd">
<tdstyle="text-align: center;"><code>(</code>, <code>)</code></td>
<td>Parenthesis</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;"><code>+</code>, <code>-</code></td>
<td>Unary</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;"><code>~</code></td>
<td>bitwise NOT</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;"><code>NOT</code> or <code>!</code></td>
<td>Logical NOT (NOT false = true)</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;"><code>^</code></td>
<td>Exponentiation</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;"><code>*</code>, <code>/</code>,
<code>\</code></td>
<td>Multiplication, Division, Integer Division</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;"><code>%</code> or <code>MOD</code></td>
<td>Reminder (QB compatible:
<code>a = int(a), b = int(b), a - b*(a/b)</code>)</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;"><code>MDL</code></td>
<td>Modulus (<code>a % b + b*(sgn(a) <> sgn(b))</code>)</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;"><code>+</code>, <code>-</code></td>
<td>Addition/Concatenation, Subtraction</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;"><code>=</code> or <code>==</code></td>
<td>Equal</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;"><code><></code> or
<code>!=</code></td>
<td>Not Equal</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;"><code>></code>,
<code><</code></td>
<td>Less Than, Greater Than</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;"><code>=></code>,
<code>=<</code></td>
<td>Less or Equal, Greater or Equal</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;"><code>>=</code>,
<code><=</code></td>
<td>Less or Equal, Greater or Equal</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;"><code>IN</code></td>
<td>belongs to</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;"><code>LIKE</code></td>
<td>Regular expression match</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;"><code>AND</code> or
<code>&&</code></td>
<td>Logical AND</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;"><code>OR</code> or <code>||</code></td>
<td>Logical OR</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;"><code>BAND</code> or
<code>&</code></td>
<td>bitwise AND</td>
</tr>
<trclass="even">
<tdstyle="text-align: center;"><code>BOR</code> or <code>|</code></td>
<td>bitwise OR</td>
</tr>
<trclass="odd">
<tdstyle="text-align: center;"><code>EQV</code></td>