Skip to content

Syntax Reference

The syntax defines the rules of the language. Only by following these rules can the compiler understand your program. Coldwave Script is inspired by BASIC but incorporates many elements from IEC‑61131‑3 (Structured Text). As a result, the syntax is clear but strict: every statement must be properly terminated.

Program Structure

vb
PROGRAM  
' Declarations  
' Instructions  
END PROGRAM

Important: Without END PROGRAM, the program will not run. Inside the program, variables can be declared, function blocks instantiated, and instructions executed.

Declarations

Variables with DIM:

vb
DIM counter AS INT = 0  
DIM T AS TON

Arrays with DIM … AS T[N]:

vb
DIM a AS INT[3] = {10, 20, 30}   ' list initialiser
DIM b AS INT[4] = 0              ' broadcast initialiser

The length N is a compile‑time positive integer constant. See the Data Types chapter for element types, initialiser rules, and bound checks.

Constants with CONST:

vb
CONST PI AS REAL = 3.14159

Typical mistake: Using a variable before it is declared.

Assignments

For variables and block parameters: =

vb
counter = counter + 1
T(IN = TRUE, PT = T#1000ms)
a[i] = value                   ' array element assignment, 1‑based index

For block declarations in global scope: :=

vb
DIM T AS TON(IN := TRUE, PT := T#1000ms)

Function Calls

vb
PRINT ABS(-5) ' Output: 5  
PRINT MAX(3,7) ' Output: 7

FUNCTION ADD2(A AS INT, B AS INT) AS INT  
    ADD2 = A + B          ' return value: assign to function name
END FUNCTION

Block Calls

vb
DIM T AS TON  
T(IN = TRUE, PT = T#500ms)  
PRINT T.Q

Parameters are named (IN := …). Order does not matter.

ALIAS

ALIAS allows mapping long device addresses or block ports to easier-to-read identifiers.

IO Aliases

vb
ALIAS %I0 AS StartButton
ALIAS %Q1 AS MotorRelay

PROGRAM
    IF StartButton THEN
        MotorRelay = TRUE
    END IF
END PROGRAM
  • %IX0.0, %QX0.1 are device/fieldbus addresses (TOKEN_IO).
  • StartButton and MotorRelay can then be used like normal variables.

Aliases for Block Ports

vb
PROGRAM
    DIM T AS TON
    ALIAS T.Q AS TimerDone

    T(IN = StartButton, PT = T#5s)
    IF TimerDone THEN
        MotorRelay = TRUE
    END IF
END PROGRAM

Aliases do not change program behavior; they only improve readability.

Control Structures

IF / ELSEIF / ELSE

vb
IF x > 10 THEN  
    PRINT "large"  
ELSEIF x > 5 THEN
    PRINT "medium"
ELSE  
    PRINT "small"  
END IF

FOR

vb
FOR i = 1 TO 5  
    PRINT i  
NEXT i

Iterating over an array:

vb
DIM a AS INT[3] = {10, 20, 30}
DIM i AS INT
FOR i = 1 TO 3
    PRINT a[i]
NEXT

The number of iterations is clear from the syntax—important for reasoning about runtime.

DO / WHILE / UNTIL

There are four valid forms:

vb
' Condition before the block (pre-test)
DO WHILE cond
    ' Instructions
LOOP

DO UNTIL cond
    ' Instructions
LOOP

' Condition after the block (post-test)
DO
    ' Instructions
LOOP WHILE cond

DO
    ' Instructions
LOOP UNTIL cond

A DO ... LOOP without a condition is treated as an infinite loop and is not allowed.

EXIT / CONTINUE

vb
EXIT        ' leave the innermost surrounding loop
CONTINUE    ' skip the rest of the current iteration
  • EXIT is allowed inside FOR, DO WHILE, DO UNTIL, LOOP WHILE, and LOOP UNTIL.
  • CONTINUE is only allowed inside FOR. Using it inside a DO/WHILE/UNTIL loop is a compile error, because it would make the static iteration bound harder to prove.
  • Using either statement outside a loop is a compile error (SCRIPT_ERR_EXIT_OUTSIDE_LOOP).

SELECT / CASE

vb
SELECT expr
CASE value1
    ' Instructions
CASE value2
    ' Instructions
CASE ELSE
    ' Instructions
END SELECT

or in short form:

vb
SELECT expr
    CASE 0: PRINT "Off"
    CASE 1: PRINT "Automatic"
    CASE ELSE: PRINT "Manual"
END SELECT

Error Handling

The VM checks many errors at runtime. Common examples:

  • Division by zero → abort.
  • Invalid type → use TO_*.
  • Block not instantiated → add DIM.

Exercises

Task 1: Program with variable n, print “small” if n < 10, otherwise “large”.

vb
PROGRAM  
    DIM n AS INT = 7  
    IF n < 10 THEN  
        PRINT "small"  
    ELSE  
        PRINT "large"  
    END IF  
END PROGRAM

' Output: small

Task 2: Loop from 1 to 5, print squares.

vb
PROGRAM  
    DIM i AS INT  
    FOR i = 1 TO 5  
    PRINT i * i  
    NEXT i  
END PROGRAM

' Output: 1 4 9 16 25

Task 3: Instantiate TON block and print Q, ET.

vb
PROGRAM  
    DIM T AS TON  
    T(IN = TRUE, PT = T#1000ms)  
    PRINT T.Q, T.ET  
END PROGRAM

' Output: Q=FALSE, ET≈elapsed time up to 1000ms

Summary

  • Every program is inside PROGRAM … END PROGRAM.
  • Variables with DIM, constants with CONST.
  • Assignments: = for variables, := for block parameters.
  • Functions return values; blocks have inputs/outputs.
  • Control structures: IF, FOR, WHILE, DO … LOOP, with EXIT / CONTINUE for loop control.
  • Arrays: DIM a AS T[N] = …, access with a[i] (1‑based).
  • Avoid common errors with checks and clear structure.