စံကား

01The language

Lists

The fixed-size list kinds, literals, cell numbers, lists of lists, passing and comparing, how a list is stored, and the mistakes with cell numbers.

A list holds a fixed number of values of one kind, next to each other in RAM. Each value sits in a cell, and a program reaches a cell through its number. The count of cells is part of the kind and is known before the program runs; a list never grows or shrinks. For a list that changes size, use a block of the heap. The Learn book starts in small steps with Lists.

စံကား

Expected output

[၇၀၊ ၀၊ ၉၀]

List kinds

အကွက် <count> ခုရှိသော <cell kind> စာရင်း

The count comes first, then the kind of each cell, then စာရင်း ("list"). The word ခု and a ၏ in front of စာရင်း are optional, so these four kinds are the same:

Kind
အကွက် ၃ ခုရှိသော ကိန်းပြည့် စာရင်း
အကွက် ၃ ရှိသော ကိန်းပြည့် စာရင်း
အကွက် ၃ ခုရှိသော ကိန်းပြည့် ၏ စာရင်း
အကွက် ၃ ရှိသော ကိန်းပြည့် ၏ စာရင်း
  • The count is a whole number above zero written with digits (ZK0227). It cannot come from a variable, because the size of a list is fixed before the program runs.
  • The cell kind is a kind of each sort: a number, a true/false value, a character, a pointer, or a list. Messages write the kind as [3]isize.
  • A declaration that gives the list a value can leave the count out, and the value tells it. In each other place a missing count is ZK0421.
  • A list takes its default from its cells: ၀ for a number, မမှန် for a true/false value, ဗလာ for an address. A list declared with no value starts with all cells at the default.
စံကား

Expected output

[၀.၀၊ ၀.၀]
[မမှန်၊ မမှန်၊ မမှန်]
[က၊ ခ]

Literals

A list value is written with its cells between ၊ and a closing တို့ပါသော စာရင်း ("the list that holds these"). စာရင်းအလွတ် ("blank list") is a list of the expected kind with each cell at its default.

၇၀၊ ၈၅၊ ၉၀ တို့ပါသော စာရင်း
စာရင်းအလွတ်
  • The number of cells in a literal must equal the count of the kind it fills (ZK0419).
  • စာရင်းအလွတ် needs a list kind from the place it stands in. Standing alone it has none (ZK0420).
  • A literal that is a cell of another literal goes in parentheses.
စံကားZK0419

Expected error

error[ZK0419]: the kind holds 3 cells, but the list has 2
စံကားZK0420

Expected error

error[ZK0420]: a blank list needs a list kind from its place
စံကားZK0421

Expected error

error[ZK0421]: this list kind has no cell count, and nothing here gives one

Cells

WrittenCell
အမှတ်များ ၏ အကွက် ၀the first (counted from 0, as programs count)
အမှတ်များ ၏ ၁ ခုမြောက်အကွက်the first (counted from 1, as people count)
အမှတ်များ ၏ အကွက် ၂the third
အမှတ်များ ၏ ၃ ခုမြောက်အကွက်the third
အမှတ်များ ၏ အကွက်အရေအတွက်the number of cells, a လက္ခဏာမဲ့ ကိန်းပြည့် and a constant

A cell is a place: it can be read, printed, assigned to, and its address taken. The cell number is an integer (ZK0423), and the value in front of ၏ has to be a list or a pointer to one (ZK0422).

စံကားZK0422

Expected error

error[ZK0422]: isize is not a list, so it has no cells

The cell number takes the whole expression

The number after အကွက် is read as far as the operator expression goes, just as ပ + ၁ ၏ တန်ဖိုး reads through ပ + ၁. To use a cell inside a sum, put the cell in parentheses.

စံကား

Expected output

၂၀
၁၁

The word အကွက်အရေအတွက် is a single word. Written with a space, အကွက် အရေအတွက် is the cell numbered with a variable named အရေအတွက်.

A counted loop visits each cell; the cell count, in parentheses, gives the last number:

စံကား

Expected output

၄၃

Lists of lists

A cell can hold a whole list, and a list of rows is a table. Each count prefix pairs with one စာရင်း, the inner ones first: in အကွက် ၂ ခုရှိသော အကွက် ၃ ခုရှိသော ကိန်းပြည့် စာရင်း စာရင်း, the ၃ belongs to the first စာရင်း (a row of 3 integers) and the ၂ to the second (2 such rows). Messages write this kind as [2][3]isize. Lists nest as deep as needed.

စံကား

Expected output

[[၀၊ ၀၊ ၀]၊ [၀၊ ၀၊ ၉]]
[၀၊ ၀၊ ၉]
၂
၃

Nested literals put each inner literal in parentheses. A kind with no counts takes all of them from the value:

