| | @@ -10,8 +10,11 @@ |
| 10 | 10 | doesn't allow, raz doesn't have. |
| 11 | 11 | |
| 12 | 12 | This is version 2026-10.13 of the report; section 12 says what each |
| 13 | | -version changed. It is a thaw the owner chose, for a language anyone |
| 14 | | -can build again, and it breaks programs written for 2026-10.10. |
| 13 | +version changed. 2026-10.11 and 2026-10.12 are a thaw the owner chose, |
| 14 | +for a language anyone can build again. They break programs written |
| 15 | +for 2026-10.10 and before: for 2026-10.9, say, the report of compiler |
| 16 | +0.5.0, the last release before them; and 2026-10.12 breaks code |
| 17 | +procedures written for 2026-10.11. |
| 15 | 18 | |
| 16 | 19 | The language is frozen, softly, from this version on: it changes only |
| 17 | 20 | to mend a defect, and its library grows only by what breaks no program |
| | @@ -138,8 +141,10 @@ |
| 138 | 141 | not a variable: nothing that could write it, or let something else |
| 139 | 142 | write it, may name it or a field or element of it. So it can't be |
| 140 | 143 | assigned, passed as a `var` argument, changed by `INC`, `DEC` or |
| 141 | | -`NEW`, given to `ADR`, sliced, or used where a slice is expected (the |
| 142 | | -silent conversion of an array, section 5.10); in `match`, its arms' |
| 144 | +`NEW`, or given to `ADR`; an array in it can't be sliced, or used |
| 145 | +where a slice is expected (the silent conversion of an array, section |
| 146 | +5.10), though a slice in it may be sliced, since that makes a new |
| 147 | +slice and can't change the variable; in `match`, its arms' |
| 143 | 148 | names stand for the fields of a copy. It can be read, copied and |
| 144 | 149 | compared, and a copy is the importer's own. What a pointer or a slice |
| 145 | 150 | in it points at is another variable, and isn't held to this: with |
| | @@ -294,7 +299,8 @@ |
| 294 | 299 | of arrays and strings. A constant is an integer, a real, a `BOOL`, a |
| 295 | 300 | name of an enumeration, a set, a string, an array or an `ERROR`. A constant of an integer type wraps as the |
| 296 | 301 | program would: `U8(250) + 10` is 4. Dividing by zero in a constant is |
| 297 | | -an error. A string constant can be indexed and sliced as an array of |
| 302 | +an error, and so is dividing the smallest value of a signed type by -1, |
| 303 | +with `div` or `mod`, which stops a program (section 6.1). A string constant can be indexed and sliced as an array of |
| 298 | 304 | `U8` is, and its `LEN` is its length in bytes. `+` joins two string |
| 299 | 305 | constants into one: |
| 300 | 306 | |
| | @@ -556,8 +562,9 @@ |
| 556 | 562 | Nothing else converts silently. In particular, there is no other |
| 557 | 563 | conversion between integer types, between integers and reals, or |
| 558 | 564 | between `F32` and `F64`, without a written conversion (section 6.7). |
| 559 | | -Several results, and `return f(x)`, need the same types exactly: an |
| 560 | | -`INT` result doesn't pass up as an `I64`. |
| 565 | +Results passed on whole need the same types exactly: in `a, b := |
| 566 | +f(x)` and in `return f(x)`, an `INT` result doesn't pass up as an |
| 567 | +`I64`. Elsewhere, `return n, n` included, an `INT` widens as above. |
| 561 | 568 | |
| 562 | 569 | ## 6. Expressions |
| 563 | 570 | |
| | @@ -1208,15 +1215,114 @@ |
| 1208 | 1215 | target is an `I64`: a file's size and position, a time in nanoseconds, |
| 1209 | 1216 | and a number read from text. |
| 1210 | 1217 | |
| 1218 | +What follows is each module's interface, as `tools/Doc.raz` prints it |
| 1219 | +from the module's file; `...` stands for fields that are the module's |
| 1220 | +own. A module's `main`, where it has one, is its test, and is left |
| 1221 | +out here. |
| 1222 | + |
| 1211 | 1223 | ``` |
| 1224 | +module Rt; |
| 1225 | +const Full*: ERROR = "too many procedures to run at exit"; |
| 1226 | +proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *) |
| 1227 | +proc exit*(status: INT); (* the atExit procedures, the last first, then the end *) |
| 1228 | +proc stop*(msg: []U8); (* msg on standard error, then exit with status 2 *) |
| 1229 | +proc trace*; (* the calls under way, and their variables, on standard error *) |
| 1230 | +``` |
| 1231 | + |
| 1232 | +`Rt` also holds what the compiler's checks call when they fail, and |
| 1233 | +the handlers of the signals that stop a program (section 10). `exit` |
| 1234 | +takes each procedure off the list before it runs it, so it runs once |
| 1235 | +even if it fails or calls `exit` itself; in a program that uses C, it |
| 1236 | +ends through C's `exit`, so that C flushes its buffers too. |
| 1237 | + |
| 1238 | +``` |
| 1212 | 1239 | module Mem; |
| 1213 | 1240 | type |
| 1214 | 1241 | Allocator* = record |
| 1215 | | - alloc*: proc(a: ^Allocator; size, align: INT): ^U8; |
| 1242 | + alloc*: proc(a: ^Allocator; size, align: INT): ^U8; (* nil if out of room *) |
| 1216 | 1243 | free*: proc(a: ^Allocator; p: ^U8); |
| 1217 | 1244 | end; |
| 1218 | | -... |
| 1219 | | -end Mem. |
| 1245 | +const Page* = 4096; |
| 1246 | +proc pages*(size: INT): ^U8; (* size bytes, in whole pages, zeroed; or nil *) |
| 1247 | +proc unmap*(p: ^U8; size: INT); (* pages given back *) |
| 1248 | +proc alloc*(a: ^Allocator; size, align: INT): ^U8; (* a^.alloc(a, size, align) *) |
| 1249 | +proc free*(a: ^Allocator; p: ^U8); (* a^.free(a, p) *) |
| 1250 | + |
| 1251 | +module Heap; |
| 1252 | +var heap*: ^Mem.Allocator; |
| 1253 | + |
| 1254 | +module Arena; |
| 1255 | +type Arena* = record |
| 1256 | + base*: Mem.Allocator; (* first, so that ^Arena(a) finds the rest *) |
| 1257 | + ... |
| 1258 | +end; |
| 1259 | +proc init*(ar: ^Arena; size: INT): BOOL; (* an arena of size bytes; false if the system has none *) |
| 1260 | +proc reset*(ar: ^Arena); (* every allocation forgotten; the memory used again *) |
| 1261 | +proc done*(ar: ^Arena); (* its memory given back *) |
| 1262 | +``` |
| 1263 | + |
| 1264 | +`NEW(p, a)` calls `a^.alloc`. `Heap` keeps blocks of nine sizes, from |
| 1265 | +16 to 4096 bytes with their header, each with a list of the free ones; |
| 1266 | +a bigger block gets pages of its own. An arena hands out memory from |
| 1267 | +one block: its `free` does nothing, and `reset` forgets everything. |
| 1268 | + |
| 1269 | +``` |
| 1270 | +module Lock; |
| 1271 | +type Lock* = record ... end; |
| 1272 | +proc take*(l: ^Lock); |
| 1273 | +proc give*(l: ^Lock); |
| 1274 | + |
| 1275 | +module Thread; |
| 1276 | +const Failed*: ERROR = "can't start a thread"; |
| 1277 | +type |
| 1278 | + Thread* = record ... end; |
| 1279 | + Mutex* = record ... end; (* zero is free *) |
| 1280 | +proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); (* p(arg) in a new thread *) |
| 1281 | +proc join*(t: ^Thread); (* waits for t to end, then frees its stack *) |
| 1282 | +proc lock*(m: ^Mutex); |
| 1283 | +proc unlock*(m: ^Mutex); |
| 1284 | +``` |
| 1285 | + |
| 1286 | +A `Lock` is for short sections: a thread that finds it taken tries |
| 1287 | +again, giving up the processor now and then. A `Mutex` sleeps. Each of |
| 1288 | +`Thread`'s threads runs on a stack of its own, with a page below it |
| 1289 | +that can't be touched, so that running off the stack stops the program. |
| 1290 | +A program that uses C makes its threads with C's `pthread_create`, and |
| 1291 | +so can't use `Thread`. |
| 1292 | + |
| 1293 | +``` |
| 1294 | +module Out; |
| 1295 | +proc str*(s: []U8); |
| 1296 | +proc char*(c: U8); |
| 1297 | +proc ln*; (* a line feed *) |
| 1298 | +proc int*(n: I64); (* n in decimal *) |
| 1299 | +proc hex*(n: U64; digits: INT); (* n in hexadecimal, digits digits at least *) |
| 1300 | +proc flush*; |
| 1301 | +``` |
| 1302 | + |
| 1303 | +**Out and C's stdio.** `Out` keeps a buffer of `Sys.outSize` bytes, |
| 1304 | +4 KB on Linux. It writes it out |
| 1305 | +when it is full, at each line feed when standard output is a terminal, |
| 1306 | +at `Out.flush`, and when the program ends by `Rt.exit`. A lock keeps |
| 1307 | +what each call writes together when threads write at once. C's `stdio` |
| 1308 | +keeps a buffer of its own for the same standard output. A program that |
| 1309 | +writes with both flushes one before writing with the other, or the |
| 1310 | +text comes out of order: |
| 1311 | + |
| 1312 | +``` |
| 1313 | +module Both; |
| 1314 | +import Out, C; (* C: printf, fflush and C's FILE *stdout *) |
| 1315 | + |
| 1316 | +proc main*; |
| 1317 | +begin |
| 1318 | + Out.str("from raz\n"); |
| 1319 | + Out.flush; |
| 1320 | + C.printf("from C\n"); |
| 1321 | + C.fflush(C.stdout); |
| 1322 | + Out.str("from raz again\n"); |
| 1323 | +end; |
| 1324 | + |
| 1325 | +end Both. |
| 1220 | 1326 | ``` |
| 1221 | 1327 | |
| 1222 | 1328 | **Sys** gives every target the same procedures, so that a program |
| | @@ -1225,6 +1331,7 @@ |
| 1225 | 1331 | number, minus an errno, as on Linux; 0 is success. |
| 1226 | 1332 | |
| 1227 | 1333 | ``` |
| 1334 | +module Sys; |
| 1228 | 1335 | const |
| 1229 | 1336 | stdin* = 0; stdout* = 1; stderr* = 2; |
| 1230 | 1337 | outSize* = 4096; (* the bytes Out keeps; each target has its own *) |
| | @@ -1258,10 +1365,18 @@ |
| 1258 | 1365 | end; |
| 1259 | 1366 | Status* = (running, exited, killed, stopped); |
| 1260 | 1367 | |
| 1368 | +proc write*(fd: INT; s: []U8): INT; (* the bytes written *) |
| 1369 | +proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) |
| 1261 | 1370 | proc open*(path: []U8; how: Accesses): INT; (* its fd *) |
| 1262 | 1371 | proc openRead*(path: []U8): INT; (* open(path, {reading}) *) |
| 1263 | 1372 | proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) |
| 1264 | 1373 | proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *) |
| 1374 | +proc close*(fd: INT); |
| 1375 | +proc isTerminal*(fd: INT): BOOL; |
| 1376 | +proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) |
| 1377 | +proc munmap*(p: ^U8; size: INT); |
| 1378 | +proc yield*; (* the processor given to another thread *) |
| 1379 | +proc exit*(status: INT); (* the program ended at once, without Rt's atExit *) |
| 1265 | 1380 | |
| 1266 | 1381 | proc rename*(old, new: []U8): INT; (* replaces what new names *) |
| 1267 | 1382 | proc remove*(path: []U8): INT; (* a file, not a directory *) |
| | @@ -1279,7 +1394,7 @@ |
| 1279 | 1394 | proc rawMode*(fd: INT): BOOL; (* the terminal on fd: no echo, no lines, no signals *) |
| 1280 | 1395 | proc restoreMode*(fd: INT); (* as rawMode found it *) |
| 1281 | 1396 | proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *) |
| 1282 | | -proc ready*(fd: INT; ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *) |
| 1397 | +proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *) |
| 1283 | 1398 | proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) |
| 1284 | 1399 | proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *) |
| 1285 | 1400 | proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) |
| | @@ -1330,19 +1445,6 @@ |
| 1330 | 1445 | CR to NL; and with output, NL to CR NL. A program that turns it on |
| 1331 | 1446 | restores it before it ends: `Rt.atExit` is the place. |
| 1332 | 1447 | |
| 1333 | | -`connect` is for talking to a server on the same machine, an X server |
| 1334 | | -at `/tmp/.X11-unix/X0` say; what it receives comes by `read`, what it |
| 1335 | | -sends goes by `send`, and `close` ends it. `send` is `write` for a |
| 1336 | | -socket, except when the other end has closed it: `write` would stop |
| 1337 | | -the program with the signal SIGPIPE, where `send` returns -32 |
| 1338 | | -(EPIPE), so that a program learns its server has gone and can end as |
| 1339 | | -it wants. `readyOf` waits on several file |
| 1340 | | -descriptors at once, as a program does that listens to a server and a |
| 1341 | | -terminal both; a descriptor at its end, or failed, counts as ready, |
| 1342 | | -since a read of it won't wait. It looks at the first 64 of `fds`, and |
| 1343 | | -`connect` takes a path of at most 107 bytes, as `struct sockaddr_un` |
| 1344 | | -holds; a longer one is -36 (ENAMETOOLONG). |
| 1345 | | - |
| 1346 | 1448 | **Files.** `open` gives a file for what `how` asks: `reading`, |
| 1347 | 1449 | `writing` or both; `creating` it if it is missing, readable by all and |
| 1348 | 1450 | writable by its owner; with `exclusive`, failing with -17 (EEXIST) if |
| | @@ -1442,10 +1544,15 @@ |
| 1442 | 1544 | write on any of `writes`. A descriptor in both is given once, when it |
| 1443 | 1545 | is ready for either; the 64 it looks at are `reads`, then `writes`. |
| 1444 | 1546 | |
| 1445 | | -**Str, Map, Sort, Math and Fmt** gain these: |
| 1547 | +**Strings, buffers and collections.** |
| 1446 | 1548 | |
| 1447 | 1549 | ``` |
| 1448 | 1550 | module Str; |
| 1551 | +proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *) |
| 1552 | +proc eq*(a, b: []U8): BOOL; |
| 1553 | +proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) |
| 1554 | +proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *) |
| 1555 | +proc hasPrefix*(s, p: []U8): BOOL; |
| 1449 | 1556 | proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *) |
| 1450 | 1557 | proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *) |
| 1451 | 1558 | proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *) |
| | @@ -1454,8 +1561,45 @@ |
| 1454 | 1561 | proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *) |
| 1455 | 1562 | proc putBE*(s: []U8; v: U64); (* v into s, the highest byte first *) |
| 1456 | 1563 | |
| 1564 | +module Buf; |
| 1565 | +type Buf* = record len*: INT; ... end; |
| 1566 | +proc init*(b: ^Buf; a: ^Mem.Allocator); |
| 1567 | +proc room*(b: ^Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) |
| 1568 | +proc add*(b: ^Buf; c: U8); |
| 1569 | +proc str*(b: ^Buf; s: []U8); |
| 1570 | +proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *) |
| 1571 | +proc reset*(b: ^Buf); (* emptied, its memory kept *) |
| 1572 | +proc done*(b: ^Buf); (* its memory given back *) |
| 1573 | + |
| 1574 | +module Fmt; |
| 1575 | +type Arg* = variant |
| 1576 | + int(i: I64); |
| 1577 | + uint(u: U64); |
| 1578 | + hex(h: U64); |
| 1579 | + str(s: []U8); |
| 1580 | + char(c: U8); |
| 1581 | + bool(b: BOOL); |
| 1582 | + real(r: F64; places: INT); |
| 1583 | +end; |
| 1584 | +proc print*(f: []U8; args: []Arg); (* to standard output *) |
| 1585 | +proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); (* added to b *) |
| 1586 | + |
| 1587 | +module List(T); |
| 1588 | +type |
| 1589 | + Node* = record next*: ^Node; val*: T; end; |
| 1590 | + List* = record head*: ^Node; len*: INT; ... end; |
| 1591 | +proc init*(l: ^List; a: ^Mem.Allocator); |
| 1592 | +proc push*(l: ^List; v: T): BOOL; (* v at the front; false if the allocator has no room *) |
| 1593 | +proc pop*(l: ^List): (T, BOOL); (* the front value, taken off; false if the list is empty *) |
| 1594 | +proc reverse*(l: ^List); |
| 1595 | +proc clear*(l: ^List); |
| 1596 | + |
| 1457 | 1597 | module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); |
| 1598 | +type Map* = record len*: INT; ... end; |
| 1599 | +proc init*(m: ^Map; a: ^Mem.Allocator); |
| 1600 | +proc put*(m: ^Map; k: K; v: V): BOOL; (* k now maps to v; false if there was no room *) |
| 1458 | 1601 | proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) |
| 1602 | +proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) |
| 1459 | 1603 | proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *) |
| 1460 | 1604 | |
| 1461 | 1605 | module Sort(T; less: proc(a, b: T): BOOL); |
| | @@ -1465,16 +1609,19 @@ |
| 1465 | 1609 | const pi* = 3.141592653589793; |
| 1466 | 1610 | proc sqrt*(x: F64): F64; |
| 1467 | 1611 | proc floor*(x: F64): F64; |
| 1468 | | -proc sin*(x: F64): F64; |
| 1469 | | -proc cos*(x: F64): F64; |
| 1470 | | -proc atan2*(y, x: F64): F64; |
| 1471 | 1612 | proc exp*(x: F64): F64; |
| 1472 | 1613 | proc ln*(x: F64): F64; |
| 1473 | | - |
| 1474 | | -module Fmt; |
| 1475 | | -type Arg* = variant ... real(r: F64; places: INT); end; |
| 1614 | +proc sin*(x: F64): F64; |
| 1615 | +proc cos*(x: F64): F64; |
| 1616 | +proc atan2*(y, x: F64): F64; (* the angle of (x, y) from the x axis, from -pi to pi *) |
| 1476 | 1617 | ``` |
| 1477 | 1618 | |
| 1619 | +A `Buf`, a `List` and a `Map` take their memory from the allocator |
| 1620 | +`init` gives them. `Fmt`'s format takes the next argument at each |
| 1621 | +`{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and |
| 1622 | +compares its keys with the procedures it is given: |
| 1623 | +`import Count := Map([]U8, INT, Str.hash, Str.eq);`. |
| 1624 | + |
| 1478 | 1625 | `Str` reads and writes UTF-8. `encode` writes a character, of at most |
| 1479 | 1626 | 10FFFFH, in one to four bytes; a number that isn't one becomes FFFDH, |
| 1480 | 1627 | the character that stands for a wrong one. `decode` reads the first |
| | @@ -1641,30 +1788,6 @@ |
| 1641 | 1788 | name with `=` in it, a letter `-`, and `-h` or `--help` in the table, |
| 1642 | 1789 | since they are `Opt`'s. |
| 1643 | 1790 | |
| 1644 | | -**Out and C's stdio.** `Out` keeps a buffer of `Sys.outSize` bytes, |
| 1645 | | -4 KB on Linux. It writes it out |
| 1646 | | -when it is full, at each line feed when standard output is a terminal, |
| 1647 | | -at `Out.flush`, and when the program ends by `Rt.exit`. C's `stdio` |
| 1648 | | -keeps a buffer of its own for the same standard output. A program that |
| 1649 | | -writes with both flushes one before writing with the other, or the |
| 1650 | | -text comes out of order: |
| 1651 | | - |
| 1652 | | -``` |
| 1653 | | -module Both; |
| 1654 | | -import Out, C; (* C: printf, fflush and C's FILE *stdout *) |
| 1655 | | - |
| 1656 | | -proc main*; |
| 1657 | | -begin |
| 1658 | | - Out.str("from raz\n"); |
| 1659 | | - Out.flush; |
| 1660 | | - C.printf("from C\n"); |
| 1661 | | - C.fflush(C.stdout); |
| 1662 | | - Out.str("from raz again\n"); |
| 1663 | | -end; |
| 1664 | | - |
| 1665 | | -end Both. |
| 1666 | | -``` |
| 1667 | | - |
| 1668 | 1791 | ## 12. Versions |
| 1669 | 1792 | |
| 1670 | 1793 | A version of this report is named by the month it was made in and a |
| | @@ -1731,8 +1854,8 @@ |
| 1731 | 1854 | Pointers, procedures, `ERROR`s, slices and variants' tags are sized |
| 1732 | 1855 | by the address, and basic types aligned as the target's C aligns |
| 1733 | 1856 | them (section 5). Reals are a target's to have or not (section |
| 1734 | | - 5.1). An exported variable can't be changed, passed by `var`, |
| 1735 | | - addressed or sliced outside its module (section 3). `check off;` |
| 1857 | + 5.1). An exported variable can't be changed, passed by `var` or |
| 1858 | + addressed outside its module, nor an array in it sliced (section 3). `check off;` |
| 1736 | 1859 | leaves a module's index and slice checks out (sections 3, 6.4 and |
| 1737 | 1860 | 6.5). An index of any integer type is checked by its value (6.5). A |
| 1738 | 1861 | shift counts modulo its operand's width, and the smallest value of |
| | @@ -1761,9 +1884,9 @@ |
| 1761 | 1884 | |
| 1762 | 1885 | Everything in this report binds every implementation of raz, except |
| 1763 | 1886 | what follows, which each chooses for itself and documents. This |
| 1764 | | -compiler's choices are in `doc/compiler.md`. The programs in |
| 1765 | | -`conform/` are how an implementation shows that it follows this |
| 1766 | | -report: it passes them all, its reals aside where its target has none; |
| 1887 | +compiler's choices are in `doc/compiler.md`, under "This compiler's |
| 1888 | +choices". The programs in `conform/` are how an implementation shows |
| 1889 | +that it follows this report: it passes them all, its reals aside where its target has none; |
| 1767 | 1890 | `conform/check.sh` runs them. |
| 1768 | 1891 | |
| 1769 | 1892 | - **The target.** Which machines and systems it compiles for, and the |