SD Core for Windows WC2.0-0

SD Basic - Program Structure

Programs, subroutines and functions; variables, matrices, common blocks and how a program is called.

This page covers the shape of an SD BASIC program: how it is declared, how it is called, what its variables are, and the declarations that have to appear before anything else.

SD folds case, so a program may be written in either case. Keywords are shown here in lower case. In the tables, italics mark something you supply and bold marks a word typed as it stands; braces mark an optional part.

The three kinds of program#

program {name}
subroutine name {(argument, ...)} {var.args}
function name {(argument, ...)} {var.args}
program runs on its own. Started by run, by its cataloged name, or from another program with execute
subroutine called by call, and returns nothing. Its arguments carry values back
function called inside an expression, and returns a value with return

A source record with no declaration at all is a program. end closes it.

program hello
   print 'hello'
end

var.args allows the caller to pass fewer arguments than are declared; arg.count() then says how many actually arrived, and reading one that was not passed is an unassigned-variable error rather than a silent empty string.

Local subroutines and functions#

local subroutine name {(arguments)}
local function name {(arguments)}

These live inside the program that uses them rather than in the catalog, and they can declare private variables of their own — which is how you get a routine whose working variables do not collide with the rest of the program.

They are not called the way external ones are. A local subroutine is reached with gosub name(…) and not call; a local function must be declared with deffun … local before it is used; and both need an explicit return, because falling into the closing end stops the program. All of that, with the scope rules and the errors each mistake produces, is on SD Basic - Modern Program Structure.

Internal subroutines#

gosub label
...
label:
   ...
   return

gosub is not a call to another program — it jumps within the current one, and shares all its variables. The nesting limit is 256 levels.

Calling#

call name {(argument, ...)}
call @variable {(argument, ...)}
call name the name is fixed at compile time
call @variable the name is taken from the variable at run time

There is no function form of call (subr() is gone). Where you want a result in an expression, declare a function with deffun and call that.

There is no call by file and program name (call "bp prog"): a program is called by its cataloged name.

Arguments are passed by reference, not by value. A subroutine that assigns to one of its parameters changes the caller's variable. That is how a subroutine returns anything at all, and it is also how a subroutine that uses a parameter as scratch space quietly corrupts its caller's data. If you do not intend to return a value through an argument, copy it into a local variable first.

Passing an expression rather than a variable — call sub(a + 0) — forces a copy, because there is no variable to write back to.

catalogued(name)

reports whether a name can be called. catalogued('ZZNOSUCH') is 0. Use it before a call @variable whose name came from data, because an uncataloged name is a runtime abort rather than a testable failure.

Variables#

Variables are not declared and have no type. A variable holds whatever was last assigned to it, and its type follows the value.

assigned(variable)
unassigned(variable)
vartype(variable)
Expression Result
assigned(neverset) 0
assigned(zz) after zz = 1 1
unassigned(neverset) 1

The compiler warns about a variable that is never assigned — "WARNING: NEVERSET is not assigned a value" — but it still compiles, and using the variable is an error only when the line is reached.

vartype()#

Returns the internal type code:

Value Code
unassigned 0
an integer 2
a floating-point number 3
a string 5

The remaining codes exist for things a program does not usually inspect: 1 address, 4 subroutine reference, 6 file variable, 7 array, 8 common block, 9 image, 11 select list, 13 socket, 15 object.

vartype() tells you how the value is held, not what it means. '12' read from a file is a string (5) even though it is a number everywhere it is used; the same value after arithmetic is an integer (2). Use num() to ask whether something is numeric — see SD Basic - Math Functions — and keep vartype() for telling an unassigned variable from an empty one.

set.unassigned returns a variable to the unassigned state, which is how you make assigned() false again after using it.

Constants#

equate name to value

equate is resolved at compile time and costs nothing at run time. It can name a literal, another variable, or a matrix element:

equate true to 1
equate esc to char(27)
equate cust.name to record<3>

An equate is a substitution, not a variable. You cannot assign to one, and it is replaced wherever the name appears. equate cust.name to record<3> means cust.name reads field 3 of whatever record holds at that moment.