စံကား

Expected output

[[၁၊ ၂]၊ [၃၊ ၄]၊ [၅၊ ၆]]
၅

How a list is stored

The cells of a list stand next to each other in one stretch of RAM, in the order of their numbers, with no gaps. The size of a list is its count times the size of one cell. A list of lists stores its rows one after another, so a table is one long stretch with the rows end to end.

  table: [2][3]isize, 6 cells of 8 octets = 48 octets

  octet    0      8      16     24     32     40     48
           +------+------+------+------+------+------+
           |  1   |  2   |  3   |  4   |  5   |  6   |
           +------+------+------+------+------+------+
  cell     [0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
           '------- row 0 ------''------- row 1 ------'

  one past the end: octet 48, not a cell
စံကား

Expected output

၄၈
၁၂
၅

The address of a cell is a pointer to the cell, so the cell after it can be reached with pointer arithmetic, and the cell after the last of a row is the first of the next row:

စံကား

Expected output

၃
၅

Passing and comparing

A list is a value, like an integer. Setting one list to another duplicates each cell, and the two lists stand apart afterwards. The same holds for a list passed as an argument, since a parameter is a new variable:

စံကား

Expected output

[၁၊ ၂၊ ၃]
[၁၀၀၊ ၂၊ ၃]
  • A change to a list parameter remains in the function. To let a function change the caller's list, or to spare it a duplicate of a large list, pass the address of the list. The parameter kind is the list kind with လိပ်စာ after it and ငှားထားသော in front.
  • A function cannot give back a list as its result (ZK0424). Pass the address of a list to fill instead.
  • A list literal as an argument takes each value back to the last ၊ that starts the argument list. When another argument comes before it, put the literal in parentheses.
  • A list whose cells hold owners moves when it is assigned or passed; see Ownership and borrowing.
စံကား

Expected output

[၂၊ ၄၊ ၆]
စံကား

Expected output

၁၀၆

Comparing

== and !=, and the verbs ညီ and မညီ, compare two lists of the same kind, one cell after another. Lists of different kinds, such as [3]isize and [2]isize, do not compare (ZK0404). Lists have no order, so < and > do not take them (ZK0403).

စံကား

Expected output

မှန်
မမှန်
မှန်
စံကားZK0404

Expected error

error[ZK0404]: cannot combine [3]isize and [2]isize without a conversion

Lists and addresses

The address of a list points at the whole list, so its kind holds the count: ငှားထားသော အကွက် ၃ ခုရှိသော ကိန်းပြည့် စာရင်း လိပ်စာ, or &*[3]isize in messages. Adding 1 to it moves one whole list, which for a list of rows is one row:

စံကား

Expected output

[၁၊ ၂၊ ၃]
[၄၊ ၅၊ ၆]
၆

The address of one cell is a plain pointer to the kind of the cell. The အကွက် forms work on a list and on a pointer to one (read through one level).

Mistakes with cell numbers

A cell number outside the list is a mistake that Zaka catches at two times.

At compile time. A constant cell number outside the list is ZK0603, in each mode. When the number is in a variable that the compiler can follow, as in a counted loop, or a constant held in a variable, the same check reports ZK0710 with the size of the list:

စံကားZK0603

Expected error

error[ZK0603]: cell 0 is outside the list, whose cells run from 1 to 3
စံကားZK0710

Expected error

error[ZK0710]: offset 3 is past the end of a block of 3 elements

A common cause is the number taking a whole sum. ကိန်းများ ၏ အကွက် ၁ + ၂ is cell 3, not cell 1 plus 2:

စံကားZK0603

Expected error

error[ZK0603]: cell 3 is outside the list, whose cells run from 0 to 2

At run time. When the number comes from input, or from a value the compiler cannot follow, the VM checks each access and stops the program with the range the list allows. This is what --mem-check warn shows for the program above:

စံကား

Expected output

the program stopped: cell 3 is outside the list, whose cells run from 0 to 2

When the compiler proves that a cell number is inside the list, the VM does not repeat the check for that access. A proof is given up whenever something the compiler cannot follow could change the number, for example a counter whose address is taken or a program that uses a raw kind; then the VM checks as before.

The other mistakes with lists, in one table:

CodeMistake
ZK0227a count that is not a whole number above zero
ZK0403<, > or another order used on lists
ZK0404lists of different kinds compared or combined
ZK0419a literal whose cell count differs from the kind
ZK0420စာရင်းအလွတ် with no list kind from its place
ZK0421a list kind with no cell count and no value to give one
ZK0422cells of a value that is not a list
ZK0423a cell number that is not an integer
ZK0424a list as the result of a function
ZK0603a constant cell number outside the list
ZK0710a cell number proven past the end

The text of each message is in Error messages.