You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
<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-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
<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>
<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>
<divclass="sourceCode" id="cb5"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb5-1"><ahref="#cb5-1" aria-hidden="true" tabindex="-1"></a>a = <spanclass="st">"this is a string</span></span>
<spanid="cb5-2"><ahref="#cb5-2" aria-hidden="true" tabindex="-1"></a>b = <spanclass="st">" and this is another string</span></span>
<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>
<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>
<divclass="sourceCode" id="cb6"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb6-1"><ahref="#cb6-1" aria-hidden="true" tabindex="-1"></a><spanclass="kw">print</span><spanclass="st">"String with line\nbreak"</span></span>
<spanid="cb6-2"><ahref="#cb6-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
<spanid="cb11-2"><ahref="#cb11-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="cb11-3"><ahref="#cb11-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>
<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="cb13"><pre
class="sourceCode smallbasic"><codeclass="sourceCode smallbasic"><spanid="cb13-1"><ahref="#cb13-1" aria-hidden="true" tabindex="-1"></a><spanclass="co">' One dimensional array of 7 elements, starting from 3</span></span>
<spanid="cb13-2"><ahref="#cb13-2" aria-hidden="true" tabindex="-1"></a><spanclass="co">' with elements A(3), A(4), ... , A(9)</span></span>
<spanid="cb29-7"><ahref="#cb29-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>
<spanid="cb36-7"><ahref="#cb36-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
<spanid="cb37-11"><ahref="#cb37-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
<spanid="cb39-3"><ahref="#cb39-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>