Data sections

A DataSection holds constant values in the program itself: tables, lists of names, messages. Read takes them out one at a time, in order.

DataSection, Read, Restore ✅

DataSection
  Primes:
    Data.i 2, 3, 5, 7, 11
  Names:
    Data.s "Ada", "Grace", "Linus"
  Mixed:
    Data.l 100
    Data.d 2.5
EndDataSection

OpenConsole()
Restore Primes
total = 0
For i = 1 To 5
  Read.i p
  total + p
Next
PrintN(Str(total))

Restore Names
For i = 1 To 3
  Read.s name$
  PrintN(name$)
Next

Restore Mixed
Read.l count.l
Read.d factor.d
PrintN(StrD(count * factor))

Output:

28
Ada
Grace
Linus
250
  • Data.type value, value, … stores constant values of one type. Any type works: .b, .a, .w, .u, .c, .l, .i, .q, .f, .d, .s.
  • A label (Name:) marks a place in the data.
  • Restore Label moves the read position to that label.
  • Read.type variable reads the next value into a variable, and moves the read position past it.
  • The type after Read is the type of the value being read, not of the variable: Read.d factor reads a double, but if factor is new it is still an integer (and 2.5 is rounded into it). Declare the variable with its type (Read.d factor.d) or beforehand.
  • Without a type, Read reads an integer, as in PureBasic, except that a string variable reads a string.
  • The type should match what was stored: the data holds no type information.

Before any Restore, reading starts at the first data in the program:

DataSection
  Data.s "red", "green", "blue", ""
EndDataSection

OpenConsole()
Repeat
  Read.s colour$
  If colour$ <> ""
    PrintN(colour$)
  EndIf
Until colour$ = ""

Output:

red
green
blue

The data in memory

Values are stored one after another, each at its own size, with no padding. Strings are stored as their characters (UTF-16) followed by a 0. This is the same layout as PureBasic's, so ?Label gives the address of the data at a label, for use with the memory functions:

DataSection
  Table:
    Data.l 10, 20, 30
EndDataSection

OpenConsole()
*p = ?Table
PrintN(Str(PeekL(*p + 4)))

Output:

20

The data is in a writable part of the program, so it can be changed through such a pointer.

Addresses in data ✅

Data.i (or Data.q) can also hold addresses: @Procedure() for a procedure, ?Label for a data label or a label in the main code. A table of procedures can then be read and called through a prototype, and data can point to other data. A label can be used before the line that defines it.

Procedure Double(x)
  ProcedureReturn x * 2
EndProcedure
Procedure Square(x)
  ProcedureReturn x * x
EndProcedure
Prototype.i Op(x)

DataSection
  Ops:
  Data.i 2, @Double(), @Square()
  MessagePtr:
  Data.i ?Message
  Message:
  Data.s "done"
EndDataSection

OpenConsole()
Define f.Op
Restore Ops
Read.i count
For i = 1 To count
  Read.i p
  f = p
  PrintN(Str(f(7)))
Next
Restore MessagePtr
Read.i m
PrintN(PeekS(m))

Output:

14
49
done

IncludeBinary ✅

DataSection
  Logo:
    IncludeBinary "images\logo.png"
  LogoEnd:
EndDataSection

IncludeBinary copies a whole file into the data. ?LogoEnd - ?Logo is its size. The file name is relative to the source file containing it, or to the main source file.

Labels

Data labels are visible everywhere, including inside procedures, so Restore works in any procedure. Their names are separate from code labels (the ones Goto and Gosub use).