Code Blocks - is..end, do..end

information about SharpBASIC language elements
Post Reply
User avatar
frank
Site Admin
Posts: 51
Joined: Sun Nov 21, 2021 12:04 pm
Location: Netherlands
Contact:

Code Blocks - is..end, do..end

Post by frank »

Easy to remember is that in SharpBASIC all program flow is placed within do .. end blocks, be it in the main section directly, with control flow statements or in subroutines or functions.

Code: Select all

main do
  print(msg);
end
The counterpart of the do .. end block is the is .. end block. Only declarations and definitions can occur within an is .. end block. Easy to remember is that is .. end blocks always precede do .. end blocks, be it in the prologue section before the main do .. end block, or before the do .. end block of a subroutine or function to declare local variables. The one exception to this rule is the let..on..is-statement.

SharpBASIC is quite flexible if it comes to is .. end blocks. They can be used with virtually any declaration to make life easier for the programmer. For example, instead of repeatedly using the dim statement:

Code: Select all

dim is
  x, z: int = 1;  ' x and z initialized as 1
  y: int;
  ' ...
end
The same code structure can also be used with decl and const declarations. Note that is .. end blocks can be used in combination with the global and extern directives too. If we would want to declare x and y as global to make them accessible in other modules, we could write:

Code: Select all

global dim is
  x, z: int = 1;
  y: int;
  ' ...
end
or, to enclose more than just the dim declarations:

Code: Select all

global is
  
  decl is
    func Times2(value: int): int;
    ' ...
  end
  
  const is
    msg = "Hello, World!"
    ' ...
  end

  dim is
    x, z: int = 1;
    y: int;
    ' ...
  end

end
Last edited by frank on Wed Mar 09, 2022 2:53 pm, edited 3 times in total.
Keep it simple!
Post Reply