Applesoft BASIC Quick Reference
This is intended as a quick reference for the Applesoft BASIC Interpreter in JavaScript by Joshua Bell.
Extensions beyond Applesoft BASIC used in the interpreter are called out with footnotes.
See also:
Variable Control
- CLEAR
- Clear all variables
- [LET] var = expr
- Assign variable
- DIM var( size [ , size2 ...] ) [ , var2( size [ , size2 ...] ) ... ]
- Allocate array
- DEF FN f(var ) = expr
- Define function of a single variable [1]
Flow Control
- GOTO
- Go to line number
- GOSUB line
- Go to subroutine
- RETURN
- Return from subroutine
- ON expr GOTO line [, line2 ... ]
- Branch based on index (value = 1, 2, ...)
- ON expr GOSUB line [, line2 ... ]
- Subroutine branch based on index (value = 1, 2, ...)
- POP
- Convert last gosub into a goto
- FOR var = start TO end [ STEP incr ]
- Loop with counter variable
- NEXT [var [, var ...] ]
- End of loop(s)
- IF expr THEN statement
IF expr GOTO line - Conditional; if expr is false, rest of line is skipped
- END
- Terminate program
- STOP
- Break, as if an error occurred
Error Handling
- ONERR GOTO line
- Set error hook
- RESUME
- Retry line that caused ONERR GOTO
Input/Output
- PRINT expr [ [;,] expr2 ... ] [;]
- Output text
- INPUT [string ;] var [, var2 ... ]
- Read line of input
- GET var
- Read single key
- HOME
- Clear text display
- HTAB expr
- Position text cursor horizontally
- VTAB expr
- Position text cursor vertically
- INVERSE
- Set output mode to black-on-white
- FLASH
- Set output mode to flashing
- NORMAL
- Set output mode to white-on-black
- TEXT
- Set display to text mode
Miscellaneous
- REM [ comment ]
- Begin a comment; rest of line is skipped
- TRACE
- Turn on trace mode (line numbers printed)
- NOTRACE
- Turn off trace mode
Inline Data
- DATA value [, value2 ... ]
- Define inline data [2]
- READ var
- Read the next DATA value
- RESTORE
- Restore the DATA pointer to the first value
Lo-Res Graphics
- GR
- Set display to lores mode, clear screen
- COLOR= expr
- Set lores color (0...15)
- PLOT expr, expr
- Plot lores point (x = 0...39, y = 0...40/48)
- HLIN expr, expr AT expr
- Plot horizontal line (x1, x2 at y)
- VLIN expr, expr AT expr
- Plot vertical line (y1, y2 at x)
Hi-Res Graphics
- HGR
- Set display to hires mode, clear screen
- HGR2
- Set display to hires mode (page 2), clear screen
- HPLOT [TO] expr, expr [ TO expr, expr ] ...
- Plot hires point/line (x=0...279, y=0...191)
- HCOLOR= expr
- Set hires color (0...7)
Hi-Res Shape Tables - NOT IMPLEMENTED
- ROT= expr
- Set hires shape table rotation (0...63)
- SCALE= expr
- Set hires shape table scale (1...255)
- DRAW expr [ AT expr, expr ]
- Draw hires shape table shape in color
- XDRAW expr [ AT expr, expr ]
- Draw hires shape table shape with XOR
Interpreter and Program State - NOT IMPLEMENTED
- CONT
- Continue from a STOP
- DEL
- Delete lines of program
- LIST [ expr [, expr ] ]
- List lines of program
- NEW
- Clear program and variables
- RUN [ expr ]
- Start program execution at line
- SPEED= expr
- Set character output delay
Native Platform Interaction - NOT IMPLEMENTED
- CALL expr
- Execute assembly code at address
- HIMEM: expr
- Set upper address of variable memory
- IN# expr
- Direct input from slot
- LOMEM: expr
- Set lower address of variable memory
- WAIT expr, expr [, expr]
- Wait until memory location masked by second argument equals third argument (or zero)
Cassette Tape - NOT IMPLEMENTED
- LOAD
- Load program from cassette
- RECALL
- Load variables from cassette
- SAVE
- Save program to cassette
- STORE
- Save variables to cassette
- SHLOAD
- Load hires shape table from cassette
- POKE expr, expr
- Set memory location to value
- POKE 32,n - Text window left edge
- POKE 33,n - Text window width
- POKE 34,n - Text window top edge
- POKE 35,n - Text window bottom
- POKE 216,n - ONERR flag (n < 128 disables ONERR handler)
- POKE 49168,0 - clear keyboard strobe
- POKE 49200,0 - toggle speaker (no-op)
- POKE 49232,0 - graphics mode
- POKE 49233,0 - text mode
- POKE 49234,0 - full graphics
- POKE 49235,0 - split screen
- POKE 49238,0 - lores
- POKE 49239,0 - hires
- PR# expr
- Direct output to slot
- PR#0 - set 40 column mode
- PR#3 - set 80 column mode
Functions
Numeric Functions
- ABS( expr )
- Absolute value of number
- ATN( expr )
- Arctangent of number
- COS( expr )
- Cosine of number
- EXP( expr )
- Raise e to number
- INT( expr )
- Integer part of number
- LOG( expr )
- Natural log of number
- RND( expr )
- Pseudo-random number generator (0 repeats last, negative reseeds)
- SGN( expr )
- Sign of number (-1,0,1)
- SIN( expr )
- Sine of number
- SQR( expr )
- Square root of number
- TAN( expr )
- Tangent of number
String Functions
- LEN( expr )
- Length of string
- LEFT$( expr, expr )
- Left portion of (string, length)
- MID$( expr, expr [, expr] )
- Substring of (string, start character, length)
- RIGHT$( expr, expr )
- Right portion of (string, length)
Type Conversion Functions
- ASC( expr )
- ASCII code for first character of string
- CHR$( expr )
- Character at specified ASCII code point
- STR$( expr )
- String representation of number
- VAL( expr )
- Parse string into number
System Interaction Functions
- FRE( expr )
- Garbage collect strings (returns 0)
- PDL( expr )
- Paddle position (paddle number)
- POS( expr )
- Horizontal cursor position
- SCRN( expr, expr )
- Lores color at pixel (x,y)
- USR( expr )
- Execute assembly code at address, return accumulator value - NOT IMPLEMENTED
User Defined Functions
- FN f( expr )
- Execute user defined function [1]
- PEEK( expr )
- Value at memory location - NOT IMPLEMENTED
- PEEK(49152) - read keyboard
- PEEK(49168) - clear keyboard strobe
- PEEK(49200) - toggle speaker (no-op)
- PEEK(49248) - Read paddle button 3
- PEEK(49249) - Read paddle button 0
- PEEK(49250) - Read paddle button 1
- PEEK(49251) - Read paddle button 2
Comparison Operators
- =
- Equality[3]
- <
- Less than
- >
- Greater than
- <=
- =<
- Less than or equal
- >=
- =>
- Greater than or equal
- <>
- ><
- Not equal
Boolean Operators
- AND
- Conjunction
- OR
- Disjunction
- NOT
- Negation
Arithmetic Operators
- ^
- Exponentiation
- *
- Multiplication
- /
- Division
- +
- Addition
- -
- Subtraction
String Operators
- +
- String Concatenation
BNF Grammar for Expressions
For the even geekier in the audience...
Expression := OrExpression
OrExpression := AndExpression [ 'OR' AndExpression ... ]
AndExpression := RelationalExpression [ 'AND' RelationalExpression ... ]
RelationalExpression := AdditiveExpression [ ( '=' | '<' | '>' | '<=' | '=<' | '>=' | '=>' | '<>' | '><' ) AdditiveExpression ... ]
AdditiveExpression := MultiplicativeExpression [ ( '+' | '-' ) MultiplicativeExpression ... ]
MultiplicativeExpression := PowerExpression [ ( '*' | '/' ) PowerExpression ... ]
PowerExpression := UnaryExpression [ '^' UnaryExpression ]
UnaryExpression := ( '+' | '-' | 'NOT' ) UnaryExpression
| FinalExpression
FinalExpression := number-literal
| string-literal
| 'FN' user_function_name '(' Expression ')'
| function_name '(' Expression [, Expression ...] ')'
| identifier [ '(' Expression [, Expression ...] ')' ]
| '(' Expression ')'
See also
DOS Commands
- MON[,C][,I][,O]
- Traces DOS 3.3 commands ('Commands', 'Input' and 'Output')
- NOMON[,C][,I][,O]
- Cancels tracing of DOS 3.3 commands ('Commands', 'Input' and 'Output')
- OPEN filename[,Llen]
- Opens a text file.
- APPEND filename
- Appends to a text file.
- CLOSE [filename]
- Closes specified (or all) open text files.
- POSITION filename[,Rnum]
- Advances position in text file.
- READ filename[,Rnum][,Bbyte]
- Reads from a text file.
- WRITE filename[,Rnum][,Bbyte]
- Writes to a text file
- DELETE filename
- Delete a file
Other DOS commands are NOT IMPLEMENTED.
Some sample files are present in a server-side store loaded on-demand into a client-side virtual file system (VFS). Creating or writing to a file will write to the VFS. Subsequent reads will read from the VFS. Files may be deleted from the VFS. These changes will not persist if the browser is refreshed or closed.
- [1] DEF FN supports string and integer functions (e.g. DEF FN IN$(X$) = " " + X$
- [2] DATA statements can contain expressions (e.g. DATA INT(RND(1) * D + 1)
- [3] == is supported for equality comparisons, in addition to =