Stage 113-118: UDTs and shortcuts (with statement)

Follow the development of the SharpBASIC compiler and share your thoughts and ideas.
Locked
User avatar
frank
Site Admin
Posts: 40
Joined: Sun Nov 21, 2021 12:04 pm
Location: Netherlands
Contact:

Stage 113-118: UDTs and shortcuts (with statement)

Post by frank »

Further support has been added for buffer types and records. SharpBASIC now supports functions of a user-defined type (currently fixed-length records, but structures will be fully supported too). See the documentation about functions.

Implementation of user-defined types usually goes hand in hand with support for a 'with' or 'using' statement. SharpBASIC supports up to three levels of nested with statements. These statements are fully managed by the compiler, i.e. they are not hard-coded at compilation time, except for address calculation of accessed fields. This means that a with statement is purely cosmetic and not affected by something like a jump statement. For more details, see the documentation about the with statement.

Up to the next stage. ;)

Example (from the documentation):

Code: Select all

' SharpBASIC 'with' programming example
' -------------------------------------
incl "lib/sys.sbi";

record A is
  x: int;
  y: int;
end

record B is
  z: int;
  a: A;
end

dim a: A;
dim b: B;

main do

  a.x = 5;
  a.y = 4;
  b.z = 10;
  
  jump @calculate;        ' this is ok
  
  print(b.z);             ' not printed

  with b do               ' block 1 (...)
    with b.a do           ' block 2 (..)
      with a do           ' block 3 (.)
        @calculate;
        ..x = .x * ...z;
        ..y = .y * ...z;
      end
    end
  end

  with b do
    with .a do
      print(.x);
      print(.y);
    end
    print(.z);
  end

end
Last edited by frank on Wed Jul 12, 2023 4:29 pm, edited 1 time in total.
Keep it simple!
Locked