raz / commit f9c2a04b

commit
f9c2a04b9eb12df4e7559de460423939644f7815
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
825d0cb6
The report made complete, the mechanical part

What the compiler already does and the report didn't say: predeclared
names and hiding them; the spelling of numbers and escapes; constants
of every kind, string constants indexed; alignments, empty records,
the layout of a variant; sameness that doesn't pass through a name;
pointers to records; shifts, unary minus, NaN; the for loop exactly;
match on a copy; the order of an assignment; what deferred statements
a failed check leaves; extern arguments past the parameters. Appendix
A says what an implementation chooses, appendix B gathers the grammar.

The questions that need the owner are left open.

patch · log

2 files changed, +235 -27

PLAN.md (history) modified +2 -1

@@ -94,7 +94,8 @@
9494 For review before anything else.
9595 - [ ] 2. The report, the new language, for review before any code: the
9696 system integer, the size of an address, which `LEN`, indexes and
97- sizes give, and how it converts to and from `I64`; reals a
97+ sizes give (the owner's choice), and how it converts to and from
98+ `I64`; reals a
9899 target may leave out; object files, a module's interface and its
99100 IR, and a generic's instances in them, named by their arguments
100101 and the fingerprints they were built from.

doc/raz.md (history) modified +233 -26

@@ -58,9 +58,19 @@
5858 xor
5959 ```
6060
61+`static`, in `link static;` (section 3), isn't one.
62+
63+**Predeclared names** are the basic types (section 5.1), `ERROR`,
64+`true`, `false`, `nil` and the built-in procedures (section 6.8). They
65+belong to a scope around every module, so a module or a procedure may
66+declare the same name for something else, which hides them there.
67+
6168 **Numbers.** `123` is decimal. `0FFH` is hexadecimal: it starts with a
62-digit and ends in `H`. `1.5` and `2.0E-3` are reals; a real always has
63-a digit on both sides of its point.
69+digit, its digits are `0` to `9` and `A` to `F`, and it ends in `H`,
70+all in capitals. `1.5` and `2.0E-3` are reals; a real always has a
71+digit on both sides of its point, and its exponent, if it has one, is
72+`E`, a sign if wanted, and digits. A decimal number above the largest
73+`I64`, or a hexadecimal one above the largest `U64`, is an error.
6474
6575 An integer is an untyped constant, which takes the type it is used as,
6676 up to the largest `I64`. A hexadecimal number above that, such as
@@ -81,7 +91,9 @@
8191 escapes `\n` (line feed), `\t` (tab), `\r` (carriage return), `\0`
8292 (zero), `\\`, `\'`, `\"`, `\u{H...}` (the code point with hexadecimal
8393 value H..., in a string its UTF-8) and, in strings only, `\xHH` (the
84-byte with hexadecimal value HH, which needn't make valid UTF-8).
94+byte with hexadecimal value HH, which needn't make valid UTF-8). In
95+an escape, the hexadecimal digits may be capitals or not. A character
96+or a string ends on the line it starts on.
8597
8698 Messages give the line and the column, counted in characters
8799 (section 10).
@@ -117,6 +129,8 @@
117129 **Exports.** A name declared at the top of a module with `*` after it
118130 is exported: other modules can use it as `Module.name`. Record fields
119131 are exported the same way. Everything else is private to the module.
132+An exported variable can be read and assigned by the modules that
133+import it.
120134
121135 **Imports.** `import Out;` makes `Out`'s exports available as `Out.x`.
122136 `import O := Out;` uses the name `O` instead. A module can't import
@@ -142,7 +156,9 @@
142156 An instance is made where its module is first used, not at the
143157 import: so the arguments may name types the importing module declares
144158 after its imports, as long as they are declared before that first
145-use.
159+use. The instance is made before the code of the procedure, or the
160+module body, that first uses it, and its arguments are read in the
161+scope there.
146162
147163 ```
148164 import Table := Map([]U8, Colour, Str.hash, Str.eq);
@@ -241,7 +257,18 @@
241257 procedure may be declared `forward` and defined later (section 8).
242258
243259 **Constants** are evaluated by the compiler. Their expressions use only
244-literals, other constants, operators, `SIZE` and the `LEN` of arrays.
260+literals, other constants, operators, conversions, `SIZE` and the `LEN`
261+of arrays and strings. A constant is an integer, a real, a `BOOL`, a
262+name of an enumeration, a set, a string or an `ERROR`. A constant of an integer type wraps as the
263+program would: `U8(250) + 10` is 4. Dividing by zero in a constant is
264+an error. A string constant can be indexed and sliced as an array of
265+`U8` is, and its `LEN` is its length in bytes:
266+
267+```
268+const Text = "the cat";
269+...
270+w := Text[4..7]; (* "cat" *)
271+```
245272
246273 **Variables** of a module live as long as the program; variables of a
247274 procedure live as long as its call. Both start at zero: every byte of
@@ -278,6 +305,9 @@
278305 | `F32` | 4 | IEEE 754 single precision |
279306 | `F64` | 8 | IEEE 754 double precision |
280307
308+Each is aligned to its size. So are `ERROR` (section 5.8), pointers
309+and procedures, 8 bytes; a slice is aligned to 8.
310+
281311 ### 5.2 Arrays
282312
283313 `[n]T` is `n` elements of type `T`, one after another. `n` is a
@@ -313,7 +343,8 @@
313343 Fields are laid out in order as C lays them out: each at the next
314344 multiple of its own alignment, and the whole record padded to a
315345 multiple of its largest alignment. A record declared like a C struct
316-has the same layout as that struct.
346+has the same layout as that struct. A record may have no fields; its
347+size is then 0.
317348
318349 A `packed record` has no padding: each field starts at the byte after
319350 the field before it, and the record's alignment is 1. A field of a
@@ -341,8 +372,12 @@
341372 way to read the fields is `match` (section 7.4).
342373
343374 Layout: an `I64` tag (0 for the first case, 1 for the second, and so
344-on) followed by the fields of the largest case, laid out as a record.
345-A variant that starts at zero therefore holds its first case.
375+on), then the space for the fields. Each case's fields are laid out
376+in that space as a record's would be, after the tag, and the cases
377+share it, so the variant is as big as its largest case needs, padded
378+to its alignment, the largest of 8 and its fields'. `variant a(x:
379+U8); b(y: I64; z: U8); end` is 24 bytes. A variant that starts at zero
380+therefore holds its first case.
346381
347382 ### 5.7 Enumerations and sets
348383
@@ -392,7 +427,9 @@
392427 ```
393428
394429 A value of a procedure type is the address of a procedure with that
395-signature, 8 bytes, or `nil`.
430+signature, 8 bytes, or `nil`. Two signatures are alike when their
431+parameters have the same types, each `var` or not alike, in order, and
432+their results are the same types; the parameters' names don't matter.
396433
397434 ### 5.10 Same and assignable
398435
@@ -401,6 +438,9 @@
401438 `^I64` wherever it is written. A named and an anonymous type are the
402439 same if the named one is built like the anonymous one, so that
403440 `p := ADR(n)` works for `p: NodePtr` with `type NodePtr = ^Node`.
441+Sameness doesn't pass through the anonymous type: with `type P1 =
442+^Node; P2 = ^Node;`, each of `P1` and `P2` is the same as `^Node`, but
443+they are two declarations, so not the same as each other.
404444
405445 A type declared with only a name on the right, `type Count = I64;`, is
406446 another name for that type, not a new one.
@@ -454,12 +494,17 @@
454494 `and`, `or` and `not` are for `BOOL`s only; `&`, `|`, `xor` and `~`
455495 are for integers only.
456496
497+A pointer to a record isn't the record: its fields are `p^.f`, never
498+`p.f`, and the elements of a pointer to an array are `p^[i]`. A
499+statement starts with a name, so `^T(x)^ := v` isn't one.
500+
457501 ### 6.1 Integers
458502
459503 Both operands of `+ - * div mod & | xor` have the same integer
460504 type, or one is an untyped constant that takes the other's type. The
461505 result has that type and wraps around at its width: in `U8`,
462-`200 + 100` is `44`.
506+`200 + 100` is `44`. A unary minus wraps too: for `x` of an unsigned
507+type, `-x` is `0 - x`.
463508
464509 `div` rounds toward zero and `mod` takes the sign of the dividend, as
465510 in C: `-7 div 2` is `-3`, `-7 mod 2` is `-1`. Dividing by zero, and
@@ -467,7 +512,9 @@
467512
468513 `&` (and), `|` (or), `xor` and `~` (not) work on every bit. `x shl n`
469514 and `x shr n` shift by `n & 63`; `shr` brings in copies of the sign bit
470-for signed types and zeros for unsigned ones.
515+for signed types and zeros for unsigned ones. `n` may be of any integer
516+type. The result has `x`'s type, cut to its width: in `U8`, `0C0H shl
517+1` is `80H`.
471518
472519 Relations compare as signed or unsigned according to the type.
473520
@@ -484,6 +531,11 @@
484531 single precision, rounded at each step as C rounds `float`. A
485532 constant expression of reals is evaluated in double precision, then
486533 rounded once to the type it takes.
534+
535+Relations are IEEE 754's: a NaN is equal to nothing, itself included,
536+so for a NaN `x`, `x # x` is true and every other relation with it is
537+false. Dividing by zero gives an infinity or a NaN; arithmetic on reals
538+never stops the program.
487539
488540 ### 6.4 Pointers
489541
@@ -514,7 +566,8 @@
514566 an element of a bigger literal. The elements must be assignable to the
515567 element type, and an array literal for `[n]T` has exactly `n` of them.
516568 A record literal names its fields, in any order, each at most once; the
517-fields it leaves out are zero. An array literal used as a slice is a
569+fields it leaves out are zero, so `{}` is the record with every field
570+zero. An array literal used as a slice is a
518571 hidden variable of the procedure, and lives as long as its call.
519572
520573 ```
@@ -545,7 +598,8 @@
545598 `{}` is the empty set, where the type it must have is known. On two sets
546599 of the same type, `+` is union, `*` intersection and `-` difference;
547600 `=` and `#` compare, and `<=` and `>=` are "is a subset of" and "is a
548-superset of". `x in s` is `true` if the name `x` is in the set `s`.
601+superset of". `x in s` is `true` if `x`, a value of the enumeration,
602+is in the set `s`.
549603
550604 ### 6.7 Conversions
551605
@@ -568,13 +622,18 @@
568622 | pointer, procedure | integer | the address |
569623 | pointer, procedure | pointer, procedure | the same address, seen as the new type |
570624
625+There are no others: no conversion between `BOOL` and integers, and
626+none to or from records, arrays, slices, variants or `ERROR`. A
627+conversion of a constant is made by the compiler, by the same rules:
628+`U8(300)` is 44.
629+
571630 ### 6.8 Built-in procedures
572631
573632 | Call | Result |
574633 |---|---|
575-| `SIZE(T)` | the size of type `T` in bytes, a constant |
634+| `SIZE(T)` | the size of type `T` in bytes, a constant; `T` is a type, not a variable |
576635 | `ADR(x)` | the address of variable `x`, a `^T` |
577-| `LEN(a)` | the number of elements of an array (a constant) or a slice, an `I64` |
636+| `LEN(a)` | the number of elements of an array or a string constant (a constant) or of a slice, an `I64` |
578637 | `NEW(p, a)` | sets `p: ^T` to a fresh, zeroed `T` from the allocator `a: ^Mem.Allocator`, or to `nil` if `a` has no room |
579638 | `MSG(e)` | the message of the error `e`, a `[]U8` |
580639 | `SYSCALL(n, x1, ..., x6)` | Linux system call `n` with up to six integer or pointer arguments; the raw result, an `I64` (a negative number is `-errno`) |
@@ -591,6 +650,11 @@
591650 system call interface, and their modules call the C library instead
592651 (section 9).
593652
653+`NEW(p, a)` calls `a^.alloc(a, SIZE(T), A)`, where `A` is the
654+alignment of `T`, and zeroes the bytes it gets if they aren't `nil`.
655+`Mem.Allocator` (section 11) is the one type of the library the
656+language names.
657+
594658 `INC` and `DEC` evaluate `v` once, so `INC(a[f(i)])` calls `f` once. They
595659 read `v` and then write it: two accesses, even through a `^VOLATILE`
596660 pointer, and not atomic; `XADD` is the atomic one.
@@ -634,11 +698,19 @@
634698 `if`, `while` and `repeat` take `BOOL` expressions. `repeat` runs its
635699 body at least once and stops when the condition is true.
636700
637-`for i := a to b by s do ... end;` sets the integer variable `i` to `a`,
638-`a + s`, ... while `i <= b` (or `i >= b` if the constant `s` is
639-negative). `s` is 1 if left out. `b` is evaluated once, before the
640-first round.
701+`for i := a to b by s do ... end;` sets `i` to `a`, `a + s`, ... while
702+`i <= b` (or `i >= b` if `s` is negative). `i` is a variable of an
703+integer or enumeration type, named by its name alone; `a` and then `b`
704+are evaluated once, before the first round; `s` is a constant other
705+than zero, and 1 if left out.
641706
707+The loop never steps past `b`, so `i` can't wrap: afterwards `i`
708+holds the last value it took, or `a` if the body never ran. The body
709+may assign `i`. Precisely, for a positive `s`: at the end of a round,
710+if `i <= b` and `b - i < s`, the loop ends with `i` as it is;
711+otherwise `i` becomes `i + s`, wrapping, and the loop goes on while
712+`i <= b`. A negative `s` is the same, the other way round.
713+
642714 `break` ends the innermost `while`, `repeat` or `for` it stands in,
643715 and the program goes on after it. There is no `goto`.
644716
@@ -663,8 +735,9 @@
663735
664736 `match` runs the arm of the case `s` holds. An arm may name the case's
665737 fields, in order, all of them; the names stand for those fields of `s`
666-itself, so assigning to one changes `s`. An arm may also leave the
667-fields unnamed. Every case must have an arm, unless there is an
738+itself, so assigning to one changes `s`; if `s` isn't a variable, they
739+stand for the fields of a copy. An arm may also leave the fields
740+unnamed. Every case must have an arm, and only one, unless there is an
668741 `| else:` arm, which must come last.
669742
670743 `match` on an enumeration works the same way: each arm names one of the
@@ -673,7 +746,8 @@
673746 ### 7.5 Order of evaluation
674747
675748 Operands, arguments and the designators of an assignment are evaluated
676-from left to right.
749+from left to right, and the designators of an assignment before its
750+right side: `a[i()] := g()` calls `i` and then `g`.
677751
678752 ### 7.6 Volatile
679753
@@ -728,7 +802,8 @@
728802
729803 `return e;` evaluates `e` before the deferred statements run. A
730804 deferred statement can't contain `return`, `break` or `defer` that
731-would leave it.
805+would leave it. A failed check, `Rt.exit` and `Sys.exit` end the
806+program without running the deferred statements still pending.
732807
733808 ## 8. Procedures
734809
@@ -782,8 +857,10 @@
782857
783858 **forward.** `proc f(x: I64): I64; forward;` declares `f` so that it
784859 can be called before its body appears; the body comes later with the
785-same signature.
860+same signature, though its parameters may have other names.
786861
862+A name after a procedure's `end`, if written, is the procedure's own.
863+
787864 Procedures don't nest.
788865
789866 **Code procedures.** A procedure whose body is `code` and a list of
@@ -829,7 +906,9 @@
829906 never by value. `...` accepts any number of further arguments of those
830907 types, and is allowed only in an `extern`. C takes no `float` there,
831908 so an `F32` given to `...` is an error: the program writes `F64(x)`.
832-A real constant there is an `F64`.
909+Past the parameters, a real constant goes as an `F64`, an integer
910+constant as an `I64`, and a string literal as a pointer to its bytes
911+followed by a zero byte.
833912
834913 An `extern` variable is C's variable itself: reading it reads C's,
835914 assigning it writes C's. It can be of any type, declared as C declares
@@ -865,7 +944,8 @@
865944 which runs only if something calls it; so a library module can carry a
866945 `main` that tests it.
867946 A program uses C if any of its modules has an `extern` declaration or
868-a `link` of a library. A program that doesn't is a static executable
947+a `link` of a library; a module's other files, read only to check its
948+interface (section 3), don't count. A program that doesn't is a static executable
869949 with no run time besides the code it uses. One that does is linked
870950 dynamically and starts through the C library, so that C's own set-up
871951 and clean-up run. A program with a module that says `link static;`
@@ -1415,3 +1495,130 @@
14151495 `Program`, and `Sys.wait` says which program changed and how. `Str`
14161496 gains UTF-8 and reading a number, `Map` removing and walking, `Fmt`
14171497 reals; `Sort`, `Math`, `Files` and `Opt` are new (section 11).
1498+
1499+## Appendix A. What an implementation chooses
1500+
1501+Everything in this report binds every implementation of raz, except
1502+what follows, which each chooses for itself and documents. This
1503+compiler's choices are in `doc/compiler.md`.
1504+
1505+- **The target.** Which machines and systems it compiles for, and the
1506+ names of their files (`linux-x86_64`, section 3).
1507+- **The calling convention.** On x86-64 Linux it is System V
1508+ (sections 8 and 9); elsewhere it is the C library's own, so that
1509+ `extern` procedures and callbacks work. How a procedure returns
1510+ several results, and how its frame is laid out, are the
1511+ implementation's.
1512+- **What is the target's.** Code procedures (section 8) hold the
1513+ target's machine code, and exist only where the implementation says
1514+ so; `SYSCALL` exists only on Linux targets (section 6.8). A record's
1515+ layout is the target's C layout (section 5.5).
1516+- **The executable.** Its format, and the names and line numbers it
1517+ carries for a debugger (section 10).
1518+- **Messages and warnings.** The text of the compiler's messages, how
1519+ many errors it reports before it stops, and which warnings it gives
1520+ (section 10). A program that is an error in this report is one in
1521+ every implementation; the words that say so are free.
1522+- **The command line.** How the compiler is run, its options, and
1523+ where its library lives (sections 1 and 3).
1524+
1525+## Appendix B. The grammar
1526+
1527+The whole syntax of raz, gathered from the sections above, with the
1528+symbols of section 2. `{ x }` is any number of `x`, `[ x ]` is `x` or
1529+nothing, and `( x | y )` is one of them.
1530+
1531+```
1532+ident = letter { letter | digit } .
1533+letter = "A" .. "Z" | "a" .. "z" | "_" .
1534+digit = "0" .. "9" .
1535+hexDigit = digit | "A" .. "F" .
1536+number = digit { digit }
1537+ | digit { hexDigit } "H"
1538+ | digit { digit } "." digit { digit } [ "E" [ "+" | "-" ] digit { digit } ] .
1539+char = "'" ( character | escape ) "'" .
1540+string = '"' { character | escape | "\x" anyHex anyHex } '"' .
1541+escape = "\n" | "\t" | "\r" | "\0" | "\\" | "\'" | '\"'
1542+ | "\u{" anyHex { anyHex } "}" .
1543+anyHex = hexDigit | "a" .. "f" .
1544+
1545+Module = "module" ident [ "(" ModParams ")" ] ";"
1546+ { Import } { Link } { Declaration }
1547+ [ "begin" Statements ] "end" ident "." .
1548+ModParams = ident { "," ident } { ";" ident ":" "proc" Signature } .
1549+Import = "import" ImportItem { "," ImportItem } ";" .
1550+ImportItem = ident [ ":=" ident [ "(" Type { "," Type } ")" ] ] .
1551+Link = "link" ( string | "static" ) ";" .
1552+
1553+Declaration = "const" { IdentDef [ ":" "ERROR" ] "=" Expr ";" }
1554+ | "type" { IdentDef "=" Type ";" }
1555+ | "var" { IdentList ":" Type ";" }
1556+ | ProcDeclaration ";"
1557+ | ExternDeclaration ";" .
1558+IdentDef = ident [ "*" ] .
1559+IdentList = IdentDef { "," IdentDef } .
1560+
1561+Type = QualIdent
1562+ | "[" Expr "]" Type
1563+ | "[" "]" Type
1564+ | "^" [ "VOLATILE" ] Type
1565+ | [ "packed" ] "record" { IdentList ":" Type ";" } "end"
1566+ | "variant" { Case ";" } "end"
1567+ | "(" ident { "," ident } ")"
1568+ | "set" "of" QualIdent
1569+ | "proc" Signature .
1570+Case = ident [ "(" FieldList { ";" FieldList } ")" ] .
1571+FieldList = ident { "," ident } ":" Type .
1572+QualIdent = [ ident "." ] ident .
1573+
1574+Expr = Conjunct { "or" Conjunct } .
1575+Conjunct = Relation { "and" Relation } .
1576+Relation = SimpleExpr [ RelOp SimpleExpr ] .
1577+RelOp = "=" | "#" | "<" | "<=" | ">" | ">=" | "in" .
1578+SimpleExpr = [ "-" ] Term { AddOp Term } .
1579+AddOp = "+" | "-" | "|" | "xor" .
1580+Term = Factor { MulOp Factor } .
1581+MulOp = "*" | "/" | "div" | "mod" | "&" | "shl" | "shr" .
1582+Factor = number | char | string | Designator | "(" Expr ")"
1583+ | "not" Factor | "~" Factor
1584+ | "[" [ Expr { "," Expr } ] "]"
1585+ | "{" [ Element { "," Element } ] "}"
1586+ | "^" [ "VOLATILE" ] Type "(" Expr ")" .
1587+Element = Expr | ident ":" Expr .
1588+Designator = QualIdent { Selector } .
1589+Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]"
1590+ | "(" [ Expr { "," Expr } ] ")" .
1591+
1592+Statements = { Statement ";" } .
1593+Statement = Designator [ ":=" Expr ]
1594+ | Designator "," Designator { "," Designator } ":=" Expr
1595+ | "if" Expr "then" Statements
1596+ { "elsif" Expr "then" Statements } [ "else" Statements ] "end"
1597+ | "while" Expr "do" Statements "end"
1598+ | "repeat" Statements "until" Expr
1599+ | "for" ident ":=" Expr "to" Expr [ "by" Expr ] "do" Statements "end"
1600+ | "match" Expr "of" { "|" Arm } "end"
1601+ | "return" [ Expr { "," Expr } ]
1602+ | "break"
1603+ | "defer" Statement .
1604+Arm = ( ident [ "(" ident { "," ident } ")" ] | "else" ) ":" Statements .
1605+
1606+ProcDeclaration = "proc" IdentDef Signature ";"
1607+ ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ]
1608+ | "code" Expr { "," Expr } ) .
1609+LocalDeclaration = "const" { ident [ ":" "ERROR" ] "=" Expr ";" }
1610+ | "type" { ident "=" Type ";" }
1611+ | "var" { ident { "," ident } ":" Type ";" } .
1612+Signature = "(" [ Params { ";" Params } [ ";" "..." ] ] ")" [ ":" Results ]
1613+ | [ ":" Results ] .
1614+Params = [ "var" ] ident { "," ident } ":" Type .
1615+Results = Type | "(" Type { "," Type } ")" .
1616+ExternDeclaration = "extern" ( "proc" IdentDef Signature
1617+ | "var" IdentDef ":" Type ) .
1618+```
1619+
1620+`character` is any character but a line break, and in a string any
1621+but `"` and `\`, in a character any but `'` and `\`. A procedure
1622+argument of a generic module (section 3) is written as a `QualIdent`,
1623+which is a `Type` here. `...` comes last in a `Signature`, and only in
1624+an `extern`.