User Tools

Site Tools


statements

Statements

There are two kinds of statements, single statements and compound statements. Single statements always end with a semicolon, which is called a statement terminator.

Single Statement

A single statement is a stand-alone statement that can be coded on a single line or over multiple lines and ends with the statement terminator.

example of a single line statement:

print("Hello World");

example of a multi-line statement:

a = (x + y)
  * (z - 4);

Compound Statement

A compound statement is a statement that can enclose other statements. In SharpBASIC there are two kinds of compound statements, the execution compound statement (ECS), marked by the keyword delimiters do..end and the declaration-definition compound statement (DCS), marked by the keyword delimiters is..end.

do..end

Easy to remember is that an ECS can only appear in the main section, in subroutines and in functions:

main do
  if x > y do
    x = y;
  end
  print(x);
end

In this example, the first do..end block marks the compound main statement. The second do..end block marks the compound if statement. The if statement contains the single statement x = y;. The main statement contains the compound statement if and a single statement print.

Keep in mind that do..end delimiters are not free to use (nor is there any need to); they are always used in combination with a keyword that marks the type of ECS, such as main, if, sub and func.

is..end

Also easy to remember is that a DCS can only appear in the declaration section, or in subroutines and in functions above their ECS. In general, a DCS always comes before an ECS:

' prologue section
struc Point is
  x, y, z: int;
end

dim p: Point;

' main section
main do
  p.x = 3;
  p.y = 5;
  p.z = 9;
end

In this example, the DCS struc is in the prologue section, followed by a single line dim statement. The main section, which is an ECS, encloses three single line assignment statements.

While a DCS such as struc or enum requires is..end delimiters, any single declaration statement such as const, dim or decl can optionally be turned into a DCS, which can be useful to group multiple declarations:

const is
  msg1 = "Welcome";
  msg2 = "This is SharpBASIC";
end

dim is
  a: uint8;
  x, y, z: int;
  s: str;
end

But SharpBASIC is even more flexible if it comes to declaration statements. For example, any declaration statement can be module level, global or external and the is..end delimiters can be very useful in grouping them. For example:

global decl sub  mySub1();
global decl sub  mySub2();
global decl func myFunc1(): int;
global decl func myFunc2(): int;
can be turned into:
global decl is
  sub  mySub1();
  sub  mySub2();
  func myFunc1(): int;
  func myFunc2(): int;
end
And this grouping can be extended even further:
global is
  decl is
    sub  mySub1();
    sub  mySub2();
    func myFunc1(): int;
    func myFunc2(): int;
  end
  const is
    msg1 = "Welcome";
    msg2 = "This is SharpBASIC";
  end
  dim is
    a: uint8;
    x, y, z: int;
    s: str;
  end
end

Similarly, DCS delimiters can be used to group declarations in subroutines and functions:

sub mySub1()
  dim is
    a, b, c: uint;
    x, y, z: int = 0;
    i, j: uint8;
  end
do
  '...
end

The one exception that a DCS can appear inside an ECS is the let..on..is assignment statement.

Finally, very easy to remember is that in SharpBASIC the keyword end is never followed by a semicolon, no exception! This is because the keyword end is considered a compound statement terminator.

statements.txt · Last modified: 2023/06/25 10:13 by admin