BASIC v2 language reference¶
Syntax highlighting support for Commodore 64 BASIC v2 (Commodore BASIC 2.0).
Control flow keywords¶
end, for, next, gosub, goto, if, then, step, stop, return, to, on
for i=1 to 10 step 2
print i
next i
if x=5 then print "found"
on n gosub 100,200,300
gosub 1000
return
end
Built-in functions¶
BASIC v2 built-in functions categorized by purpose:
Mathematical functions¶
abs, atn, cos, exp, int, log, rnd, sgn, sin, sqr, tan
abs(x)- Absolute valueatn(x)- Arc tangent (result in radians)cos(x)- Cosine (argument in radians)exp(x)- Exponential (e^x)int(x)- Integer portion (truncates toward zero)log(x)- Natural logarithm (base e)rnd(x)- Random number (0 ≤ result < 1)sgn(x)- Sign function (-1, 0, or 1)sin(x)- Sine (argument in radians)sqr(x)- Square roottan(x)- Tangent (argument in radians)
String functions¶
asc, chr$, left$, len, mid$, right$, str$, val
asc(str$)- Returns ASCII/PETSCII code of first characterchr$(code)- Converts numeric code to characterleft$(str$,n)- Returns n leftmost characterslen(str$)- Returns string length (0-255)mid$(str$,start,length)- Extracts substringright$(str$,n)- Returns n rightmost charactersstr$(num)- Converts number to stringval(str$)- Converts string to number
System functions¶
fre, peek, pos, usr
fre(0)- Returns free BASIC memory in bytespeek(addr)- Reads byte from memory addresspos(0)- Returns cursor column position (0-39)usr(x)- Calls machine language routine
a = abs(-5)
b$ = chr$(65)
c = peek(53280)
d$ = left$("hello",2)
m = fre(0)
User-defined functions¶
def fn declarations and fn function calls:
def fn sq(x) = x * x
a = fn sq(5)
I/O and disk keywords¶
print, print#, input, input#, open, close, load, list, save, restore, read, get, get#, poke, peek, sys, run, verify, wait
print "Hello"
input "Name";n$
open 1,8,2,"file,s,w"
load "program",8
poke 53280,0
sys 49152
Storage keywords¶
dim, data, let, def
dim array(10)
data 1,2,3,4,5
read a,b,c
let x = 42
Variables¶
Variable names with optional type suffixes:
- Standard (float):
a,counter- 40-bit floating point (default) - String:
name$,text$- Variable-length strings (up to 255 chars) - Integer:
x%,count%- 16-bit signed (-32768 to 32767)
a = 10
name$ = "hello"
count% = 100
Variable naming rules¶
BASIC v2 enforces these constraints:
- First two characters significant -
counterandcountare the same variable - Must start with letter - Cannot begin with digit or special character
- Type suffix last - Dollar sign ($) or percent (%) must be final character
- Case insensitive -
NAME$andname$refer to same variable - Reserved words forbidden - Cannot use BASIC keywords as variable names
Operators¶
- Arithmetic:
+,-,*,/,^ - Comparison:
=,<>,<,>,<=,>= - Logical:
and,or,not
a = 5 + 3
if a>5 and b<10 then print "yes"
Numbers¶
- Line numbers: At start of line (e.g.,
10,100) - Decimal:
255,3.14159 - Hexadecimal:
$ff,$c000(BPP+ preprocessor feature) - Binary:
%11111111,%10101010(BPP+ preprocessor feature)
10 a = 255
20 b = $ff
30 c = %11111111
Comments¶
- REM comments:
rem This is a comment - Semicolon comments:
; BPP+ comment syntax
rem BASIC comment
10 rem Comment after line number
; BPP+ comment
BPP+ removes both comment types during preprocessing. Exception: Blitz! compiler directives (rem **) are preserved.
Strings¶
Double-quoted strings and embedded PETSCII tokens:
a$ = "simple string"
b$ = "{clr}{wht}Hello"
c$ = "{10 space}Indented"
Token syntax inside strings:
- Basic tokens:
{clr},{wht},{down} - Repetition:
{10 space},{5 down} - Ranges:
{a-z},{0-9}
Extension symbols¶
See Extension symbols for MCI commands and Prof. Plum extensions.