| | @@ -14,3 +14,133 @@ |
| 14 | 14 | (about 130 bytes) became a value parameter, copied at every call. |
| 15 | 15 | Measured once the port built: not what stands out, the compiler's |
| 16 | 16 | profile is flat (`Scan.tokPos` is 9%). |
| 17 | + |
| 18 | +## Text that runs over lines, or comes from a file |
| 19 | + |
| 20 | +A string ends on the line it starts on, so a long text, a program's |
| 21 | +help or a script it carries, is written as one constant of many |
| 22 | +strings joined by `+`, a `\n` at the end of each. Two ways out, either |
| 23 | +enough: |
| 24 | + |
| 25 | +- a string that may run over lines, its line breaks part of it; |
| 26 | +- a constant whose value is a file's bytes, read when the module is |
| 27 | + compiled, found as a module is: `const Prelude = file "prelude.fs";`. |
| 28 | + The second keeps the text a file of its own, which an editor and |
| 29 | + other tools see as what it is. |
| 30 | + |
| 31 | +Met in tery, a Forth in raz, whose prelude of 270 lines of Forth is |
| 32 | +made into a module of 270 strings by `sed` in its Makefile, every `\` |
| 33 | +and `"` escaped. |
| 34 | + |
| 35 | +## Dereferencing a conversion |
| 36 | + |
| 37 | +`^I64(ADR(mem[a]))^` is an error today ("a conversion can't be |
| 38 | +dereferenced"), and a statement can't start with one, so a program |
| 39 | +that reads and writes typed values in an array of bytes declares a |
| 40 | +pointer variable for each access: |
| 41 | + |
| 42 | +``` |
| 43 | +p := ^I64(ADR(mem[a])); |
| 44 | +p^ := v; |
| 45 | +``` |
| 46 | + |
| 47 | +The proposal: a conversion to a pointer type may be followed by `^`, |
| 48 | +and the designator it makes may stand where any other does, on the |
| 49 | +left of `:=` too, so that the two lines are `^I64(ADR(mem[a]))^ := v;`. |
| 50 | +The grammar's `Designator` would start with a `QualIdent` or such a |
| 51 | +conversion. |
| 52 | + |
| 53 | +Met in tery, whose memory is an array of bytes holding cells: every |
| 54 | +fetch and store of a cell, and in any program that reads a file |
| 55 | +format or a device's registers through a byte buffer. |
| 56 | + |
| 57 | +## A minus sign after an operator |
| 58 | + |
| 59 | +`x & -8` and `n * -1` are errors: a unary minus belongs to the start |
| 60 | +of a `SimpleExpr` (6), not to a factor, so after an operator it needs |
| 61 | +parentheses, `x & (-8)`, or another spelling, `x & ~7`. This is the |
| 62 | +report's grammar, Oberon's, and the compiler is right to refuse it; |
| 63 | +but nearly every other language takes it, and the error ("expected an |
| 64 | +expression, found -") surprises. |
| 65 | + |
| 66 | +The proposal: `"-" Factor` is a factor too. Where the two readings |
| 67 | +both apply, as `-a * b`, they give the same value: an integer's |
| 68 | +negation and product wrap alike, `-(a * b)` and `(-a) * b` are the |
| 69 | +same bits, and so are they for reals. The smallest value of a signed |
| 70 | +type is no exception: it is its own negation. |
| 71 | + |
| 72 | +Met in tery's `aligned`, `(a + 7) & -8`. |
| 73 | + |
| 74 | +## INT and I64 in one expression |
| 75 | + |
| 76 | +An `INT` goes silently into an `I64` where it is assigned, passed or |
| 77 | +returned (5.10), but not in an expression: with `n: INT` and `a: I64`, |
| 78 | +`a + n` is an error, and must be `a + I64(n)`. A program whose numbers |
| 79 | +are `I64`s, a file's size and position, a time, a Forth's cells, and |
| 80 | +whose lengths and indexes are `INT`s, writes `I64(...)` and `INT(...)` |
| 81 | +on line after line. |
| 82 | + |
| 83 | +The proposal: where one operand of an arithmetic operator or a |
| 84 | +relation is an `INT` and the other an `I64` (or `UINT` and `U64`), |
| 85 | +the `INT` is widened, as it would be by an assignment, and the result |
| 86 | +is an `I64`; which, as the assignment's widening, loses nothing on |
| 87 | +any target. The other way, an `I64` into an `INT`, stays written. |
| 88 | + |
| 89 | +Met in tery throughout: its cells are `I64`, and every slice of its |
| 90 | +memory, every stack depth and every length is an `INT`. |
| 91 | + |
| 92 | +## Dividing 128 bits by 64 |
| 93 | + |
| 94 | +`Math.mul` gives the 128-bit product of two `U64`s; there is nothing |
| 95 | +to divide such a number back down. Forth's `UM/MOD` and `M*/`, and |
| 96 | +any arithmetic on numbers wider than a word (a bignum, a fixed-point |
| 97 | +product scaled back), need "this 128-bit number, given as its high |
| 98 | +and low words, divided by a 64-bit one: the quotient and the |
| 99 | +remainder". x86-64 does it in one instruction, `div`; a raz program |
| 100 | +does it by a loop of 64 shifts and subtractions, or a code procedure |
| 101 | +per target. |
| 102 | + |
| 103 | +The proposal, in the library: |
| 104 | + |
| 105 | +``` |
| 106 | +proc div*(hi, lo, d: U64): (U64, U64); (* hi, lo divided by d: the quotient and the remainder; hi < d *) |
| 107 | +``` |
| 108 | + |
| 109 | +in `Math`, beside `mul`; `hi >= d`, whose quotient doesn't fit a word, |
| 110 | +and `d = 0` stop the program as a division by zero does. It breaks no |
| 111 | +program, so it could come with any release, not only a thaw. |
| 112 | + |
| 113 | +Met in tery's `udivmod`, a loop of 64 rounds. |
| 114 | + |
| 115 | +## Sys: the position in a file, and its size |
| 116 | + |
| 117 | +`Sys.seek` moves to a position from a file's start or end, and says |
| 118 | +the new one; nothing says where a file is without moving, nor makes a |
| 119 | +file shorter. tery's `FILE-POSITION`, `FILE-SIZE` and `RESIZE-FILE` |
| 120 | +make the system calls themselves, by their Linux numbers (`lseek`'s |
| 121 | +`SEEK_CUR`, `ftruncate`), which is what `Sys` is there to keep out of |
| 122 | +programs. |
| 123 | + |
| 124 | +The proposal, in `Sys`: |
| 125 | + |
| 126 | +``` |
| 127 | +proc position*(fd: INT): I64; (* where the next read or write is, or minus an errno *) |
| 128 | +proc truncate*(fd: INT; size: I64): INT; (* the file made size bytes long *) |
| 129 | +proc sync*(fd: INT): INT; (* what was written to it on the disk *) |
| 130 | +``` |
| 131 | + |
| 132 | +Each is a system call on Linux and a C call elsewhere, as the rest of |
| 133 | +`Sys` is; they break no program. |
| 134 | + |
| 135 | +## Opt: where the operands are |
| 136 | + |
| 137 | +`Opt.operand` gives the operands one at a time, as slices; nothing |
| 138 | +says where they are among the program's arguments. An interpreter |
| 139 | +whose first operand is a script and the rest the script's own |
| 140 | +arguments, as `tery file arg ...` or `sh script arg ...`, copies |
| 141 | +each into an array of its own to hand them on. |
| 142 | + |
| 143 | +The proposal: `proc at*(p: ^Parser): INT;`, the number of the |
| 144 | +argument `operand` would give next, after what options are left; so |
| 145 | +that the arguments from there on are `Files.arg(i)` for `i` from |
| 146 | +`at(p)` to `ARGC() - 1`, as they are. It breaks no program. |