BASIC language subset/dialect in C++

This one came to me from Peter, a set of C++ macros and define sets that implement a subset of BASIC.

YES, that is correct, BASIC.  It even has line number!  Let’s look at a simple hello world!

type hello.bas
#include “ptsvubas.cc

BEGINBASIC(int,main,(int argc, char* argv[]))
10 PRINT “Hello, world!”;
20 GOTO 40;
30 PRINT “I am not printed”;
40 END;
ENDBASIC

It’s compiled with GCC like this:

c++ -pipe -xc++ -fpermissive -w -o hello.exe hello.bas

And then run it!

Hello, world!

Neat!

From the docs in the main .cc file:

Implemented a BASIC language dialect/subset, with the following restrictions:

  •  All variables and statements must be allcaps
  • All lines must begin with a line number from 1 to 32767
  • All lines must have a trailing semicolon
  • One statement per line (except IF-THEN)
  • “THEN” must NOT be followed by a “GOTO” nor “GOSUB”!
    • Implemented keywords:
    • IF <C-style expression with “==” in case of equality> THEN <label>
    • GOTO <label>
    • GOSUB <label> / RETURN
    • STOP [optional errorlevel/return value]
    • END [optional errorlevel/return value]
    • LET <variable>=<expression>
    • PRINT <strings and/or variables in arbitrary order>
    • INPUT <one or more variables>
    • DIM <array variables with number of elements, p.ex. “NP(42)”>
    • FOR/NEXT loop (NEXT’s argument is mandatory, exactly one variable)
    • PRINT interprets comma as semicolon, except on the ENDS
      • thus PRINT does NOT interpret comma as tab injection
        basically, PRINT is some kind of writeln()
        but can be tricked: if CHR$(0) is inserted somewhere, it won’t print
        the remaining part including the trailing newline!
        variables are all float types, predeclared, and their names at most 2 chars long
    • (numerical) arrays can be used, must be declared before their first usage
    • Array numberings: DIM A(8) means that A(0) till A(7) are declared this way

So it’s not 100%, there is no strings, not even a REM (conver to C++ comments) so it’ll be a while until you can build more traditional basic programs.

Simple Mandelbrot set in BASIC

I used “mingw-i686-7.1.0-win32-dwarf-rt_v5-rev0” to test this on Windows.

You can download ptsvubas from it’s site here: vm01.unsoft.hu/~np/basic/latest/

4 thoughts on “BASIC language subset/dialect in C++

Leave a Reply