Step 6b: array constants const P: [n]T = [...], T an integer, real, BOOL, enum or set type. ConstDecl's ArrayConst reads the literal, each element a constant coerced to T, into the bytes of a rodata chunk named for the constant, as a string constant's chunk is. MakeItem makes it an mVar at that chunk with isVar false: it indexes, slices, LENs, compares, assigns and passes as [n]T or []T like a variable; := to it or an element of it says 'P is a constant, and can't be changed' (Assignable); var args and ADR refuse it as they refuse any non-variable. Local array constants work the same. conform/run/ArrayConst; conform/err/ArrayConst*; StmtErrType's message now says 'only an ERROR or an array constant has a type'.
| @@ -122,7 +122,7 @@ | ||
| 122 | 122 | - [x] 5. The system integer. |
| 123 | 123 | - [x] 6. Optional reals: a target says whether it has them. |
| 124 | 124 | - [x] 6a. `+` joins string constants (report, section 4). |
| 125 | -- [ ] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only | |
| 125 | +- [x] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only | |
| 126 | 126 | data, used as a variable that can't be changed (section 4). |
| 127 | 127 | Done so that a string constant is the same mechanism. |
| 128 | 128 | - [ ] 6c. `match` on integers: constants and inclusive ranges as |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module ArrayConstAdr; | |
| 2 | +const P: [2]I64 = [1, 2]; | |
| 3 | +proc main*; | |
| 4 | +var p: ^[2]I64; | |
| 5 | +begin | |
| 6 | + p := ADR(P); | |
| 7 | +end; | |
| 8 | +end ArrayConstAdr. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ArrayConstAssign; | |
| 2 | +const P: [2]I64 = [1, 2]; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + P[0] := 3; | |
| 6 | +end; | |
| 7 | +end ArrayConstAssign. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module ArrayConstCount; | |
| 2 | +const P: [3]I64 = [1, 2]; | |
| 3 | +end ArrayConstCount. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +module ArrayConstElem; | |
| 2 | +type R = record a: I64; end; | |
| 3 | +const P: [1]R = [{a: 1}]; | |
| 4 | +end ArrayConstElem. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module ArrayConstFit; | |
| 2 | +const P: [2]U8 = [1, 256]; | |
| 3 | +end ArrayConstFit. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +module ArrayConstNotConst; | |
| 2 | +var v: I64; | |
| 3 | +const P: [2]I64 = [1, v]; | |
| 4 | +end ArrayConstNotConst. |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module ArrayConstVar; | |
| 2 | +const P: [2]I64 = [1, 2]; | |
| 3 | +proc put(var a: [2]I64); | |
| 4 | +begin | |
| 5 | + a[0] := 0; | |
| 6 | +end; | |
| 7 | +proc main*; | |
| 8 | +begin | |
| 9 | + put(P); | |
| 10 | +end; | |
| 11 | +end ArrayConstVar. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ArrayConstWhole; | |
| 2 | +const P: [2]I64 = [1, 2]; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + P := [3, 4]; | |
| 6 | +end; | |
| 7 | +end ArrayConstWhole. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +2 3 5 7 11 | |
| 2 | +28 8 2 | |
| 3 | +differ same | |
| 4 | +1 2 255 65 | |
| 5 | +reals bools enums | |
| 6 | +-1 -32768 32767 | |
| 7 | +42 2 |
| @@ -0,0 +1,53 @@ | ||
| 1 | +module ArrayConst; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +type Color = (red, green, blue); | |
| 5 | + | |
| 6 | +const | |
| 7 | + Primes*: [5]I64 = [2, 3, 5, 7, 11]; | |
| 8 | + Small: [4]U8 = [1, 2, 0FFH, 'A']; | |
| 9 | + Halves: [3]F64 = [0.5, 1.5, -2.5]; | |
| 10 | + Narrow: [2]F32 = [0.25, 3.0]; | |
| 11 | + Flags: [3]BOOL = [true, false, true]; | |
| 12 | + Colors: [2]Color = [blue, red]; | |
| 13 | + Mixes: [2]set of Color = [{red, blue}, {}]; | |
| 14 | + Neg: [3]I16 = [-1, -32768, 32767]; | |
| 15 | + | |
| 16 | +proc sum(a: []I64): I64; | |
| 17 | +var s: I64; i: INT; | |
| 18 | +begin | |
| 19 | + for i := 0 to LEN(a) - 1 do s := s + a[i]; end; | |
| 20 | + return s; | |
| 21 | +end; | |
| 22 | + | |
| 23 | +proc first(a: [5]I64): I64; | |
| 24 | +begin | |
| 25 | + return a[0]; | |
| 26 | +end; | |
| 27 | + | |
| 28 | +proc main*; | |
| 29 | +const Local: [2]I32 = [40, 2]; | |
| 30 | +var a: [5]I64; i: INT; | |
| 31 | +begin | |
| 32 | + for i := 0 to LEN(Primes) - 1 do Out.int(Primes[i]); Out.str(" "); end; | |
| 33 | + Out.ln; | |
| 34 | + Out.int(sum(Primes)); Out.str(" "); | |
| 35 | + Out.int(sum(Primes[1..3])); Out.str(" "); | |
| 36 | + Out.int(first(Primes)); Out.ln; | |
| 37 | + a := Primes; | |
| 38 | + a[0] := 1; | |
| 39 | + if a # Primes then Out.str("differ "); end; | |
| 40 | + a[0] := 2; | |
| 41 | + if a = Primes then Out.str("same"); end; | |
| 42 | + Out.ln; | |
| 43 | + for i := 0 to 3 do Out.int(I64(Small[i])); Out.str(" "); end; | |
| 44 | + Out.ln; | |
| 45 | + if (Halves[0] = 0.5) and (Halves[2] = -2.5) and (Narrow[1] = 3.0) then Out.str("reals "); end; | |
| 46 | + if Flags[0] and not Flags[1] then Out.str("bools "); end; | |
| 47 | + if (Colors[0] = blue) and not (green in Mixes[0]) and (blue in Mixes[0]) then Out.str("enums"); end; | |
| 48 | + Out.ln; | |
| 49 | + Out.int(I64(Neg[0])); Out.str(" "); Out.int(I64(Neg[1])); Out.str(" "); Out.int(I64(Neg[2])); Out.ln; | |
| 50 | + Out.int(I64(Local[0] + Local[1])); Out.str(" "); Out.int(LEN(Local)); Out.ln; | |
| 51 | +end; | |
| 52 | + | |
| 53 | +end ArrayConst. |
| @@ -283,7 +283,11 @@ | ||
| 283 | 283 | variable into an item for part of it, mostly by adding to `off`. An |
| 284 | 284 | item for another module's exported variable, or part of it, is `ro`: |
| 285 | 285 | it isn't a variable (`isVar`), and `NotReadOnly` says why where one is |
| 286 | -wanted; what it points at is. | |
| 286 | +wanted; what it points at is. An array constant is a variable too, | |
| 287 | +in read-only data: `ArrayConst` (`parse.pas`) reads its literal into the | |
| 288 | +bytes of a chunk, as a string constant's are, and `MakeItem` makes it | |
| 289 | +an `mVar` there that isn't `isVar`, so it is read, indexed, sliced and | |
| 290 | +passed, never changed. | |
| 287 | 291 | `Index` and `Slice` emit the bounds checks, unless the module says |
| 288 | 292 | `check off;` (`checks`, which `CompileFile` sets for each module), and |
| 289 | 293 | `Convert` the check of an enumeration's position; `Check` jumps over a call to `Rt.fail` when |
| @@ -255,6 +255,10 @@ | ||
| 255 | 255 | oConst: |
| 256 | 256 | if o^.typ = tStr then begin |
| 257 | 257 | x.mode := mStr; x.val := o^.val; x.chunk := o^.chunk |
| 258 | + end else if o^.typ^.form = fArray then begin | |
| 259 | + { an array constant: a variable in read-only data, not one of | |
| 260 | + the program's, so nothing can change it } | |
| 261 | + x.mode := mVar; x.base := bGlobal; x.chunk := o^.chunk | |
| 258 | 262 | end else begin |
| 259 | 263 | x.mode := mConst; x.val := o^.val |
| 260 | 264 | end; |
| @@ -1383,6 +1383,15 @@ | ||
| 1383 | 1383 | else Subject := 'a value'; |
| 1384 | 1384 | end; |
| 1385 | 1385 | |
| 1386 | +{ x, named first n, is a variable the program may assign: not an | |
| 1387 | + array constant, nor any part of one. } | |
| 1388 | +procedure Assignable(const x: TItem; n: longint); | |
| 1389 | +begin | |
| 1390 | + if (x.mode = mVar) and (x.obj <> nil) and (x.obj^.cls = oConst) then | |
| 1391 | + MarkAt(x.pos, NameOf(x.obj^.name) + ' is a constant, and can''t be changed'); | |
| 1392 | + if (x.mode <> mVar) or not x.isVar then MarkAt(x.pos, 'can''t assign to ' + Subject(x, n)); | |
| 1393 | +end; | |
| 1394 | + | |
| 1386 | 1395 | { The designator a statement starts with. A variable named whole |
| 1387 | 1396 | before := is assigned, not read: naming it there leaves it as unread |
| 1388 | 1397 | as it was. } |
| @@ -1407,7 +1416,7 @@ | ||
| 1407 | 1416 | dn[0] := first; |
| 1408 | 1417 | repeat |
| 1409 | 1418 | NotReadOnly(d[n]); |
| 1410 | - if (d[n].mode <> mVar) or not d[n].isVar then MarkAt(d[n].pos, 'can''t assign to ' + Subject(d[n], dn[n])); | |
| 1419 | + Assignable(d[n], dn[n]); | |
| 1411 | 1420 | Pin(d[n]); |
| 1412 | 1421 | inc(n); |
| 1413 | 1422 | if tok <> sComma then break; |
| @@ -1489,7 +1498,7 @@ | ||
| 1489 | 1498 | LeftSide(x, called); |
| 1490 | 1499 | if tok = sBecomes then begin |
| 1491 | 1500 | NotReadOnly(x); |
| 1492 | - if (x.mode <> mVar) or not x.isVar then MarkAt(x.pos, 'can''t assign to ' + Subject(x, first)); | |
| 1501 | + Assignable(x, first); | |
| 1493 | 1502 | Save(x); |
| 1494 | 1503 | Next; |
| 1495 | 1504 | Expression(y, x.typ); |
| @@ -1758,6 +1767,41 @@ | ||
| 1758 | 1767 | Declare(o); |
| 1759 | 1768 | end; |
| 1760 | 1769 | |
| 1770 | +{ The value of n, an array constant of type t: an array literal of | |
| 1771 | + constants, which become the bytes of a read-only chunk, as a string | |
| 1772 | + constant's do (report section 4). The bytes are the compiler's own | |
| 1773 | + machine's, little-endian, as the target's are. } | |
| 1774 | +function ArrayConst(t: PType; n: longint): longint; | |
| 1775 | +var et: PType; e: TItem; p: PBytes; k, es: int64; f: single; c: longint; start: TPos; | |
| 1776 | +begin | |
| 1777 | + et := t^.base; | |
| 1778 | + es := et^.size; | |
| 1779 | + start := TokPos; | |
| 1780 | + if tok <> sLbrak then Expected('an array literal'); | |
| 1781 | + Next; | |
| 1782 | + p := nil; k := 0; | |
| 1783 | + if tok <> sRbrak then | |
| 1784 | + repeat | |
| 1785 | + Expression(e, et); | |
| 1786 | + Coerce(e, et); | |
| 1787 | + if e.mode <> mConst then MarkAt(e.pos, 'expected a constant, found ' + What(e)); | |
| 1788 | + ReAllocMem(p, (k + 1) * es); | |
| 1789 | + if (et^.form = fReal) and (es = 4) then begin | |
| 1790 | + f := RealOf(e); | |
| 1791 | + Move(f, p^[k * es], 4); | |
| 1792 | + end else Move(e.val, p^[k * es], es); | |
| 1793 | + inc(k); | |
| 1794 | + if tok <> sComma then break; | |
| 1795 | + Next; | |
| 1796 | + until false; | |
| 1797 | + Expect(sRbrak); | |
| 1798 | + if k <> t^.len then MarkAt(SpanFrom(start), 'expected ' + IntStr(t^.len) + ' elements, found ' + IntStr(k)); | |
| 1799 | + c := NewChunk(secRodata, curModule^.sname, n); | |
| 1800 | + SetData(c, p, t^.size, t^.align); | |
| 1801 | + FreeMem(p); | |
| 1802 | + ArrayConst := c; | |
| 1803 | +end; | |
| 1804 | + | |
| 1761 | 1805 | procedure ConstDecl(top: boolean); |
| 1762 | 1806 | var n: longint; exp: boolean; x: TItem; o: PObj; t: PType; start, at: TPos; |
| 1763 | 1807 | begin |
| @@ -1768,9 +1812,21 @@ | ||
| 1768 | 1812 | Next; |
| 1769 | 1813 | start := TokPos; |
| 1770 | 1814 | t := ParseType; |
| 1771 | - if t <> tError then MarkAt(SpanFrom(start), 'only an ERROR constant has a type'); | |
| 1815 | + if (t^.form = fArray) and not (t^.base^.form in [fInt, fReal, fBool, fEnum, fSet]) then | |
| 1816 | + MarkAt(SpanFrom(start), 'an array constant holds integers, reals, BOOLs, enumerations or sets, found ' + TypeName(t^.base)) | |
| 1817 | + else if (t <> tError) and (t^.form <> fArray) then | |
| 1818 | + MarkAt(SpanFrom(start), 'only an ERROR or an array constant has a type'); | |
| 1772 | 1819 | end; |
| 1773 | 1820 | Expect(sEql); |
| 1821 | + if (t <> nil) and (t^.form = fArray) then begin | |
| 1822 | + o := NewObj(oConst, n, t); | |
| 1823 | + o^.chunk := ArrayConst(t, n); | |
| 1824 | + o^.pos := at; | |
| 1825 | + o^.exported := exp and top; | |
| 1826 | + Declare(o); | |
| 1827 | + Expect(sSemicolon); | |
| 1828 | + exit; | |
| 1829 | + end; | |
| 1774 | 1830 | Expression(x, nil); |
| 1775 | 1831 | o := NewObj(oConst, n, x.typ); |
| 1776 | 1832 | o^.pos := at; |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ArrayConstAdr.raz:6:14: error: ADR needs a variable | |
| 2 | + 6 | p := ADR(P); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ArrayConstAssign.raz:5:5: error: P is a constant, and can't be changed | |
| 2 | + 5 | P[0] := 3; | |
| 3 | + | ^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ArrayConstCount.raz:2:19: error: expected 3 elements, found 2 | |
| 2 | + 2 | const P: [3]I64 = [1, 2]; | |
| 3 | + | ^^^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ArrayConstElem.raz:3:10: error: an array constant holds integers, reals, BOOLs, enumerations or sets, found R | |
| 2 | + 3 | const P: [1]R = [{a: 1}]; | |
| 3 | + | ^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ArrayConstFit.raz:2:22: error: 256 doesn't fit U8 | |
| 2 | + 2 | const P: [2]U8 = [1, 256]; | |
| 3 | + | ^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ArrayConstNotConst.raz:3:23: error: expected a constant, found I64 | |
| 2 | + 3 | const P: [2]I64 = [1, v]; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ArrayConstVar.raz:9:9: error: a var parameter needs a variable | |
| 2 | + 9 | put(P); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ArrayConstWhole.raz:5:5: error: P is a constant, and can't be changed | |
| 2 | + 5 | P := [3, 4]; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1,4 +1,4 @@ | ||
| 1 | -conform/err/StmtErrType.raz:2:10: error: only an ERROR constant has a type | |
| 1 | +conform/err/StmtErrType.raz:2:10: error: only an ERROR or an array constant has a type | |
| 2 | 2 | 2 | const k: I64 = "a"; |
| 3 | 3 | | ^^^ |
| 4 | 4 | raz: 1 error |