| | @@ -88,8 +88,11 @@ |
| 88 | 88 | **Operators and delimiters:** |
| 89 | 89 | |
| 90 | 90 | ``` |
| 91 | | -+ - * / = # < <= > >= := : ; , . .. ... ( ) [ ] { } ^ | |
| 91 | ++ - * / & | ~ = # < <= > >= := : ; , . .. ... ( ) [ ] { } ^ |
| 92 | 92 | ``` |
| 93 | + |
| 94 | +`|` is also what starts an arm of `match` (section 7.4); an arm comes |
| 95 | +where a statement could, never after an operand, so the two don't mix. |
| 93 | 96 | |
| 94 | 97 | ## 3. Modules |
| 95 | 98 | |
| | @@ -130,7 +133,20 @@ |
| 130 | 133 | share one instance. Since every instance is checked on its own, an |
| 131 | 134 | operation the argument type doesn't support (adding two records, say) |
| 132 | 135 | is reported as an error in that instance. |
| 136 | + |
| 137 | +An instance is made where its module is first used, not at the |
| 138 | +import: so the arguments may name types the importing module declares |
| 139 | +after its imports, as long as they are declared before that first |
| 140 | +use. |
| 133 | 141 | |
| 142 | +``` |
| 143 | +import Table := Map([]U8, Colour, Str.hash, Str.eq); |
| 144 | + |
| 145 | +type Colour = (red, green, blue); |
| 146 | + |
| 147 | +var names: Table.Map; (* the instance is made here *) |
| 148 | +``` |
| 149 | + |
| 134 | 150 | What a generic module needs to do with its types, beyond moving them |
| 135 | 151 | about, it is given as procedures, after the types: |
| 136 | 152 | |
| | @@ -389,14 +405,16 @@ |
| 389 | 405 | ## 6. Expressions |
| 390 | 406 | |
| 391 | 407 | ``` |
| 392 | | -Expr = SimpleExpr [ Relation SimpleExpr ] . |
| 393 | | -Relation = "=" | "#" | "<" | "<=" | ">" | ">=" | "in" . |
| 408 | +Expr = Conjunct { "or" Conjunct } . |
| 409 | +Conjunct = Relation { "and" Relation } . |
| 410 | +Relation = SimpleExpr [ RelOp SimpleExpr ] . |
| 411 | +RelOp = "=" | "#" | "<" | "<=" | ">" | ">=" | "in" . |
| 394 | 412 | SimpleExpr = [ "-" ] Term { AddOp Term } . |
| 395 | | -AddOp = "+" | "-" | "or" | "xor" . |
| 413 | +AddOp = "+" | "-" | "|" | "xor" . |
| 396 | 414 | Term = Factor { MulOp Factor } . |
| 397 | | -MulOp = "*" | "/" | "div" | "mod" | "and" | "shl" | "shr" . |
| 415 | +MulOp = "*" | "/" | "div" | "mod" | "&" | "shl" | "shr" . |
| 398 | 416 | Factor = number | char | string | Designator | "(" Expr ")" |
| 399 | | - | "not" Factor |
| 417 | + | "not" Factor | "~" Factor |
| 400 | 418 | | "[" [ Expr { "," Expr } ] "]" |
| 401 | 419 | | "{" [ Element { "," Element } ] "}" |
| 402 | 420 | | "^" [ "VOLATILE" ] Type "(" Expr ")" . |
| | @@ -406,14 +424,19 @@ |
| 406 | 424 | | "(" [ Expr { "," Expr } ] ")" . |
| 407 | 425 | ``` |
| 408 | 426 | |
| 409 | | -The operators bind in the order of the grammar: `not` most tightly, |
| 410 | | -then the multiplying operators, then the adding ones, then the |
| 411 | | -relations. So `a + b * c = d` is `(a + (b * c)) = d`. A unary minus |
| 412 | | -applies to the first term: `-a * b` is `-(a * b)`. |
| 427 | +The operators bind in the order of the grammar: `not` and `~` most |
| 428 | +tightly, then the multiplying operators, then the adding ones, then |
| 429 | +the relations, then `and`, then `or`. So `a + b * c = d` is |
| 430 | +`(a + (b * c)) = d`, `x & 0FH = 0` is `(x & 0FH) = 0`, and |
| 431 | +`i < n and a[i] # 0 or done` is `((i < n) and (a[i] # 0)) or done`. A |
| 432 | +unary minus applies to the first term: `-a * b` is `-(a * b)`. |
| 413 | 433 | |
| 434 | +`and`, `or` and `not` are for `BOOL`s only; `&`, `|`, `xor` and `~` |
| 435 | +are for integers only. |
| 436 | + |
| 414 | 437 | ### 6.1 Integers |
| 415 | 438 | |
| 416 | | -Both operands of `+ - * div mod and or xor` have the same integer |
| 439 | +Both operands of `+ - * div mod & | xor` have the same integer |
| 417 | 440 | type, or one is an untyped constant that takes the other's type. The |
| 418 | 441 | result has that type and wraps around at its width: in `U8`, |
| 419 | 442 | `200 + 100` is `44`. |
| | @@ -422,17 +445,16 @@ |
| 422 | 445 | in C: `-7 div 2` is `-3`, `-7 mod 2` is `-1`. Dividing by zero, and |
| 423 | 446 | dividing the smallest `I64` by `-1`, stop the program (`SIGFPE`). |
| 424 | 447 | |
| 425 | | -`and`, `or`, `xor` and `not` work on every bit. `x shl n` and |
| 426 | | -`x shr n` shift by `n and 63`; `shr` brings in copies of the sign bit |
| 448 | +`&` (and), `|` (or), `xor` and `~` (not) work on every bit. `x shl n` |
| 449 | +and `x shr n` shift by `n & 63`; `shr` brings in copies of the sign bit |
| 427 | 450 | for signed types and zeros for unsigned ones. |
| 428 | 451 | |
| 429 | 452 | Relations compare as signed or unsigned according to the type. |
| 430 | 453 | |
| 431 | 454 | ### 6.2 Booleans |
| 432 | 455 | |
| 433 | | -`and` and `or` on `BOOL` evaluate their right operand only when it |
| 434 | | -decides the result. `not` negates, `xor` is "different", and `=` and |
| 435 | | -`#` compare. |
| 456 | +`and` and `or` evaluate their right operand only when it decides the |
| 457 | +result. `not` negates, and `=` and `#` compare; `#` is "different". |
| 436 | 458 | |
| 437 | 459 | ### 6.3 Reals |
| 438 | 460 | |
| | @@ -480,10 +502,17 @@ |
| 480 | 502 | Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)]); |
| 481 | 503 | ``` |
| 482 | 504 | |
| 483 | | -**Strings compare.** Two operands that are each a `[]U8`, a `[n]U8` or |
| 484 | | -a string literal are `=` when they have the same length and the same |
| 485 | | -bytes, and `#` otherwise. Other arrays and slices, records and variants |
| 486 | | -can't be compared; write a loop. Ordering strings is the library's |
| 505 | +**Arrays and strings compare.** Two operands that are each a `[]T` or |
| 506 | +a `[n]T`, of the same element type `T`, are `=` when they have the |
| 507 | +same length and the same elements, and `#` otherwise, where `T` is an |
| 508 | +integer type, `BOOL`, an enumeration, a set or a pointer type. A string |
| 509 | +literal compares as a `[]U8`. The comparison is code of its own where |
| 510 | +it stands, a check of the lengths and then of the bytes, not a call. |
| 511 | + |
| 512 | +Arrays of reals don't compare this way, since `-0.0 = 0.0` and a NaN |
| 513 | +is equal to nothing, though their bytes say otherwise; nor do arrays |
| 514 | +of records, variants or procedures, nor records and variants |
| 515 | +themselves. Write a loop. Ordering strings is the library's |
| 487 | 516 | `Str.cmp`. |
| 488 | 517 | |
| 489 | 518 | ### 6.6 Enumerations and sets |
| | @@ -876,7 +905,7 @@ |
| 876 | 905 | |
| 877 | 906 | | Module | What | |
| 878 | 907 | |---|---| |
| 879 | | -| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; what the compiler's checks call when they fail; string comparison | |
| 908 | +| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; what the compiler's checks call when they fail | |
| 880 | 909 | | `Sys` | system calls: `openRead`, `create`, `read`, `write`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `makeDir`, `info`, `openDir`, `nextEntry`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; and `env` and `workDir`. One file per target, and `Sys.raz` over the C library for the others | |
| 881 | 910 | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | |
| 882 | 911 | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | |
| | @@ -977,4 +1006,11 @@ |
| 977 | 1006 | - **2026-10.2.** `Sys` gains files and directories, the terminal, and |
| 978 | 1007 | the environment (section 11). |
| 979 | 1008 | - **2026-10.3.** `return f(x);` passes on the several results of `f` |
| 980 | | - (sections 7.3 and 8). |
| 1009 | + (sections 7.3 and 8). `and`, `or` and `not` are for `BOOL`s, and |
| 1010 | + bind below the relations; `&`, `|` and `~` are the operators on bits, |
| 1011 | + and `xor` is for integers only (sections 2 and 6). Arrays and slices |
| 1012 | + of integers, `BOOL`s, enumerations, sets and pointers compare with |
| 1013 | + `=`, by code where the comparison stands, not by a call into `Rt` |
| 1014 | + (section 6.5). A generic module's instance is made where it is first |
| 1015 | + used, so its arguments may name types declared after the imports |
| 1016 | + (section 3). |