Sage
Tusk Language
Welcome to Sage
Volume (57%) Hide Volume
Topics
Statements

Tusk mirrors Delphi's statement syntax very closely, but this article briefly summarizes the different kinds of statements for easy reference.

Assignment

Tusk uses the := operator for assignment statements.

The left-hand side must be one of the following…
  • a variable: x, value, etc.
  • a dot expression: p.x, Employee.LastName, etc.
  • a subscript expression: a[i], List.Values[Index], etc.
  • a dereference expression: p^, PtrArray[k]^

In addition, the left-hand side must be modifiable (it can't be a constant or a read-only property).

Examples…

i := f(j, j); p^ := q^; a[i] := b[j] as IStringList;

Syntax Diagram


Conditional

If

Tusk uses the if, then, and else keywords for the basic conditional statement.

Examples…

if a < b then Writeln('Too small'); if IsSpecial(k) then begin Inc(SpecialCount); Writeln('special'); end else Writeln('boring');

Syntax Diagram


Case

Tusk uses the case keyword to support the switch statement.

The following details are important…
  • The expression must be an integer, enum, or character;
  • Tusk does not use fall-through;
  • Tusk supports multiple values (using ,);
  • Tusk supports ranges of values (using ..);
  • Tusk offers a novel strict option for case statements.

Examples…

case f(x) of 0..9: Writeln('Single Digit'); 10..99: Writeln('Double Digit'); else Writeln('Triple Digit or more'); end; var yn: TYesNo := IsValid(a, b); case:strict yn of ynUnknown: HandleUnknown(a, b); ynNo: begin Writeln('skipping item'); Inc(SkipCount); end; ynYes: ProcessItem(a, b); end; case n of 0, 1: Writeln('?'); 2, 3, 5, 7: Writeln('prime'); else Writeln('composite'); end;

Syntax Diagram


Loop

While

Tusk supports while loops with the while keyword.

Examples…

while a < b do Inc(a); while not i.EOF do begin Writeln(i.Value); i.Next; end;

Syntax Diagram


Repeat

Tusk uses the repeat and until keywords to support the do-while loop.

Example…

repeat Success := TryProcess(Item); Writeln(Success); until not Success;

Syntax Diagram


For Range

Tusk uses the for, to, downto, and do keywords to support for loops.

The following details are important…
  • The loop variable must be an integer, enum, or character;
  • The start and stop values are evaluated once, before executing the first iteration of the loop;
  • The loop variable is incremented or decremented by one each iteration;
  • The loop variable may not be modified within the loop;
  • A new loop variable may be declared using the var keyword (the variable is in scope only within the loop).

Examples…

var ValidCount := 0; var k: Char; for k := Low(Char) to High(Char) do if IsValidChar(k) then Inc(ValidCount); for var i := 1 to 100 do Writeln(i); for var Index: Int64 := HighIndex downto 0 do begin DoSomething(Index); Inc(Count); end;

Syntax Diagram


For In

Tusk uses the for, in, and do keywords to support for-each loops.

The following details are important…
  • The loop variable may not be modified within the loop;
  • The container expression may be: a string, an AnsiString, a set, an array, or a suitable interface or record;
  • A new loop variable may be declared using the var keyword (the variable is in scope only within the loop).

Examples…

var a := TArray<string>.Create('hello', 'goodbye', 'yes', 'no'); var s: string; for s in a do Writeln(s); for Reader in ReadOnlyDB.RunReader(Query) do begin Writeln('Date: ', Reader['trd_date'], '; Qty: ', Reader['qty']); ProcessTrade(Reader); end;

Syntax Diagram


Control

Break

Tusk uses the Break keyword to terminate a loop early.

Examples…

for i := 1 to 100 do begin Writeln(i); if NotValid(i) then Break; end; while a < b do begin a := f(a); b := f(b); if b < 0 then Break; end;

Syntax Diagram


Continue

Tusk uses the Continue keyword to jump to the next loop iteration.

Example…

for Reader in ReadOnlyDB.RunReader(Query) do begin if ShouldSkip(Reader['customer_id']) then Continue; Writeln('Date: ', Reader['trd_date'], '; Qty: ', Reader['qty']); ProcessTrade(Reader); end;

Syntax Diagram


Exit

Tusk uses the Exit keyword to implement the return statment.

Examples…

procedure Work; begin for var i := 1 to 10 do begin if f(i) < 0 then Exit; Writeln(i); end; Writeln('complete'); end; function GetDescr(i: Integer): string; begin if i < 0 then Exit('Negative'); Result := IntToStr(i); if i < 100 then Exit; Result := Result + ' (large value)'); end;

As demonstrated above, Exit can be used without an argument (in procedures and functions) or with an argument (in functions only).

Syntax Diagram


Try/Finally and Try/Except

Tusk uses the try, finally, and except keywords to implement exception handling.

Examples…

Setup; try Work1; Work2; Work3; finally Cleanup; end; try Work; except Writeln('Work failed'); raise; end;

Raise

Tusk uses the raise keyword to raise exceptions.

Examples…

try Work; except Writeln('Work failed'); raise; end;

Important

Tusk does not support classes yet, and in Delphi, all exceptions are objects derived from the Exception class. Therefore, Tusk does not yet support catching exceptions, or raising new exceptions with the raise keyword. Instead, use routines like DSGenUtil.DSRaise to raise exceptions in Tusk.


Syntax Diagram


Blocks

Tusk uses the begin and end keywords for compound statements.

Syntax Diagram


Last Modified: 2/15 10:06:11 am
In this article (top)  View article's Sage markup
2/15 10:06:11 am