Stages 81-86

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

Stages 81-86

Post by frank »

Adding string support began in November and in the meantime, along the way several string functions have been added. Recently added are ucase (upper case), lcase (lower case), tcase (title case) and strep (string repeat). Also added (primarily for debugging purposes) is the function refc (reference count).

Key with implementing string support is memory management, such as garbage collection or reference-counting. SharpBASIC uses reference counting, which is now being implemented and tested. The following example (which is key to the success of reference-count implementation) demonstrates what happens with a second reference (a parameter) if the first reference changes. The objective is that the second reference does not change and instead becomes the new owner of the string that was passed. This was successfully tested during stage 86:

Code: Select all

decl func utcase(t: str): str;

dim s: str;

main do
  s = "HELLO world!";
  print(utcase(s));             ' Hello World
  print(s);                     ' not hello (changed in function)
end;

' user-defined tcase
func utcase(t: str): str
do
  print(refc(t));               ' 2
  s = "not hello";              ' change string referenced by t
  print(refc(t));               ' 1
  utcase = tcase(lcase(t));     ' t not changed
end;
The reference-count system is not finished yet; it is a long, step-by-step process of taking into account life-time and everything else that can happen to a string, including functions passing strings as function result directly to other functions, as in the example above.
Keep it simple!
Post Reply