raz / commit 182251da

commit
182251da5d427881e8984ef857cf57804c4d12f3
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
0c7aae87
An array constant typed []T takes its literal's length (report 2026-10.29)

constDecl takes a slice type for a constant; literalArray makes it
[n]T, with n counted ahead by literalLength, which reads the literal
once, counting the commas outside inner brackets, and goes back to its
"[" (saveScan, restoreScan), its slips told on the second reading. An
empty literal is an array of length 0, an error; anything but a
literal is "expected a literal of []T". raz follows 2026-10.29.

conform/run/ArrayConstLen (and Widths, a table exported to another
module, LEN of it a constant), conform/err/ArrayConstLenEmpty and
ArrayConstLenLiteral, with their messages in test/msg; newer than the
seed in src/check.sh. compiler.md says it; 40 lines of code.

patch · log

12 files changed, +141 -10

conform/err/ArrayConstLenEmpty.raz added +3 -0

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

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

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

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

@@ -0,0 +1,7 @@
1+5 28 5
2+same
3+firefox 2
4+mpv 3
5+three 3
6+4 4
7+wide 3

conform/run/ArrayConstLen.raz (history) added +46 -0

@@ -0,0 +1,46 @@
1+module ArrayConstLen;
2+import Out, Widths;
3+
4+type Rule = record class: []U8; tag: INT; end;
5+
6+const
7+ Primes: []I64 = [2, 3, 5, 7, 11];
8+ Rules: []Rule = [{class: "firefox", tag: 2}, {class: "mpv", tag: 3}];
9+ Names: [][]U8 = ["one", "two", "three"];
10+ Count = LEN(Primes); (* a constant *)
11+
12+var buf: [Count]I64;
13+
14+proc sum(a: []I64): I64;
15+var s: I64; i: INT;
16+begin
17+ for i := 0 to LEN(a) - 1 do s := s + a[i]; end;
18+ return s;
19+end;
20+
21+proc wide(c: I32): BOOL;
22+var i: INT;
23+begin
24+ for i := 0 to LEN(Widths.Wide) - 1 do
25+ if (c >= Widths.Wide[i][0]) and (c <= Widths.Wide[i][1]) then return true; end;
26+ end;
27+ return false;
28+end;
29+
30+proc main*;
31+const Local: []U8 = [1, 2, 3, 4];
32+var i: INT;
33+begin
34+ Out.int(LEN(Primes)); Out.str(" "); Out.int(sum(Primes)); Out.str(" ");
35+ Out.int(LEN(buf)); Out.ln;
36+ buf := Primes;
37+ if buf = Primes then Out.str("same"); end;
38+ Out.ln;
39+ for i := 0 to LEN(Rules) - 1 do Out.str(Rules[i].class); Out.str(" "); Out.int(Rules[i].tag); Out.ln; end;
40+ Out.str(Names[2]); Out.str(" "); Out.int(LEN(Names)); Out.ln;
41+ Out.int(LEN(Local)); Out.str(" "); Out.int(I64(Local[3])); Out.ln;
42+ if wide(0AC01H) and not wide(41H) then Out.str("wide"); end;
43+ Out.str(" "); Out.int(LEN(Widths.Wide)); Out.ln;
44+end;
45+
46+end ArrayConstLen.

conform/run/mods/Widths.raz (history) added +6 -0

@@ -0,0 +1,6 @@
1+module Widths;
2+
3+(* ranges of wide characters, first and last, as a table made by a program would be *)
4+const Wide*: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]];
5+
6+end Widths.

