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.
| @@ -0,0 +1,3 @@ | ||
| 1 | +module ArrayConstLenEmpty; | |
| 2 | +const P: []I64 = []; | |
| 3 | +end ArrayConstLenEmpty. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +module ArrayConstLenLiteral; | |
| 2 | +const Q: [3]I64 = [1, 2, 3]; | |
| 3 | + P: []I64 = Q; | |
| 4 | +end ArrayConstLenLiteral. |
| @@ -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 |
| @@ -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. |
| @@ -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. |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 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 | |
| 4 | 4 | budget is 14,000). It is its own compiler: the |
| 5 | 5 | frozen Pascal compiler in `seed/` builds `src/` the first time, and |
| 6 | 6 | after that a raz builds `src/` (`make fixpoint` checks that it makes |
| @@ -20,7 +20,7 @@ | ||
| 20 | 20 | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | |
| 21 | 21 | | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 22 | 22 | | `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 | | |
| 24 | 24 | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| 26 | 26 | | `src/Raz.raz` | 305 | 238 | The command line | |
| @@ -1267,7 +1267,16 @@ | ||
| 1267 | 1267 | read-only data: `aggregateConst` (`Parse.raz`) reads its literal, part |
| 1268 | 1268 | by part in `constPart`, into the bytes of a chunk, as a string |
| 1269 | 1269 | 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 | |
| 1271 | 1280 | `mVar` there that isn't `isVar`, so it is read, indexed, sliced and |
| 1272 | 1281 | passed, never changed. `index` and `slice` have Gen make the bounds |
| 1273 | 1282 | checks, unless the module says `check off;` (`checks`, set by |
| @@ -34,9 +34,6 @@ | ||
| 34 | 34 | sixth thaw: an array constant typed `[]T` takes its length from its |
| 35 | 35 | literal (section 4); it breaks no program. |
| 36 | 36 | |
| 37 | -The compiler follows version 2026-10.28 until it has what 2026-10.29 | |
| 38 | -adds. | |
| 39 | - | |
| 40 | 37 | The language is frozen, softly, from this version on: it changes only |
| 41 | 38 | to mend a defect, and its library grows only by what breaks no program |
| 42 | 39 | written for it. What anyone wishes it had waits on a list, for a later |
| @@ -3130,6 +3130,53 @@ | ||
| 3130 | 3130 | return c, nil; |
| 3131 | 3131 | end; |
| 3132 | 3132 | |
| 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 | + | |
| 3133 | 3180 | proc constDecl(top: BOOL): ERROR; |
| 3134 | 3181 | var n, c: INT; exp: BOOL; x: TItem; o: PObj; t: PType; start, at: Diag.Pos; |
| 3135 | 3182 | begin |
| @@ -3140,11 +3187,13 @@ | ||
| 3140 | 3187 | Scan.next; |
| 3141 | 3188 | start := Scan.tokPos(); |
| 3142 | 3189 | 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 | |
| 3144 | 3192 | return Scan.markAt(Scan.spanFrom(start), "only an ERROR, an array or a record constant has a type"); |
| 3145 | 3193 | end; |
| 3146 | 3194 | end; |
| 3147 | 3195 | try expect(Scan.sEql); |
| 3196 | + if (t # nil) and (t^.form = Sym.fSlice) then t := try literalArray(t, start); end; | |
| 3148 | 3197 | if (t # nil) and ((t^.form = Sym.fArray) or (t^.form = Sym.fRecord)) then |
| 3149 | 3198 | o := Sym.newObj(Sym.oConst, n, t); |
| 3150 | 3199 | o^.chunk := try aggregateConst(t, n); |
| @@ -29,7 +29,7 @@ | ||
| 29 | 29 | import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; |
| 30 | 30 | |
| 31 | 31 | 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 *) | |
| 33 | 33 | MemfdCreate = 319; (* the system call *) |
| 34 | 34 | MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) |
| 35 | 35 | Execve = 59; |
| @@ -13,7 +13,8 @@ | ||
| 13 | 13 | # converts the narrower of two integers; and views, v.(T), |
| 14 | 14 | # whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz |
| 15 | 15 | # 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). | |
| 17 | 18 | newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz |
| 18 | 19 | conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz |
| 19 | 20 | conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz |
| @@ -21,7 +22,8 @@ | ||
| 21 | 22 | conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz |
| 22 | 23 | conform/err/IntResultsNarrow.raz conform/err/GenIntTypes.raz |
| 23 | 24 | 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" | |
| 25 | 27 | newer() { |
| 26 | 28 | case $1 in conform/*/View*.raz) return 0 ;; esac |
| 27 | 29 | for g in $newer; do [ $g = $1 ] && return 0; done |
| @@ -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 |
| @@ -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 |