Showing posts with label bwbasic. Show all posts
Showing posts with label bwbasic. Show all posts

Sunday, December 28, 2025

Revival of BASIC

BASIC, the language of my youth (quickly replaced by F0RTRAN), came back in a surprising way. 

Some months ago, a PicoCalc finally arrived at my doorstep. The kit is brilliant and so is the product.
I toyed with the Lisp interpreter as an RPN calculator... fantastic! However, most of my readers are probably not interested in either RPN (reverse Polish notation) or in Lisp (and probably not in F0RTRAN either).

So, today I want to share some views of mine using the MMBASIC, which is part of the PicoCalc package.
MMBASIC allows for classical line numbering. We all know how convenient that is. Also, the line numbering BASIC code often leads to some severe spaghetti-code.

MMBASIC however, also allows for a more modern approach w/o line numbers, but labels instead. Generally labels are great, however, one needs to be careful to avoid any keywords when choosing labels.

In a label based programming language, one obviously wants to focus on routines to avoid spaghetti code. As know from many programming languages, such (sub)routines (procedures, functions or whatever the name is) usually are coded before the main program loop. BASIC does not respect this convention, first come first served in the doctrine here. 

Here is how to overcome BASIC's ignorance.

I usually have a block of constants and variables at the beginning of my code. That's cool with BASIC too.
In my usual programming, routines come next. BASIC would usually just start executing those and spit out an error when the first "return" was encountered. Fair enough, a return without a call cannot be performed.

In order to keep the traditional order of routines and functions before the main program loop, we need to pull a trick.
Similar to what is done in C, my main program loop in BASIC (at the end of the code) will get a label main:. So, the very first command after the definition of constants and variables is goto main. Obviously, this forces the interpreter to directly execute the main-loop.

I am not sure if this is elegant, however, this is how I program in modern BASIC.



Monday, April 14, 2025

BASIC on a Chromebook

This continues my series into the past of many of us. Many of us learned programming by using the BASIC language. I school, I was forced to learn PASCAL, which was not of much use, other than understanding programming principles, which I already knew from BASIC. Learning F0RTRAN was the real step into the world of computing.

Anyway, this post is about BASIC on Chromebooks. I do own several of those. Not everyone of those is able to run Linux easily (there is always a way to force the hardware, one way or the other). This post is concerned with Chromebooks which can be easily set to run Linux applications.

To set any such Chromebook to do that, access "Settings" => "About ChromeOS" => "Linux Development Environment". If your Chromebook has that, install the Linux-thing. This might take a while.

Once your Chromebook has the virtual machine with (Debian) linux on board, you might start a (Linux) terminal and install "BWBASIC" by typing
sudo apt install bwbasic
in the terminal.

For the fun of it, I did install geany the same way. Now you can run your BASIC programs on a Chromebook in style. 
I have not yet checked the use of the FreeBasic Compiler. I see no reason why this should not work.

Can I run Python/Thonny on my ChromeBook? Stay tuned!


Sunday, March 23, 2025

Raspberry Pi running BASIC

Earlier on this blog, I posted something about the BASIC language on Linux. 

It turns out that all the tools mentioned in this posting are available on Raspberry Pi computers too. In fact, BWBASIC, FreeBasic and Geany work in the exact same way.


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

You may have noticed, that there is an additional empty line at the of the output. This line can also be suppressed with the stream editor. For doing that, you might want to use the following:
$ bwbasic count.bas | sed '1,4d;$d'
In my view, this is the preferred way of using the basic interpreter in a script envirronment.

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;$d' Obviously, this run option won't function on interactive programs... just saying.

Finally, one can write a little script, to run basic sources from the command line. Here is an example:
#!/bin/sh
bwbasic $1 | sed '1,4d;$d'
Of course, this can also be defined as a alias or function in you shell environment... One such option could be:
$ function run () { bwbasic $1 | sed '1,4d;$d';}