Skip to content
Adam
Language

Statements

Loops, match, defer, exceptions and sequences. Every statement ends with ;, and there is no empty statement.

Examples: res\examples\language\statements holds small, commented programs for this page's features, each listing the output it prints; run one with adam build <file> -r.

Loops

for I := A to B do .. end; and downto step a variable declared before the loop; while tests before each pass and repeat .. until after it. leave ends the innermost loop or match, and skip goes on to the next pass.

statements_loops.adam
module exe statements_loops;

// A grade for a score
func Grade(const AScore: int32): string;
var
  LGrade: string;
begin
  match AScore of
    90..100:
      LGrade := "A";
    75..89:
      LGrade := "B";
  else
    LGrade := "C";
  end;
  return LGrade;
end;

// defer runs when its block ends, the last one first
func Steps();
begin
  defer println("deferred 1");
  defer println("deferred 2");
  println("body");
end;

var
  GI: int64;
  GTotal: int64;
  GWord: string;
  GLine: string;

begin
  GTotal := 0;
  for GI := 1 to 10 do
    if GI mod 2 = 0 then
      skip;
    end;
    if GI > 7 then
      leave;
    end;
    GTotal += GI;
  end;
  println("odd sum below 8: {}", GTotal);
  GI := 3;
  while GI > 0 do
    GI -= 1;
  end;
  repeat
    GI += 2;
  until GI >= 5;
  println("repeat stopped at {}", GI);
  GLine := "";
  for GI := 3 downto 1 do
    if GI < 3 then
      GLine := GLine + " ";
    end;
    GLine := GLine + FormatStr("{}", GI);
  end;
  println("{}", GLine);
  println("{} {} {}", Grade(95), Grade(80), Grade(10));
  GWord := "stop";
  match GWord of
    "go":
      println("going");
    "stop":
      println("stopping");
  end;
  Steps();
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
odd sum below 8: 16
repeat stopped at 6
3 2 1
A B C
stopping
body
deferred 2
deferred 1

Match

match picks the arm whose label equals the value. A label may be a choice value, a range (90..100:) or a list of labels; else takes every other value, and a string subject takes string labels, compared exactly. A match on a choices value with no else that leaves a named value out is a warning naming it.

Defer

defer S runs S when the statement list holding it ends, by any way out; several defers run the last one first.

Exceptions

raise leaves a guard's body for its except, where excmsg() gives the message; finally runs on every way out.

statements_guard.adam
module exe statements_guard;

// AValue, when it is not negative
func Checked(const AValue: int32): int32;
begin
  if AValue < 0 then
    raise("negative value");
  end;
  return AValue;
end;

begin
  guard
    println("{}", Checked(4));
    println("{}", Checked(-1));
    println("not reached");
  except
    println("caught: {}", excmsg());
  finally
    println("finally runs");
  end;
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
4
caught: negative value
finally runs

Sequences

A routine whose result type is sequence of T yields its values one at a time with yield, and for .. in takes them as they come. for X in S also walks an array, a string, a wstring and a set, and yield from S hands on every value of S. A bare bracket literal walks in written order as an array of X's type: for LN in [3, 1, 2] do gives 3, 1, 2.

statements_sequences.adam
module exe statements_sequences;

// The first ACount squares, made one at a time
func Squares(const ACount: int32): sequence of int32;
var
  LI: int32;
begin
  for LI := 1 to ACount do
    yield LI * LI;
  end;
end;

var
  GValue: int32;

begin
  for GValue in Squares(5) do
    println("{}", GValue);
  end;
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
1
4
9
16
25

Ending a program

In an exe's body return E; ends the program with E as its exit code.

↑ ↓ moveEnter openEsc close