Friday, February 28, 2025

BASIC on Linux

Who does not remember the good old times when all (home) computers came with BASIC. To recreate the good old time feeling, BASIC had to be back on my Linux systems.

I figured out a combination of bwbasic and FreeBasic

The Bywater BASIC interpreter offers an interactive environment, similar to GW-BASIC. With a tiny trick, bwbasic can be used in a batch mode directly from shell. The "trick" being using the SYSTEM or QUIT statement at the end of the code, rather than the END statement. Here is an example:

10 for i=0 to 10
20 gosub 100
30 next
40 ?
50 system
100 ? i;
110 return

Line numbers are not necessary, however, line numbers allow to edit the code in the bwbasics interactive environment.

Sometimes, one might want to compile a program. In comes the FreeBasic compiler. It is important to point out that bwbasic and FreeBasic are not 100% compatible. I order successfully compile code as displayed above, one needs to the a "-lang" option. I found "fbc -lang qb [filename]" to work fine.
Another remark, while SYSTEM and QUIT are equivalents under bwbasic, fbc does understand SYSTEM, but not QUIT.

One interesting comparison is the performance of the interpreter and the compiled code.

Interpreter:
$ time bwbasic count.bas
Bywater BASIC Interpreter/Shell, version 2.20 patch level 2
Copyright (c) 1993, Ted A. Campbell
Copyright (c) 1995-1997, Jon B. Volkoff

 0 1 2 3 4 5 6 7 8 9 10

real 0m0.007s
user 0m0.003s
sys 0m0.004s

Compiled code:
$ time ./count
 0 1 2 3 4 5 6 7 8 9 10

real 0m0.038s
user 0m0.005s
sys 0m0.007s

When using the interpreter, the first 4 lines of the output are not controllable by the user of bwbasic. However, a very simple trick with the stream editor gets rid of the problem. It does not even cost a lot of extra time. This way, the output can be piped to yet another program. Have a look:
$ time bwbasic count.bas | sed 1,4d
 0 1 2 3 4 5 6 7 8 9 10


real 0m0.013s
user 0m0.005s
sys 0m0.018s


There is yet another program I want to push into the mix, Geany. This IDE integrates perfectly with the FreeBasic compiler. Just be sure to add the "-lang qb" to the Build Commands. Click Build -> Set Build Commands. The "Compile" Command would therefore look like this: fbc -lang qb -w all "%f"
Geany also supports "Run" Commands. Making use od the stream editor as above, the command looks like this: bwbasic "%f" | sed 1,4d Obviously, this run option won't function on interactive programs... just saying.