XXIIVV
Sunflower BASIC, Moebius × Rek Bell
Sunflower BASIC, Moebius × Rek Bell 19R04

10 PRINT "Hello World"

This guide is written specifically for the Sunflower BASIC implementation, but it should still give a general idea of how to use the language's primitives which are also found in most BASIC implementations.

Typical BASIC Programmer

Introduction

A BASIC program is made of lines beginning with a line number, followed by an operand and an expression. Evaluation walks through the lines of the program in order from small to large until the program reaches the END keyword.

1 REM This is a comment.
2 LET I:I+1, F:I%3=0, B:I%5=0
3 IF F PRINT "FIZZ";
4 IF B PRINT "BUZZ";
5 IF F+B=0 PRINT I;
6 PRINT ""
7 IF I<100 GOTO 2
8 END

Expressions can include numbers, variables and arithmetic operators. Numbers are decimal, but can be prefixed with $ for hexadecimal, there are no precedence rules.

10 LET A:6+7
20 PRINT 52+$20*A
30 END
1092

Valid arithmetic operators inside of expressions are as follow:

Interpreter

The interpreter below allows you to play with the examples on this page.

Variables

Variables are single uppercase letters of the alphabet that can store the value of a number. The value of a variable is set with the LET command.

10 LET A:123
20 PRINT "The value of A is " A "."
30 END
The value of A is 123

The R variable is special, reading its value will always give a random number between 0 and 65536. For example, if you wanted to do a program to roll a 6-sided dice:

10 PRINT "You rolled " R%6+1 "."
30 END
You rolled 5.

When a variable is followed by a dot and an expression, the variable to read or write will have an offset equal to the value of the expression:

10 LET X:2, A.X:808
20 PRINT C
30 END
808

Loops

Loops are created by jumping back to the start of the loop with a GOTO, until a specific condition is met with IF. Using a variable as counter, a program can loop over a number of lines five times:

10 LET I:0
20    PRINT "Count: " I
25    LET I:I+1
30    IF I<5 GOTO 20
40 END
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4

Loops can also be nested using two counters:

10 LET A:0
20    LET B:0
21        PRINT A ", " B
22        LET B:B+1
23        IF B<3 GOTO 21
30    LET A:A+1
31    IF A<3 GOTO 20
32 END

Functions

Functions are created with GOSUB, similarly to GOTO, this operation moves evaluation to a different line, except that they keep a return address in memory to be jumped back to upon RETURN.

1 LET N:5
2 GOSUB 50
3 END

49 REM Fib(N)
50 LET A:3, B:1
51     LET C:A, A:3*A-B, B:C, I:I+1
52     IF I<N GOTO 51
53 PRINT "Result: " C
54 RETURN
Result: 144

Interaction

The INPUT statement, will halt the execution and wait for an input. This can be used to store a value into a variable. For example, to capture the position of the mouse cursor:

10 PRINT "Touch the screen"
20 INPUT X, Y
30 PRINT "You touched at: " X ", " Y
50 PRINT "Press a button"
60 INPUT B
70 PRINT "You pressed: " B
80 END

Manual

Hypervisor RUNUsed to begin program execution at the lowest line number.
RUN
LISTCauses part or all of the user program to be listed. If no parameters are given, the whole program is listed. A single expression parameter in evaluated to a line number which, if it exists, is listed. A two expressions will print a range of lines.
LIST 200
LIST 10,30
LIST
CLEARFormats the user program space, deleting any previous programs. If included in a program the program becomes suicidal when the statement is executed.
CLEAR
Core REMPermits to add remarks to a program source.
REM This is a comment.
LETAssigns the value of an expression to a variable.
LET A:123/2, B:$20%2
IFIf the result of the expression is not zero, the statement is executed; if False, the associated statement is skipped.
IF A>B-2 PRINT "A is greater."
GOTOChanges the sequence of program execution.
GOTO 50+A>B
ENDMust be the last executable statement in a program. Failure to include an END statement will result in an error stop after the last line of the program is executed.
END
Subroutines GOSUBChanges the sequence of program execution, and remembers the line number of the GOSUB statement, so that the next occurrence of a RETURN statement will result in execution proceeding from the statement following the GOSUB.
GOSUB 220
RETURNTransfers execution control to the line following the most recent unRETURNed GOSUB. If there is no matching GOSUB an error stop occurs.
RETURN
I/O PRINTPrints the values of the expressions and/or the contents of the strings in the console. To print the result of an expression as an ascii character, use #$41, to print the character A, end with a semi-colon to print without a linebreak.
PRINT "The result is: " A+B
INPUTHalts evaluation, and assigns the result of expressions to each of the variables listed in the argument. Expressions are entered sequencially and separated by a line break, a list of two arguments, will expect two input lines.
INPUT X, Y
File SAVEExports your program to example.bas.
SAVE example.bas
LOADImports the example.bas program, replaces your current program.
LOAD example.bas
Screen COLORSets the interface RGB colors, see theme.
COLOR $50f2, $b0f9, $a0f8
CLSClears the screen.
CLS
DRAWSets position of drawing.
DRAW 100, 320
MODESets drawing mode, see varvara.
MODE 04
POKEStores data into the font memory bank, the first value is an ascii byte value.
POKE 1, $1c1c, $087f, $0814, $2241
PICTDraws a picture from a file, uses MODE.
PICT image10x10.icn
PICT image12x18.chr

Tic-Tac-Toe

Here's a complete example that uses everything above and implements a graphical tic-tac-toe game.

1 REM Create Assets: play[012] grid[567]
2 POKE 1, $c3e7, $7e3c, $3c7e, $e7c3
3 POKE 2, $187e, $7eff, $ff7e, $7e18
4 POKE 5, $1818, $1818, $1818, $1818
5 POKE 6, $0000, $00ff, $ff00, $0000
6 POKE 7, $1818, $3cff, $ff3c, $1818

7 REM Game Loop
8 GOSUB 160
9 GOSUB 120
10 IF W=0 GOTO 8
11 GOSUB 160
12 PRINT "WINNER: " #W
13 END

120 REM Request Player Turn
121 PRINT "Player " #P+1 " input position[0-8]:"
122 INPUT X
124 IF X>8 RETURN

130 REM Record Player Move
131 IF A.X GOTO 140
132 LET P:P=0, A.X:P+1
133 GOTO 150

140 REM Invalid Move
141 PRINT "This space is already taken! Input another one:"
142 GOTO 122

150 REM Validate board
151 IF A&B&C LET W:A
152 IF D&E&F LET W:D
153 IF G&H&I LET W:G
154 IF A&E&I LET W:A
155 IF G&E&C LET W:G
156 IF A&D&G LET W:A
157 IF B&E&H LET W:B
158 IF C&F&I LET W:C
159 RETURN

160 REM Draw Grid
161 CLS
162 PRINT #A #5 #B #5 #C
163 PRINT #6 #7 #6 #7 #6
164 PRINT #D #5 #E #5 #F
165 PRINT #6 #7 #6 #7 #6
166 PRINT #G #5 #H #5 #I
167 PRINT ""
168 RETURN
Kurtz & Kemeny book cover
Sunflower BASIC, Moebius × Rek Bell(inside)
Sunflower BASIC, Moebius × Rek Bell(inside)19R03

incoming: imperative 2025 2022