doc/compiler.md (history) modified +12 -3

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
3-The compiler is eleven files of raz, about 11,100 lines of code (the
3+The compiler is eleven files of raz, about 11,200 lines of code (the
44 budget is 14,000). It is its own compiler: the
55 frozen Pascal compiler in `seed/` builds `src/` the first time, and
66 after that a raz builds `src/` (`make fixpoint` checks that it makes
@@ -20,7 +20,7 @@
2020 | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler |
2121 | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2222 | `src/Item.raz` | 1,986 | 1,617 | Items: types, constants, variables, operators, conversions, calls |
23-| `src/Parse.raz` | 4,000 | 3,478 | Declarations, statements, expressions, built-ins, recovery, warnings |
23+| `src/Parse.raz` | 4,049 | 3,518 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 305 | 238 | The command line |
@@ -1267,7 +1267,16 @@
12671267 read-only data: `aggregateConst` (`Parse.raz`) reads its literal, part
12681268 by part in `constPart`, into the bytes of a chunk, as a string
12691269 constant's are; an address in it, of a string, an array constant, a
1270-procedure or an `ERROR`, becomes a data fixup. `makeItem` makes it an
1270+procedure or an `ERROR`, becomes a data fixup. A constant typed `[]T`
1271+takes its length from its literal: `literalArray` makes the type
1272+`[n]T`, as if `n` were written, with `n` counted ahead by
1273+`literalLength`, which reads the literal once, counting the commas
1274+outside any inner brackets, and goes back to its `[` (`saveScan`,
1275+`restoreScan`), so `constPart` reads it as it reads any array's:
1276+
1277+ const P: []I64 = [2, 3, 5]; (* read as *) const P: [3]I64 = [2, 3, 5];
1278+
1279+`makeItem` makes it an
12711280 `mVar` there that isn't `isVar`, so it is read, indexed, sliced and
12721281 passed, never changed. `index` and `slice` have Gen make the bounds
12731282 checks, unless the module says `check off;` (`checks`, set by

doc/raz.md (history) modified +0 -3

@@ -34,9 +34,6 @@
3434 sixth thaw: an array constant typed `[]T` takes its length from its
3535 literal (section 4); it breaks no program.
3636
37-The compiler follows version 2026-10.28 until it has what 2026-10.29
38-adds.
39-
4037 The language is frozen, softly, from this version on: it changes only
4138 to mend a defect, and its library grows only by what breaks no program
4239 written for it. What anyone wishes it had waits on a list, for a later

src/Parse.raz (history) modified +50 -1

@@ -3130,6 +3130,53 @@
31303130 return c, nil;
31313131 end;
31323132
3133+(* The number of elements of the array literal at the scanner, at its
3134+ "[": read ahead, counting the commas outside any inner brackets,
3135+ then read again from the "[" (saveScan, restoreScan). Its slips are
3136+ told when it is read again. *)
3137+proc literalLength: INT;
3138+var st: Scan.State; depth, n: INT; wasSkipping: BOOL;
3139+begin
3140+ Scan.saveScan(st);
3141+ wasSkipping := Scan.skipping;
3142+ Scan.setSkipping(true);
3143+ Scan.next;
3144+ n := 0;
3145+ if Scan.tok # Scan.sRbrak then n := 1; end;
3146+ depth := 0;
3147+ while (Scan.tok # Scan.sEof) and ((depth > 0) or (Scan.tok # Scan.sRbrak)) do
3148+ match Scan.tok of
3149+ | sLparen, sLbrak, sLbrace: INC(depth);
3150+ | sRparen, sRbrak, sRbrace: DEC(depth);
3151+ | sComma: if depth = 0 then INC(n); end;
3152+ | else:
3153+ end;
3154+ Scan.next;
3155+ end;
3156+ Scan.setSkipping(wasSkipping);
3157+ Scan.restoreScan(st);
3158+ return n;
3159+end;
3160+
3161+(* A constant typed []T is an array constant whose length is its
3162+ literal's (report, 4): t, the slice type, becomes [n]T, as if n had
3163+ been written, with n counted ahead (literalLength). *)
3164+proc literalArray(t: PType; start: Diag.Pos): (PType, ERROR);
3165+var a: PType; n: INT;
3166+begin
3167+ if Scan.tok # Scan.sLbrak then
3168+ return nil, Scan.markAt(Scan.tokPos(), say1("expected a literal of {}", Sym.typeName(t)));
3169+ end;
3170+ n := literalLength();
3171+ if n = 0 then
3172+ return nil, Scan.markAt(Scan.spanFrom(start), "an array's length is from 1 to 2^40, found 0");
3173+ end;
3174+ a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align);
3175+ a^.len := I64(n);
3176+ a^.base := t^.base;
3177+ return a, nil;
3178+end;
3179+
31333180 proc constDecl(top: BOOL): ERROR;
31343181 var n, c: INT; exp: BOOL; x: TItem; o: PObj; t: PType; start, at: Diag.Pos;
31353182 begin
@@ -3140,11 +3187,13 @@
31403187 Scan.next;
31413188 start := Scan.tokPos();
31423189 t := try parseType();
3143- if (t # Sym.tError) and not ((t^.form = Sym.fArray) or ((t^.form = Sym.fRecord) and not t^.tuple)) then
3190+ if (t # Sym.tError) and not ((t^.form = Sym.fArray) or (t^.form = Sym.fSlice)
3191+ or ((t^.form = Sym.fRecord) and not t^.tuple)) then
31443192 return Scan.markAt(Scan.spanFrom(start), "only an ERROR, an array or a record constant has a type");
31453193 end;
31463194 end;
31473195 try expect(Scan.sEql);
3196+ if (t # nil) and (t^.form = Sym.fSlice) then t := try literalArray(t, start); end;
31483197 if (t # nil) and ((t^.form = Sym.fArray) or (t^.form = Sym.fRecord)) then
31493198 o := Sym.newObj(Sym.oConst, n, t);
31503199 o^.chunk := try aggregateConst(t, n);

src/Raz.raz (history) modified +1 -1

@@ -29,7 +29,7 @@
2929 import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf;
3030
3131 const
32- Report = "2026-10.28"; (* the version of doc/raz.md it follows *)
32+ Report = "2026-10.29"; (* the version of doc/raz.md it follows *)
3333 MemfdCreate = 319; (* the system call *)
3434 MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *)
3535 Execve = 59;

src/check.sh modified +4 -2

@@ -13,7 +13,8 @@
1313 # converts the narrower of two integers; and views, v.(T),
1414 # whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz
1515 # and, through Math, test/bench/NBody.raz use; and returning a slice of
16-# the frame as an error (report 2026-10.27).
16+# the frame as an error (report 2026-10.27); and an array constant
17+# typed []T (report 2026-10.29).
1718 newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz
1819 conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz
1920 conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz
@@ -21,7 +22,8 @@
2122 conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz
2223 conform/err/IntResultsNarrow.raz conform/err/GenIntTypes.raz
2324 lib/Math.raz examples/Elf.raz test/bench/NBody.raz
24- conform/err/FrameSlice.raz"
25+ conform/err/FrameSlice.raz
26+ conform/run/ArrayConstLen.raz conform/err/ArrayConstLenEmpty.raz conform/err/ArrayConstLenLiteral.raz"
2527 newer() {
2628 case $1 in conform/*/View*.raz) return 0 ;; esac
2729 for g in $newer; do [ $g = $1 ] && return 0; done

test/msg/ArrayConstLenEmpty.err added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/ArrayConstLenEmpty.raz:2:10: error: an array's length is from 1 to 2^40, found 0
2+ 2 | const P: []I64 = [];
3+ | ^^^^^^^
4+raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/ArrayConstLenLiteral.raz:3:16: error: expected a literal of []I64
2+ 3 | P: []I64 = Q;
3+ | ^
4+raz: 1 error