The compiler: a variant in a constant (report 2026-10.33, section 4) A typed constant may be a variant, or hold variants among its elements and fields. constCase reads the case by its name, not by expression, which would make the code that builds one, and writes its number and then each field where the case lays it out, the rest zero. A constant of a variant type is a variable in read-only data, as an array or record constant is; a match on it gives names that are only read. An untyped variant constant is an error that says to write its type. conform/err's ArrayConstElem, which said a constant holds no variant, now tests a field that isn't a constant.
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | module ArrayConstElem; |
| 2 | -type S = variant a; b; end; | |
| 3 | -const P: [1]S = [a]; | |
| 2 | +type S = variant a(n: INT); b; end; | |
| 3 | +var v: INT; | |
| 4 | +const P: [1]S = [a(v)]; | |
| 4 | 5 | end ArrayConstElem. |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module VariantConstMatch; | |
| 2 | +type Shape = variant circle(r: INT); dot; end; | |
| 3 | +const C: Shape = circle(1); | |
| 4 | +proc main*; | |
| 5 | +begin | |
| 6 | + match C of | |
| 7 | + | circle(r): r := 2; | |
| 8 | + | dot: | |
| 9 | + end; | |
| 10 | +end; | |
| 11 | +end VariantConstMatch. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +module VariantConstUntyped; | |
| 2 | +type Shape = variant circle(r: INT); dot; end; | |
| 3 | +const D = dot; | |
| 4 | +end VariantConstUntyped. |
| @@ -0,0 +1,8 @@ | ||
| 1 | +circle 3 | |
| 2 | +rect 2 3 | |
| 3 | +dot | |
| 4 | +box 7 | |
| 5 | +rect 4 5 | |
| 6 | +x 1 | |
| 7 | +rect 4 5 | |
| 8 | +20 |
| @@ -0,0 +1,38 @@ | ||
| 1 | +module VariantConst; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +type | |
| 5 | + Shape = variant circle(r: F64); rect(w, h: INT); dot; named(name: []U8; n: U8); end; | |
| 6 | + Rule = record tag: INT; s: Shape; end; | |
| 7 | + | |
| 8 | +const | |
| 9 | + Shapes: []Shape = [circle(1.5), rect(2, 3), dot, named("box", 7)]; | |
| 10 | + One: Shape = rect(4, 5); | |
| 11 | + Rules: [2]Rule = [{tag: 1, s: dot}, {tag: 2, s: named("x", 1)}]; | |
| 12 | + | |
| 13 | +proc show(const s: Shape); | |
| 14 | +begin | |
| 15 | + match s of | |
| 16 | + | circle(r): Out.str("circle "); Out.int(I64(r * 2.0)); | |
| 17 | + | rect(w, h): Out.str("rect "); Out.int(w); Out.char(' '); Out.int(h); | |
| 18 | + | dot: Out.str("dot"); | |
| 19 | + | named(name, n): Out.str(name); Out.char(' '); Out.int(I64(n)); | |
| 20 | + end; | |
| 21 | + Out.ln; | |
| 22 | +end; | |
| 23 | + | |
| 24 | +proc main*; | |
| 25 | +var i: INT; s: Shape; | |
| 26 | +begin | |
| 27 | + for i := 0 to LEN(Shapes) - 1 do show(Shapes[i]); end; | |
| 28 | + show(One); | |
| 29 | + show(Rules[1].s); | |
| 30 | + s := One; | |
| 31 | + show(s); | |
| 32 | + match One of | |
| 33 | + | rect(w, h): Out.int(w * h); Out.ln; | |
| 34 | + | else: | |
| 35 | + end; | |
| 36 | +end; | |
| 37 | + | |
| 38 | +end VariantConst. |
An empty file.
| @@ -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` | 2,091 | 1,682 | Items: types, constants, variables, operators, conversions, calls | |
| 23 | -| `src/Parse.raz` | 4,182 | 3,619 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 23 | +| `src/Parse.raz` | 4,218 | 3,648 | 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` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| 26 | 26 | | `src/Raz.raz` | 326 | 253 | The command line | |
| @@ -1286,6 +1286,15 @@ | ||
| 1286 | 1286 | `restoreScan`), so `constPart` reads it as it reads any array's: |
| 1287 | 1287 | |
| 1288 | 1288 | const P: []I64 = [2, 3, 5]; (* read as *) const P: [3]I64 = [2, 3, 5]; |
| 1289 | + | |
| 1290 | +A variant in a constant, `const S: Shape = circle(1.0)` or one among | |
| 1291 | +an array constant's elements, is read by `constCase`, not by | |
| 1292 | +`expression`, which would make the code that builds a case: it finds | |
| 1293 | +the case by its name (`qualIdent`), writes its number in the first 8 | |
| 1294 | +bytes, then each field at the offset the case gives it, by `constPart`, | |
| 1295 | +leaving the rest zero, as a variable starts. A constant of a variant | |
| 1296 | +type is then a variable in read-only data, as an array or record | |
| 1297 | +constant is, and a `match` on it gives names that are only read. | |
| 1289 | 1298 | |
| 1290 | 1299 | With no element, `[]`, the type stays `[]T`, and the constant is 16 |
| 1291 | 1300 | zero bytes in read-only data: the empty slice, `ptr` `nil` and `len` 0, |
| @@ -213,6 +213,8 @@ | ||
| 213 | 213 | .Cm dot , |
| 214 | 214 | and read only by |
| 215 | 215 | .Cm match . |
| 216 | +A typed constant may hold variants: | |
| 217 | +.Cm "const Shapes: []Shape = [circle(1.0), dot];" | |
| 216 | 218 | .It |
| 217 | 219 | .Cm "^VOLATILE T" |
| 218 | 220 | points at what something else may change: each access is one access, |
| @@ -258,9 +258,9 @@ | ||
| 258 | 258 | | oConst: |
| 259 | 259 | if o^.typ = Sym.tStr then |
| 260 | 260 | x.mode := mStr; x.val := o^.val; x.chunk := o^.chunk; |
| 261 | - elsif o^.typ^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice} then | |
| 262 | - (* an array, record or empty slice constant: a variable in read-only | |
| 263 | - data, not one of the program's, so nothing can change it *) | |
| 261 | + elsif o^.typ^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice, Sym.fVariant} then | |
| 262 | + (* an array, record, variant or empty slice constant: a variable in | |
| 263 | + read-only data, not one of the program's, so nothing can change it *) | |
| 264 | 264 | x.mode := mVar; x.base := Gen.bGlobal; x.chunk := o^.chunk; |
| 265 | 265 | else |
| 266 | 266 | x.mode := mConst; x.val := o^.val; |
| @@ -3182,6 +3182,37 @@ | ||
| 3182 | 3182 | return n; |
| 3183 | 3183 | end; |
| 3184 | 3184 | |
| 3185 | +(* A variant's case built from constants, at offset off of the constant | |
| 3186 | + (report section 4): its number, then each field where the case lays | |
| 3187 | + it out, as constructCase builds one in a program; the rest stays | |
| 3188 | + zero. The case is read here, by its name, since expression would | |
| 3189 | + make the code that builds one. *) | |
| 3190 | +proc constPart(t: PType; off: INT): ERROR; forward; | |
| 3191 | + | |
| 3192 | +proc constCase(t: PType; off: INT): ERROR; | |
| 3193 | +var c, f: PObj; at: Diag.Pos; | |
| 3194 | +begin | |
| 3195 | + at := Scan.tokPos(); | |
| 3196 | + c := try qualIdent(); | |
| 3197 | + if (c^.cls # Sym.oCase) or (c^.typ # t) then | |
| 3198 | + return Scan.markAt(Scan.spanFrom(at), say1("expected a case of {}", Sym.typeName(t))); | |
| 3199 | + end; | |
| 3200 | + putBytes(off, 8, c^.val); | |
| 3201 | + f := c^.fields; | |
| 3202 | + if Scan.tok = Scan.sLparen then | |
| 3203 | + Scan.next; | |
| 3204 | + while f # nil do | |
| 3205 | + try constPart(f^.typ, off + INT(f^.val)); | |
| 3206 | + f := f^.next; | |
| 3207 | + if f # nil then try expect(Scan.sComma); end; | |
| 3208 | + end; | |
| 3209 | + return expect(Scan.sRparen); | |
| 3210 | + elsif f # nil then | |
| 3211 | + return Scan.markAt(Scan.spanFrom(at), say1("{} needs its fields", nameOf(c^.name))); | |
| 3212 | + end; | |
| 3213 | + return nil; | |
| 3214 | +end; | |
| 3215 | + | |
| 3185 | 3216 | (* The part of type t at offset off of the constant *) |
| 3186 | 3217 | proc constPart(t: PType; off: INT): ERROR; |
| 3187 | 3218 | var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st: INT; |
| @@ -3228,6 +3259,7 @@ | ||
| 3228 | 3259 | end; |
| 3229 | 3260 | return expect(Scan.sRbrace); |
| 3230 | 3261 | end; |
| 3262 | + if (t^.form = Sym.fVariant) and (Scan.tok = Scan.sIdent) then return constCase(t, off); end; | |
| 3231 | 3263 | try expression(e, t); |
| 3232 | 3264 | if (e.mode = Item.mStr) and ((t^.form = Sym.fSlice) or (t^.form = Sym.fArray)) and (t^.base = Sym.tU8) then |
| 3233 | 3265 | if t^.form = Sym.fSlice then |
| @@ -3251,7 +3283,7 @@ | ||
| 3251 | 3283 | putBytes(off + 8, 8, e.typ^.len); |
| 3252 | 3284 | end; |
| 3253 | 3285 | elsif t^.form = Sym.fVariant then |
| 3254 | - return Scan.markAt(e.pos, "a constant holds no variant"); | |
| 3286 | + return Scan.markAt(e.pos, say1("expected a case of {}", Sym.typeName(t))); | |
| 3255 | 3287 | elsif (t^.form = Sym.fArray) or (t^.form = Sym.fRecord) then |
| 3256 | 3288 | return Scan.markAt(e.pos, say1("expected a literal of {}", Sym.typeName(t))); |
| 3257 | 3289 | else |
| @@ -3319,13 +3351,13 @@ | ||
| 3319 | 3351 | start := Scan.tokPos(); |
| 3320 | 3352 | t := try parseType(); |
| 3321 | 3353 | if (t # Sym.tError) and not ((t^.form = Sym.fArray) or (t^.form = Sym.fSlice) |
| 3322 | - or ((t^.form = Sym.fRecord) and not t^.tuple)) then | |
| 3323 | - return Scan.markAt(Scan.spanFrom(start), "only an ERROR, an array or a record constant has a type"); | |
| 3354 | + or ((t^.form = Sym.fRecord) and not t^.tuple) or (t^.form = Sym.fVariant)) then | |
| 3355 | + return Scan.markAt(Scan.spanFrom(start), "only an ERROR, an array, a record or a variant constant has a type"); | |
| 3324 | 3356 | end; |
| 3325 | 3357 | end; |
| 3326 | 3358 | try expect(Scan.sEql); |
| 3327 | 3359 | if (t # nil) and (t^.form = Sym.fSlice) then t := try literalArray(t); end; |
| 3328 | - if (t # nil) and (t^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice}) then | |
| 3360 | + if (t # nil) and (t^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice, Sym.fVariant}) then | |
| 3329 | 3361 | o := Sym.newObj(Sym.oConst, n, t); |
| 3330 | 3362 | o^.chunk := try aggregateConst(t, n); |
| 3331 | 3363 | o^.pos := at; |
| @@ -3334,6 +3366,10 @@ | ||
| 3334 | 3366 | return expect(Scan.sSemicolon); |
| 3335 | 3367 | end; |
| 3336 | 3368 | try expression(x, nil); |
| 3369 | + if x.typ^.form = Sym.fVariant then | |
| 3370 | + return Scan.markHelp(x.pos, "a variant constant needs its type", | |
| 3371 | + say2("write const {}: {} = ...", nameOf(n), Sym.typeName(x.typ))); | |
| 3372 | + end; | |
| 3337 | 3373 | o := Sym.newObj(Sym.oConst, n, x.typ); |
| 3338 | 3374 | o^.pos := at; |
| 3339 | 3375 | o^.exported := exp and top; |
| @@ -1,4 +1,4 @@ | ||
| 1 | -conform/err/ArrayConstElem.raz:3:18: error: a constant holds no variant | |
| 2 | - 3 | const P: [1]S = [a]; | |
| 3 | - | ^ | |
| 1 | +conform/err/ArrayConstElem.raz:4:20: error: expected a constant, found INT | |
| 2 | + 4 | const P: [1]S = [a(v)]; | |
| 3 | + | ^ | |
| 4 | 4 | raz: 1 error |
| @@ -1,4 +1,4 @@ | ||
| 1 | -conform/err/StmtErrType.raz:2:10: error: only an ERROR, an array or a record constant has a type | |
| 1 | +conform/err/StmtErrType.raz:2:10: error: only an ERROR, an array, a record or a variant constant has a type | |
| 2 | 2 | 2 | const k: I64 = "a"; |
| 3 | 3 | | ^^^ |
| 4 | 4 | raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/VariantConstMatch.raz:7:18: error: r is only read here, and can't be changed | |
| 2 | + 7 | | circle(r): r := 2; | |
| 3 | + | ^ | |
| 4 | +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/VariantConstUntyped.raz:3:11: error: a variant constant needs its type | |
| 2 | + 3 | const D = dot; | |
| 3 | + | ^^^ | |
| 4 | +help: write const D: Shape = ... | |
| 5 | +raz: 1 error |