# MacBASIC

Designed by Donn Denmann, Marianne Hsiung, Larry Kenyon, Bryan Stearns 1982 None

Macintosh Basic, or MacBASIC, was both a comprehensive programming language and a fully interactive development environment designed by Apple Inc. for the original Macintosh computer. It was developed by original Macintosh team member Donn Denman, with help from fellow Apple programmers Marianne Hsiung, Larry Kenyon, and Bryan Stearns, as part of the original Macintosh development effort starting in late 1981.MacBASIC was released as beta software in 1985, and was adopted for use in places such as the Dartmouth College computer science department, for use in an introductory programming course. Although Apple retracted MacBASIC, pirated copies of the software and manual still floated around, but because MacBASIC was no longer supported by Apple and not designed to be 32-bit-clean, interest eventually died out. Benchmarks published in the April 1984 issue of BYTE magazine suggested that MacBASIC had better performance as compared to Microsoft BASIC. The language included modern looping control structures, user-defined functions, graphics, and access to the Macintosh Toolbox.

## Description

#### General

Macintosh BASIC is semicompiled. When you type in a new program line, the line is immediately passed to a part of the system called the B-code generator. This generator compiles the program line and updates the program data structures. The system checks the syntax of the line as the line is compiled and provides immediate feedback as to the line's general lexical correctness. Later,when the program is executed, the compiler makes another quick pass through the program (about 2 seconds for a 50K-byte program) to check the integrity of its control structures. Assuming there are no final compilation errors, program execution continues. The compact B-code is then interpreted, making for a very fast BASIC.

Macintosh BASIC lets you execute any number of programs simultaneously and develop one or more additional programs at the same time. Each program is granted a fixed amount of execution time in 1/60-second increments or any interval set by the programmer. When a program's time slice is up, the system moves to the next program for interpretation.

#### Types and variables

Variable names in Macintosh BASIC can be of any length, and all characters in the name are significant. The first character must be alphabetic; the rest can be nearly anything you can type from the keyboard, which includes the entire ASCII code set plus nonroman and other special characters. The only exceptions are arithmetic symbols and other delimiters (comma, semicolon, colon, and space).

Macintosh BASIC supports array variables for all eight data types and subtypes discussed later. Arrays can have any number of dimensions, and each dimension can have 32,767 elements. All arrays must be dimensioned before use. When you DIM an array, you can specify ranges for element numbers. Thus, you can say:

DIM YEAR%(1900 TO 1986)

to specify an 87-element integer (the % denotes integer) array whose first number is 1900 and whose last number is 1986. You can also stipulate ranges for separate dimensions, as in:

