basic/doc/
chapter_1.rs

1/*!
2# Expressions and Types
3
464K BASIC supports four types of data. This data is stored in a variable.
5Variables are simply names that refer to a data value. Variable names
6consist of ASCII alphabetic characters followed by optional ASCII numeric
7characters. No special characters, such as underbars (_), are valid.
8
9A value is assigned to a variable with the `LET` statement. `LET` has a
10shortcut which is that using the word `LET` is optional. If you are familiar
11with other languages, it may look like an assignment operation without the `LET`
12but there is technically no assignment operator in BASIC.
13
14```text
15LET PI = 3.14
16PI2 = 6.28
17```
18
19There are three numeric types and the string type. Decorating the variable
20with "!", "#", "%", and "$" will explicitly request a type.
21
22```text
23LET A! = 1.5 ' Single, 32-bit floating point
24LET A# = 1.5 ' Double, 64-bit floating point
25LET A% = 5   ' Integer, signed 16-bit
26LET A$ = "X" ' String of up to 255 characters
27LET A = 1.5  ' Single unless changed with `DEFINT/SNG/DBL/STR`
28```
29
30Literals are unchanging values included in your source code. For example, "1.5" is
31a literal. Literals numbers may be typed using the "!", "#", and "%" decorators.
32
33```text
34PRINT 3# + 0.14#
35```
36
37If you don't decorate a literal number, it will be assigned an appropriate type.
38
39 * If the number contains an exponent with the letter E, it is a Single.
40 * If the number contains an exponent with the letter D, it is a Double.
41 * If the number contains a decimal, it is a Single unless more than 7 digits.
42 * If the number has more than 7 digits, it is a Double.
43 * If the number fits into an Integer (-32767 to 32767), it is an Integer.
44 * Anything that doesn't match the above is a Single.
45
46You can't have -32768 as a literal Integer although you can store -32768
47as the result of an expression into an Integer variable. This is one of
48many quirks of BASIC that 64K BASIC preserves.
49
50Integers may also be specified in hexadecimal or octal with the "&" decorator.
51
52```text
53&10  ' Octal for 8
54&010 ' Octal for 8
55&H0D ' Hex for 13
56```
57
58Values are promoted as needed to preserve precision. For example, adding
59a Single to a Double causes the Single to be promoted and the addition
60done on two Doubles with the result being a Double.
61
62Values are automatically demoted only by assignment with `LET`. If the value
63won't fit into an integer variable then you get an `?OVERFLOW` error.
64Storing a Double value in a Single variable will result in a loss
65of precision.
66
67```text
68A% = 300*300         ' ?OVERFLOW
69A% = 300+300         ' 600
70A! = 1.2345678912345 ' 1.2345679
71```
72
73Strings may contain up to 255 characters. These are unicode characters, not graphemes
74or bytes. String literals are surrounded by quotation marks. There is no escape
75sequence so getting quotation marks in your string requires the use of a function.
76Source files are UTF-8 but only strings and comments may contain non-ASCII.
77
78```text
79A1$ = "Hello"
80A2$ = CHR$(34) + "HELLO" + CHR$(34)
81```
82
83Expressions are anything that evaluates to a variable. The number `1` is an expression;
84literals are a specific kind of expression. The variable `PI#` is also an expression.
85An expression may also perform arithmetic, compare values, and call functions.
86Here are some example of expressions.
87
88```text
89A + PI
902 / (A + B)
91CHR$(34)
92```
93
9464K BASIC supports the following operators, listed in order of precedence.
95
96| Precedence | Operators | Meaning |
97|-|-|-|
98| 13 | ^ | Raise to a power |
99| 12 | - + | Unary negation and unity |
100| 11 | * / | Multiplication and division |
101| 10 | \   | Integer division |
102| 9 | %   | Remainder (aka Modulo) |
103| 8 | + - | Addition and subtraction |
104| 7 | = <> < <= > >= | Relational |
105| 6 | NOT | Bitwise not, unary |
106| 5 | AND | Bitwise and |
107| 4 | OR | Bitwise or |
108| 3 | XOR | Bitwise exclusive or |
109| 2 | IMP | Bitwise imp |
110| 1 | EQV | Bitwise eqv |
111
112Integer division (`\`) is a quirk of BASIC. Regular division of two Integers
113will promote both Integers to Singles first. Integer division is performed
114only on Integers and may result in a `?DIVIDE BY ZERO` or `?OVERFLOW`
115error. Only regular division has this quirky promotion behavior to maintain
116compatibility with early versions of BASIC that don't have an Integer type.
117All Integer arithmetic is always checked for overflows and division by zero.
118
119```text
120PRINT 10000*3 ' 30000
121PRINT 10000*4 ' ?OVERFLOW
122PRINT 10/3     ' 3.3333333
123PRINT 10\3     ' 3
124PRINT 10/0     ' inf
125PRINT 10\0     ' ?DIVISION BY ZERO
126```
127
128Relational operators always return an Integer with a value of 0 for true
129or -1 (&xFFFF) for false. All relational operators evaluate at the same
130precedence. These are typically used with IF statements, but there are
131other uses if you take advantage of the 0 and -1 value guarantee.
132
133| Operator | Meaning |
134|-|-|
135| = | Equality |
136| <> | Inequality |
137| < | Less than |
138| <= | Less than or equal |
139| > | Greater than |
140| >= | Greater than or equal |
141
142```text
143IF 10 < 100 THEN PRINT "INDEED"
144((A<B)*1)+((A>B)*-1)
145```
146
147Logical operators perform bit-level arithmetic on Integers. Each of the 16 bits are
148computed with these truth tables.
149
150<pre><code><u>    X    NOT X </u>      <u> X  Y    X AND Y </u>     <u> X  Y    X OR Y </u>
151    1      0          1  1       1          1  1       1
152    0      1          1  0       0          1  0       1
153                      0  1       0          0  1       1
154                      0  0       0          0  0       0
155
156<u> X  Y    X XOR Y</u>     <u> X  Y    X IMP Y </u>     <u> X  Y    X EQV Y </u>
157 1  1       0         1  1       1          1  1       1
158 1  0       1         1  0       0          1  0       0
159 0  1       1         0  1       1          0  1       0
160 0  0       0         0  0       1          0  0       1
161</code></pre>
162
163Arrays are an extension of variables. You can make single dimension arrays
164(vectors) or multi dimensional arrays (matrices). Arrays are dimensioned
165before they are used. If you use an array before it is explicitly dimensioned,
166it will be automatically set to a dimension of 10. Dimensions must be positive
167Integers (0 to 32767). A dimension of 10 is actually 11 values (0 to 10)
168and a dimension of 0 is a single value numbered 0.
169
170```text
17110 DIM BOARD(10,10)
17220 LET BOARD(5,5) = 12
173```
174
175Arrays are sparse in 64K BASIC. This means you can dimension 32767 values but
176none use any memory unless they are set them to something other than default (0 or "").
177Some BASIC implementations would let you set whether arrays start numbering at
1780 or 1. Sparse arrays make this irrelevant so arrays always start at 0.
179
180The next two chapters of this manual is a reference for statements and functions.
18164K BASIC is a comprehensive implementation of early BASIC. Most programs only
182used a small subset, sometimes called minimal BASIC, so it's not necessary to
183read to the end of this book if all you want to do is port programs.
184
185Here's a program you can decipher with the reference to get a feel for BASIC.
186Use CHR$(79) if the emoji isn't supported by your terminal.
187
188```text
18910 INPUT ,"What is your name";NAME$
19020 IF VAL(LEFT$(TIME$,2)) < 5 OR VAL(LEFT$(TIME$,2)) > 10 GOTO 40
19130 PRINT "Good morning, " NAME$ ".":GOTO 50
19240 PRINT "Hello, " NAME$ "."
19350 INPUT "How many cookies would you like";COOKIES
19460 ON COOKIES GOTO 80,90,90
19570 PRINT "You can't have" COOKIES "cookies.":GOTO 50
19680 PRINT "Here is your cookie: " CHR$(127850):END
19790 PRINT "COOKIES: ";
198100 FOR I=1 TO COOKIES:PRINT CHR$(127850);:NEXT:PRINT
199```
200
201*/