Language
BASIC
- First appeared
- 1964
- Paradigm
- Imperative, unstructured
- Toolchain
- Bywater BASIC 2.20
- Container image
debian:trixie-20261027-slim + bwbasic 2.20pl2-14
BASIC (Beginner’s All-purpose Symbolic Instruction Code) was created at Dartmouth College in 1964 by John Kemeny and Thomas Kurtz, so that students who weren’t scientists could write programs. A version of it came built into most home computers of the late 1970s and 1980s, and for many people it was their first language. Here it runs in Bywater BASIC, an interpreter for the classic line-numbered style.
Every line has a number, and the program runs in number order. GOTO
jumps to a line, and GOSUB jumps to a subroutine, a block of lines that
ends in RETURN. Subroutines take no arguments, and every variable is
global, so the programs here pass values by setting variables before each
GOSUB. A name ending in $, such as L$, holds text. There’s no sort,
no growable array, and no CSV reader, so the implementations write their
own.
Quirks
- Every array needs a
DIMwith a fixed size before it’s used.A(3) = 1on an undeclaredAstops the program with “Assignment must be to variable”. \isn’t integer division here:7 \ 2gives 3.5.INT(7 / 2)gives 3.- Numbers are stored in double precision, but
PRINTshows only about seven digits:SQR(2)prints as 1.4142136. So0.1 + 0.2prints 0.3 yet isn’t equal to 0.3.PRINT USING "#.######"shows six decimals. STR$puts a space before every number, even a negative one:STR$(2)is" 2". There’s noLTRIM$, so the programs drop the space withMID$(STR$(N), 2).- A
FORloop with nothing to count still runs once:FOR E = 1 TO 0runs its body withE= 1. Where a range can be empty, the programs useWHILEinstead. FORandNEXTcan’t share a line:FOR J = 1 TO 3 : NEXT Jstops with “FOR without NEXT”.- bwbasic can’t read command-line arguments, so the run script passes the
data path in the
DATAenvironment variable, read withENVIRON$.OPEN "I", #1, ENVIRON$("DATA")fails to parse, so the programs copy it intoF$first. - An
IFinside anotherIF’sTHENignores its own condition:IF A = 1 THEN IF B = 0 THEN PRINT "yes" ELSE PRINT "no"prints “yes” whenBis 5. The programs join conditions withANDinstead. - An array element used inside a 2-D subscript can read the wrong cell:
with
FEAT(3) = 2,X(1, FEAT(3))gaveX(3, 2). CopyingFEAT(3)into a plain variable first gives the right one. - A misspelled statement isn’t an error. bwbasic stops at the line and
hands it and every line after it to the shell, so
PRNT "b"prints “PRNT: not found”. - An error prints a message and drops to the
bwBASIC:prompt, yet the interpreter still exits with status 0. The run script watches the output forERRORor the prompt and fails the run instead.
Concepts in BASIC
- Cross-validation
- Decision tree
- Descriptive statistics
- k-means clustering
- k-nearest neighbors
- Linear regression
- Logistic regression
- Naive Bayes
- Principal component analysis
Run the examples locally
You need Docker and Python 3. The harness builds the pinned container and runs every BASIC example:
git clone https://github.com/qnixon/dswithx.git
cd dswithx
python3 harness/run_tests.py --language basic