ScriptBasic Syntax

Started by root, May 12, 2026, 02:28 PM

Previous topic - Next topic

root

This thread will show some of the enhanced language features of ScriptBasic. With VBScript being deprecated by Microsoft, ScriptBasic is the only embedded BASIC scripting language available going forward.

Variables

In ScriptBasic, you don't declare types — the interpreter assigns and converts them automatically based on usage, making it dynamically typed but with predictable conversion rules. Variables are stored as a variant with the following internal types.
  • undef - Indicates the variable hasn't been assigned a value yet.
  • integer - Indicates this value is a whole number with a integer type.
  • real - Indicates a real floating point type.
  • string - Indicates a ScriptBasic string that can contain embedded nulls.
  • stringz - Indicates a null terminated C style string.
  • array - Indicates an array that could be indexed based, associative or a combination of both.

ScriptBasic string and array size is only limited to available memory. (WoW64 32 bit addressing)

These examples show how type is assigned at use. The & symbol indicates concatenating of strings.

a = 1
b = 2
c = "3"
d = "4"

PRINT a + b
PRINT a & b
PRINT c + d
PRINT c & d

Output:

3
12
7
34

The $ character can be used to append string variables and functions. This is optional and used mostly when converting other dialects of BASIC such as ProvideX. I rarely use the STR() and VAL() functions in ScriptBasic.

a$ = "A  String"
a$ = STR$(123)

Arrays

In ScriptBasic, arrays are flexible data structures created automatically as soon as you assign a value to a subscripted variable. They do not require a formal declaration or a fixed size, and they can hold elements of any data type.

Key Characteristics
  • Automatic Creation: There is no DIM statement; arrays are initialized the moment they are used.
  • Dynamic Sizing: Addressable indices increase automatically as needed.
  • Heterogeneous Content: A single array can store different types of data, such as integers, reals, and strings.
  • Uninitialized Elements: Elements that have not been assigned a value return undef.
Standard Arrays: Use square brackets [] for numeric indices.

a[0] = "Hello"
a[1] = 123.45

Multidimensional Arrays: Supported through nested brackets or comma-separated indices.

matrix[1][2] = 50
' or alternatively:
matrix[1, 2] = 50

Associative Mode: ScriptBasic allows arrays to function in an associative mode using curly braces {}.

user{"name"} = "ScriptBasic"
user{"age"} = 26

LBOUND / UBOUND: Used to determine the upper and lower boundaries of an array. An array index traditionally starts a index zero but can begin and end at users choice. Elements between define LBOUND / UBOUND are declared undef.

a[100] = 100
PRINT LBOUND(a)
PRINT UBOUND(a)
a[50] = 50
PRINT LBOUND(a)
PRINT UBOUND(a)

Output

100
100
50
100

Array element segments can be copied to a new array. I use this feature with JSON associative arrays responses to build update requests.

root

#1
The ScriptBasic comment character ' is used as the first character of the line. Comments can't be added to the end of a line. The following example shows creating a multi-line comment. The """ feature also works with multi-line string. I use this extensively with outputting HTML with the ScriptBasic webserver.

' This is a single line comment.

'"""

This is a
multi-line comment

"""

PRINT "Hello, World!","\n"


Labels

ScriptBasic supports labels for GOTO, GOSUB and ON ERROR. I also use them to exit a FOR/NEXT loop with a GOTO.

MyLabel:

ScriptBasic also supports line numbers as a special form of label. If using line numbers from a legacy BASIC, you only need them for GOTO / GOSUB reference.

10 LET A$="Hello World!"
20 PRINT A$,"\n"
30 END

Note: LET is an optional keyword and used with legacy conversions to ScriptBasic.


MODULE

In ScriptBasic, namespaces provide a mechanism to organize code and avoid variable or function name collisions, especially when using external modules. The core command for managing these is MODULE, which defines a specific namespace for subsequent code.

Key Namespace Concepts in ScriptBasic
  • Module Definition: Using the MODULE name statement creates a new namespace. All global variables and functions declared after this statement belong to that namespace until the module is ended or changed.
  • Accessing Members: To call a function or access a variable inside a module from the main program, you use the double colon :: or dot syntax, such as modulename::functionname or modulename.functionname.
  • The Main Namespace: Code that is not inside any MODULE block belongs to the default "main" namespace.
  • External Modules: Extension modules like those for cURL, MySQL and ODBC automatically introduce their own namespaces to keep their internal functions separate from your main script.
  • Nesting: ScriptBasic supports nested namespaces, which are managed internally via a namespace stack during syntax analysis.

This is the COM extension MODULE.

' COM/OLE Automation Extension Module Import

GLOBAL CONST :CALL   = 1
GLOBAL CONST :GET    = 2
GLOBAL CONST :LET    = 4
GLOBAL CONST :SET    = 8

MODULE COM

DECLARE SUB ::CREATE  ALIAS "CreateObject"      LIB "com"
DECLARE SUB ::CBN     ALIAS "CallByName"        LIB "com"
DECLARE SUB ::RELEASE ALIAS "ReleaseObject"     LIB "com"
DECLARE SUB ::GHO     ALIAS "GetHostObject"     LIB "com"
DECLARE SUB ::GHS     ALIAS "GetHostString"     LIB "com"
DECLARE SUB ::SVT     ALIAS "ShowVariantType"   LIB "com"
DECLARE SUB ::TN      ALIAS "TypeName"          LIB "com"
DECLARE SUB ::DI      ALIAS "DescribeInterface" LIB "com"
DECLARE SUB ::CB      ALIAS "SBCallBack"        LIB "com"

END MODULE