DIM NAME$(10 TO 75, 165 TO 300) There are three main data types in the language: strings, pictures, and numerics. • Strings are pretty standard; they are enclosed in quotes, either single or double, and their variable names end in the usual BASIC symbol, $.
• You create a picture data type by either creating a shape or a whole picture in a graphics application (like Mac Paint) and transferring it to BASIC through the Clipboard or by drawing a shape in BASIC using various graphics commands. You can then assign the shape to a picture variable; the variable name ends in the symbol @.
• The numeric data type is further divided into six subtypes: Booleans, two types of integers, and three types of reals. In addition to the five standard arithmetic operators (+, -, *, /, "), Macintosh BASIC includes DIV for integer division and MOD for modulo, defined as the arithmetic remainder of integer division. The relational operators ( <, =, >, < >, > < ) and logical operators (AND, OR, and NOT) are standard; the string concatenation symbol is &.

Macintosh BASIC has the usual range of arithmetic, trigonometric, and string functions, including DEF FN (user-defined functions) for both numbers and strings. Table 2 describes many of the functions that don't appear in most other BASICs or are unique to Macintosh BASIC in some major way. The term "interactive," appearing in several of the descriptions, refers to graphics objects (entire menus, buttons, dials) you can make appear on the screen and can manipulate with the mouse.

#### Control Structures

Most flow of control statements in Macintosh BASIC take the form of control structures. The language has a GOTO statement, but you never have to use it.

Besides the familiar FOR / NEXT structure and the DO / EXIT / LOOP structure, in which all statements between the keywords DO and LOOP are repeated infinitely (EXIT lets you escape the loop), this BASIC includes some variations on new structures proposed in the 1982 ANSI (American National Standards Institute) BASIC proposal.

A multiline IF / THEN / ELSE / ENDIF lets you execute as many statements as you want if a condition is either true or false.

IF DAY.TEMP > HOT THEN
CLOTHES$= SWIM.STUFF$
GOSUB BEACH:
ELSE CLOTHES$= WALK.STUFF$
GOSUB CITY:
ENDIF

An extremely flexible SELECT CASE / ENDCASE construct takes the place of the restrictive ON...GOSUB statement. It enables the program to transfer execution to sets of statements based on the value of some expression. There can be multiple statements for each case or range of cases.

SELECT CASE PARTY$CASE "REPUBLICAN" CALL REPUBLICAN.PRIMARY.BALLOT CASE "COMMUNIST" CALL MILITIA CASE "DEMOCRAT", "TORY", INDEPENDENT$
GOSUB FURTHER.CHOICE:
CALL GENERAL.PARTY.BALLOT
CASE LEFT$(PARTY$, 3) = "SEP"
CALL SEPARITIST.PRIMARY.BALLOT
CASE ELSE
CALL GEINERAL.PRIMARY.BALLOT
ENDSELECT

And using numeric values:

SELECT CASE AGE
CASE 2, 4, 5
GOSUB PRESCHOOL:
CASE 6 TO 12
GOSUB GRAMMAR.SCHOOL:
CASE 15, 18 TO 35, VAL (OLD$) CALL CHECK.DEMOORAPHICS CASE > 70 GOSUB MIGHT.BE.RETIRED: ENDSELECT The language has several interrupt control structures, all of which are based on a structure bounded by the keywords WHEN and ENDWHEN; these interrupts let you determine which code is executed if any one of a number of events occurs anytime during program execution. You can plan interrupts to occur whenever a key is pressed, when the mouse is moved or its button pushed, when an error occurs, or at other times. WHEN ERR CALL ERROR.CHECK GOSUB SECOND.CHANCE: ENDWHEN WHEN KBD PRINT "WHICH MENU DO YOU NEED?" GOSUB MENU.CHOICE: CALL MENUS ENDWHEN Subroutines are handled in the usual way (except that the language uses GOSUB labels); additionally there's a CALL statement that enables entire programs to act as subroutines. CALL lets you pass parameters back and forth with the summoned program; when the called program ends, control returns to the statement following CALL in the source program. #### Graphics Macintosh BASIC provides commands for both static and animated graphics on a bit-mapped 512 by 342 point screen. You can plot points with the PLOT command (controlling the size of the pixels with PENSIZE) or create shapes with the keywords RECT (for rectangle), ROUNDRECT (a rectangle with rounded corners), or OVAL. Shapes can be outlined (FRAME), filled in (PAINT) with a preestablished shade or pattern (SET PATTERN), complemented from the last appearance (INVERT), or erased altogether (ERASE). The various shapes can then be combined into a single picture and stored in a picture variable (RECORD PICTURE) and recalled (DRAW PICTURE). For animated sequences , you can ROTATE, SCALE, and ANIMATE a picture, moving it across the display. All graphics appear within the Output window, so your Listing and Debugging windows can coexist with a running graphics program. #### Disk File Structures There are three types of file organization: sequential (serial access for text data), stream (serial access for binary data), and relative (random access, usually for text data). Length of a record in a relative file must be set in advance, but it can be any length you choose; there is no limit (beyond memory) to the number of records in a file. The keyword RECTYPE determines how data is stored: display files are standard ASCII and can be shown in a window or be printed; internal files are binary and are for storage only. You can designate any file, no matter what type, to be an input file (a file which can only be read), an output file (to which data is sent but not retrieved), or OUTIN (accessible for both input and output). You can later change a file's access designation. You create a file, and later make it accessible for use, with the OPEN statement. OPEN sets the channel number (1 through 99) that links the file to the system and the name of the file associated with the channel: OPEN #3: NAME " Macmumble" The preceding statement opens a file named Macmumble and assigns it to channel #3. Since no further parameters are given, the file uses the default parameters, which makes it a sequential display file enabling OUTIN access. The following statement opens a relative file with a record length of 250 characters for the storage of ASCII text data. OPEN #54: NAME "Foobar", ACCESS OUTPUT, ORGANIZATION RELATIVE, RECSIZE 250 The OPEN statement is intelligent: after you've opened a file, you don't have to restate the organization, record size, or RECTYPE each time you use it. Assuming you've issued the CLOSE statement for the file "Foobar" described above, you can later access it again by just saying OPEN #54: NAME "Foobar". Up to 10 channels can be open at the same time. All devices (like the serial port, the printer, windows on the desktop) can be accessed in the same manner as files. Device names are specified in the same way as any filename except that the first character in the name is a period. ## Performance Five programs used for benchmark tests are presented here. Listing six is the Sieve benchtest program using Print Quick from the Program menu. 1) Empty GOSUBs print time$; ' at start'
for x=1 to 5000
gosub foo:
next x
print time$; ' when done' end foo: return 2) Empty FOR / NEXT loop ! Loops Print "Start at ";time$
FOR I=1 TO 5000
NEXT I
PRINT "Done at ";time$ 3) Midstring search print time$;' at start'
b$='Apple Computer Inc' for x=1 to 5000 a$=mid$(b$,6,7)
next x
print time$;' when done' print a$
end

4) Real divide (by 3)

print time$;' at start' for x=1 to 5000 y=x / 3 next x print time$;' when done'

5) Integer divide (by 3)

print time$;' at start' for x=1 to 5000 y=x div 3 next x print time$,' when done'

6) Eratosthenes Sieve (1899 primes)

! Sieve
s %= 8190
dim f1ags%(s% + 1)
print " start "; time$count% = 0 for i% = 0 to s% f1ags%(i%) = 1 next i% for i% = 0 to s% if flags%(i%) = 0 then goto 250 prime% = i% + i% + 3 k% = i% + prime% 200 if k% > s% then goto 240 f1ags%(k%) = 0 k% = k% + prime% goto 200 240 count% = count% + 1 250 next i% print ' done '; time$
print count% ;' primes'
Test TIme (Seconds)
Empty GOSUBs 3.0
Empty FOR / NEXT loop 1.5
Midstring search 9.0
Real divide (by 3) 18.0
Integer divide (by 3) 3.0
Eratosthenes Sieve (1899 primes) 31.5