From 182251da5d427881e8984ef857cf57804c4d12f3 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 07:19:58 +0000 Subject: [PATCH] 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. --- diff --git a/conform/err/ArrayConstLenEmpty.raz b/conform/err/ArrayConstLenEmpty.raz new file mode 100644 index 0000000..d615ec4 --- /dev/null +++ b/conform/err/ArrayConstLenEmpty.raz @@ -0,0 +1,3 @@ +module ArrayConstLenEmpty; +const P: []I64 = []; +end ArrayConstLenEmpty. diff --git a/conform/err/ArrayConstLenLiteral.raz b/conform/err/ArrayConstLenLiteral.raz new file mode 100644 index 0000000..d70a4f2 --- /dev/null +++ b/conform/err/ArrayConstLenLiteral.raz @@ -0,0 +1,4 @@ +module ArrayConstLenLiteral; +const Q: [3]I64 = [1, 2, 3]; + P: []I64 = Q; +end ArrayConstLenLiteral. diff --git a/conform/run/ArrayConstLen.expect b/conform/run/ArrayConstLen.expect new file mode 100644 index 0000000..e2dab60 --- /dev/null +++ b/conform/run/ArrayConstLen.expect @@ -0,0 +1,7 @@ +5 28 5 +same +firefox 2 +mpv 3 +three 3 +4 4 +wide 3 diff --git a/conform/run/ArrayConstLen.raz b/conform/run/ArrayConstLen.raz new file mode 100644 index 0000000..ee0e492 --- /dev/null +++ b/conform/run/ArrayConstLen.raz @@ -0,0 +1,46 @@ +module ArrayConstLen; +import Out, Widths; + +type Rule = record class: []U8; tag: INT; end; + +const + Primes: []I64 = [2, 3, 5, 7, 11]; + Rules: []Rule = [{class: "firefox", tag: 2}, {class: "mpv", tag: 3}]; + Names: [][]U8 = ["one", "two", "three"]; + Count = LEN(Primes); (* a constant *) + +var buf: [Count]I64; + +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 wide(c: I32): BOOL; +var i: INT; +begin + for i := 0 to LEN(Widths.Wide) - 1 do + if (c >= Widths.Wide[i][0]) and (c <= Widths.Wide[i][1]) then return true; end; + end; + return false; +end; + +proc main*; +const Local: []U8 = [1, 2, 3, 4]; +var i: INT; +begin + Out.int(LEN(Primes)); Out.str(" "); Out.int(sum(Primes)); Out.str(" "); + Out.int(LEN(buf)); Out.ln; + buf := Primes; + if buf = Primes then Out.str("same"); end; + Out.ln; + for i := 0 to LEN(Rules) - 1 do Out.str(Rules[i].class); Out.str(" "); Out.int(Rules[i].tag); Out.ln; end; + Out.str(Names[2]); Out.str(" "); Out.int(LEN(Names)); Out.ln; + Out.int(LEN(Local)); Out.str(" "); Out.int(I64(Local[3])); Out.ln; + if wide(0AC01H) and not wide(41H) then Out.str("wide"); end; + Out.str(" "); Out.int(LEN(Widths.Wide)); Out.ln; +end; + +end ArrayConstLen. diff --git a/conform/run/mods/Widths.raz b/conform/run/mods/Widths.raz new file mode 100644 index 0000000..fb881a3 --- /dev/null +++ b/conform/run/mods/Widths.raz @@ -0,0 +1,6 @@ +module Widths; + +(* ranges of wide characters, first and last, as a table made by a program would be *) +const Wide*: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]]; + +end Widths. diff --git a/doc/compiler.md b/doc/compiler.md index c0463c2..d541ede 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is eleven files of raz, about 11,100 lines of code (the +The compiler is eleven files of raz, about 11,200 lines of code (the budget is 14,000). It is its own compiler: the frozen Pascal compiler in `seed/` builds `src/` the first time, and after that a raz builds `src/` (`make fixpoint` checks that it makes @@ -20,7 +20,7 @@ | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 1,986 | 1,617 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,000 | 3,478 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,049 | 3,518 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 305 | 238 | The command line | @@ -1267,7 +1267,16 @@ read-only data: `aggregateConst` (`Parse.raz`) reads its literal, part by part in `constPart`, into the bytes of a chunk, as a string constant's are; an address in it, of a string, an array constant, a -procedure or an `ERROR`, becomes a data fixup. `makeItem` makes it an +procedure or an `ERROR`, becomes a data fixup. A constant typed `[]T` +takes its length from its literal: `literalArray` makes the type +`[n]T`, as if `n` were written, with `n` counted ahead by +`literalLength`, which reads the literal once, counting the commas +outside any inner brackets, and goes back to its `[` (`saveScan`, +`restoreScan`), so `constPart` reads it as it reads any array's: + + const P: []I64 = [2, 3, 5]; (* read as *) const P: [3]I64 = [2, 3, 5]; + +`makeItem` makes it an `mVar` there that isn't `isVar`, so it is read, indexed, sliced and passed, never changed. `index` and `slice` have Gen make the bounds checks, unless the module says `check off;` (`checks`, set by diff --git a/doc/raz.md b/doc/raz.md index 7de68ca..d4551c6 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -34,9 +34,6 @@ sixth thaw: an array constant typed `[]T` takes its length from its literal (section 4); it breaks no program. -The compiler follows version 2026-10.28 until it has what 2026-10.29 -adds. - The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program written for it. What anyone wishes it had waits on a list, for a later diff --git a/src/Parse.raz b/src/Parse.raz index 705010a..cf562c0 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3130,6 +3130,53 @@ return c, nil; end; +(* The number of elements of the array literal at the scanner, at its + "[": read ahead, counting the commas outside any inner brackets, + then read again from the "[" (saveScan, restoreScan). Its slips are + told when it is read again. *) +proc literalLength: INT; +var st: Scan.State; depth, n: INT; wasSkipping: BOOL; +begin + Scan.saveScan(st); + wasSkipping := Scan.skipping; + Scan.setSkipping(true); + Scan.next; + n := 0; + if Scan.tok # Scan.sRbrak then n := 1; end; + depth := 0; + while (Scan.tok # Scan.sEof) and ((depth > 0) or (Scan.tok # Scan.sRbrak)) do + match Scan.tok of + | sLparen, sLbrak, sLbrace: INC(depth); + | sRparen, sRbrak, sRbrace: DEC(depth); + | sComma: if depth = 0 then INC(n); end; + | else: + end; + Scan.next; + end; + Scan.setSkipping(wasSkipping); + Scan.restoreScan(st); + return n; +end; + +(* A constant typed []T is an array constant whose length is its + literal's (report, 4): t, the slice type, becomes [n]T, as if n had + been written, with n counted ahead (literalLength). *) +proc literalArray(t: PType; start: Diag.Pos): (PType, ERROR); +var a: PType; n: INT; +begin + if Scan.tok # Scan.sLbrak then + return nil, Scan.markAt(Scan.tokPos(), say1("expected a literal of {}", Sym.typeName(t))); + end; + n := literalLength(); + if n = 0 then + return nil, Scan.markAt(Scan.spanFrom(start), "an array's length is from 1 to 2^40, found 0"); + end; + a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align); + a^.len := I64(n); + a^.base := t^.base; + return a, nil; +end; + proc constDecl(top: BOOL): ERROR; var n, c: INT; exp: BOOL; x: TItem; o: PObj; t: PType; start, at: Diag.Pos; begin @@ -3140,11 +3187,13 @@ Scan.next; start := Scan.tokPos(); t := try parseType(); - if (t # Sym.tError) and not ((t^.form = Sym.fArray) or ((t^.form = Sym.fRecord) and not t^.tuple)) then + if (t # Sym.tError) and not ((t^.form = Sym.fArray) or (t^.form = Sym.fSlice) + or ((t^.form = Sym.fRecord) and not t^.tuple)) then return Scan.markAt(Scan.spanFrom(start), "only an ERROR, an array or a record constant has a type"); end; end; try expect(Scan.sEql); + if (t # nil) and (t^.form = Sym.fSlice) then t := try literalArray(t, start); end; if (t # nil) and ((t^.form = Sym.fArray) or (t^.form = Sym.fRecord)) then o := Sym.newObj(Sym.oConst, n, t); o^.chunk := try aggregateConst(t, n); diff --git a/src/Raz.raz b/src/Raz.raz index e9d7e4a..0961567 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -29,7 +29,7 @@ import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; const - Report = "2026-10.28"; (* the version of doc/raz.md it follows *) + Report = "2026-10.29"; (* the version of doc/raz.md it follows *) MemfdCreate = 319; (* the system call *) MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) Execve = 59; diff --git a/src/check.sh b/src/check.sh index 5aa210b..7a32a61 100755 --- a/src/check.sh +++ b/src/check.sh @@ -13,7 +13,8 @@ # converts the narrower of two integers; and views, v.(T), # whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz # and, through Math, test/bench/NBody.raz use; and returning a slice of -# the frame as an error (report 2026-10.27). +# the frame as an error (report 2026-10.27); and an array constant +# typed []T (report 2026-10.29). newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz @@ -21,7 +22,8 @@ conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz conform/err/IntResultsNarrow.raz conform/err/GenIntTypes.raz lib/Math.raz examples/Elf.raz test/bench/NBody.raz - conform/err/FrameSlice.raz" + conform/err/FrameSlice.raz + conform/run/ArrayConstLen.raz conform/err/ArrayConstLenEmpty.raz conform/err/ArrayConstLenLiteral.raz" newer() { case $1 in conform/*/View*.raz) return 0 ;; esac for g in $newer; do [ $g = $1 ] && return 0; done diff --git a/test/msg/ArrayConstLenEmpty.err b/test/msg/ArrayConstLenEmpty.err new file mode 100644 index 0000000..8439585 --- /dev/null +++ b/test/msg/ArrayConstLenEmpty.err @@ -0,0 +1,4 @@ +conform/err/ArrayConstLenEmpty.raz:2:10: error: an array's length is from 1 to 2^40, found 0 + 2 | const P: []I64 = []; + | ^^^^^^^ +raz: 1 error diff --git a/test/msg/ArrayConstLenLiteral.err b/test/msg/ArrayConstLenLiteral.err new file mode 100644 index 0000000..3319855 --- /dev/null +++ b/test/msg/ArrayConstLenLiteral.err @@ -0,0 +1,4 @@ +conform/err/ArrayConstLenLiteral.raz:3:16: error: expected a literal of []I64 + 3 | P: []I64 = Q; + | ^ +raz: 1 error