-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathguide.html
More file actions
2014 lines (2012 loc) · 147 KB
/
Copy pathguide.html
File metadata and controls
2014 lines (2012 loc) · 147 KB
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>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SmallBASIC | guide</title>
<meta name="description" content="SmallBASIC | One more basic">
<link rel="canonical" href="/guide.html">
<link rel="keywords" href="guide">
<link rel="stylesheet" href="/css/style.css">
<link rel="icon" type="image/png" href="/images/sb-desktop-32x32.png">
<script src="/clipboard.js"></script>
</head>
<body>
<button onclick="topFunction()" id="BackToTopBtn" title="Go to top">⯅</button>
<script src="/backtotop.js"></script>
<div class="wrapAll clearfix">
<nav class="navigation">
<div class="logo">
<a href="/"><img src='/images/sb-logo.png?v=2' alt="logo"></a>
</div>
<div class="navlinks">
<a href="/pages/download.html">Download</a>
<a href="/pages/news.html">News</a>
<a href="/pages/community.html">Community</a>
<a href="/pages/articles.html">Resources</a>
<a href="/pages/reference.html">Language Reference</a>
<a class='active' href="/pages/guide.html">SmallBASIC Manual</a>
</div>
</nav>
<div class="mainsection">
<div class="tabs clearfix">
<div class="tabsRight">
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/blob/master/_build/pages/guide.markdown">Edit</a>
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/commits/master/_build/pages/guide.markdown">History</a>
</div>
</div>
<div class="article">
<h1 id="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>
<div class="articleRight">
<div class="articleRightInner">
<p>Contents</p>
</div>
<ul>
<li><a href="#GettingStarted">Getting Started</a>
<ul>
<li><a href="#SmallbasicVersions">SmallBASIC Versions</a></li>
<li><a href="#Windows">Windows</a></li>
<li><a href="#Linux">Linux</a></li>
<li><a href="#Android">Android</a></li>
<li><a href="#Teensy">Teensy</a></li>
<li><a href="#BuildFromSource">Build from Source</a></li>
<li><a href="#UsingtheIntegratedDevelopmentEnvironment">Using the
IDE</a></li>
</ul></li>
<li><a href="#SourceCodeFormat">Source Code Format</a></li>
<li><a href="#Comments">Comments</a></li>
<li><a href="#NumberAndStrings">Numbers and Srings</a>
<ul>
<li><a href="#Integers">Integers</a></li>
<li><a href="#Reals">Reals</a></li>
<li><a href="#Strings">String Literals</a></li>
</ul></li>
<li><a href="#ConstantsVariables">Variables and Constants</a>
<ul>
<li><a href="#Variable">Variable names</a></li>
<li><a href="#AssigningValuesToVariables">Assigning Values to
Variables</a></li>
<li><a href="#Constants">Constants</a></li>
</ul></li>
<li><a href="#Arrays">Arrays</a>
<ul>
<li><a href="#CreatingArrays">Creating Arrays</a></li>
<li><a href="#AccessingElementsOfAnArray">Accessing Elements of an
Array</a></li>
<li><a href="#NestedArrays">Nested Arrays</a></li>
<li><a href="#PackedUnpackedAssignment">Packed/Unpacked
Assignment</a></li>
<li><a href="#ArrayOperations">Array Operations</a></li>
</ul></li>
<li><a href="#Matrices">Matrices</a>
<ul>
<li><a href="#Creating2DMatrices">Creating 2D Matrices</a></li>
<li><a href="#AccessingElementsOfA2DMatrix">Accessing Elements of a 2D
Matrix</a></li>
<li><a href="#MatrixOperations">2D Matrix Operations</a></li>
<li><a href="#HigherDimensionalMatrices">Higher Dimensional
Matrices</a></li>
<li><a href="#RowAndColumnVectors">Row and Column Vectors</a></li>
</ul></li>
<li><a href="#Maps">Maps</a>
<ul>
<li><a href="#CreatingAMapVariable">Creating a Map Variable</a></li>
<li><a href="#AccessingElementsOfAMapVariable">Accessing Elements of a
Map Variable</a></li>
<li><a href="#AddKeyValuePairs">Add Key-Value Pairs</a></li>
<li><a href="#KeyValuePairsWithReferences">Key-Value Pairs with
References</a></li>
<li><a href="#MapsAsPseudoObjectForOOP">Maps as Pseudo Objects for
OOP</a></li>
</ul></li>
<li><a href="#Operators">Operators</a>
<ul>
<li><a href="#SupportedOperators">Supported Operators</a></li>
<li><a href="#PseudoOperators">Pseudo-Operators</a></li>
</ul></li>
<li><a href="#Expressions">Expressions</a></li>
<li><a href="#SubroutinesAndFunctions">Subroutines and Functions</a>
<ul>
<li><a href="#SubroutinesAndFunctionsNames">Names</a></li>
<li><a href="#DeclarationOfSubroutines">Declaration of
Subroutines</a></li>
<li><a href="#DeclarationOfFunctions">Declaration of Functions</a></li>
<li><a href="#Parameters">Parameters</a></li>
<li><a href="#ExitAFunctionOrSubroutine">Exit a function or
subroutine</a></li>
<li><a href="#SingleLineFunctions">Single-Line Functions</a></li>
<li><a href="#LogicalExpressionShortCircuitEvaluation">Short-circuit
evaluation</a></li>
<li><a href="#UsingLocalVariables">Using Local Variables</a></li>
<li><a href="#NestedRoutines">Nested Routines</a></li>
<li><a href="#FunctionAndRoutinePointers">Function and Routine
Pointers</a></li>
<li><a href="#DeclarationsInPalmOS">Declarations in PalmOS</a></li>
</ul></li>
<li><a href="#Conditions">Conditions</a>
<ul>
<li><a href="#IfThenElseifEndif">IF-THEN-ELSIF-ELSE</a></li>
<li><a href="#SingleLineIfThenElse">Single-Line IF-THEN-ELSE</a></li>
<li><a href="#InlineVersionOfIf">Inline Version of IF</a></li>
<li><a href="#SelectCase">SELECT CASE</a></li>
</ul></li>
<li><a href="#Loops">Loops</a>
<ul>
<li><a href="#ForToNextLoop">FOR-TO-NEXT Loop</a></li>
<li><a href="#ForInNextLoop">FOR-IN-NEXT Loop</a></li>
<li><a href="#WhileWendLoop">WHILE-WEND Loop</a></li>
<li><a href="#RepeatUntilLoop">REPEAT-UNTIL Loop</a></li>
<li><a href="#TheDoKeyword">The DO Keyword</a></li>
</ul></li>
<li><a href="#Units">Units</a>
<ul>
<li><a href="#UnitsDeclaration">Declaration</a></li>
<li><a href="#UnitsImport">Import</a></li>
<li><a href="#NamePathing">Name Pathing</a></li>
</ul></li>
<li><a href="#InputAndOutput">Input and Output</a>
<ul>
<li><a href="#PrintOnScreen">Print on Screen</a></li>
<li><a href="#ReadInputFromKeyboard">Read Input from the
Keyboard</a></li>
<li><a href="#DrawGraphicsOnScreen">Draw Graphics on Screen</a></li>
<li><a href="#BasicFileReadAndWrite">Basic File Read and Write</a></li>
<li><a href="#SaveArraysAndMapsToAFile">Save Arrays and Maps to a
File</a></li>
<li><a href="#SaveVariablesToAFile">Save Variables to a File</a></li>
<li><a href="#BinaryFiles">Binary Files</a></li>
</ul></li>
<li><a href="#TheUseKeyword">The USE Keyword</a></li>
<li><a href="#Statement1">OPTION</a>
<ul>
<li><a href="#Statement2">OPTION BASE</a></li>
<li><a href="#Statement3">OPTION MATCH</a></li>
<li><a href="#Statement4">OPTION PREDEF</a></li>
</ul></li>
<li><a href="#Meta">Meta Commands</a></li>
<li><a href="#ExceptionHandling">Exception Handling</a></li>
</ul>
</div>
<h2 id="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>
<h3 id="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>
<h3 id="Windows">Windows</h3>
<p>Download the <a href="/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>
<h3 id="Linux">Linux</h3>
<p>Download the <a href="/pages/download.html">latest release of
SmallBASIC</a>. Currently only the SDL version of SmallBASIC is provided
as a flatpak package. The other versions can be <a
href="https://github.com/smallbasic/SmallBASIC">build from
source</a>.</p>
<h3 id="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>
<h3 id="Teensy">Teensy</h3>
<p>SmallBASIC runs on the Teensy 4.0 and Teensy 4.1. For more
information please read the <a href="/pages/teensy.html">Teensy
guide</a>.</p>
<h3 id="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>
<h3 id="UsingtheIntegratedDevelopmentEnvironment">Using the Integrated
Development Environment</h3>
<p>Please read the separate articles for the different versions of
SmallBASIC:</p>
<ul>
<li><a href="/pages/sdl.html">SDL</a></li>
<li><a href="/pages/android.html">Android</a></li>
<li><a href="/pages/fltk.html">FLTK</a></li>
</ul>
<h2 id="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>
<h2 id="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>
<div class="sourceCode" id="cb1"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">' This is a comment</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="co">REM This is a comment</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>a = <span class="dv">1</span> <span class="co">' This is a comment</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>a = <span class="dv">1</span> : <span class="co">REM This is a comment</span></span></code></pre></div>
<h2 id="NumberAndStrings">Numbers and Strings</h2>
<h3 id="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>
<div class="sourceCode" id="cb2"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="dv">256</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="dv">0x100</span> <span class="co">' Hexadecimal form 1</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="dv">&h100</span> <span class="co">' Hexadecimal form 2</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="dv">0o400</span> <span class="co">' Octal form 1</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="dv">&o400</span> <span class="co">' Octal form 2</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="dv">0b111</span> <span class="co">' Binary form 1</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a><span class="dv">&b111</span> <span class="co">' Binary form 2</span></span></code></pre></div>
<h3 id="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>
<div class="sourceCode" id="cb3"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="dv">1.2</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="dv">.25</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="dv">1E2</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="dv">1.2E-2</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="dv">2.6E-0.25</span></span></code></pre></div>
<h3 id="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>
<div class="sourceCode" id="cb4"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="st">"This is a string"</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="st">"Hello, "</span> + <span class="st">"world!"</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="st">"""This is</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>a <span class="fu">string</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a>over several lines</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a>with <span class="fu">line</span> breaks<span class="st">"""</span></span></code></pre></div>
<p>If the closing quote character is not present then the end of line
marker is used to terminate the string definition, for example:</p>
<div class="sourceCode" id="cb5"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>a = <span class="st">"this is a string</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>b = <span class="st">" and this is another string</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="kw">PRINT</span> a;b</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>
<col style="width: 13%" />
<col style="width: 86%" />
</colgroup>
<thead>
<tr>
<th>Code</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>\a</code></td>
<td>beep</td>
</tr>
<tr>
<td><code>\t</code></td>
<td>tab (20 px)</td>
</tr>
<tr>
<td><code>\r</code></td>
<td>return</td>
</tr>
<tr>
<td><code>\n</code></td>
<td>next line</td>
</tr>
<tr>
<td><code>\"</code></td>
<td>quote <code>"</code></td>
</tr>
<tr>
<td><code>\\</code></td>
<td>Backslash <code>\</code></td>
</tr>
<tr>
<td><code>\e[K</code></td>
<td>clear to end of line</td>
</tr>
<tr>
<td><code>\e[nG</code></td>
<td>move to column <code>n</code></td>
</tr>
<tr>
<td><code>\e[s</code></td>
<td>save cursor position</td>
</tr>
<tr>
<td><code>\e[u</code></td>
<td>restore cursor position</td>
</tr>
<tr>
<td><code>\e[0m</code></td>
<td>reset all attributes to their defaults</td>
</tr>
<tr>
<td><code>\e[1m</code></td>
<td>set bold on</td>
</tr>
<tr>
<td><code>\e[3m</code></td>
<td>set italic on</td>
</tr>
<tr>
<td><code>\e[4m</code></td>
<td>set underline on</td>
</tr>
<tr>
<td><code>\e[7m</code></td>
<td>reverse video</td>
</tr>
<tr>
<td><code>\e[21m</code></td>
<td>set bold off</td>
</tr>
<tr>
<td><code>\e[23m</code></td>
<td>set italic off</td>
</tr>
<tr>
<td><code>\e[24m</code></td>
<td>set underline off</td>
</tr>
<tr>
<td><code>\e[27m</code></td>
<td>set reverse off</td>
</tr>
<tr>
<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>
<div class="sourceCode" id="cb6"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> <span class="st">"String with line\nbreak"</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> <span class="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>
<h2 id="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>
<h3 id="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>
<h3 id="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>
<div class="sourceCode" id="cb8"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>a = <span class="dv">256</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>b = <span class="dv">0x100</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>c = <span class="dv">0o400</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a>d = <span class="dv">0b111</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a>e = <span class="st">"This is a string"</span></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>g = <span class="st">"""This is</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a>a <span class="fu">string</span></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a>over several lines</span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a>with <span class="fu">line</span> breaks<span class="st">"""</span></span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a>h = <span class="dv">1</span> + <span class="dv">1</span></span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a>i = h</span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a>j = <span class="dv">1</span> + h</span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a>k = <span class="st">"Hello, "</span> + <span class="st">"world"</span></span>
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a>l = k + <span class="st">"!"</span></span>
<span id="cb8-18"><a href="#cb8-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-19"><a href="#cb8-19" aria-hidden="true" tabindex="-1"></a>m = j > h</span>
<span id="cb8-20"><a href="#cb8-20" aria-hidden="true" tabindex="-1"></a>n = h == j</span>
<span id="cb8-21"><a href="#cb8-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-22"><a href="#cb8-22" aria-hidden="true" tabindex="-1"></a>o = <span class="fu">sin</span>(<span class="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>
<div class="sourceCode" id="cb9"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="dt">let</span> x = <span class="dv">20</span></span></code></pre></div>
<h3 id="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>
<div class="sourceCode" id="cb10"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="dt">CONST</span> my_pi = <span class="dv">3.14</span></span></code></pre></div>
<p>SmallBASIC predefines the following constants:</p>
<table>
<thead>
<tr>
<th style="text-align: center;">Constant</th>
<th style="text-align: center;">Value</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;">PI</td>
<td style="text-align: center;">3.14..</td>
</tr>
<tr>
<td style="text-align: center;">TRUE</td>
<td style="text-align: center;">1</td>
</tr>
<tr>
<td style="text-align: center;">FALSE</td>
<td style="text-align: center;">0</td>
</tr>
<tr>
<td style="text-align: center;">NIL</td>
<td style="text-align: center;">nil (not set)</td>
</tr>
</tbody>
</table>
<h2 id="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>
<h3 id="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>
<div class="sourceCode" id="cb11"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>]</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>A = [,<span class="dv">2</span>,,<span class="dv">4</span>] <span class="co">' Equal to A = [0,2,0,4]</span></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">1</span>,<span class="st">"A"</span>, <span class="dv">2.2</span>] <span class="co">' Array with elements of different data-type</span></span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">1</span>,<span class="st">"A"</span>, [<span class="dv">2</span>,<span class="dv">3</span>]] <span class="co">' Creates an nested array</span></span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a>S = [<span class="dv">1</span>, <span class="st">"Small"</span>, <span class="st">"BASIC"</span>] <span class="co">' Numbers and strings</span></span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a>M = [{x: <span class="dv">1</span>, y: <span class="dv">2</span>}, {x: <span class="dv">4</span>, y: <span class="dv">7</span>}] <span class="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>
<div class="sourceCode" id="cb12"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> A(<span class="dv">3</span>) <span class="co">' Array A with 4 elements</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> A[<span class="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>
<div class="sourceCode" id="cb13"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co">' One dimensional array of 7 elements, starting from 3</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="co">' with elements A(3), A(4), ... , A(9)</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> A(<span class="dv">3</span> <span class="kw">TO</span> <span class="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>
<div class="sourceCode" id="cb14"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>A = []</span></code></pre></div>
<h3 id="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>
<div class="sourceCode" id="cb15"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>]</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A(<span class="dv">1</span>) <span class="co">' Output: 2</span></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a>A[<span class="dv">1</span>] = <span class="dv">5</span> <span class="co">' A = [1,5,3,4]</span></span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a>A(<span class="dv">0</span>) = A(<span class="dv">3</span>) <span class="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>
<h3 id="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>
<div class="sourceCode" id="cb16"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>A = [[<span class="dv">1</span>,<span class="dv">2</span>] , [<span class="dv">3</span>,<span class="dv">4</span>]]</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>A = [[<span class="dv">1</span>,<span class="dv">2</span>] , [<span class="dv">3</span>, [<span class="dv">4</span>, <span class="dv">5</span>]]]</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a>B = [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]</span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a>C = [<span class="dv">4</span>, <span class="dv">5</span>]</span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a>B(<span class="dv">2</span>) = C <span class="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>
<div class="sourceCode" id="cb17"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">1</span>, <span class="dv">2</span>, [<span class="dv">4</span>, <span class="dv">5</span>]]</span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a><span class="kw">PRINT</span> A[<span class="dv">2</span>][<span class="dv">0</span>] <span class="co">' Output: 4</span></span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a><span class="kw">PRINT</span> A(<span class="dv">2</span>)(<span class="dv">1</span>) <span class="co">' Output: 5</span></span></code></pre></div>
<h3 id="PackedUnpackedAssignment">Packed/Unpacked Assignment</h3>
<p>An array can be unpacked using the unpack assignment:</p>
<pre><code>(var1,...,varN) = array[element1,...,elementN]</code></pre>
<p>The number of variables must match the number of elements.</p>
<div class="sourceCode" id="cb19"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>v = [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>]</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a>(x,y,z) = v</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="kw">PRINT</span> x, y, z</span></code></pre></div>
<h3 id="ArrayOperations">Array Operations</h3>
<p>SmallBASIC supports basic array operations using the standard
operators:</p>
<p><strong>Element-wise Addition and Subtraction</strong></p>
<div class="sourceCode" id="cb20"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>]</span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>B = [<span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>]</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a>C = B + A</span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a>D = A - B</span></code></pre></div>
<p><strong>Element-wise Multiplication</strong></p>
<div class="sourceCode" id="cb21"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>]</span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>B = [<span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>]</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a>C = A * B</span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a>D = <span class="dv">0.8</span> * A</span></code></pre></div>
<p><strong>Equality</strong></p>
<div class="sourceCode" id="cb22"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>]</span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a>B = [<span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>]</span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a>e = A==B</span></code></pre></div>
<p><strong>Unary</strong></p>
<div class="sourceCode" id="cb23"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">5</span>, <span class="dv">6</span>; <span class="dv">7</span>, <span class="dv">8</span>]</span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-3"><a href="#cb23-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>
<div class="sourceCode" id="cb24"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>A << <span class="dv">1</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>A << <span class="dv">2</span></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a>A << <span class="dv">3</span></span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a><span class="kw">PRINT</span> A(<span class="dv">1</span>) <span class="co">' Output: 2</span></span></code></pre></div>
<p><strong>IN Operator</strong></p>
<p>When applied to arrays the <code>IN</code> operator returns the 1
based index position of the element, for example:</p>
<div class="sourceCode" id="cb25"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"cat"</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a>a << <span class="st">"dog"</span></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="kw">PRINT</span> <span class="st">"dog"</span> <span class="kw">IN</span> a <span class="co">' Output: 2</span></span></code></pre></div>
<h2 id="Matrices">Matrices</h2>
<h3 id="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>
<div class="sourceCode" id="cb26"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>M = [<span class="dv">11</span>, <span class="dv">12</span>; <span class="dv">21</span>, <span class="dv">22</span>; <span class="dv">31</span>, <span class="dv">32</span>]</span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a><span class="co">' Creates the 3x2 matrix</span></span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a><span class="co">' 11 12</span></span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a><span class="co">' 21 22</span></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a><span class="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>
<div class="sourceCode" id="cb28"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> M(<span class="dv">3</span>,<span class="dv">2</span>)</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a><span class="co">' Creates the 3x4 matrix</span></span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a><span class="co">' 0 0 0</span></span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a><span class="co">' 0 0 0</span></span>
<span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></a><span class="co">' 0 0 0</span></span>
<span id="cb28-7"><a href="#cb28-7" aria-hidden="true" tabindex="-1"></a><span class="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>
<div class="sourceCode" id="cb29"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> M(<span class="dv">2</span> <span class="kw">to</span> <span class="dv">4</span>, <span class="dv">3</span> <span class="kw">to</span> <span class="dv">7</span>)</span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a><span class="co">' Creates the 5x3 matrix</span></span>
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a><span class="co">' 0 0 0 0 0</span></span>
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a><span class="co">' 0 0 0 0 0</span></span>
<span id="cb29-6"><a href="#cb29-6" aria-hidden="true" tabindex="-1"></a><span class="co">' 0 0 0 0 0</span></span>
<span id="cb29-7"><a href="#cb29-7" aria-hidden="true" tabindex="-1"></a><span class="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>
<h3 id="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>
<div class="sourceCode" id="cb30"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>M = [<span class="dv">1</span>,<span class="dv">2</span>;<span class="dv">3</span>,<span class="dv">4</span>]</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> M(<span class="dv">0</span>,<span class="dv">0</span>) <span class="co">' Output: 1</span></span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a>M[<span class="dv">1</span>,<span class="dv">0</span>] = <span class="dv">5</span> <span class="co">' M = [1,2;5,4]</span></span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a>M(<span class="dv">0</span>,<span class="dv">1</span>) = M(<span class="dv">0</span>,<span class="dv">0</span>) <span class="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>
<h3 id="MatrixOperations">2D Matrix Operations</h3>
<p>SmallBASIC supports basic matrix operations using the standard
operators:</p>
<p><strong>Addition and Subtraction</strong></p>
<div class="sourceCode" id="cb31"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">1</span>, <span class="dv">2</span>; <span class="dv">3</span>, <span class="dv">4</span>]</span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a>B = [<span class="dv">5</span>, <span class="dv">6</span>; <span class="dv">7</span>, <span class="dv">8</span>]</span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a>C = B + A</span>
<span id="cb31-5"><a href="#cb31-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>
<div class="sourceCode" id="cb33"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">1</span>, <span class="dv">2</span>; <span class="dv">3</span>, <span class="dv">4</span>]</span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a>B = [<span class="dv">5</span>, <span class="dv">6</span>; <span class="dv">7</span>, <span class="dv">8</span>]</span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a>e = A==B</span></code></pre></div>
<p><strong>Unary</strong></p>
<div class="sourceCode" id="cb34"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>A = [<span class="dv">5</span>, <span class="dv">6</span>; <span class="dv">7</span>, <span class="dv">8</span>]</span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a>B = -A</span></code></pre></div>
<h3 id="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>
<div class="sourceCode" id="cb35"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> M(<span class="dv">2</span>,<span class="dv">2</span>,<span class="dv">3</span>) <span class="co">' Creates a 3x3x4 3D Matrix</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a>M[<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">2</span>] = <span class="dv">5</span></span></code></pre></div>
<p>Equality is the only valid matrix operating for higher dimensional
matrices.</p>
<h3 id="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>
<div class="sourceCode" id="cb36"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a>v = [<span class="dv">1</span>;<span class="dv">2</span>;<span class="dv">3</span>]</span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a><span class="co">' Creates the column vector</span></span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a><span class="co">' 1</span></span>
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a><span class="co">' 2</span></span>
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a><span class="co">' 3</span></span>
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a><span class="kw">PRINT</span> v(<span class="dv">1</span>,<span class="dv">0</span>) <span class="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>
<div class="sourceCode" id="cb37"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> A(<span class="dv">2</span>)</span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a><span class="co">' Creates a row vector</span></span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a><span class="co">' 0 0 0</span></span>
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a><span class="dt">DIM</span> B(<span class="dv">2</span>, <span class="dv">0</span>)</span>
<span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a><span class="co">' Creates a column vector</span></span>
<span id="cb37-7"><a href="#cb37-7" aria-hidden="true" tabindex="-1"></a><span class="co">' 0</span></span>
<span id="cb37-8"><a href="#cb37-8" aria-hidden="true" tabindex="-1"></a><span class="co">' 0</span></span>
<span id="cb37-9"><a href="#cb37-9" aria-hidden="true" tabindex="-1"></a><span class="co">' 0</span></span>
<span id="cb37-10"><a href="#cb37-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb37-11"><a href="#cb37-11" aria-hidden="true" tabindex="-1"></a><span class="kw">PRINT</span> B(<span class="dv">1</span>,<span class="dv">0</span>) <span class="co">' Prints the value of the element in row 2</span></span></code></pre></div>
<h2 id="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>
<h3 id="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>
<div class="sourceCode" id="cb38"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a>A = {x:<span class="dv">1</span>, y:<span class="dv">2</span>, z:<span class="dv">3</span>}</span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a>B = {<span class="st">"x"</span>:<span class="dv">1</span>, <span class="st">"y"</span>:<span class="dv">2</span>, <span class="st">"z"</span>:<span class="dv">3</span>}</span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a>C = {<span class="dv">1</span>:<span class="dv">10</span>, <span class="dv">2</span>:<span class="dv">5</span>, Name: <span class="st">"SmallBASIC"</span>}</span>
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a>D = {ArrayData:[<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>], MapData:{node:<span class="dv">1</span>, value:<span class="dv">5</span>}}</span>
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a>E.x = <span class="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>
<div class="sourceCode" id="cb39"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a>M = <span class="kw">ARRAY</span>(<span class="st">"{x:1, y:2, z:3}"</span>)</span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a>s = <span class="st">"{1:10, 2:5, Name:\"</span>SmallBASIC\<span class="st">"}"</span> <span class="co">' The quotation marks of the string SmallBASIC are escaped with \"</span></span>
<span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></a>M = <span class="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>
<div class="sourceCode" id="cb40"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>A = {}</span></code></pre></div>
<h3 id="AccessingElementsOfAMapVariable">Accessing Elements of a Map
Variable</h3>
<p>An element of a map variable can be accessed in the following three
ways:</p>
<ol type="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>
<div class="sourceCode" id="cb41"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>A = {x:<span class="dv">1</span>, y:<span class="dv">2</span>, z:<span class="dv">3</span>}</span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A.x</span>
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A(<span class="st">"x"</span>)</span>
<span id="cb41-5"><a href="#cb41-5" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A[<span class="st">"x"</span>]</span>
<span id="cb41-6"><a href="#cb41-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb41-7"><a href="#cb41-7" aria-hidden="true" tabindex="-1"></a>A.y = <span class="dv">3</span></span>
<span id="cb41-8"><a href="#cb41-8" aria-hidden="true" tabindex="-1"></a>A(<span class="st">"y"</span>) = <span class="dv">3</span></span>
<span id="cb41-9"><a href="#cb41-9" aria-hidden="true" tabindex="-1"></a>A[<span class="st">"y"</span>] = <span class="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>
<div class="sourceCode" id="cb42"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>A = {x:[<span class="dv">2</span>,<span class="dv">3</span>], y:[<span class="dv">1</span>,<span class="dv">2</span>;<span class="dv">3</span>,<span class="dv">4</span>]}</span>
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A.x(<span class="dv">0</span>) <span class="co">' Output: 2</span></span>
<span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A.x[<span class="dv">1</span>] <span class="co">' Output: 3</span></span>
<span id="cb42-5"><a href="#cb42-5" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A.y[<span class="dv">1</span>,<span class="dv">1</span>] <span class="co">' Output: 4</span></span>
<span id="cb42-6"><a href="#cb42-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-7"><a href="#cb42-7" aria-hidden="true" tabindex="-1"></a>A.x(<span class="dv">0</span>) = <span class="dv">5</span></span>
<span id="cb42-8"><a href="#cb42-8" aria-hidden="true" tabindex="-1"></a>A.x[<span class="dv">1</span>] = <span class="dv">6</span></span>
<span id="cb42-9"><a href="#cb42-9" aria-hidden="true" tabindex="-1"></a>A.y[<span class="dv">1</span>,<span class="dv">1</span>] = <span class="dv">7</span></span>
<span id="cb42-10"><a href="#cb42-10" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A <span class="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>
<div class="sourceCode" id="cb43"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>A = {x:{a:<span class="dv">1</span>, b:{c:<span class="dv">2</span>}}}</span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A.x.a <span class="co">' Output: 1</span></span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A.x.b.c <span class="co">' Output: 2</span></span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A(<span class="st">"x"</span>)(<span class="st">"a"</span>) <span class="co">' Output: 1</span></span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A(<span class="st">"x"</span>)(<span class="st">"b"</span>)(<span class="st">"c"</span>) <span class="co">' Output: 2</span></span>
<span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A(<span class="st">"x"</span>)(<span class="st">"b"</span>).c <span class="co">' Output: 2</span></span>
<span id="cb43-8"><a href="#cb43-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-9"><a href="#cb43-9" aria-hidden="true" tabindex="-1"></a>A.x.a = <span class="dv">3</span></span>
<span id="cb43-10"><a href="#cb43-10" aria-hidden="true" tabindex="-1"></a>A.x.b.c = <span class="dv">4</span></span>
<span id="cb43-11"><a href="#cb43-11" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A <span class="co">' Output: {"x":{"a":3,"b":{"c":4</span><span class="re">}}}</span></span>
<span id="cb43-12"><a href="#cb43-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-13"><a href="#cb43-13" aria-hidden="true" tabindex="-1"></a>A(<span class="st">"x"</span>)(<span class="st">"a"</span>) = <span class="dv">5</span></span>
<span id="cb43-14"><a href="#cb43-14" aria-hidden="true" tabindex="-1"></a>A(<span class="st">"x"</span>)(<span class="st">"b"</span>)(<span class="st">"c"</span>) = <span class="dv">6</span></span>
<span id="cb43-15"><a href="#cb43-15" aria-hidden="true" tabindex="-1"></a>A(<span class="st">"x"</span>)(<span class="st">"b"</span>).c = <span class="dv">7</span></span>
<span id="cb43-16"><a href="#cb43-16" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A <span class="co">' Output: {"x":{"a":5,"b":{"c":7</span><span class="re">}}}</span><span class="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>
<h3 id="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>
<div class="sourceCode" id="cb44"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a>A = {a:<span class="dv">1</span>, b:<span class="dv">2</span>}</span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a>A.c = <span class="dv">3</span></span>
<span id="cb44-4"><a href="#cb44-4" aria-hidden="true" tabindex="-1"></a>A(<span class="st">"d"</span>) = <span class="dv">4</span></span>
<span id="cb44-5"><a href="#cb44-5" aria-hidden="true" tabindex="-1"></a>A.e = {x:<span class="dv">5</span>, y:<span class="dv">6</span>}</span></code></pre></div>
<h3 id="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>
<div class="sourceCode" id="cb45"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>A = {}</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a>A.x = <span class="dv">1</span></span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a>A.Add = @Add</span>
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a>A.PrintBold = @PrintBold</span>
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-6"><a href="#cb45-6" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A <span class="co">' Output: {"x":1,"Add":func,"PrintBold":func}</span></span>
<span id="cb45-7"><a href="#cb45-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-8"><a href="#cb45-8" aria-hidden="true" tabindex="-1"></a><span class="co">' Execute function and subroutine</span></span>
<span id="cb45-9"><a href="#cb45-9" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A.Add(<span class="dv">1</span>, <span class="dv">2</span>)</span>
<span id="cb45-10"><a href="#cb45-10" aria-hidden="true" tabindex="-1"></a>A.PrintBold(<span class="st">"Test"</span>)</span>
<span id="cb45-11"><a href="#cb45-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-12"><a href="#cb45-12" aria-hidden="true" tabindex="-1"></a><span class="co">' Define function and subroutine</span></span>
<span id="cb45-13"><a href="#cb45-13" aria-hidden="true" tabindex="-1"></a><span class="kw">func </span>Add(a,b)</span>
<span id="cb45-14"><a href="#cb45-14" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> a + b</span>
<span id="cb45-15"><a href="#cb45-15" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb45-16"><a href="#cb45-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-17"><a href="#cb45-17" aria-hidden="true" tabindex="-1"></a><span class="kw">sub </span>PrintBold(s)</span>
<span id="cb45-18"><a href="#cb45-18" aria-hidden="true" tabindex="-1"></a> <span class="kw">print</span> <span class="st">"\e[1m"</span>; s; <span class="st">"\e[21m"</span></span>
<span id="cb45-19"><a href="#cb45-19" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code></pre></div>
<h3 id="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>
<div class="sourceCode" id="cb46"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a>A = MyAddClass(<span class="dv">3</span>,<span class="dv">4</span>)</span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> A.Add()</span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a><span class="co">' Definitions</span></span>
<span id="cb46-5"><a href="#cb46-5" aria-hidden="true" tabindex="-1"></a><span class="kw">func </span>MyAddClass(x,y)</span>
<span id="cb46-6"><a href="#cb46-6" aria-hidden="true" tabindex="-1"></a> <span class="dt">local</span> TempMap</span>
<span id="cb46-7"><a href="#cb46-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb46-8"><a href="#cb46-8" aria-hidden="true" tabindex="-1"></a> <span class="co">' Properties</span></span>
<span id="cb46-9"><a href="#cb46-9" aria-hidden="true" tabindex="-1"></a> TempMap.x = x</span>
<span id="cb46-10"><a href="#cb46-10" aria-hidden="true" tabindex="-1"></a> TempMap.y = y</span>
<span id="cb46-11"><a href="#cb46-11" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb46-12"><a href="#cb46-12" aria-hidden="true" tabindex="-1"></a> <span class="co">' Methods</span></span>
<span id="cb46-13"><a href="#cb46-13" aria-hidden="true" tabindex="-1"></a> TempMap.Add = @AddFunction</span>
<span id="cb46-14"><a href="#cb46-14" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb46-15"><a href="#cb46-15" aria-hidden="true" tabindex="-1"></a> <span class="co">' Functions and subroutines</span></span>
<span id="cb46-16"><a href="#cb46-16" aria-hidden="true" tabindex="-1"></a> <span class="kw">func </span>AddFunction()</span>
<span id="cb46-17"><a href="#cb46-17" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> <span class="dt">self</span>.x + <span class="dt">self</span>.y</span>
<span id="cb46-18"><a href="#cb46-18" aria-hidden="true" tabindex="-1"></a> <span class="kw">end</span></span>
<span id="cb46-19"><a href="#cb46-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb46-20"><a href="#cb46-20" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> TempMap</span>
<span id="cb46-21"><a href="#cb46-21" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code></pre></div>
<h2 id="Operators">Operators</h2>
<h3 id="SupportedOperators">Supported Operators</h3>
<p>SmallBASIC supports the following operators, sorted by priority:</p>
<table>
<colgroup>
<col style="width: 18%" />
<col style="width: 81%" />
</colgroup>
<thead>
<tr>
<th style="text-align: center;">Operator</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;"><code>(</code>, <code>)</code></td>
<td>Parenthesis</td>
</tr>
<tr>
<td style="text-align: center;"><code>+</code>, <code>-</code></td>
<td>Unary</td>
</tr>
<tr>
<td style="text-align: center;"><code>~</code></td>
<td>bitwise NOT</td>
</tr>
<tr>
<td style="text-align: center;"><code>NOT</code> or <code>!</code></td>
<td>Logical NOT (NOT false = true)</td>
</tr>
<tr>
<td style="text-align: center;"><code>^</code></td>
<td>Exponentiation</td>
</tr>
<tr>
<td style="text-align: center;"><code>*</code>, <code>/</code>,
<code>\</code></td>
<td>Multiplication, Division, Integer Division</td>
</tr>
<tr>
<td style="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>
<tr>
<td style="text-align: center;"><code>MDL</code></td>
<td>Modulus (<code>a % b + b*(sgn(a) <> sgn(b))</code>)</td>
</tr>
<tr>
<td style="text-align: center;"><code>+</code>, <code>-</code></td>
<td>Addition/Concatenation, Subtraction</td>
</tr>
<tr>
<td style="text-align: center;"><code>=</code> or <code>==</code></td>
<td>Equal</td>
</tr>
<tr>
<td style="text-align: center;"><code><></code> or
<code>!=</code></td>
<td>Not Equal</td>
</tr>
<tr>
<td style="text-align: center;"><code>></code>,
<code><</code></td>
<td>Less Than, Greater Than</td>