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
<p>Here you learn about the basics of BASIC, so to speak. This chapter deals with the way a SmallBASIC program composed.</p>
72
-
<p>A program consists of a single source file. It is possible to include libraries from external source files with the Unit-mechanism (see ).</p>
72
+
<p>A program consists of a single source file. It is possible to include libraries from external source files with the Unit-mechanism.</p>
73
73
<p>Source files are simple text files. They can be written in <strong>ASCII</strong> or <strong>UTF-8</strong>.</p>
74
-
<p>The basic program component is a line of text, ending with or characters.<ahref="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a></p>
75
-
<p>SmallBASIC is <strong>case-insensitive</strong>: The names and will always refer to the same variable or function. Likewise, keywords are case-insensitive: Both and are legal variants of the same command.</p>
74
+
<p>The basic program component is a line of text, ending with <sub>CR</sub> or <sub>LF/CR</sub> characters.<ahref="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a></p>
75
+
<p>SmallBASIC is <strong>case-insensitive</strong>: The names <code>myvar</code>and <code>MyVar</code>will always refer to the same variable or function. Likewise, keywords are case-insensitive: Both <code>print</code>and<code>PRINT</code> are legal variants of the same command.</p>
76
76
<p><strong>Whitespace</strong> – i.e., non-printing characters like spaces and tabs – is ignored in SmallBASIC, except inside string literals where it is retained. <ahref="#fn2" class="footnote-ref" id="fnref2"><sup>2</sup></a></p>
77
77
<pre><code>for a = 0 to 10
78
78
79
79
for a=0to 10</code></pre>
80
80
<p>But note that the omission of whitespace can lead to parsing errors: If the above line were abbreviated to</p>
81
81
<pre><code>fora=0to10</code></pre>
82
-
<p>this would cause an error, because and wouldn’t be recognized as keywords anymore. Rather, SmallBASIC would consider and to be variable names.<ahref="#fn3" class="footnote-ref" id="fnref3"><sup>3</sup></a></p>
83
-
<p>Each program line contains one or more commands. Multiple commands on a line are <strong>separated by a colon</strong>.</p>
82
+
<p>this would cause an error, because <code>for</code>and <code>to</code>wouldn’t be recognized as keywords anymore. Rather, SmallBASIC would consider <code>fora</code>and<code>to10</code> to be variable names.<ahref="#fn3" class="footnote-ref" id="fnref3"><sup>3</sup></a></p>
83
+
<p>Each program line contains one or more commands. Multiple commands on a line are <strong>separated by a colon</strong><sub>:</sub>.</p>
<p><strong>Line continuation</strong>: If the ampersand is the last character on a line, then the interpreter will assume that the current command extends to the next line as well.</p>
88
+
<p><strong>Line continuation</strong>: If the ampersand <sub>&</sub>is the last character on a line, then the interpreter will assume that the current command extends to the next line as well.</p>
89
89
<pre><code>X = 245 * 198 - sqr(5)
90
90
91
91
X = 245 * 198 &
92
92
- sqr(5)</code></pre>
93
93
<h1id="literals">Literals</h1>
94
94
<h2id="numbers">Numbers</h2>
95
-
<p>Numbers can be written in the usual manner, using either »conventional« or scientific notation. All of the following examples are legal numbers in:</p>
95
+
<p>Numbers can be written in the usual manner, using either `conventional~ or scientific notation. All of the following examples are legal numbers in:</p>
<p>String literals are character sequences which are to be treated as program data »as is«, not as variable or keyword names. String literals are bracketed by double quotes.</p>
105
+
<p>String literals are character sequences which are to be treated as program data ~as is~, not as variable or keyword names. String literals are bracketed by double quotes<sub>"</sub>.</p>
106
106
<pre><code>"This is a string literal"
107
107
this will be considered as a sequence of keywords</code></pre>
108
-
<p>Note that when a string literal is to be extended across more than one line, it must be properly closed before the continuation ampersand and re-opened on the subsequent line with the delimiter character:</p>
108
+
<p>Note that when a string literal is to be extended across more than one line, it must be properly closed before the continuation ampersand and re-opened on the subsequent line with the <sub>"</sub>delimiter character:</p>
109
109
<pre><code>print "Hello &
110
110
world!" ' error
111
111
112
112
print "Hello " &
113
113
"world!" ' correct</code></pre>
114
114
<h1id="identifiers">Identifiers</h1>
115
-
<p>Identifiers – »names« for variables and functions – follow the usual conventions:</p>
116
-
<p>They consist of a letter or an underscore , followed by one or more of the following:</p>
115
+
<p>Identifiers – <sub>names</sub> for variables and functions – follow the usual conventions:</p>
116
+
<p>They consist of a letter or an underscore <sub>_</sub>, followed by one or more of the following:</p>
117
117
<ul>
118
118
<li><p>other letters</p></li>
119
-
<li><p>digits ( – )</p></li>
120
-
<li><p>the underscore</p></li>
121
-
<li><p>the dollar sign (only as the last character of the identifier, see below)</p></li>
<li><p>the dollar sign <sub>$</sub>(only as the last character of the identifier, see below)</p></li>
122
122
</ul>
123
-
<p>A single underscore is a legal complete identifier.</p>
123
+
<p>A single underscore <sub>_</sub>is a legal complete identifier.</p>
124
124
<p>Identifiers can have virtually unlimited length. All characters are significant in resolving an identifier (ie, to determine whether two identifiers refer to the same variable.)<ahref="#fn4" class="footnote-ref" id="fnref4"><sup>4</sup></a></p>
125
-
<p>Traditionally, in BASIC the <strong>dollar sign</strong> serves as a signal to indicate that a name identifies a string variable, if used as the last character of the identifier (i.e., <code>my_name$</code>).</p>
126
-
<p>Since SmallBASIC is a typeless language (see below) where variables can hold values of any type, such a signal would be misleading, yet it has been retained for the sake of compatibility. It may be placed as the last character of an identifier only. Here it serves two distinguish between identifiers ( and are two different identifiers), but has otherwise no function.</p>
125
+
<p>Traditionally, in BASIC the <strong>dollar sign</strong><code>$</code>serves as a signal to indicate that a name identifies a string variable, if used as the last character of the identifier (i.e., <code>my_name$</code>).</p>
126
+
<p>Since SmallBASIC is a typeless language (see below) where variables can hold values of any type, such a signal would be misleading, yet it has been retained for the sake of compatibility. It may be placed as the last character of an identifier only. Here it serves two distinguish between identifiers (<code>harry</code> and<code>harry$</code> are two different identifiers), but has otherwise no function.</p>
127
127
<h1id="comments">Comments</h1>
128
128
<p><strong>Line comments</strong> can be introduced in three ways:</p>
129
129
<ul>
130
-
<li><p>With the keyword,</p></li>
131
-
<li><p>With the apostrophe character,</p></li>
132
-
<li><p>With a hash sign.</p></li>
130
+
<li><p>With the keyword<code>rem</code>,</p></li>
131
+
<li><p>With the apostrophe character<sub>’</sub>,</p></li>
132
+
<li><p>With a hash sign<sub>#</sub>.</p></li>
133
133
</ul>
134
134
<p>Everything on the current line following the comment introduction will be ignored in program execution.</p>
135
-
<p>If the keyword is used and it is preceded by other commands on the current line, it must be separated from the previous commands by a colon If the hash sign is used, it must be the first character on the line. (See also the use of a hash sign in »shebanging« a script, )</p>
135
+
<p>If the <code>rem</code>keyword is used and it is preceded by other commands on the current line, it must be separated from the previous commands by a colon <sub>:</sub>If the hash sign is used, it must be the first character on the line.</p>
136
136
<pre><code>for a=0 to 10 ' this is a valid comment
137
137
print a : rem this also
138
138
# this is a whole line commented out
139
139
next a
140
140
rem the last comment
141
141
142
142
print "Hello world!" rem vain commenting attempt</code></pre>
143
-
<p>The in the last line above will cause an error, because it needs to be preceded with a colon .</p>
143
+
<p>The <code>rem</code>in the last line above will cause an error, because it needs to be preceded with a colon <sub>:</sub>.</p>
144
144
<p>There are no provision for <strong>block comments</strong>.</p>
145
145
<sectionclass="footnotes">
146
146
<hr/>
147
147
<ol>
148
148
<liid="fn1"><p>Don’t worry about this, your operating system will handle it right. It may only every be an issue if you use source files written in one OS and then transferred to a different one.<ahref="#fnref1" class="footnote-back">↩</a></p></li>
149
-
<liid="fn2"><p>All listings in the vademecum follow the same convention and show the source code as you would have typed it in in the IDE. If a line in the listing begins with a greater-than sign , this indicates a response of the program printed on your screen.<ahref="#fnref2" class="footnote-back">↩</a></p></li>
149
+
<liid="fn2"><p>All listings in the vademecum follow the same convention and show the source code as you would have typed it in in the IDE. If a line in the listing begins with a greater-than sign <sub>></sub>, this indicates a response of the program printed on your screen.<ahref="#fnref2" class="footnote-back">↩</a></p></li>
150
150
<liid="fn3"><p>As a rule of thumb, it’s advisable to always leave spaces around keywords.<ahref="#fnref3" class="footnote-back">↩</a></p></li>
151
-
<liid="fn4"><p>This is in contrast to many older BASICs. The <em>Commodore BASIC</em> shipped with the honourable C64, for example, allowed identifiers of arbitrary length, but used only the first two letters for resolution: and were considered to refer to the same variable.<ahref="#fnref4" class="footnote-back">↩</a></p></li>
151
+
<liid="fn4"><p>This is in contrast to many older BASICs. The <em>Commodore BASIC</em> shipped with the honourable C64, for example, allowed identifiers of arbitrary length, but used only the first two letters for resolution: <code>hoogla</code>and<code>hooray</code> were considered to refer to the same variable.<ahref="#fnref4" class="footnote-back">↩</a></p></li>
0 commit comments