| | @@ -277,7 +277,7 @@ |
| 277 | 277 | ## 4. Declarations |
| 278 | 278 | |
| 279 | 279 | ``` |
| 280 | | -Declaration = "const" { IdentDef [ ":" "ERROR" ] "=" Expr ";" } |
| 280 | +Declaration = "const" { IdentDef [ ":" Type ] "=" Expr ";" } |
| 281 | 281 | | "type" { IdentDef "=" Type ";" } |
| 282 | 282 | | "var" { IdentList ":" Type ";" } |
| 283 | 283 | | ProcDeclaration ";" |
| | @@ -294,17 +294,36 @@ |
| 294 | 294 | **Constants** are evaluated by the compiler. Their expressions use only |
| 295 | 295 | literals, other constants, operators, conversions, `SIZE` and the `LEN` |
| 296 | 296 | of arrays and strings. A constant is an integer, a real, a `BOOL`, a |
| 297 | | -name of an enumeration, a set, a string or an `ERROR`. A constant of an integer type wraps as the |
| 297 | +name of an enumeration, a set, a string, an array or an `ERROR`. A constant of an integer type wraps as the |
| 298 | 298 | program would: `U8(250) + 10` is 4. Dividing by zero in a constant is |
| 299 | 299 | an error. A string constant can be indexed and sliced as an array of |
| 300 | | -`U8` is, and its `LEN` is its length in bytes: |
| 300 | +`U8` is, and its `LEN` is its length in bytes. `+` joins two string |
| 301 | +constants into one: |
| 301 | 302 | |
| 302 | 303 | ``` |
| 303 | 304 | const Text = "the cat"; |
| 305 | + Line = Text + "\n"; |
| 304 | 306 | ... |
| 305 | 307 | w := Text[4..7]; (* "cat" *) |
| 306 | 308 | ``` |
| 307 | 309 | |
| 310 | +A constant with a type is an `ERROR` (section 5.8) or an array. An |
| 311 | +array constant has a type `[n]T`, where `T` is an integer type, a |
| 312 | +real, `BOOL`, an enumeration or a set, and its value is an array |
| 313 | +literal (section 6.5) of constants: |
| 314 | + |
| 315 | +``` |
| 316 | +const Primes: [5]I64 = [2, 3, 5, 7, 11]; |
| 317 | +... |
| 318 | +p := Primes[i]; |
| 319 | +``` |
| 320 | + |
| 321 | +It is used as a variable of that type is, but can't be changed: it can |
| 322 | +be indexed and sliced, its `LEN` is `n`, it can be assigned and |
| 323 | +compared, and it can be passed as a `[n]T` or a `[]T`. Its elements |
| 324 | +are constant as a string's bytes are (section 5.10). An element of it |
| 325 | +isn't a constant, even at a constant index. |
| 326 | + |
| 308 | 327 | **Variables** of a module live as long as the program; variables of a |
| 309 | 328 | procedure live as long as its call. Both start at zero: every byte of |
| 310 | 329 | them is zero. So a number starts as `0` (`0.0` for `F32` and `F64`), |
| | @@ -781,7 +800,9 @@ |
| 781 | 800 | | "return" [ Expr { "," Expr } ] |
| 782 | 801 | | "break" |
| 783 | 802 | | "defer" Statement . |
| 784 | | -Arm = ( ident [ "(" ident { "," ident } ")" ] | "else" ) ":" Statements . |
| 803 | +Arm = ( ident [ "(" ident { "," ident } ")" ] |
| 804 | + | Label { "," Label } | "else" ) ":" Statements . |
| 805 | +Label = Expr [ ".." Expr ] . |
| 785 | 806 | ``` |
| 786 | 807 | |
| 787 | 808 | Every statement ends in `;`, including the ones that end in `end` or |
| | @@ -848,6 +869,23 @@ |
| 848 | 869 | `match` on an enumeration works the same way: each arm names one of the |
| 849 | 870 | enumeration's names. |
| 850 | 871 | |
| 872 | +`match` on an integer runs the arm whose labels hold its value: |
| 873 | + |
| 874 | +``` |
| 875 | +match ch of |
| 876 | +| ' ', '\t': skip := true; |
| 877 | +| '0' .. '9': digit := true; |
| 878 | +| else: |
| 879 | +end; |
| 880 | +``` |
| 881 | + |
| 882 | +A label is a constant, or `lo .. hi`, the constants from `lo` to `hi`, |
| 883 | +both included (unlike a slice, which leaves `hi` out). Each must fit |
| 884 | +the integer's type, and no value may be in two labels. An integer |
| 885 | +`match` must have an `| else:` arm, which may be empty. Whether an arm |
| 886 | +names a case or gives labels is set by the type matched: `| Max:` is a |
| 887 | +case of a variant or an enumeration, and a constant for an integer. |
| 888 | + |
| 851 | 889 | ### 7.5 Order of evaluation |
| 852 | 890 | |
| 853 | 891 | Operands, arguments and the designators of an assignment are evaluated |
| | @@ -927,7 +965,7 @@ |
| 927 | 965 | ProcDeclaration = "proc" IdentDef Signature ";" |
| 928 | 966 | ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ] |
| 929 | 967 | | "code" Expr { "," Expr } ) . |
| 930 | | -LocalDeclaration = "const" { ident [ ":" "ERROR" ] "=" Expr ";" } |
| 968 | +LocalDeclaration = "const" { ident [ ":" Type ] "=" Expr ";" } |
| 931 | 969 | | "type" { ident "=" Type ";" } |
| 932 | 970 | | "var" { ident { "," ident } ":" Type ";" } . |
| 933 | 971 | Signature = "(" [ Params { ";" Params } [ ";" "..." ] ] ")" [ ":" Results ] |
| | @@ -1684,7 +1722,9 @@ |
| 1684 | 1722 | `main`'s and `Rt.exit`'s among them, and `I32` for a character; |
| 1685 | 1723 | `I64` stays for what may pass an `INT` on a small target: a file's |
| 1686 | 1724 | size and position, times, and numbers read from text (section |
| 1687 | | - 11). |
| 1725 | + 11). An array constant has a type and is used as a variable that |
| 1726 | + can't be changed; `+` joins string constants (section 4). `match` |
| 1727 | + takes an integer, with constants and ranges as labels (7.4). |
| 1688 | 1728 | |
| 1689 | 1729 | ## Appendix A. What an implementation chooses |
| 1690 | 1730 | |
| | @@ -1769,7 +1809,7 @@ |
| 1769 | 1809 | Link = "link" ( string | "static" ) ";" . |
| 1770 | 1810 | Check = "check" "off" ";" . |
| 1771 | 1811 | |
| 1772 | | -Declaration = "const" { IdentDef [ ":" "ERROR" ] "=" Expr ";" } |
| 1812 | +Declaration = "const" { IdentDef [ ":" Type ] "=" Expr ";" } |
| 1773 | 1813 | | "type" { IdentDef "=" Type ";" } |
| 1774 | 1814 | | "var" { IdentList ":" Type ";" } |
| 1775 | 1815 | | ProcDeclaration ";" |
| | @@ -1820,12 +1860,14 @@ |
| 1820 | 1860 | | "return" [ Expr { "," Expr } ] |
| 1821 | 1861 | | "break" |
| 1822 | 1862 | | "defer" Statement . |
| 1823 | | -Arm = ( ident [ "(" ident { "," ident } ")" ] | "else" ) ":" Statements . |
| 1863 | +Arm = ( ident [ "(" ident { "," ident } ")" ] |
| 1864 | + | Label { "," Label } | "else" ) ":" Statements . |
| 1865 | +Label = Expr [ ".." Expr ] . |
| 1824 | 1866 | |
| 1825 | 1867 | ProcDeclaration = "proc" IdentDef Signature ";" |
| 1826 | 1868 | ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ] |
| 1827 | 1869 | | "code" Expr { "," Expr } ) . |
| 1828 | | -LocalDeclaration = "const" { ident [ ":" "ERROR" ] "=" Expr ";" } |
| 1870 | +LocalDeclaration = "const" { ident [ ":" Type ] "=" Expr ";" } |
| 1829 | 1871 | | "type" { ident "=" Type ";" } |
| 1830 | 1872 | | "var" { ident { "," ident } ":" Type ";" } . |
| 1831 | 1873 | Signature = "(" [ Params { ";" Params } [ ";" "..." ] ] ")" [ ":" Results ] |