$include file.name record.name

pulls in another source record at compile time. The system definitions live in syscom — $include syscom keys.h for the key numbers used by fileinfo(), selectinfo() and the rest.

Where an $include with no file name looks. Source file first, then syscom. A record $basic.options (type x, in the file that holds the source or, failing that, in the VOC) can change that with a line search file.list, the files separated by commas (or spaces) and tried in that order, @ standing for the file the source is in:

x
search incfile,@

A file in the list that does not exist, or does not hold the record, is passed over; the include is an error only when the last has not got it either.

Matrices#

dim name(rows {, columns})
mat name = expression
mat name = mat other.name

A matrix is a fixed set of numbered variables, not a dynamic array. dim must appear before any use, and the bounds are fixed at that point.

dim m(3)
mat m = 7          ;* every element becomes 7

After mat m = 7, m(1), m(2) and m(3) all read 7.

A matrix has a zero element and it is not counted in the dimension. dim m(3) gives m(0) through m(3). m(0) is where matparse puts anything left over, and it is easy to forget it exists.

Between a matrix and a string#

matbuild variable from matrix {, start {, end}}
matparse matrix from string, delimiter
inmat({matrix})
matbuild joins the elements into one string
matparse splits a string across the elements
inmat() after matparse, how many elements were filled

With m holding 7, 7, 7:

matbuild bs from m the three elements joined with field marks
matbuild bs from m, 1, 2 the first two only
matparse m from 'x' : @vm : 'y' : @vm : 'z', @vm then m(1)…m(3) x y z
inmat() after that 3

The using clause of matbuild does not work, in either case. The documented syntax matbuild var from mat using delimiter compiles the keyword as a variable name and the program aborts at run time with "Unassigned variable USING", with both using and USING.

The delimiter is always a field mark. If you need another one, use matbuild and then change(), or build the string with a for loop.

matparse splits on the delimiter you give and stops when the matrix is full; anything left over goes into element zero.

Common blocks#

common {/name/} variable, ...
clearcommon
delete.common name

Variables in a common block survive chain and are shared between programs that declare the same block. An unnamed common block — common // a, b, c — belongs to the program run and is cleared when it ends; a named one persists for the session.

// is the unnamed common marker, which is why it is not an integer-divide operator. See SD Basic - Math Functions.

clearcommon sets every unnamed common variable to zero. delete.common discards a named block entirely, which is the only way to reclaim it within a session.

A common block is matched by name and position, not by variable name. Two programs declaring the same block with different variable lists will read each other's data at the wrong offsets, silently. Put the declaration in an include record and $include it everywhere, so there is one copy.

Class modules and objects#

SD BASIC has class modules with member variables, methods, properties, constructors, destructors and inheritance. They have their own page, because they are a different way of writing a program rather than a feature of this one: SD Basic - Modern Program Structure.

The short form:

class name {inherits base}
private variable, ...          public variable, ...
public subroutine name(...)    public function name(...)
get name                       set name(value)
end
o = object('CLASSNAME')
o->method(args)
o->property = value

SD's own client library is a class module. gpl.bp/sdclient has public members and both lifecycle hooks — connecting to another SD server from BASIC means instantiating it.

Arguments#

arg.count()
arg(n)
set.arg n, value

These read the arguments of the command line that started the program, not the arguments of a subroutine call. arg(1) is the first word after the verb.

Arguments that may be left out#

subroutine name(a, b = 7, c = 'none') var.args
function name(a, flag = @true) var.args
arg.present(name)
reusing(name)

An argument may be given a default value: a number (a minus sign allowed), a string in single or double quotes (not one that holds both kinds), @true or @false. The program then needs var.args, or the compiler refuses it; a default that is anything else, such as a variable or an expression, is A default argument value must be a number, a string, @TRUE or @FALSE. When the caller passes fewer arguments than there are, the missing ones take their defaults. arg.count() is still the number the caller passed, not what the defaults make of the rest. The program's own lines keep their line numbers.

