About SmallBASIC

by Chris Warren-Smith

The SmallBASIC project commenced in May 2000 by Nicholas Christopoulos and was initially designed for the Palm Pilot.

The 'Small' prefix in the project name reflects the project's original intention of being used with this small hand-held device.

SmallBASIC now also runs under a wide range of platforms including Nokia 770 internet tablet, Franklin eBookman, Linux and Windows.

The language supports old-school BASIC syntax including line numbers, GOSUB and the [in]famous goto statement. This along with various high level built-in routines make it easy for beginners to type in a few lines of code and quickly see some thing happen. Example charts:

dat = [5,6,3,7,10]

chart LINECHART, dat, 3, 20, 17, xmax, ymax

SmallBASIC also supports modern structured syntax allowing you to write maintainable and re-usable code.

Supported syntax includes familiar if/while/until/case etc statements along with nest-able user defined functions and sub-routines. You can further partition your code into separate source modules using the unit keyword.

SmallBASIC is an excellent tool for mathematics. It contains built-in functions for Matrices, Unit conversion, Trigonometry, Logarithms, Statistics (eg SUMSQ, STATSPREADS) Equations (eg LINEQN, DIFFEQN), 2D Algebra (eg PTDISTSEG, SEGLEN) and 2D & 3D graphics transformations.

? "Solve this:"

? "  5x - 2y + 3z = -2"

? " -2x + 7y + 5z =  7"

? "  3x + 5y + 6z =  9"

?

A = [ 5, -2, 3; -2, 7, 5; 3, 5, 6]

B = [ -2; 7; 9]

C = LinEqn(A, B)

? "[x;y;z] = "; C

A = [1, 2 ; 3, 4]

B = [5, 6 ; 7, 8]

? A * B

? A + B

? A - B

SmallBASIC was recently changed to support variables that can reference (point to) SUB or FUNC blocks using the @ operator and the CALL statement. Here's a few examples:

SUB foo1

? "in foo1"

END

FUNC foo3

foo3 = 43

END

fp = @foo3

n = call(fp)

fp = @foo1

CALL fp

A pointer variable can also be passed as an argument to another SUB or FUNC. This offers a similar pattern of use to "virtual functions" available in other languages.

Another area that has been recently enhanced is the CHAIN statement. CHAIN now takes a string or array of strings variable and executes it as a separate process. Program control returns to the calling program once the CHAIN'ed program has completed.

Examples:

code = "for i = 0 to 10: ? i: next i"

CHAIN code

TLOAD "program.bas", code

CHAIN code

? "program.bas completed"

Internally SmallBASIC consists of a set of core modules developed in the "C" language. In addition, there are several "driver" implementation modules that target the code for a particular platform. The main drivers currently being developed are the FLTK based module which builds under both Linux and Windows, and the GTK module designed for the Nokia 770.

It was never really intended that SmallBASIC would be used to deliver code to a paying customer. However the console version can be a useful tool for building relatively complex scripts as part of your development process. For example I have used SmallBASIC to generate complicated SQL statements from an input properties file. I have also used it to create a Java database access layer code generator. Like other BASIC's SmallBASIC is great for doing string manipulation.

The GTK driver has enhanced support for the BUTTON and DOFORM statements. With these statements SmallBASIC could be used as kind of "ultra RAD" tool for building a forms based workflow applications. There is potential here for SmallBASIC to be used in a more serious way.

Although the SmallBASIC community forum is a bit "slow", the source forge download counters indicate the project is still reasonably popular.

Here are few tasks that may help raise the project profile:

  • Web site overhaul
  • Linking into the wider BASIC community perhaps with a web-ring.
  • Updated user documentation.
  • More sample programs to demo new features.
  • More articles in PCOPY!

There are still a few core syntax ideas that are being considered for further enhancement including user defined structures and if test short circuit evaluation. The FLTK version may also gain some form of rudimentary debugger perhaps with breakpoints, line stepping and variable inspection.

Please feel free to give SmallBASIC a try and let us know what you think. SmallBASIC is released under the GPL licence so you are also able to examine its internal workings.

ASCII-World stuff notes:
Article was originally released at PCopy! #20

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.