raz / commit 8465197c

commit
8465197c9560fd8db4f40c9575c2bb14cfaeca55
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
c6f0123e
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'.

patch · log

23 files changed, +209 -6

PLAN.md (history) modified +1 -1

@@ -122,7 +122,7 @@
122122 - [x] 5. The system integer.
123123 - [x] 6. Optional reals: a target says whether it has them.
124124 - [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
126126 data, used as a variable that can't be changed (section 4).
127127 Done so that a string constant is the same mechanism.
128128 - [ ] 6c. `match` on integers: constants and inclusive ranges as

conform/err/ArrayConstAdr.raz (history) added +8 -0

@@ -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.

conform/err/ArrayConstAssign.raz (history) added +7 -0

@@ -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.

conform/err/ArrayConstCount.raz (history) added +3 -0

@@ -0,0 +1,3 @@
1+module ArrayConstCount;
2+const P: [3]I64 = [1, 2];
3+end ArrayConstCount.

conform/err/ArrayConstElem.raz (history) added +4 -0

@@ -0,0 +1,4 @@
1+module ArrayConstElem;
2+type R = record a: I64; end;
3+const P: [1]R = [{a: 1}];
4+end ArrayConstElem.

conform/err/ArrayConstFit.raz (history) added +3 -0

@@ -0,0 +1,3 @@
1+module ArrayConstFit;
2+const P: [2]U8 = [1, 256];
3+end ArrayConstFit.

conform/err/ArrayConstNotConst.raz (history) added +4 -0

@@ -0,0 +1,4 @@
1+module ArrayConstNotConst;
2+var v: I64;
3+const P: [2]I64 = [1, v];
4+end ArrayConstNotConst.

conform/err/ArrayConstVar.raz (history) added +11 -0

@@ -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.

conform/err/ArrayConstWhole.raz (history) added +7 -0

@@ -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.

conform/run/ArrayConst.expect (history) added +7 -0

@@ -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

conform/run/ArrayConst.raz (history) added +53 -0

@@ -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.

doc/compiler.md (history) modified +5 -1

@@ -283,7 +283,11 @@
283283 variable into an item for part of it, mostly by adding to `off`. An
284284 item for another module's exported variable, or part of it, is `ro`:
285285 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.
287291 `Index` and `Slice` emit the bounds checks, unless the module says
288292 `check off;` (`checks`, which `CompileFile` sets for each module), and
289293 `Convert` the check of an enumeration's position; `Check` jumps over a call to `Rt.fail` when

src/gen.pas modified +4 -0

@@ -255,6 +255,10 @@
255255 oConst:
256256 if o^.typ = tStr then begin
257257 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
258262 end else begin
259263 x.mode := mConst; x.val := o^.val
260264 end;

src/parse.pas modified +59 -3

@@ -1383,6 +1383,15 @@
13831383 else Subject := 'a value';
13841384 end;
13851385
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+
13861395 { The designator a statement starts with. A variable named whole
13871396 before := is assigned, not read: naming it there leaves it as unread
13881397 as it was. }
@@ -1407,7 +1416,7 @@
14071416 dn[0] := first;
14081417 repeat
14091418 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]);
14111420 Pin(d[n]);
14121421 inc(n);
14131422 if tok <> sComma then break;
@@ -1489,7 +1498,7 @@
14891498 LeftSide(x, called);
14901499 if tok = sBecomes then begin
14911500 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);
14931502 Save(x);
14941503 Next;
14951504 Expression(y, x.typ);
@@ -1758,6 +1767,41 @@
17581767 Declare(o);
17591768 end;
17601769
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+
17611805 procedure ConstDecl(top: boolean);
17621806 var n: longint; exp: boolean; x: TItem; o: PObj; t: PType; start, at: TPos;
17631807 begin
@@ -1768,9 +1812,21 @@
17681812 Next;
17691813 start := TokPos;
17701814 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');
17721819 end;
17731820 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;
17741830 Expression(x, nil);
17751831 o := NewObj(oConst, n, x.typ);
17761832 o^.pos := at;

test/msg/ArrayConstAdr.err (history) added +4 -0

@@ -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

test/msg/ArrayConstAssign.err (history) added +4 -0

@@ -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

test/msg/ArrayConstCount.err (history) added +4 -0

@@ -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

test/msg/ArrayConstElem.err (history) added +4 -0

@@ -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

test/msg/ArrayConstFit.err (history) added +4 -0

@@ -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

test/msg/ArrayConstNotConst.err (history) added +4 -0

@@ -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

test/msg/ArrayConstVar.err (history) added +4 -0

@@ -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

test/msg/ArrayConstWhole.err (history) added +4 -0

@@ -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

test/msg/StmtErrType.err (history) modified +1 -1

@@ -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
22 2 | const k: I64 = "a";
33 | ^^^
44 raz: 1 error