arg.present(name) is true if the caller passed that argument, whether or not it has a default now. It names an argument of the subroutine or function it is in; any other name, or use outside one, is a compile error (xx is not an argument of this subroutine or function). reusing(name) is true if the caller passed the argument with reuse(), and does not clear it.

Case sensitivity of strings: casing#

casing on
casing off
casing expression

Switches case sensitivity of string comparisons and the string functions for the rest of the running program, the way $nocase.strings does for the whole of it. system(1086) says which is in effect. A casing with nothing after it is a compile error.

Discarding a result#

void expression

Calls a function and throws the answer away. It exists because SD BASIC has no statement form for a function call — void kernel(...) is the idiom you will see throughout gpl.bp.

Compiler modes: $mode#

$mode name {, name …}
$mode -name

A mode changes how one program is compiled. Every mode is off unless the program asks for it, and a program that does not ask sees no change. A name with a minus in front turns the mode off again. The ones below are the 2.0-0 additions; each is described by what it does and does not do.

mode what it does
partial.expressions A and B does not evaluate B when A is false, and A or B does not evaluate B when A is true. That makes if idx matches '1-2N' and s<idx> # 0 safe, and a function with a side effect on the right-hand side runs only when it is needed. The value of every expression is the same as without the mode; only what is evaluated changes
debug.equates a program compiled for the debugger also keeps its equate and $define tokens in the object, so the debugger can show, set and watch them (see Debugging). It makes the object a little larger; programs compiled without the mode are as they were
explicit every scalar has to be named in a dim statement before it is used, or the compiler refuses the program with Variable xx has not been declared
trap.unused.main like trap.unused, but it warns only about a variable that is assigned a value in the program itself, not in an include record. Both warn only of a variable given its value by a plain x = ... and never read: a variable a statement fills (readu, locate ... setting, findstr, input, capturing) is not reported, and a call made only for its effect is written void name(...)
non.durable.txn every commit the program contains is a non-durable commit: a transaction nested in another writes nothing at its commit but hands its records, deletions and locks to the enclosing transaction, which writes them when it commits or throws them away if it aborts. A commit that is not nested writes as ever. See SD Basic - Locks and Transactions

Options that change how SD runs a program#

option on its own lists every option and its state; option name on or off sets one (see SD Basic - Numeric Tests and Formatting). All of these are off by default.

option what it does
inherit.ownership create.file gives a new file (the directory and everything in it) the owner and group of the account directory in place of those of the user who made it. A user may only hand a file to a group they belong to, which the account's own group is, and not to another owner: where the system refuses nothing is said, as in openQM. Off by default, and the group it gives is the account's, so its members then have on the file what they have on the account: turn it on for an account in its login paragraph, as an administrator's decision
git a directory file keeps .gitignore and .gitattributes under their own names on the disk. Without it the leading dot of every such name is replaced by %D, which git does not read. Only these two names: any other name with a leading dot is mapped as ever
dir.dtm writing a record to a directory file moves the directory's date and time modified. The system moves it only when a name is added or removed, so a replaced record leaves it as it was. Closing a written sequential file does not do this
spool.command shows the operating system command that prints a job, just before it runs; a diagnostic aid

Changing the size of a matrix passed in: redim#

redim name(rows {, cols})

Inside a subroutine, redim gives a matrix argument (mat a) a new size, and the caller's matrix takes that size: what the subroutine stores in it, in the old elements and in the new ones, is there when the call returns.

subroutine grow(mat a)
   redim a(10)
   a(8) = 'eight'
   return
end

A matrix argument needs no dim of its own in the subroutine, in a local subroutine either: it is the caller's matrix, with the caller's size, until a redim changes it.

What is not here#

Nothing in the program-structure group has been removed from this port. The package statements — enter.package, exit.package and package — went before SD Core for Windows, and varset is present but is an internal debugging aid rather than something application code uses.

See also#

SD Basic - Program Control · SD Basic - Dynamic Arrays · SD Basic - File Handling · SD Basic - Math Functions.