Sets

Sets in Luna are special expressions that summarize a collection of constant values ​​and facilitate the managing or assigning data collections.

A set stands in the source code within curly braces {…}. If the set is based on a structure declaration, then sets can be embedded within a lot more subsets.

A set basically consists of a so-called baseset and if necessary other subsets. The first pair of parentheses is called the baseset, in which the values ​​and if necessary. other subsets are. Furthermore, it begins with a numeric data type or structure name. Subsets turn consist only of the parantheses with {…}.

If a set introduced by a structure declaration, the data types specified in the corresponding structure are expected. Structural elements in turn expecting a subset with a matching name structure (nested). See Example 2.

  • Note: Intrinsic data types such as string, memoryblock, graphics, dptr, eptr, sptr are not allowed.

Examples

The following is a baseset in which all values are considered as type „byte“. This also applies to the string which in this case is a collection of individual bytes - the letter - represents.

byte{ 1, 2, 3, "hello" } 

The following is a baseset in which all values ​​are considered as type „uint24“

uint24{ 1, 2, 3 } 

The following is a baseset in which the values are different types. „Which is which“ is defined by a structure declaration.

struct mystruct
  byte myarray(2)
  word value1
  long value2
endstruct
 
mystruct{ { 1, 2 ,3 }, 4, 5 }

Sets defines the records in the program memory (flash) or the allocation of data to a storage area in memory (sram) or eeprom (eram).

Using data sets/values that would otherwise be present individually are grouped together for easier for people to read the source code. Furthermore, they allow only the assignment of whole records with elements of different types „in one go“ to memory areas, eg an array.

sets in flash memory

Example 1

struct mystruct
  byte   myarray(2)
  word   level
  string text[14]
endstruct
 
data table
  mystruct{ { 1, 2 ,3 }, 4, "hello" } 'Text is defined in the structure declaration, so
  mystruct{ { 5, 6 ,7 }, 8, "bello" } 'defined length of "text" filled with blanks.
enddata

The above example is the same as the single statement with db, dw, etc…:

data table
  .db 1, 2, 3
  .dw 4
  .db "hallo         "
  .db 5, 6, 7
  .dw 8
  .db "ballo         "
enddata

Base sets also allow „normal“ data types like byte, word and so on, which of course you little to be gained in flash data objects, but it is possible:

data table
  byte{ 1, 2, 3, "hallo" }
  word{ 0x1122, 0xabcd9 }
enddata

The above example is the same as the single statement with db, dw, etc…:

data table
  .db 1, 2, 3, "hallo"
  .dw 0x1122, 0xabcd9
enddata

Example 2

Base set with structure and subset with structure.

struct pair
  byte   value1
  byte   value2
endstruct
struct mystruct
  word   level
  string text[14]
  pair   bytepair
endstruct
 
data table
  mystruct{ 1, "hello", pair{ 23, 42 } } 
  mystruct{ 2, "world", pair{ 10, 20 } } 
  '         ^     ^      ^    ^   ^
  '         |     |      |    |   + mystruct.bytepair.value2
  '         |     |      |    + mystruct.bytepair.value1
  '         |     |      +- mystruct.bytepair
  '         |     +- mystruct.text
  '         +- mystruct.level
enddata

Assignment to the work memory/Eeprom

An array of a data type is nothing but concatenated memory cells which can further be addressed individually. They can therefore be considered as a whole as well as a continuous block of memory.

Array

Following is an example to demonstrate an array in one go with fixed values:

dim a(7) as byte '8 Elemente
a() = byte{ 11, 22, 33, 44, 55, 66, 77, 88 }
'The array contains for this assignment in the element a (0) is 11, the
'Element a (1) has the value 22, etc.

Note: Watching you have here with the number of items in the set, because the number of the memory location is exceeded by the array be if necessary. subsequent data overwritten. The amount is not directly related to the array. From the perspective of the amount it is just a memory.

It can also be assigned only a part:

dim a(7) as byte '8 Elements
a(3) = byte{ 11, 22 }
'The array contains after this assignment in the element a (3) has the value 11 and in
'Element a (4) the value 22

Pointer/MemoryBlock/String

Also intrinsic data types such as pointers or memory block containing a pointer to a memory area can be described with data. Here, the destination address is read from the pointer / memory block / string and used as a storage target. That e.g. when a memory block or string is the destination of the data of the memory block which corresponds to the memory block or the string is assigned.

Important: It must be ensured that the pointer to the memory block or string is a valid memory block or a destination address has been assigned actually!

dim m as MemoryBlock
m = new MemoryBlock(16) 'Creating a memory block with 16 bytes of memory
if m <> nil then          'Check was whether the generated block of memory allocated.
  m = byte{ 11, "Hallo" } 'Writes the byte 11 and the following bytes of text in the memory block.
end if
dim p as sptr          'Creating a Pointer
dim buffer(15) as byte 'A storage area with 16 bytes create (array)
 
p = buffer(4).Addr      'Assign start address from buffer element (4)
p = byte{ 11, "Hello" }
'the array buffer contains now:
'  buffer(4) = 11
'  buffer(5) = 72  (ASCII "H")
'  buffer(6) = 101 (ASCII "e")
'  buffer(7) = 108 (ASCII "l")
'  buffer(8) = 108 (ASCII "l")
'  buffer(9) = 111 (ASCII "o")