Commands
This page lists the bunnyBASIC statements and built-in functions that are implemented in the current browser runtime.
Program statements
Print one or more expressions.
10 PRINT "score", 42, TRUE
Arguments are comma-separated, and the runtime prints them with spaces between values.
Assignment
Assign an expression to a variable:
10 $NAME = "bunny"
20 $COUNT = 12
IF / ELSE / END IF
Multi-line conditional blocks:
10 IF $COUNT > 10
20 PRINT "large"
30 ELSE
40 PRINT "small"
50 END IF
FOR / END FOR
Ascending loop with optional STEP:
10 FOR $I = 0 TO 10 STEP 2
20 PRINT $I
30 END FOR
The current runtime is designed around upward iteration. Write FOR loops from lower values to higher values.
WHILE / END WHILE
Standard condition-controlled loop:
10 $N = 0
20 WHILE $N < 3
30 PRINT $N
40 $N = $N + 1
50 END WHILE
Functions
FUNCTION / END FUNCTION
Define a function. Parameters are variables, so they use $.
10 PRINT DOUBLE(21)
20 FUNCTION DOUBLE($N)
30 RETURN $N + $N
40 END FUNCTION
FUN is also accepted as a shorter spelling, but the full form is the clearest one to document and use.
GLOBALS
Inside a function, variables are local by default. Add GLOBALS (...) when you need to touch top-level variables.
10 $COUNT = 0
20 CALL BUMP()
30 PRINT $COUNT
40 FUNCTION BUMP() GLOBALS ($COUNT)
50 $COUNT = $COUNT + 1
60 END FUNCTION
RETURN
Return a value from a function:
10 PRINT ADD(2, 3)
20 FUNCTION ADD($A, $B)
30 RETURN $A + $B
40 END FUNCTION
Function calls in expressions
Call a function anywhere an expression is allowed:
10 PRINT SIN(DEG(90.0))
CALL
Use CALL when you want to invoke a function as a statement and ignore its return value:
10 CALL SHOW("hello")
20 FUNCTION SHOW($TEXT)
30 PRINT $TEXT
40 END FUNCTION
Timers and delays
CALL ... AFTER
Schedule a user-defined function once:
10 CALL HELLO("timer") AFTER 500 MS ID 1
20 DELAY 1 SEC
30 FUNCTION HELLO($NAME)
40 PRINT "hello", $NAME
50 END FUNCTION
CALL ... EVERY
Schedule a repeating timer:
10 CALL TICK() EVERY 1 SEC ID 1
20 DELAY 3 SEC
30 CANCEL TIMER 1
40 FUNCTION TICK()
50 PRINT "tick", SECOND()
60 END FUNCTION
DELAY
Pause program execution for an integer amount of time:
10 PRINT "wait"
20 DELAY 250 MS
30 PRINT "done"
Units:
MSSEC
CANCEL TIMER
Cancel a timer created with ID:
10 CANCEL TIMER 1
Arrays
Array literal
Create arrays directly:
10 $WORDS = ["bunny", "basic"]
Index read and write
10 $NUMS = [10, 20, 30]
20 PRINT $NUMS[1]
30 $NUMS[1] = 99
Push and unshift
10 $NUMS = [1, 2]
20 $NUMS[] = 3
30 $NUMS() = 0
Pop and shift as expressions
10 $NUMS = [1, 2, 3]
20 PRINT $NUMS[]
30 PRINT $NUMS()
Graphics statements
The immediate graphics API works on a 512 x 512 pixel canvas and expects integer arguments.
CLS
Clear the canvas and terminal:
10 CLS
PIXEL
Draw a single pixel:
10 PIXEL (100, 120), 12
LINE
Draw a line:
10 LINE (20, 20)-(200, 120), 10
CIRCLE
Draw a circle outline or rasterized circle with center, radius, and palette index:
10 CIRCLE (256, 256), 80, 12
PALETTE
Override a palette entry with RGB values from 0 to 255:
10 PALETTE 20, (255, 128, 0)
20 LINE (20, 20)-(180, 100), 20
DRAW
Render a shape expression:
10 $SHIP = <256.0, 256.0> + (48.0 * RECT())
20 DRAW $SHIP
REPL and file commands
These commands are intended for the terminal prompt, not for storing inside a numbered program.
RUN
Run the current stored program:
RUN
LIST
Show the numbered program listing:
LIST
PRETTY
Show a formatted version of the current program:
PRETTY
RENUM
Renumber the stored program. Default start is 10.
RENUM
RENUM 100
NEW
Clear the stored program:
NEW
DIR
List saved files:
DIR
SAVE / LOAD / RM
Persist a program in browser storage:
SAVE "demo.bas"
LOAD "demo.bas"
RM "demo.bas"
Filenames currently need to match this shape:
- quoted
- end in
.bas - letters, digits,
_, and-
Built-in functions
The following built-ins are available as expressions.
| Function | Returns | Notes |
|---|---|---|
EXP(x) |
DECIMAL |
x must be decimal |
LEN(arr) |
INTEGER |
array length only |
RAND(n) |
INTEGER |
random integer in [0, n) |
SIN(x) |
DECIMAL |
decimal input |
COS(x) |
DECIMAL |
decimal input |
INT(x) |
INTEGER |
decimal to integer |
DECIMAL(x) |
DECIMAL |
integer to decimal |
DEG(x) |
DECIMAL |
degrees to radians |
RECT() |
SHAPE |
unit rectangle |
CIRCLE() |
SHAPE |
unit circle |
HOUR() |
INTEGER |
current browser hour |
MINUTE() |
INTEGER |
current browser minute |
SECOND() |
INTEGER |
current browser second |
POP(arr) |
array element | removes last element |
SHIFT(arr) |
array element | removes first element |
GETPIXEL(x, y) |
INTEGER |
palette index at pixel |
Examples:
10 PRINT RAND(10)
20 PRINT SIN(DEG(90.0))
30 PRINT GETPIXEL(10, 10)