Statements
DQ statements are separated by newlines or semicolons. Blocks normally use
: plus an end... keyword, and may also use braces.
For complete loop, transfer-of-control, and exception rules, see Statements and Control Flow.
Variable Declaration
var x : int = 0
var y : int
var registers : ? = ^SRegisters(address)
var inferred_array : [?]int = [1, 2, 3]
? requests restricted inference from an initializer whose type is a concrete
typed pointer, structure, or object reference. It cannot infer numeric or other
types and cannot be used without =.
Local reference variables use ref.
var value : int = 10
ref alias = value
alias = 20 // value is now 20
Constant Declaration
const DEFAULT_PORT : int = 8080
const REGISTERS : ? = ^SRegisters(0x40000000)
Constants must be initialized with compile-time constant expressions.
Constants with a common type can be grouped using
const(type): ... endconst; the group does not introduce a new scope.
Assignment
x = 1
x += 1
arr[i] = x
obj.property = value
Assignment is not an expression.
If
if condition:
DoSomething()
elif other_condition:
DoOther()
else:
DoFallback()
endif
Conditions must be bool. Numbers are not compatible with bool, comparisons must be used.
While
while i < limit:
i += 1
endwhile
break leaves the nearest loop. continue continues with the next iteration.
For
DQ numeric for loops have several forms.
for i : int = 0 to 5:
PrintInt(i)
endfor
for i : int = 5 downto 1:
PrintInt(i)
endfor
for i : int = 0 count 5:
PrintInt(i)
endfor
for i : int = 10 downcount 5:
PrintInt(i)
endfor
for i : int = 0 while i < 10 step 2:
PrintInt(i)
endfor
The loop variable may be declared in the loop header or may refer to an existing variable.
var i : int = 0
for i = 0 to 10 step 2:
// ...
endfor
step must be positive for to, downto, count, and downcount forms. The
while form may use a negative step when the condition is written accordingly.
Return
return
return value
Functions with a return type may either return value or assign to the built-in
result variable.
function Square(x : int) -> int:
result = x * x
endfunc
Break and Continue
while true:
if Done():
break
endif
if SkipThis():
continue
endif
endwhile
break and continue apply to the nearest enclosing loop.
Try, Except, Finally
Exceptions are handled with try, except, and finally.
try:
MayRaise()
except Exception as e:
e.PrintMessage()
finally:
Cleanup()
endtry
finally runs when control leaves the try, including normal completion,
exception propagation, return, break, and continue.
Multiple except clauses may be used to handle different exception object
types. An exception can be raised with raise.
raise Exception("message")
Use in Methods
Object methods may use a restricted local use statement to bring selected
module-scope names into method lookup.
function OThing.Method():
use .
use helper_module
endfunc
This form is only valid in object methods. See Modules.
Compound Brace Statements
Many block statements can use braces instead of : and end....
if ok
{
Run()
}
The colon form is the canonical form used by most DQ code.