libfawk's BASIC: fbas
Libfawk BASIC, fbas (for function-BASIC), is a modern, third-generation dialect
of the BASIC language. It features control flow (loops) and functions.
Inherited from libfawk, it also supports array-in-array, struct-like
array addressing syntax, function references saved in variables and
the builtin functions and variables of fawk.
Although there is no GOSUB, fbas still has GOTO and optional line
numbering (that can be used mixed with line labeling). This allows
traditional (1970s/1980s style) basic programming.
Major differences from the common dialects
- Variable names, parameter names and function names are case-sensitive.
- There is no interactive mode, only stored programs; e.g. there is no
LIST command.
- Line numbering is optional. Each line is prefixed with either a
line number, an alphanumeric label followed by a colon or has
no prerix. Labels and line numbers are used by goto.
- There is no PRINT or INPUT instructions. Use the functions
provided by the host app, or use
fawk_print()
for debug prints. Reason: in most use cases printing directly
to stdout does not make sense in the host application.
- Two strings can not be concatenated using the + operator; instead the
+ operator will convert the strings to numbers and sum them. Use the
@ operator for string concatenation (this is a libfawk-specific feature
that is present in all libfawk scripting languages).
- Arrays are indexed using the [] brackets, not () parenthesis;
reason: function reference can be saved in arrays.
- There is no DIM and REDIM: arrays are created automatically upon
first reference with [].
- There is no GOSUB, RETURN, because
they are incompatible with libfawk functions
- Labels (and line numbers) are function-local; that means the same
label (or line number) can appear in multiple different functions and
in the main program (but only once in each function or program) and
will still mean a different thing. This also means GOTO can not
be used to do a cross-function jump as it can not address labels
of a different function.
- A single line number without the goto keyword in an if-then will
NOT be a goto.
- Single line if-then is accepted, but if-then-else always needs to be
multi-line, with an end if terminator. There is no support
for elseif.
- There is no DATA, READ and RESTORE. Just use arrays, they are powerful
in this dialect.
- $ suffix in variable names are allowed but makes no difference in
variable type (i.e. strings don't require $ and $ won't force
the variable to be a string)
- there is no "exit for" or "exit do"; use goto to break loops
- there is support for variable argument (vararg) functions
- text-block support: multiline template-like string "literal" type with expression inserts
Rationale
Why bracketed arrays?
Consider A[1](2). It clearly means A is an array, A[1] is a member
of the array referencing a function and (2) is the call parameter.
If it was A(1)(2), it could also be interpreted as an
array-in-array situation (equivalent to A[1][2] in the current syntax).
Why no gosub?
Soft reason: size vs. features. Gosub does not make it possible to pass
function parameters while the alternative syntax with def/function does.
To keep the code small, only one syntax is implemented for the same feature,
which is def/function in this case.
Hard reason: the libfawk VM and the host application expects to call a
function called main() within the script. With BASIC this is not the case
since the main program just starts outside of functions. To make this
possible, there is a trick in the compiler - a trick that needs to know
the start of each function, which is not possible with subroutines defined
for gosub.