BASIC Reference

Overview

A bytecode-compiled line-number BASIC. Programs are written as numbered lines and compiled through a tokenizer, parser, and compiler into a stack-based VM. Output from PRINT statements is returned as the result.

Data Types

Undefined variables default to 0 (numeric) or "" (string).

Syntax

Statements

StatementSyntaxDescription
LETLET X = expr or X = exprAssign a value to a variable (LET keyword is optional)
PRINTPRINT expr, expr ...Print values; semicolon at end suppresses newline
INPUTINPUT XRead a value from input
IFIF expr THEN statementsConditional; also supports IF expr THEN line (implicit GOTO)
GOTOGOTO lineUnconditional jump to a line number
GOSUBGOSUB lineCall subroutine at line number (saves return address)
RETURNRETURNReturn from subroutine
FORFOR I = a TO b [STEP c]Begin counted loop (default step is 1)
NEXTNEXT IEnd of FOR loop; increment and test
ENDENDTerminate program
REMREM comment textComment (rest of line is ignored)
HTTPHEADERHTTPHEADER name$, value$Set a custom header for the next HTTP request (auto-cleared after use)

Operators

OperatorDescriptionNotes
+Addition / string concatenation"A" + "B""AB"
-Subtraction / unary negation
*Multiplication
/DivisionError if divisor is zero
%Modulo (remainder)
=EqualReturns -1 (true) or 0 (false)
<>Not equal
<Less than
>Greater than
<=Less than or equal
>=Greater than or equal

Precedence (high to low): unary -, then * / %, then + -, then comparisons. Comparisons return -1 (true) or 0 (false), matching classic BASIC convention.

Built-in Functions

FunctionSyntaxDescription
ABSABS(n)Absolute value
INTINT(n)Floor (truncate toward negative infinity)
RNDRND(0)Random number in [0, 1) using xorshift64 PRNG
LENLEN(s$)String length
LEFT$LEFT$(s$, n)First n characters
RIGHT$RIGHT$(s$, n)Last n characters
MID$MID$(s$, start, len)Substring (1-indexed start position)
STR$STR$(n)Convert number to string
VALVAL(s$)Convert string to number (0 if non-numeric)
CHR$CHR$(n)Character from ASCII/Unicode code
ASCASC(s$)ASCII/Unicode code of first character (0 if empty)
INSTRINSTR(haystack$, needle$)Find substring position (1-based); returns 0 if not found

HTTP Outbound

FunctionSyntaxDescription
HTTPGET$HTTPGET$("url")HTTP GET request; returns response body
HTTPPOST$HTTPPOST$("url", "body")HTTP POST request; returns response body. Sends Content-Type: application/json by default.

Secrets

FunctionSyntaxDescription
ENV$ENV$("KEY")Read a secret by name; returns its value

KV Storage

FunctionSyntaxDescription
KVGET$KVGET$("key")Get value for key; returns empty string if not found
KVSETKVSET("key", "value")Set key to value; returns empty string
KVDELKVDEL("key")Delete key

KV storage is only available for saved functions (not ad-hoc /api/run).

Examples

Hello World

10 PRINT "Hello, World!"

FizzBuzz with GOSUB

10 FOR I = 1 TO 20
20 GOSUB 100
30 NEXT I
40 END
100 REM FizzBuzz subroutine
110 IF I % 15 = 0 THEN PRINT "FizzBuzz" : RETURN
120 IF I % 3 = 0 THEN PRINT "Fizz" : RETURN
130 IF I % 5 = 0 THEN PRINT "Buzz" : RETURN
140 PRINT I
150 RETURN

String manipulation

10 LET N$ = "Hello, World!"
20 PRINT LEN(N$)
30 PRINT LEFT$(N$, 5)
40 PRINT MID$(N$, 8, 5)

Fibonacci

10 A = 0 : B = 1
20 FOR I = 1 TO 10
30 PRINT A;
40 C = A + B
50 A = B : B = C
60 NEXT I

Constraints