What general tips do you have for golfing in BBC BASIC? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to BBC BASIC (e.g. "remove comments" is not an answer).
Spaces after keywords are optional because of tokenisation. For example:
INPUT answer$ PRINT answer$ IF answer$ = 'Hi' THEN PRINT 'Hi!' ENDIF
Can be shortened to
INPUTanswer$ PRINTanswer$ IFanswer$='Hi'THEN PRINT'Hi!' ENDIF
The corollary of this is, a variable name cannot start with a keyword. So
PRINTa is not a valid variable name. However
Spaces before keywords can be more tricky. Variable names can end with a keyword, so if variable name and keyword are run together they will be interpreted as a variable name.
For example the following gives a "no such variable" error because the variable
aPRINT is not defined:
By rearranging this can be avoided:
This is a trivial example, but frequently rearranging an expression to end in a number or bracket instead of a variable name will enable you to eliminate a trailing space.
Use VDU codes to perform graphics tasks.
All graphics in BBC basic was performed via machine-specific ASCII control characters. The
VDU command exists to enable you to pass ASCII characters direct to the video controller.
For example in
PLOT101,500,300 (draw a rectangle whose diagonal corners are the last position of the graphics cursor and the new point 500,300) can be subsituted by
VDU25,101,500;300; The coordinates are followed by
; because they are double byte values. We can also replace
25881; which is the 16-bit little-endian representation of these 2 bytes.
Although this actually makes things longer when you have only one PLOT statement, if you have several it is shorter. When you have a long string of VDU codes every PLOT can be replaced by
25, (or if you declare
a=25 simply by
Here's an example of this technique: https://codegolf.stackexchange.com/a/26732/15599
Note that this is not just limited to
MODE, GCOL, COLOUR, ORIGIN etc can all work out shorter when expressed as a series of numeric values in a long
Choose your interpreter well.
Very few of us have access to an original BBC micro, There's a whole range of interpreters at http://www.bbcbasic.co.uk/bbcbasic.html . Each implementation is slightly different.
My preferred interpreter is the first one on the list, "BBC Basic for Windows" written by RT Russell and available for Linux through Wine. It covers virtually all the features of original BBC Basic, including for example sound through the windows API, and has many enhancements. Its IDE and help are easy to use and feature packed.
It frequently allows you to omit colons between statements (which other implementations also do to some extent, but less) and it does not require you to number the lines of your program. On the downside, it only handles strings up to 255 characters and the evaluation version is restricted to 16k memory.
The other major family of BBC basics is Brandy Basic and its derivatives. Written in C, this is extremely portable, but has less features. For example it does not include sound, and rather than simply ignoring sound commands the program crashes. It is however able to handle longer strings than R T Russell's implementation so I have used it on occasion. There is no modern IDE, but it replicates BBC Basic's commandline fairly well (so it's not difficult to use if you are already familiar with BBC Basic.)
Use FN instead of PROC or GOSUB
GOSUB is very old fashioned. it requires a line number, so you have the following:
GOSUB90 . . 90 dosomething RETURN
7 characters to call, 8 characters to define and return.
PROC doesnt require a line number. Here you have
PROCa(x,y,z) . . DEFPROCa(x,y,z) dosomething ENDPROC
5 characters to call (excluding arguments), 13 characters to define and return
With FN you have
q=FNa(x,y,z) . . DEFFNa(x,y,z) dosomething =1
5 characters to call (excluding arguments, but including the obligatory use of the return value.) 8 characters to define and return
FN wins pretty much all the time, especially if you can make use of the return value. If you have no arguments you can omit the brackets.
Use Abbreviated Keywords
Surprised this wasn't mentioned before, but BBC Basic supports abbreviated versions of keywords so that, eg:
1 FOR X=1 TO 10 STEP 2:PRINT X:NEXT X
Can be rewritten as (omitting space where possible too):
Some keywords don't have abbreviations (eg,
IF). The complete list is in the BBC Basic manual chapter 48, there's a handy copy here too.
The most surprising part is that abbreviations for some functions include the opening parenthesis, eg:
PRINT MID$("HELLO",3,2) is abbreviated as
P.M."HELLO",3,2) - note the closing parenthesis is required even though the opening one was abbreviated away.