Wednesday, June 10, 2015

BASIC: A Primer Before Porting

As part of this series on converting "101 BASIC Computer Games" to Javascript, I realized that there are more than a few younger folks out there who might never have seen classic BASIC. So here's a primer that will get you started as we dive into porting these games into Javascript. Mind you, this is no tutorial or deep-dive; just a skim over some basic structures and language features. Keep in mind, too, that everyone's flavor of BASIC in the microcomputer era had slight implementation differences that might make porting any one game just a bit problematic.

BASIC is an acronym for "Beginner's All-Purpose Symbolic Instruction Code," and had it's origins back in the late 60's as a "starting" computer language. It had a limited instruction set, limited control structures, and limited extensibility - but it worked.

Each line of a BASIC program consisted of a line number, followed by one or more statements. Multiple statements could be combined on a single line by separating them with colons, eg

10 PRINT "HELLO":PRINT "GOODBYE"
20 IF A=1 THEN PRINT "NO"

The BASIC interpreter executed statements in increasing line number order; it was convention to space the numbers 10 apart to allow for additional statements between lines as a program evolved.

The nickel tour


BASIC's variables, all of which were global, could be string, integer, and single/double precision floating point; but no notion of custom types or structures were available - only arrays, which could be multidimensional.  For control structures, BASIC offered FOR-NEXT, IF-THEN, WHILE-WEND, GOTO, and GOSUB. BASIC's output was the PRINT statement, followed by a string of text or a combination of text and variables. For data entry, there was the INPUT statement; execution would stop at the INPUT statement and await the user to enter data, which was assigned to the variable noted in the statement. Lastly, comments could be added to the text via the REM statement, or the single-quote (apostrophe):

5  REM THIS IS MY PROGRAM REMARK
10 DIM A$
15 DIM B(5,6) ' DECLARE A 5 by 6 array
20 INPUT "ENTER YOUR NAME: "; A$
30 PRINT "YOUR NAME IS ";A$

Variables were declared by the DIM statement, but not all dialects required variables to be defined. Some if not all dialects honored the "$" as a type declaration character for string variables. The DEFINT, DEFSTR, DEFDBL, and DEFSNG statement defined variables of integer, string, double, and single-precision types, respectively, without any type declaration characters:

10 DIM A$:DEFINT A
20 A$="HELLO":A=26

IF-THEN-ELSE


The IF-THEN-ELSE structure set up a simple test-and-branch mechanism; if the IF test was true, branch to the linenumber specified in the THEN statement; otherwise, control continued with the next highest-numbered line in the source. An ELSE clause could specify a linenumber as well.

10 IF A<>1 THEN 40
20 PRINT "A IS 1"
30 GOTO 50
40 PRINT "A IS NOT 1"
50 END

The "THEN" and "ELSE" portions of the IF could specify a statement rather than a line number, as shown in the PRINT statement above.

WHILE-WEND


This simple WHILE loop structure establishes a test condition at the top of a loop, and executes all subsequent statements thereafter until a WEND statement is found:

10 DIM A:A=1
20 WHILE A<=10
30 PRINT "A STILL LESS THAN 10"
40 A=A+1
50 WEND
60 END

FOR-NEXT


This simple, traditional control structure established a loop that would take a variable from a starting value to an ending value, bumping the variable by 1 unless a different value were provided via the optional STEP keyword. The scope of the loop extended from the base FOR statement to the NEXT keyword:

10 DIM A
20 FOR A = 2 to 50 STEP 2
30 PRINT "EVEN NUMBER: "; A
40 NEXT A

GOTO-GOSUB


GOTO and GOSUB are first cousins. GOTO linenumber transfers immediate program control to the line number specified in the statement. GOSUB linenumber does the same thing, with a difference; it remembers the point at which it was invoked, and will return to that point when a RETURN statement is encountered. This gave BASIC at least the illusion of subroutines. Each also had a "computed" counterpart, which would accept an integer value and branch to the line number corresponding to that value's ordinal position in a list of line numbers:

100 ON X GOTO 200,210,220,230

In this example, X=1 would branch to line 200; X=2 would branch to 210, and so on.

Function Definitions


Some implementations of BASIC allowed for function definitions via the DEF FNx statement. This example declares a function "C" that accepts a single argument, and performs a Farenheit-to-Celsius conversion. The function is called in the next line:

10 DEF FNC(X) = (x-32)*5/9

20 PRINT "32 DEGREES F = " ; FNC(32); " CELSIUS."

DATA statements


One unique feature of BASIC was the ability to store what amounted to raw data within the program text. DATA statements allowed for a comma-delimited set of arbitrary values as program code, which could then be loaded into program variables by READ statements:

10 DIM X,Y(6)
20 FOR X=1 to 6
30 READ Y(X)
40 NEXT X
..
1000 DATA 125,26,159,1000,2,3142


That's It!!


That's the quick-hitter tour of BASIC's most important features. We didn't go into all the math functions and operators, as they're all fairly obvious from other languages, and any special attention needed as the conversions go along will be duly noted. Be ready to go forth and BASIC :)

No comments:

Post a Comment