From 8465197c9560fd8db4f40c9575c2bb14cfaeca55 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 09:17:27 +0100 Subject: [PATCH] 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'. --- diff --git a/PLAN.md b/PLAN.md index 7a7f5f3..40c8429 100644 --- a/PLAN.md +++ b/PLAN.md @@ -122,7 +122,7 @@ - [x] 5. The system integer. - [x] 6. Optional reals: a target says whether it has them. - [x] 6a. `+` joins string constants (report, section 4). -- [ ] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only +- [x] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only data, used as a variable that can't be changed (section 4). Done so that a string constant is the same mechanism. - [ ] 6c. `match` on integers: constants and inclusive ranges as diff --git a/conform/err/ArrayConstAdr.raz b/conform/err/ArrayConstAdr.raz new file mode 100644 index 0000000..12170da --- /dev/null +++ b/conform/err/ArrayConstAdr.raz @@ -0,0 +1,8 @@ +module ArrayConstAdr; +const P: [2]I64 = [1, 2]; +proc main*; +var p: ^[2]I64; +begin + p := ADR(P); +end; +end ArrayConstAdr. diff --git a/conform/err/ArrayConstAssign.raz b/conform/err/ArrayConstAssign.raz new file mode 100644 index 0000000..9f96a55 --- /dev/null +++ b/conform/err/ArrayConstAssign.raz @@ -0,0 +1,7 @@ +module ArrayConstAssign; +const P: [2]I64 = [1, 2]; +proc main*; +begin + P[0] := 3; +end; +end ArrayConstAssign. diff --git a/conform/err/ArrayConstCount.raz b/conform/err/ArrayConstCount.raz new file mode 100644 index 0000000..ba38895 --- /dev/null +++ b/conform/err/ArrayConstCount.raz @@ -0,0 +1,3 @@ +module ArrayConstCount; +const P: [3]I64 = [1, 2]; +end ArrayConstCount. diff --git a/conform/err/ArrayConstElem.raz b/conform/err/ArrayConstElem.raz new file mode 100644 index 0000000..da1aabe --- /dev/null +++ b/conform/err/ArrayConstElem.raz @@ -0,0 +1,4 @@ +module ArrayConstElem; +type R = record a: I64; end; +const P: [1]R = [{a: 1}]; +end ArrayConstElem. diff --git a/conform/err/ArrayConstFit.raz b/conform/err/ArrayConstFit.raz new file mode 100644 index 0000000..3b52e02 --- /dev/null +++ b/conform/err/ArrayConstFit.raz @@ -0,0 +1,3 @@ +module ArrayConstFit; +const P: [2]U8 = [1, 256]; +end ArrayConstFit. diff --git a/conform/err/ArrayConstNotConst.raz b/conform/err/ArrayConstNotConst.raz new file mode 100644 index 0000000..e56b195 --- /dev/null +++ b/conform/err/ArrayConstNotConst.raz @@ -0,0 +1,4 @@ +module ArrayConstNotConst; +var v: I64; +const P: [2]I64 = [1, v]; +end ArrayConstNotConst. diff --git a/conform/err/ArrayConstVar.raz b/conform/err/ArrayConstVar.raz new file mode 100644 index 0000000..a6b6ce2 --- /dev/null +++ b/conform/err/ArrayConstVar.raz @@ -0,0 +1,11 @@ +module ArrayConstVar; +const P: [2]I64 = [1, 2]; +proc put(var a: [2]I64); +begin + a[0] := 0; +end; +proc main*; +begin + put(P); +end; +end ArrayConstVar. diff --git a/conform/err/ArrayConstWhole.raz b/conform/err/ArrayConstWhole.raz new file mode 100644 index 0000000..a979fef --- /dev/null +++ b/conform/err/ArrayConstWhole.raz @@ -0,0 +1,7 @@ +module ArrayConstWhole; +const P: [2]I64 = [1, 2]; +proc main*; +begin + P := [3, 4]; +end; +end ArrayConstWhole. diff --git a/conform/run/ArrayConst.expect b/conform/run/ArrayConst.expect new file mode 100644 index 0000000..646ef40 --- /dev/null +++ b/conform/run/ArrayConst.expect @@ -0,0 +1,7 @@ +2 3 5 7 11 +28 8 2 +differ same +1 2 255 65 +reals bools enums +-1 -32768 32767 +42 2 diff --git a/conform/run/ArrayConst.raz b/conform/run/ArrayConst.raz new file mode 100644 index 0000000..56fcda0 --- /dev/null +++ b/conform/run/ArrayConst.raz @@ -0,0 +1,53 @@ +module ArrayConst; +import Out; + +type Color = (red, green, blue); + +const + Primes*: [5]I64 = [2, 3, 5, 7, 11]; + Small: [4]U8 = [1, 2, 0FFH, 'A']; + Halves: [3]F64 = [0.5, 1.5, -2.5]; + Narrow: [2]F32 = [0.25, 3.0]; + Flags: [3]BOOL = [true, false, true]; + Colors: [2]Color = [blue, red]; + Mixes: [2]set of Color = [{red, blue}, {}]; + Neg: [3]I16 = [-1, -32768, 32767]; + +proc sum(a: []I64): I64; +var s: I64; i: INT; +begin + for i := 0 to LEN(a) - 1 do s := s + a[i]; end; + return s; +end; + +proc first(a: [5]I64): I64; +begin + return a[0]; +end; + +proc main*; +const Local: [2]I32 = [40, 2]; +var a: [5]I64; i: INT; +begin + for i := 0 to LEN(Primes) - 1 do Out.int(Primes[i]); Out.str(" "); end; + Out.ln; + Out.int(sum(Primes)); Out.str(" "); + Out.int(sum(Primes[1..3])); Out.str(" "); + Out.int(first(Primes)); Out.ln; + a := Primes; + a[0] := 1; + if a # Primes then Out.str("differ "); end; + a[0] := 2; + if a = Primes then Out.str("same"); end; + Out.ln; + for i := 0 to 3 do Out.int(I64(Small[i])); Out.str(" "); end; + Out.ln; + if (Halves[0] = 0.5) and (Halves[2] = -2.5) and (Narrow[1] = 3.0) then Out.str("reals "); end; + if Flags[0] and not Flags[1] then Out.str("bools "); end; + if (Colors[0] = blue) and not (green in Mixes[0]) and (blue in Mixes[0]) then Out.str("enums"); end; + Out.ln; + Out.int(I64(Neg[0])); Out.str(" "); Out.int(I64(Neg[1])); Out.str(" "); Out.int(I64(Neg[2])); Out.ln; + Out.int(I64(Local[0] + Local[1])); Out.str(" "); Out.int(LEN(Local)); Out.ln; +end; + +end ArrayConst. diff --git a/doc/compiler.md b/doc/compiler.md index 2f92134..e6e9f4a 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -283,7 +283,11 @@ variable into an item for part of it, mostly by adding to `off`. An item for another module's exported variable, or part of it, is `ro`: it isn't a variable (`isVar`), and `NotReadOnly` says why where one is -wanted; what it points at is. +wanted; what it points at is. An array constant is a variable too, +in read-only data: `ArrayConst` (`parse.pas`) reads its literal into the +bytes of a chunk, as a string constant's are, and `MakeItem` makes it +an `mVar` there that isn't `isVar`, so it is read, indexed, sliced and +passed, never changed. `Index` and `Slice` emit the bounds checks, unless the module says `check off;` (`checks`, which `CompileFile` sets for each module), and `Convert` the check of an enumeration's position; `Check` jumps over a call to `Rt.fail` when diff --git a/src/gen.pas b/src/gen.pas index a739c2d..360c80c 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -255,6 +255,10 @@ oConst: if o^.typ = tStr then begin x.mode := mStr; x.val := o^.val; x.chunk := o^.chunk + end else if o^.typ^.form = fArray then begin + { an array constant: a variable in read-only data, not one of + the program's, so nothing can change it } + x.mode := mVar; x.base := bGlobal; x.chunk := o^.chunk end else begin x.mode := mConst; x.val := o^.val end; diff --git a/src/parse.pas b/src/parse.pas index fe2aea5..17175ef 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1383,6 +1383,15 @@ else Subject := 'a value'; end; +{ x, named first n, is a variable the program may assign: not an + array constant, nor any part of one. } +procedure Assignable(const x: TItem; n: longint); +begin + if (x.mode = mVar) and (x.obj <> nil) and (x.obj^.cls = oConst) then + MarkAt(x.pos, NameOf(x.obj^.name) + ' is a constant, and can''t be changed'); + if (x.mode <> mVar) or not x.isVar then MarkAt(x.pos, 'can''t assign to ' + Subject(x, n)); +end; + { The designator a statement starts with. A variable named whole before := is assigned, not read: naming it there leaves it as unread as it was. } @@ -1407,7 +1416,7 @@ dn[0] := first; repeat NotReadOnly(d[n]); - if (d[n].mode <> mVar) or not d[n].isVar then MarkAt(d[n].pos, 'can''t assign to ' + Subject(d[n], dn[n])); + Assignable(d[n], dn[n]); Pin(d[n]); inc(n); if tok <> sComma then break; @@ -1489,7 +1498,7 @@ LeftSide(x, called); if tok = sBecomes then begin NotReadOnly(x); - if (x.mode <> mVar) or not x.isVar then MarkAt(x.pos, 'can''t assign to ' + Subject(x, first)); + Assignable(x, first); Save(x); Next; Expression(y, x.typ); @@ -1758,6 +1767,41 @@ Declare(o); end; +{ The value of n, an array constant of type t: an array literal of + constants, which become the bytes of a read-only chunk, as a string + constant's do (report section 4). The bytes are the compiler's own + machine's, little-endian, as the target's are. } +function ArrayConst(t: PType; n: longint): longint; +var et: PType; e: TItem; p: PBytes; k, es: int64; f: single; c: longint; start: TPos; +begin + et := t^.base; + es := et^.size; + start := TokPos; + if tok <> sLbrak then Expected('an array literal'); + Next; + p := nil; k := 0; + if tok <> sRbrak then + repeat + Expression(e, et); + Coerce(e, et); + if e.mode <> mConst then MarkAt(e.pos, 'expected a constant, found ' + What(e)); + ReAllocMem(p, (k + 1) * es); + if (et^.form = fReal) and (es = 4) then begin + f := RealOf(e); + Move(f, p^[k * es], 4); + end else Move(e.val, p^[k * es], es); + inc(k); + if tok <> sComma then break; + Next; + until false; + Expect(sRbrak); + if k <> t^.len then MarkAt(SpanFrom(start), 'expected ' + IntStr(t^.len) + ' elements, found ' + IntStr(k)); + c := NewChunk(secRodata, curModule^.sname, n); + SetData(c, p, t^.size, t^.align); + FreeMem(p); + ArrayConst := c; +end; + procedure ConstDecl(top: boolean); var n: longint; exp: boolean; x: TItem; o: PObj; t: PType; start, at: TPos; begin @@ -1768,9 +1812,21 @@ Next; start := TokPos; t := ParseType; - if t <> tError then MarkAt(SpanFrom(start), 'only an ERROR constant has a type'); + if (t^.form = fArray) and not (t^.base^.form in [fInt, fReal, fBool, fEnum, fSet]) then + MarkAt(SpanFrom(start), 'an array constant holds integers, reals, BOOLs, enumerations or sets, found ' + TypeName(t^.base)) + else if (t <> tError) and (t^.form <> fArray) then + MarkAt(SpanFrom(start), 'only an ERROR or an array constant has a type'); end; Expect(sEql); + if (t <> nil) and (t^.form = fArray) then begin + o := NewObj(oConst, n, t); + o^.chunk := ArrayConst(t, n); + o^.pos := at; + o^.exported := exp and top; + Declare(o); + Expect(sSemicolon); + exit; + end; Expression(x, nil); o := NewObj(oConst, n, x.typ); o^.pos := at; diff --git a/test/msg/ArrayConstAdr.err b/test/msg/ArrayConstAdr.err new file mode 100644 index 0000000..c2e9bd5 --- /dev/null +++ b/test/msg/ArrayConstAdr.err @@ -0,0 +1,4 @@ +conform/err/ArrayConstAdr.raz:6:14: error: ADR needs a variable + 6 | p := ADR(P); + | ^ +raz: 1 error diff --git a/test/msg/ArrayConstAssign.err b/test/msg/ArrayConstAssign.err new file mode 100644 index 0000000..4a01e4e --- /dev/null +++ b/test/msg/ArrayConstAssign.err @@ -0,0 +1,4 @@ +conform/err/ArrayConstAssign.raz:5:5: error: P is a constant, and can't be changed + 5 | P[0] := 3; + | ^^^^ +raz: 1 error diff --git a/test/msg/ArrayConstCount.err b/test/msg/ArrayConstCount.err new file mode 100644 index 0000000..6c57a1c --- /dev/null +++ b/test/msg/ArrayConstCount.err @@ -0,0 +1,4 @@ +conform/err/ArrayConstCount.raz:2:19: error: expected 3 elements, found 2 + 2 | const P: [3]I64 = [1, 2]; + | ^^^^^^ +raz: 1 error diff --git a/test/msg/ArrayConstElem.err b/test/msg/ArrayConstElem.err new file mode 100644 index 0000000..11fc7bb --- /dev/null +++ b/test/msg/ArrayConstElem.err @@ -0,0 +1,4 @@ +conform/err/ArrayConstElem.raz:3:10: error: an array constant holds integers, reals, BOOLs, enumerations or sets, found R + 3 | const P: [1]R = [{a: 1}]; + | ^^^^ +raz: 1 error diff --git a/test/msg/ArrayConstFit.err b/test/msg/ArrayConstFit.err new file mode 100644 index 0000000..dab1449 --- /dev/null +++ b/test/msg/ArrayConstFit.err @@ -0,0 +1,4 @@ +conform/err/ArrayConstFit.raz:2:22: error: 256 doesn't fit U8 + 2 | const P: [2]U8 = [1, 256]; + | ^^^ +raz: 1 error diff --git a/test/msg/ArrayConstNotConst.err b/test/msg/ArrayConstNotConst.err new file mode 100644 index 0000000..8ba9116 --- /dev/null +++ b/test/msg/ArrayConstNotConst.err @@ -0,0 +1,4 @@ +conform/err/ArrayConstNotConst.raz:3:23: error: expected a constant, found I64 + 3 | const P: [2]I64 = [1, v]; + | ^ +raz: 1 error diff --git a/test/msg/ArrayConstVar.err b/test/msg/ArrayConstVar.err new file mode 100644 index 0000000..10e0014 --- /dev/null +++ b/test/msg/ArrayConstVar.err @@ -0,0 +1,4 @@ +conform/err/ArrayConstVar.raz:9:9: error: a var parameter needs a variable + 9 | put(P); + | ^ +raz: 1 error diff --git a/test/msg/ArrayConstWhole.err b/test/msg/ArrayConstWhole.err new file mode 100644 index 0000000..529dc67 --- /dev/null +++ b/test/msg/ArrayConstWhole.err @@ -0,0 +1,4 @@ +conform/err/ArrayConstWhole.raz:5:5: error: P is a constant, and can't be changed + 5 | P := [3, 4]; + | ^ +raz: 1 error diff --git a/test/msg/StmtErrType.err b/test/msg/StmtErrType.err index 1b7c344..c7f0fbd 100644 --- a/test/msg/StmtErrType.err +++ b/test/msg/StmtErrType.err @@ -1,4 +1,4 @@ -conform/err/StmtErrType.raz:2:10: error: only an ERROR constant has a type +conform/err/StmtErrType.raz:2:10: error: only an ERROR or an array constant has a type 2 | const k: I64 = "a"; | ^^^ raz: 1 error