| | @@ -536,3 +536,184 @@ |
| 536 | 536 | used a `Vec` of rule numbers instead, searched in turn. Library growth |
| 537 | 537 | that breaks no program; about ten lines, with a test in `Map`'s |
| 538 | 538 | `main`. |
| 539 | + |
| 540 | +## A slice of an array a call returns, kept |
| 541 | + |
| 542 | +A defect, of the report or the compiler, not a wish. `f()[0..4]`, a |
| 543 | +slice of the array a call returns, is a slice of a temporary of the |
| 544 | +caller's: the compiler takes the array's place from `allocTemp`, and |
| 545 | +`releaseTemps` gives it back at the end of the statement. Kept in a |
| 546 | +variable, the slice outlives it, and reads what the next statement |
| 547 | +puts there: |
| 548 | + |
| 549 | +``` |
| 550 | +proc f(c: U8): [8]U8; |
| 551 | +var a: [8]U8; i: INT; |
| 552 | +begin |
| 553 | + for i := 0 to 7 do a[i] := c; end; |
| 554 | + return a; |
| 555 | +end; |
| 556 | + |
| 557 | +proc main*; |
| 558 | +var s, t: []U8; i: INT; |
| 559 | +begin |
| 560 | + s := f('a')[0..4]; |
| 561 | + for i := 1 to 3 do t := f('b')[0..4]; end; |
| 562 | + Out.str(s); Out.str(" "); Out.str(t); Out.ln; (* "bbbb" and four blanks *) |
| 563 | +end; |
| 564 | +``` |
| 565 | + |
| 566 | +The report doesn't say how long a returned array lives. 7.3 makes |
| 567 | +returning a slice of one an error, as it is the frame's; section 10 |
| 568 | +leaves undefined only "a slice kept after the call whose frame holds |
| 569 | +what it points at", and `main`'s call hasn't ended. So by the report |
| 570 | +`s` is good until `main` returns, and by the compiler it isn't. |
| 571 | + |
| 572 | +Either mend closes it: the report says the array lives to the end of |
| 573 | +the statement, a slice kept past it undefined, with section 10's |
| 574 | +list; or slicing an array a call returns, anywhere but as an argument |
| 575 | +or in the expression it is used in, is an error, as 7.3's return of |
| 576 | +one is. The second is what 7.3 already checks (`Item.frame`), at more |
| 577 | +places; the first changes nothing but the report. |
| 578 | + |
| 579 | +Met writing libro, trying what the report allows; libro doesn't do it. |
| 580 | + |
| 581 | +## Enumerations' names, each its own |
| 582 | + |
| 583 | +An enumeration's names are declared in the scope around its type |
| 584 | +(5.7), so two enumerations of a module can't share a name, nor a |
| 585 | +procedure have one: with `Style = (bold, italic, ...)`, a second |
| 586 | +`Elem = (..., bold, ...)` is "bold is declared twice", and so is |
| 587 | +`proc rule` beside `Kind = (para, rule)`. A module with two lists of |
| 588 | +kinds, a style and the HTML element that makes it, names one of them |
| 589 | +again: libro's `Flow` has `bEl`, `iEl`, `pEl`, ... for it. |
| 590 | + |
| 591 | +The proposal: a name that two enumerations of a scope share is written |
| 592 | +with its type, `Elem.bold`, where the type doesn't already say which |
| 593 | +(`match` on a `Style`, a `Style` assigned, compared or passed, all say |
| 594 | +which); a name only one enumeration has stays as it is. No program |
| 595 | +changes, since every program today has each name once. It is more |
| 596 | +work for the compiler's lookup, which must hold a name that is two |
| 597 | +things until the type is known. |
| 598 | + |
| 599 | +Met in libro's `Flow`. |
| 600 | + |
| 601 | +## `continue` |
| 602 | + |
| 603 | +`break` leaves a loop; nothing goes on to its next round. A loop that |
| 604 | +passes over some of what it reads (a byte to skip, an entry that |
| 605 | +doesn't match) puts the rest of its body in an `if`, or a flag, and a |
| 606 | +loop with several such tests grows a level of `if` for each: |
| 607 | + |
| 608 | +``` |
| 609 | +while i < LEN(t) do |
| 610 | + ch, n := Str.decode(t[i..LEN(t)]); |
| 611 | + if ch = 0ADH then |
| 612 | + (* passed over *) |
| 613 | + elsif ... then |
| 614 | + end; |
| 615 | + i := i + n; |
| 616 | +end; |
| 617 | +``` |
| 618 | + |
| 619 | +The proposal: `continue`, in a `while`, `repeat` or `for`, ends the |
| 620 | +round, as reaching the end of the body would: a `for` steps its |
| 621 | +variable, `repeat` tests its condition. A deferred statement can't |
| 622 | +hold it, as it can't hold `break`. It breaks a program that has a name |
| 623 | +`continue`, so it waits for a thaw. |
| 624 | + |
| 625 | +Met in libro and rml, more than once each. |
| 626 | + |
| 627 | +## A byte into an `INT` |
| 628 | + |
| 629 | +A `U8` doesn't go into an `INT` without a conversion, though no value |
| 630 | +of one is lost there: `c := keys[at]` is "can't use U8 as INT", and a |
| 631 | +parser of digits writes `INT(s[i]) - '0'` at every digit. 2026-10.26 |
| 632 | +made `INT` widen to `I64` wherever it meets one, for the same reason; |
| 633 | +the small types into `INT` are the same, and more common. |
| 634 | + |
| 635 | +The proposal: `U8`, `U16`, `U32`, `I8`, `I16` and `I32` widen to `INT` |
| 636 | +where their value goes to one (assigned, passed, returned, compared, |
| 637 | +in an operator with an `INT`), as `INT` to `I64` does (5.10). Not `U32` |
| 638 | +to an `INT` of 32 bits, on a target that has one, and never a wider |
| 639 | +type to a narrower, nor `INT` back to any. No program changes. |
| 640 | + |
| 641 | +Met in libro's `Term`, `Flow` and `Epub`, and rml's `Rml`, wherever |
| 642 | +a byte is a number. |
| 643 | + |
| 644 | +## An array constant's length, from its literal |
| 645 | + |
| 646 | +A typed array constant states its length, and its literal must have |
| 647 | +that many elements (4, 6.5): `Zero: [357]Range = [...]`. A table a |
| 648 | +program makes, as libro's tables of character widths are made from |
| 649 | +Unicode's data, must be counted, and the count changed with it; one |
| 650 | +written by hand is counted by hand. `const T: []INT = [1, 2, 3]` is |
| 651 | +"only an ERROR, an array or a record constant has a type". |
| 652 | + |
| 653 | +The proposal: `[]T` as the type of an array constant, its length the |
| 654 | +literal's, as a string constant's is the string's; it is used as an |
| 655 | +array constant of that length is, `LEN` a constant. No program |
| 656 | +changes. |
| 657 | + |
| 658 | +Met in libro's `Layout` and rml's entities, 357, 121 and 253 elements. |
| 659 | + |
| 660 | +## `Str.free` |
| 661 | + |
| 662 | +`Str.copy(a, s)` gives a copy from the allocator `a`; giving it back |
| 663 | +is `Mem.free(a, ADR(c[0]))`, and only if `LEN(c) > 0`, since an empty |
| 664 | +copy is `""` and was never the allocator's. Every program that copies |
| 665 | +strings and frees them writes that test. |
| 666 | + |
| 667 | +``` |
| 668 | +proc free*(a: ^Mem.Allocator; s: []U8); (* a copy Str.copy made, given back; "" is none *) |
| 669 | +``` |
| 670 | + |
| 671 | +Library growth that breaks no program; three lines. Met in libro's |
| 672 | +`Epub.done`. |
| 673 | + |
| 674 | +## The width of a character on a terminal |
| 675 | + |
| 676 | +`Str` says, rightly, that it doesn't know how wide a character is on a |
| 677 | +terminal; a program with a screen must, to wrap its lines and to cut |
| 678 | +them at the window's edge, and each writes wcwidth again. libro's |
| 679 | +`Layout.width` is one: 0 for the combining marks, format characters |
| 680 | +and Hangul's medial jamo, 2 for East Asian Wide and Fullwidth, emoji |
| 681 | +among them, 1 for the rest, from two sorted tables of ranges (357 and |
| 682 | +121) made with Python's `unicodedata`, Unicode 15.1. |
| 683 | + |
| 684 | +The proposal: that, as `Str.width(c: I32): INT` and |
| 685 | +`Str.columns(s: []U8): INT`, or a module of its own, since the tables |
| 686 | +are about 4 KB that only such programs want. It is the same answer as |
| 687 | +glibc's `wcwidth` for nearly every character; the differences, the |
| 688 | +ambiguous widths among them, are taken as 1, as most terminals take |
| 689 | +them. |
| 690 | + |
| 691 | +Met in libro; every program with a screen, rmacs and ravite among |
| 692 | +them, needs the same. |
| 693 | + |
| 694 | +## Why `Files.read` failed |
| 695 | + |
| 696 | +`Files.read(path, b)` is false if the file can't be read, and says no |
| 697 | +more: libro can say "can't read the file", but not that it is missing, |
| 698 | +a directory, or not allowed. The program that wants to know opens it |
| 699 | +itself, with `Sys.openRead`, and reads it as `Files.read` does. |
| 700 | + |
| 701 | +The proposal: `proc load*(path: []U8; b: ^Buf.Buf): INT`, 0, or minus |
| 702 | +the errno of the open or the read that failed, as `Sys` gives them; |
| 703 | +`read` stays as it is. Library growth that breaks no program. |
| 704 | + |
| 705 | +Met in libro. |
| 706 | + |
| 707 | +## `Rt.stop`'s line feed, said |
| 708 | + |
| 709 | +`Rt.stop(msg)` writes `msg` and then a line feed, but its interface's |
| 710 | +comment, and the library volume's, say "msg on standard error, then |
| 711 | +exit with status 2": a program that ends its message with `\n`, as a |
| 712 | +message to standard error usually is, prints a blank line after it. |
| 713 | +libro's first did. |
| 714 | + |
| 715 | +The mend: the comment says "msg, and a line feed, on standard error, |
| 716 | +then exit with status 2", in `lib/Rt.raz` and `doc/raz-lib.md`. No |
| 717 | +program changes. |
| 718 | + |
| 719 | +Met in libro. |