Data Types

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:

Data Types

Post by frank »

SharpBASIC supports 21 predefined data types plus a user-defined type (UDT) on 32-bit platforms. The predefined data types can be categorized in numeric types, character or string types and general buffer types. Numeric data types do not have traditional names such as short, long or longint. This is because with each new architecture these names usually have a different word-size. To avoid confusion, SharpBASIC adds the bit-size to numeric and character data types, such as int8 and uint8 for 8-bits signed and unsigned or chr8 and chr16 for 8-bits and 16-bits characters.

The following numeric data types are supported on 32-bit systems:

Code: Select all

bool		- boolean (false (0) or true (-1)
int8		- 8 bits signed integer 
uint8		- 8 bits unsigned integer
int16		- 16 bits signed integer
uint16		- 16 bits unsigned integer
int32		- 32 bits signed integer
uint32		- 32 bits unsigned integer
int			- signed integer, architecture default size
uint		- unsigned integer, architecture default size
real32		- 32 bits (4 bytes) floating point
real64		- 64 bits (8 bytes) floating point
real80		- 80 bits (10 bytes) extended floating point
ptr			- pointer, architecture default size 
Example of numeric data type declarations:

Code: Select all

dim a: int;
dim b, c, d: uint;
dim x, y, z: real32 = 0;
dim p: ptr;
The following character data types are supported:

Code: Select all

str			- null-terminated string
str * n		- fixed length string, not null-terminated
chr8		- 8 bits (ascii) character
chr16		- 16 bits (2 bytes) character
chr32		- 32 bits (4 bytes) character
Example of character data type declarations:

Code: Select all

const msg = "these are character data types";
dim text: str;
dim f: str * 255;
dim c: chr8;
The general string type str is a null-terminated variable length string or a constant string. The fixed-length string str * n has a fixed length of n bytes and is not null-terminated. This means that for fixed-length strings the null-character is a valid character with no special meaning.

The character family, chr8, chr16 and chr32, is particularly useful with arrays for specific text encodings, such as UTF8, UTF16 and UTF32, or any other encoding that fits the appropriate character type. Note that a chr8 array declaration would be the same as a byte-string of type str.

SharpBASIC also supports a general buffer type, which can be declared as either byte (8 bits), word (16 bits) or dword (32 bits). Buffer types are declared with the dim statement and the length of the buffer must be specified similar to the fixed-length string. The difference between a buffer type and a fixed-length string is that a buffer type does not have a descriptor. Basically, a buffer type is the same as a const string type, except the data of a buffer type can be changed. Neither are null-terminated.

Example of buffer type declarations:

Code: Select all

dim bdata: byte * 256;      ' 256 bytes buffer
dim wdata: word * 256;      ' 512 bytes buffer (256 * 2)
dim ddata: dword * 256;     ' 1024 bytes buffer (256 * 4)
Buffer types cannot be part of an expression, nor can they be assigned values with the assignment statement. They can only be accessed using pointers and data can be written to and from them directly by copying memory blocks.

buffer type example:

Code: Select all

dim bdata   : byte * 26;    ' 26 bytes buffer
dim s       : str * 26;     ' 26 bytes fixed length string
dim size : uint8;
dim i: uiter;
dim pb, ps  : ptr;

main do

  ' buffer size
  size = len(bdata);

  ' buffer address
  pb = aof(bdata);

  ' populate buffer
  for i = 0 to size - 1 :++
    do
      stob(pb + i, 65 + i);
    end

  ' fixed length string address
  ps = sadd(s);

  ' copy buffer to string
  cpmem(pb, ps, size);

  ' print string
  print(s);

end
The built-in function typof can be used to determine an identifier’s type. The function returns the following codes:

Code: Select all

01		- bool
02		- int8
03		- uint8
04		- int16
05		- uint16
06		- int32
07		- int
08		- uint32
09		- uint
10		- real32
11		- real64
12		- real80
13		- [reserved]
14		- ptr
15		- str
16		- chr8
17		- chr16
18		- chr32
19		- [reserved]
20		- byte
21		- word
22		- dword
23		- udt (user-defined type)
Example:

Code: Select all

dim s: str = "Hello world!";

main do

  if typof(s) == 15 do
    print(s);
  end

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