Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1 | <!-- |
| 2 | Copyright 2018 The CUE Authors |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | --> |
| 16 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 17 | # The CUE Language Specification |
| 18 | |
| 19 | ## Introduction |
| 20 | |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 21 | This is a reference manual for the CUE data constraint language. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 22 | CUE, pronounced cue or Q, is a general-purpose and strongly typed |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 23 | constraint-based language. |
| 24 | It can be used for data templating, data validation, code generation, scripting, |
| 25 | and many other applications involving structured data. |
| 26 | The CUE tooling, layered on top of CUE, provides |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 27 | a general purpose scripting language for creating scripts as well as |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 28 | simple servers, also expressed in CUE. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 29 | |
| 30 | CUE was designed with cloud configuration, and related systems, in mind, |
| 31 | but is not limited to this domain. |
| 32 | It derives its formalism from relational programming languages. |
| 33 | This formalism allows for managing and reasoning over large amounts of |
Marcel van Lohuizen | 5953c66 | 2019-01-26 13:26:04 +0100 | [diff] [blame] | 34 | data in a straightforward manner. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 35 | |
| 36 | The grammar is compact and regular, allowing for easy analysis by automatic |
| 37 | tools such as integrated development environments. |
| 38 | |
| 39 | This document is maintained by mpvl@golang.org. |
| 40 | CUE has a lot of similarities with the Go language. This document draws heavily |
Marcel van Lohuizen | 73f14eb | 2019-01-30 17:11:17 +0100 | [diff] [blame] | 41 | from the Go specification as a result. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 42 | |
| 43 | CUE draws its influence from many languages. |
| 44 | Its main influences were BCL/ GCL (internal to Google), |
| 45 | LKB (LinGO), Go, and JSON. |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 46 | Others are Swift, Typescript, Javascript, Prolog, NCL (internal to Google), |
| 47 | Jsonnet, HCL, Flabbergast, JSONPath, Haskell, Objective-C, and Python. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 48 | |
| 49 | |
| 50 | ## Notation |
| 51 | |
| 52 | The syntax is specified using Extended Backus-Naur Form (EBNF): |
| 53 | |
| 54 | ``` |
| 55 | Production = production_name "=" [ Expression ] "." . |
| 56 | Expression = Alternative { "|" Alternative } . |
| 57 | Alternative = Term { Term } . |
| 58 | Term = production_name | token [ "…" token ] | Group | Option | Repetition . |
| 59 | Group = "(" Expression ")" . |
| 60 | Option = "[" Expression "]" . |
| 61 | Repetition = "{" Expression "}" . |
| 62 | ``` |
| 63 | |
| 64 | Productions are expressions constructed from terms and the following operators, |
| 65 | in increasing precedence: |
| 66 | |
| 67 | ``` |
| 68 | | alternation |
| 69 | () grouping |
| 70 | [] option (0 or 1 times) |
| 71 | {} repetition (0 to n times) |
| 72 | ``` |
| 73 | |
| 74 | Lower-case production names are used to identify lexical tokens. Non-terminals |
| 75 | are in CamelCase. Lexical tokens are enclosed in double quotes "" or back quotes |
| 76 | ``. |
| 77 | |
| 78 | The form a … b represents the set of characters from a through b as |
| 79 | alternatives. The horizontal ellipsis … is also used elsewhere in the spec to |
| 80 | informally denote various enumerations or code snippets that are not further |
| 81 | specified. The character … (as opposed to the three characters ...) is not a |
| 82 | token of the Go language. |
| 83 | |
| 84 | |
| 85 | ## Source code representation |
| 86 | |
| 87 | Source code is Unicode text encoded in UTF-8. |
| 88 | Unless otherwise noted, the text is not canonicalized, so a single |
| 89 | accented code point is distinct from the same character constructed from |
| 90 | combining an accent and a letter; those are treated as two code points. |
| 91 | For simplicity, this document will use the unqualified term character to refer |
| 92 | to a Unicode code point in the source text. |
| 93 | |
| 94 | Each code point is distinct; for instance, upper and lower case letters are |
| 95 | different characters. |
| 96 | |
| 97 | Implementation restriction: For compatibility with other tools, a compiler may |
| 98 | disallow the NUL character (U+0000) in the source text. |
| 99 | |
| 100 | Implementation restriction: For compatibility with other tools, a compiler may |
| 101 | ignore a UTF-8-encoded byte order mark (U+FEFF) if it is the first Unicode code |
| 102 | point in the source text. A byte order mark may be disallowed anywhere else in |
| 103 | the source. |
| 104 | |
| 105 | |
| 106 | ### Characters |
| 107 | |
| 108 | The following terms are used to denote specific Unicode character classes: |
| 109 | |
| 110 | ``` |
| 111 | newline = /* the Unicode code point U+000A */ . |
| 112 | unicode_char = /* an arbitrary Unicode code point except newline */ . |
| 113 | unicode_letter = /* a Unicode code point classified as "Letter" */ . |
| 114 | unicode_digit = /* a Unicode code point classified as "Number, decimal digit" */ . |
| 115 | ``` |
| 116 | |
| 117 | In The Unicode Standard 8.0, Section 4.5 "General Category" defines a set of |
| 118 | character categories. |
| 119 | CUE treats all characters in any of the Letter categories Lu, Ll, Lt, Lm, or Lo |
| 120 | as Unicode letters, and those in the Number category Nd as Unicode digits. |
| 121 | |
| 122 | |
| 123 | ### Letters and digits |
| 124 | |
| 125 | The underscore character _ (U+005F) is considered a letter. |
| 126 | |
| 127 | ``` |
| 128 | letter = unicode_letter | "_" . |
| 129 | decimal_digit = "0" … "9" . |
| 130 | octal_digit = "0" … "7" . |
| 131 | hex_digit = "0" … "9" | "A" … "F" | "a" … "f" . |
| 132 | ``` |
| 133 | |
| 134 | |
| 135 | ## Lexical elements |
| 136 | |
| 137 | ### Comments |
| 138 | Comments serve as program documentation. There are two forms: |
| 139 | |
| 140 | 1. Line comments start with the character sequence // and stop at the end of the line. |
| 141 | 2. General comments start with the character sequence /* and stop with the first subsequent character sequence */. |
| 142 | |
| 143 | A comment cannot start inside string literal or inside a comment. |
| 144 | A general comment containing no newlines acts like a space. |
| 145 | Any other comment acts like a newline. |
| 146 | |
| 147 | |
| 148 | ### Tokens |
| 149 | |
| 150 | Tokens form the vocabulary of the CUE language. There are four classes: |
| 151 | identifiers, keywords, operators and punctuation, and literals. White space, |
| 152 | formed from spaces (U+0020), horizontal tabs (U+0009), carriage returns |
| 153 | (U+000D), and newlines (U+000A), is ignored except as it separates tokens that |
| 154 | would otherwise combine into a single token. Also, a newline or end of file may |
| 155 | trigger the insertion of a comma. While breaking the input into tokens, the |
| 156 | next token is the longest sequence of characters that form a valid token. |
| 157 | |
| 158 | |
| 159 | ### Commas |
| 160 | |
| 161 | The formal grammar uses commas "," as terminators in a number of productions. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 162 | CUE programs may omit most of these commas using the following two rules: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 163 | |
| 164 | When the input is broken into tokens, a comma is automatically inserted into |
| 165 | the token stream immediately after a line's final token if that token is |
| 166 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 167 | - an identifier |
| 168 | - null, true, false, bottom, or an integer, floating-point, or string literal |
| 169 | - one of the characters ), ], or } |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 170 | |
| 171 | |
| 172 | Although commas are automatically inserted, the parser will require |
| 173 | explicit commas between two list elements. |
| 174 | |
| 175 | To reflect idiomatic use, examples in this document elide commas using |
| 176 | these rules. |
| 177 | |
| 178 | |
| 179 | ### Identifiers |
| 180 | |
| 181 | Identifiers name entities such as fields and aliases. |
| 182 | An identifier is a sequence of one or more letters and digits. |
| 183 | It may not be `_`. |
| 184 | The first character in an identifier must be a letter. |
| 185 | |
| 186 | <!-- |
| 187 | TODO: allow identifiers as defined in Unicode UAX #31 |
| 188 | (https://unicode.org/reports/tr31/). |
| 189 | |
| 190 | Identifiers are normalized using the NFC normal form. |
| 191 | --> |
| 192 | |
| 193 | ``` |
| 194 | identifier = letter { letter | unicode_digit } . |
| 195 | ``` |
| 196 | |
| 197 | ``` |
| 198 | a |
| 199 | _x9 |
| 200 | fieldName |
| 201 | αβ |
| 202 | ``` |
| 203 | |
| 204 | <!-- TODO: Allow Unicode identifiers TR 32 http://unicode.org/reports/tr31/ --> |
| 205 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 206 | Some identifiers are [predeclared](#predeclared-identifiers). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 207 | |
| 208 | |
| 209 | ### Keywords |
| 210 | |
| 211 | CUE has a limited set of keywords. |
| 212 | All keywords may be used as labels (field names). |
| 213 | They cannot, however, be used as identifiers to refer to the same name. |
| 214 | |
| 215 | |
| 216 | #### Values |
| 217 | |
| 218 | The following keywords are values. |
| 219 | |
| 220 | ``` |
| 221 | null true false |
| 222 | ``` |
| 223 | |
| 224 | These can never be used to refer to a field of the same name. |
| 225 | This restriction is to ensure compatibility with JSON configuration files. |
| 226 | |
| 227 | |
| 228 | #### Preamble |
| 229 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 230 | The following keywords are used at the preamble of a CUE file. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 231 | After the preamble, they may be used as identifiers to refer to namesake fields. |
| 232 | |
| 233 | ``` |
| 234 | package import |
| 235 | ``` |
| 236 | |
| 237 | |
| 238 | #### Comprehension clauses |
| 239 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 240 | The following keywords are used in comprehensions. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 241 | |
| 242 | ``` |
| 243 | for in if let |
| 244 | ``` |
| 245 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 246 | The keywords `for`, `if` and `let` cannot be used as identifiers to |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 247 | refer to fields. All others can. |
| 248 | |
| 249 | <!-- |
| 250 | TODO: |
| 251 | reduce [to] |
| 252 | order [by] |
| 253 | --> |
| 254 | |
| 255 | |
| 256 | #### Arithmetic |
| 257 | |
| 258 | The following pseudo keywords can be used as operators in expressions. |
| 259 | |
| 260 | ``` |
| 261 | div mod quo rem |
| 262 | ``` |
| 263 | |
| 264 | These may be used as identifiers to refer to fields in all other contexts. |
| 265 | |
| 266 | |
| 267 | ### Operators and punctuation |
| 268 | |
| 269 | The following character sequences represent operators and punctuation: |
| 270 | |
| 271 | ``` |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 272 | + div && == < . ( ) |
| 273 | - mod || != > : { } |
| 274 | * quo & =~ <= = [ ] |
| 275 | / rem | !~ >= <- ... , |
| 276 | % _|_ ! ; |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 277 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 278 | <!-- :: for "is-a" definitions --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 279 | |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 280 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 281 | ### Integer literals |
| 282 | |
| 283 | An integer literal is a sequence of digits representing an integer value. |
| 284 | An optional prefix sets a non-decimal base: 0 for octal, |
| 285 | 0x or 0X for hexadecimal, and 0b for binary. |
| 286 | In hexadecimal literals, letters a-f and A-F represent values 10 through 15. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 287 | All integers allow interstitial underscores "_"; |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 288 | these have no meaning and are solely for readability. |
| 289 | |
| 290 | Decimal integers may have a SI or IEC multiplier. |
| 291 | Multipliers can be used with fractional numbers. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 292 | When multiplying a fraction by a multiplier, the result is truncated |
| 293 | towards zero if it is not an integer. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 294 | |
| 295 | ``` |
Marcel van Lohuizen | afb4db6 | 2019-05-31 00:23:24 +0200 | [diff] [blame] | 296 | int_lit = decimal_lit | si_lit | octal_lit | binary_lit | hex_lit . |
| 297 | decimal_lit = ( "1" … "9" ) { [ "_" ] decimal_digit } . |
| 298 | decimals = decimal_digit { [ "_" ] decimal_digit } . |
| 299 | si_it = decimals [ "." decimals ] multiplier | |
| 300 | "." decimals multiplier . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 301 | binary_lit = "0b" binary_digit { binary_digit } . |
| 302 | hex_lit = "0" ( "x" | "X" ) hex_digit { [ "_" ] hex_digit } . |
Marcel van Lohuizen | afb4db6 | 2019-05-31 00:23:24 +0200 | [diff] [blame] | 303 | octal_lit = "0" [ "o" ] octal_digit { [ "_" ] octal_digit } . |
Jonathan Amsterdam | abeffa4 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 304 | multiplier = ( "K" | "M" | "G" | "T" | "P" | "E" | "Y" | "Z" ) [ "i" ] |
Marcel van Lohuizen | afb4db6 | 2019-05-31 00:23:24 +0200 | [diff] [blame] | 305 | |
| 306 | float_lit = decimals "." [ decimals ] [ exponent ] | |
| 307 | decimals exponent | |
| 308 | "." decimals [ exponent ]. |
| 309 | exponent = ( "e" | "E" ) [ "+" | "-" ] decimals . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 310 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 311 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 312 | ``` |
| 313 | 42 |
| 314 | 1.5Gi |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 315 | 170_141_183_460_469_231_731_687_303_715_884_105_727 |
Marcel van Lohuizen | fc6303c | 2019-02-07 17:49:04 +0100 | [diff] [blame] | 316 | 0xBad_Face |
| 317 | 0o755 |
| 318 | 0b0101_0001 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 319 | ``` |
| 320 | |
| 321 | ### Decimal floating-point literals |
| 322 | |
| 323 | A decimal floating-point literal is a representation of |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 324 | a decimal floating-point value (a _float_). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 325 | It has an integer part, a decimal point, a fractional part, and an |
| 326 | exponent part. |
| 327 | The integer and fractional part comprise decimal digits; the |
| 328 | exponent part is an `e` or `E` followed by an optionally signed decimal exponent. |
| 329 | One of the integer part or the fractional part may be elided; one of the decimal |
| 330 | point or the exponent may be elided. |
| 331 | |
| 332 | ``` |
| 333 | decimal_lit = decimals "." [ decimals ] [ exponent ] | |
| 334 | decimals exponent | |
| 335 | "." decimals [ exponent ] . |
| 336 | exponent = ( "e" | "E" ) [ "+" | "-" ] decimals . |
| 337 | ``` |
| 338 | |
| 339 | ``` |
| 340 | 0. |
| 341 | 72.40 |
| 342 | 072.40 // == 72.40 |
| 343 | 2.71828 |
| 344 | 1.e+0 |
| 345 | 6.67428e-11 |
| 346 | 1E6 |
| 347 | .25 |
| 348 | .12345E+5 |
| 349 | ``` |
| 350 | |
| 351 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 352 | ### String and byte sequence literals |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 353 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 354 | A string literal represents a string constant obtained from concatenating a |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 355 | sequence of characters. |
| 356 | Byte sequences are a sequence of bytes. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 357 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 358 | String and byte sequence literals are character sequences between, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 359 | respectively, double and single quotes, as in `"bar"` and `'bar'`. |
| 360 | Within the quotes, any character may appear except newline and, |
| 361 | respectively, unescaped double or single quote. |
| 362 | String literals may only be valid UTF-8. |
| 363 | Byte sequences may contain any sequence of bytes. |
| 364 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 365 | Several escape sequences allow arbitrary values to be encoded as ASCII text. |
| 366 | An escape sequence starts with an _escape delimiter_, which is `\` by default. |
| 367 | The escape delimiter may be altered to be `\` plus a fixed number of |
| 368 | hash symbols `#` |
| 369 | by padding the start and end of a string or byte sequence literal |
| 370 | with this number of hash symbols. |
| 371 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 372 | There are four ways to represent the integer value as a numeric constant: `\x` |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 373 | followed by exactly two hexadecimal digits; `\u` followed by exactly four |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 374 | hexadecimal digits; `\U` followed by exactly eight hexadecimal digits, and a |
| 375 | plain backslash `\` followed by exactly three octal digits. |
| 376 | In each case the value of the literal is the value represented by the |
| 377 | digits in the corresponding base. |
| 378 | Hexadecimal and octal escapes are only allowed within byte sequences |
| 379 | (single quotes). |
| 380 | |
| 381 | Although these representations all result in an integer, they have different |
| 382 | valid ranges. |
| 383 | Octal escapes must represent a value between 0 and 255 inclusive. |
| 384 | Hexadecimal escapes satisfy this condition by construction. |
| 385 | The escapes `\u` and `\U` represent Unicode code points so within them |
| 386 | some values are illegal, in particular those above `0x10FFFF`. |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 387 | Surrogate halves are allowed, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 388 | but are translated into their non-surrogate equivalent internally. |
| 389 | |
| 390 | The three-digit octal (`\nnn`) and two-digit hexadecimal (`\xnn`) escapes |
| 391 | represent individual bytes of the resulting string; all other escapes represent |
| 392 | the (possibly multi-byte) UTF-8 encoding of individual characters. |
| 393 | Thus inside a string literal `\377` and `\xFF` represent a single byte of |
| 394 | value `0xFF=255`, while `ÿ`, `\u00FF`, `\U000000FF` and `\xc3\xbf` represent |
| 395 | the two bytes `0xc3 0xbf` of the UTF-8 |
| 396 | encoding of character `U+00FF`. |
| 397 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 398 | ``` |
| 399 | \a U+0007 alert or bell |
| 400 | \b U+0008 backspace |
| 401 | \f U+000C form feed |
| 402 | \n U+000A line feed or newline |
| 403 | \r U+000D carriage return |
| 404 | \t U+0009 horizontal tab |
| 405 | \v U+000b vertical tab |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 406 | \/ U+002f slash (solidus) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 407 | \\ U+005c backslash |
| 408 | \' U+0027 single quote (valid escape only within single quoted literals) |
| 409 | \" U+0022 double quote (valid escape only within double quoted literals) |
| 410 | ``` |
| 411 | |
| 412 | The escape `\(` is used as an escape for string interpolation. |
| 413 | A `\(` must be followed by a valid CUE Expression, followed by a `)`. |
| 414 | |
| 415 | All other sequences starting with a backslash are illegal inside literals. |
| 416 | |
| 417 | ``` |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 418 | escaped_char = `\` { `#` } ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 419 | byte_value = octal_byte_value | hex_byte_value . |
| 420 | octal_byte_value = `\` octal_digit octal_digit octal_digit . |
| 421 | hex_byte_value = `\` "x" hex_digit hex_digit . |
| 422 | little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit . |
| 423 | big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit |
| 424 | hex_digit hex_digit hex_digit hex_digit . |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 425 | unicode_value = unicode_char | little_u_value | big_u_value | escaped_char . |
| 426 | interpolation = "\(" Expression ")" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 427 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 428 | string_lit = simple_string_lit | |
| 429 | multiline_string_lit | |
| 430 | simple_bytes_lit | |
| 431 | multiline_bytes_lit | |
| 432 | `#` string_lit `#` . |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 433 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 434 | simple_string_lit = `"` { unicode_value | interpolation } `"` . |
| 435 | simple_bytes_lit = `"` { unicode_value | interpolation | byte_value } `"` . |
| 436 | multiline_string_lit = `"""` newline |
| 437 | { unicode_value | interpolation | newline } |
| 438 | newline `"""` . |
| 439 | multiline_bytes_lit = "'''" newline |
| 440 | { unicode_value | interpolation | byte_value | newline } |
| 441 | newline "'''" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 442 | ``` |
| 443 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 444 | Carriage return characters (`\r`) inside string literals are discarded from |
Marcel van Lohuizen | db9d25a | 2019-02-21 23:54:43 +0100 | [diff] [blame] | 445 | the string value. |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 446 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 447 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 448 | 'a\000\xab' |
| 449 | '\007' |
| 450 | '\377' |
| 451 | '\xa' // illegal: too few hexadecimal digits |
| 452 | "\n" |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 453 | "\"" |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 454 | 'Hello, world!\n' |
| 455 | "Hello, \( name )!" |
| 456 | "日本語" |
| 457 | "\u65e5本\U00008a9e" |
| 458 | "\xff\u00FF" |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 459 | "\uD800" // illegal: surrogate half (TODO: probably should allow) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 460 | "\U00110000" // illegal: invalid Unicode code point |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 461 | |
| 462 | #"This is not an \(interpolation)"# |
| 463 | #"This is an \#(interpolation)"# |
| 464 | #"The sequence "\U0001F604" renders as \#U0001F604."# |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 465 | ``` |
| 466 | |
| 467 | These examples all represent the same string: |
| 468 | |
| 469 | ``` |
| 470 | "日本語" // UTF-8 input text |
| 471 | '日本語' // UTF-8 input text as byte sequence |
| 472 | `日本語` // UTF-8 input text as a raw literal |
| 473 | "\u65e5\u672c\u8a9e" // the explicit Unicode code points |
| 474 | "\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points |
| 475 | "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes |
| 476 | ``` |
| 477 | |
| 478 | If the source code represents a character as two code points, such as a |
| 479 | combining form involving an accent and a letter, the result will appear as two |
| 480 | code points if placed in a string literal. |
| 481 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 482 | Strings and byte sequences have a multiline equivalent. |
| 483 | Multiline strings are like their single-line equivalent, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 484 | but allow newline characters. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 485 | |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 486 | Multiline strings and byte sequences respectively start with |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 487 | a triple double quote (`"""`) or triple single quote (`'''`), |
| 488 | immediately followed by a newline, which is discarded from the string contents. |
| 489 | The string is closed by a matching triple quote, which must be by itself |
| 490 | on a newline, preceded by optional whitespace. |
| 491 | The whitespace before a closing triple quote must appear before any non-empty |
| 492 | line after the opening quote and will be removed from each of these |
| 493 | lines in the string literal. |
| 494 | A closing triple quote may not appear in the string. |
| 495 | To include it is suffices to escape one of the quotes. |
| 496 | |
| 497 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 498 | """ |
| 499 | lily: |
| 500 | out of the water |
| 501 | out of itself |
| 502 | |
| 503 | bass |
| 504 | picking bugs |
| 505 | off the moon |
| 506 | — Nick Virgilio, Selected Haiku, 1988 |
| 507 | """ |
| 508 | ``` |
| 509 | |
| 510 | This represents the same string as: |
| 511 | |
| 512 | ``` |
| 513 | "lily:\nout of the water\nout of itself\n\n" + |
| 514 | "bass\npicking bugs\noff the moon\n" + |
| 515 | " — Nick Virgilio, Selected Haiku, 1988" |
| 516 | ``` |
| 517 | |
| 518 | <!-- TODO: other values |
| 519 | |
| 520 | Support for other values: |
| 521 | - Duration literals |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 522 | - regular expessions: `re("[a-z]")` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 523 | --> |
| 524 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 525 | |
| 526 | ## Values |
| 527 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 528 | In addition to simple values like `"hello"` and `42.0`, CUE has _structs_. |
| 529 | A struct is a map from labels to values, like `{a: 42.0, b: "hello"}`. |
| 530 | Structs are CUE's only way of building up complex values; |
| 531 | lists, which we will see later, |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 532 | are defined in terms of structs. |
| 533 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 534 | All possible values are ordered in a lattice, |
| 535 | a partial order where every two elements have a single greatest lower bound. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 536 | A value `a` is an _instance_ of a value `b`, |
| 537 | denoted `a ⊑ b`, if `b == a` or `b` is more general than `a`, |
| 538 | that is if `a` orders before `b` in the partial order |
| 539 | (`⊑` is _not_ a CUE operator). |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 540 | We also say that `b` _subsumes_ `a` in this case. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 541 | In graphical terms, `b` is "above" `a` in the lattice. |
| 542 | |
| 543 | At the top of the lattice is the single ancestor of all values, called |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 544 | _top_, denoted `_` in CUE. |
| 545 | Every value is an instance of top. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 546 | |
| 547 | At the bottom of the lattice is the value called _bottom_, denoted `_|_`. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 548 | A bottom value usually indicates an error. |
| 549 | Bottom is an instance of every value. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 550 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 551 | An _atom_ is any value whose only instances are itself and bottom. |
| 552 | Examples of atoms are `42.0`, `"hello"`, `true`, `null`. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 553 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 554 | A value is _concrete_ if it is either an atom, or a struct all of whose |
| 555 | field values are themselves concrete, recursively. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 556 | |
| 557 | CUE's values also include what we normally think of as types, like `string` and |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 558 | `float`. |
| 559 | But CUE does not distinguish between types and values; only the |
| 560 | relationship of values in the lattice is important. |
| 561 | Each CUE "type" subsumes the concrete values that one would normally think |
| 562 | of as part of that type. |
| 563 | For example, "hello" is an instance of `string`, and `42.0` is an instance of |
| 564 | `float`. |
| 565 | In addition to `string` and `float`, CUE has `null`, `int`, `bool` and `bytes`. |
| 566 | We informally call these CUE's "basic types". |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 567 | |
| 568 | |
| 569 | ``` |
| 570 | false ⊑ bool |
| 571 | true ⊑ bool |
| 572 | true ⊑ true |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 573 | 5.0 ⊑ float |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 574 | bool ⊑ _ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 575 | _|_ ⊑ _ |
| 576 | _|_ ⊑ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 577 | |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 578 | _ ⋢ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 579 | _ ⋢ bool |
| 580 | int ⋢ bool |
| 581 | bool ⋢ int |
| 582 | false ⋢ true |
| 583 | true ⋢ false |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 584 | float ⋢ 5.0 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 585 | 5 ⋢ 6 |
| 586 | ``` |
| 587 | |
| 588 | |
| 589 | ### Unification |
| 590 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 591 | The _unification_ of values `a` and `b` |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 592 | is defined as the greatest lower bound of `a` and `b`. (That is, the |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 593 | value `u` such that `u ⊑ a` and `u ⊑ b`, |
| 594 | and for any other value `v` for which `v ⊑ a` and `v ⊑ b` |
| 595 | it holds that `v ⊑ u`.) |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 596 | Since CUE values form a lattice, the unification of two CUE values is |
| 597 | always unique. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 598 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 599 | These all follow from the definition of unification: |
| 600 | - The unification of `a` with itself is always `a`. |
| 601 | - The unification of values `a` and `b` where `a ⊑ b` is always `a`. |
| 602 | - The unification of a value with bottom is always bottom. |
| 603 | |
| 604 | Unification in CUE is a [binary expression](#Operands), written `a & b`. |
| 605 | It is commutative and associative. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 606 | As a consequence, order of evaluation is irrelevant, a property that is key |
| 607 | to many of the constructs in the CUE language as well as the tooling layered |
| 608 | on top of it. |
| 609 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 610 | |
| 611 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 612 | <!-- TODO: explicitly mention that disjunction is not a binary operation |
| 613 | but a definition of a single value?--> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 614 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 615 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 616 | ### Disjunction |
| 617 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 618 | The _disjunction_ of values `a` and `b` |
| 619 | is defined as the least upper bound of `a` and `b`. |
| 620 | (That is, the value `d` such that `a ⊑ d` and `b ⊑ d`, |
| 621 | and for any other value `e` for which `a ⊑ e` and `b ⊑ e`, |
| 622 | it holds that `d ⊑ e`.) |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 623 | This style of disjunctions is sometimes also referred to as sum types. |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 624 | Since CUE values form a lattice, the disjunction of two CUE values is always unique. |
| 625 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 626 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 627 | These all follow from the definition of disjunction: |
| 628 | - The disjunction of `a` with itself is always `a`. |
| 629 | - The disjunction of a value `a` and `b` where `a ⊑ b` is always `b`. |
| 630 | - The disjunction of a value `a` with bottom is always `a`. |
| 631 | - The disjunction of two bottom values is bottom. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 632 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame] | 633 | Disjunction in CUE is a [binary expression](#Operands), written `a | b`. |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 634 | It is commutative, associative, and idempotent. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 635 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 636 | The unification of a disjunction with another value is equal to the disjunction |
| 637 | composed of the unification of this value with all of the original elements |
| 638 | of the disjunction. |
| 639 | In other words, unification distributes over disjunction. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 640 | |
| 641 | ``` |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 642 | (a_0 | ... |a_n) & b ==> a_0&b | ... | a_n&b. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 643 | ``` |
| 644 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 645 | ``` |
| 646 | Expression Result |
| 647 | ({a:1} | {b:2}) & {c:3} {a:1, c:3} | {b:2, c:3} |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 648 | (int | string) & "foo" "foo" |
| 649 | ("a" | "b") & "c" _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 650 | ``` |
| 651 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 652 | A disjunction is _normalized_ if there is no element |
| 653 | `a` for which there is an element `b` such that `a ⊑ b`. |
| 654 | |
| 655 | <!-- |
| 656 | Normalization is important, as we need to account for spurious elements |
| 657 | For instance "tcp" | "tcp" should resolve to "tcp". |
| 658 | |
| 659 | Also consider |
| 660 | |
| 661 | ({a:1} | {b:1}) & ({a:1} | {b:2}) -> {a:1} | {a:1,b:1} | {a:1,b:2}, |
| 662 | |
| 663 | in this case, elements {a:1,b:1} and {a:1,b:2} are subsumed by {a:1} and thus |
| 664 | this expression is logically equivalent to {a:1} and should therefore be |
| 665 | considered to be unambiguous and resolve to {a:1} if a concrete value is needed. |
| 666 | |
| 667 | For instance, in |
| 668 | |
| 669 | x: ({a:1} | {b:1}) & ({a:1} | {b:2}) // -> {a:1} | {a:1,b:1} | {a:1,b:2} |
| 670 | y: x.a // 1 |
| 671 | |
| 672 | y should resolve to 1, and not an error. |
| 673 | |
| 674 | For comparison, in |
| 675 | |
| 676 | x: ({a:1, b:1} | {b:2}) & {a:1} // -> {a:1,b:1} | {a:1,b:2} |
| 677 | y: x.a // _|_ |
| 678 | |
| 679 | y should be an error as x is still ambiguous before the selector is applied, |
| 680 | even though `a` resolves to 1 in all cases. |
| 681 | --> |
| 682 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 683 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 684 | #### Default values |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 685 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 686 | Any element of a disjunction can be marked as a default |
| 687 | by prefixing it with an asterisk '*'. |
| 688 | Intuitively, when an expression needs to be resolved for an operation other |
| 689 | than unification or disjunctions, |
| 690 | non-starred elements are dropped in favor of starred ones if the starred ones |
| 691 | do not resolve to bottom. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 692 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 693 | More precisely, any value `v` may be associated with a default value `d`, |
| 694 | denoted `(v, d)` (not CUE syntax), |
| 695 | where `d` must be in instance of `v` (`d ⊑ v`). |
| 696 | The rules for unifying and disjoining such values are as follows: |
| 697 | |
| 698 | ``` |
| 699 | U1: (v1, d1) & v2 => (v1&v2, d1&v2) |
| 700 | U2: (v1, d1) & (v2, d2) => (v1&v2, d1&d2) |
| 701 | |
| 702 | D1: (v1, d1) | v2 => (v1|v2, d1) |
| 703 | D2: (v1, d1) | (v2, d2) => (v1|v2, d1|d2) |
| 704 | ``` |
| 705 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 706 | Default values may be introduced within disjunctions |
| 707 | by _marking_ terms of a disjunction with an asterisk `*` |
| 708 | ([a unary expression](#Operators)). |
| 709 | The default value of a disjunction with marked terms is the disjunction |
| 710 | of those marked terms, applying the following rules for marks: |
| 711 | |
| 712 | ``` |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 713 | M1: *v => (v, v) |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 714 | M2: *(v1, d1) => (v1, d1) |
| 715 | ``` |
| 716 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 717 | In general, any operation in CUE involving default values proceeds along the |
| 718 | following lines |
| 719 | ``` |
| 720 | O1: f((v1, d1), ..., (vn, dn)) => (fn(v1, ..., vn), fn(d1, ..., dn)) |
| 721 | ``` |
| 722 | where, with the exception of disjunction, a value `v` without a default |
| 723 | value is promoted to `(v, v)`. |
| 724 | |
| 725 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 726 | ``` |
| 727 | Expression Value-default pair Rules applied |
| 728 | *"tcp" | "udp" ("tcp"|"udp", "tcp") M1, D1 |
| 729 | string | *"foo" (string, "foo") M1, D1 |
| 730 | |
| 731 | *1 | 2 | 3 (1|2|3, 1) M1, D1 |
| 732 | |
| 733 | (*1|2|3) | (1|*2|3) (1|2|3, 1|2) M1, D1, D2 |
| 734 | (*1|2|3) | *(1|*2|3) (1|2|3, 1|2) M1, D1, M2, D2 |
| 735 | (*1|2|3) | (1|*2|3)&2 (1|2|3, 1|2) M1, D1, U1, D2 |
| 736 | |
| 737 | (*1|2) & (1|*2) (1|2, _|_) M1, D1, U2 |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 738 | |
| 739 | (*1|2) + (1|*2) ((1|2)+(1|2), 3) M1, D1, O1 |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 740 | ``` |
| 741 | |
| 742 | The rules of subsumption for defaults can be derived from the above definitions |
| 743 | and are as follows. |
| 744 | |
| 745 | ``` |
| 746 | (v2, d2) ⊑ (v1, d1) if v2 ⊑ v1 and d2 ⊑ d1 |
| 747 | (v1, d1) ⊑ v if v1 ⊑ v |
| 748 | v ⊑ (v1, d1) if v ⊑ d1 |
| 749 | ``` |
| 750 | |
| 751 | <!-- |
| 752 | For the second rule, note that by definition d1 ⊑ v1, so d1 ⊑ v1 ⊑ v. |
| 753 | |
| 754 | The last one is so restrictive as v could still be made more specific by |
| 755 | associating it with a default that is not subsumed by d1. |
| 756 | |
| 757 | Proof: |
| 758 | by definition for any d ⊑ v, it holds that (v, d) ⊑ v, |
| 759 | where the most general value is (v, v). |
| 760 | Given the subsumption rule for (v2, d2) ⊑ (v1, d1), |
| 761 | from (v, v) ⊑ v ⊑ (v1, d1) it follows that v ⊑ d1 |
| 762 | exactly defines the boundary of this subsumption. |
| 763 | --> |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 764 | |
| 765 | <!-- |
| 766 | (non-normalized entries could also be implicitly marked, allowing writing |
| 767 | int | 1, instead of int | *1, but that can be done in a backwards |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 768 | compatible way later if really desirable, as long as we require that |
| 769 | disjunction literals be normalized). |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 770 | --> |
| 771 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 772 | |
| 773 | ``` |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 774 | Expression Resolves to |
| 775 | "tcp" | "udp" "tcp" | "udp" |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 776 | *"tcp" | "udp" "tcp" |
| 777 | float | *1 1 |
| 778 | *string | 1.0 string |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 779 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 780 | (*1|2|3) | (1|*2|3) 1|2 |
| 781 | (*1|2|3) & (1|*2|3) 1|2|3 // default is _|_ |
| 782 | |
| 783 | (* >=5 | int) & (* <=5 | int) 5 |
| 784 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 785 | (*"tcp"|"udp") & ("udp"|*"tcp") "tcp" |
| 786 | (*"tcp"|"udp") & ("udp"|"tcp") "tcp" |
| 787 | (*"tcp"|"udp") & "tcp" "tcp" |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 788 | (*"tcp"|"udp") & (*"udp"|"tcp") "tcp" | "udp" // default is _|_ |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 789 | |
| 790 | (*true | false) & bool true |
| 791 | (*true | false) & (true | false) true |
| 792 | |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 793 | {a: 1} | {b: 1} {a: 1} | {b: 1} |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 794 | {a: 1} | *{b: 1} {b:1} |
Marcel van Lohuizen | 6e5d993 | 2019-03-14 15:52:48 +0100 | [diff] [blame] | 795 | *{a: 1} | *{b: 1} {a: 1} | {b: 1} |
| 796 | ({a: 1} | {b: 1}) & {a:1} {a:1} // after eliminating {a:1,b:1} by normalization |
| 797 | ({a:1}|*{b:1}) & ({a:1}|*{b:1}) {b:1} // after eliminating {a:1,b:1} by normalization |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 798 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 799 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 800 | |
| 801 | ### Bottom and errors |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 802 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 803 | Any evaluation error in CUE results in a bottom value, respresented by |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 804 | the token '_|_'. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 805 | Bottom is an instance of every other value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 806 | Any evaluation error is represented as bottom. |
| 807 | |
| 808 | Implementations may associate error strings with different instances of bottom; |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 809 | logically they all remain the same value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 810 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 811 | |
| 812 | ### Top |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 813 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 814 | Top is represented by the underscore character '_', lexically an identifier. |
| 815 | Unifying any value `v` with top results `v` itself. |
| 816 | |
| 817 | ``` |
| 818 | Expr Result |
| 819 | _ & 5 5 |
| 820 | _ & _ _ |
| 821 | _ & _|_ _|_ |
| 822 | _ | _|_ _ |
| 823 | ``` |
| 824 | |
| 825 | |
| 826 | ### Null |
| 827 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 828 | The _null value_ is represented with the keyword `null`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 829 | It has only one parent, top, and one child, bottom. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 830 | It is unordered with respect to any other value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 831 | |
| 832 | ``` |
| 833 | null_lit = "null" |
| 834 | ``` |
| 835 | |
| 836 | ``` |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 837 | null & 8 _|_ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 838 | null & _ null |
| 839 | null & _|_ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 840 | ``` |
| 841 | |
| 842 | |
| 843 | ### Boolean values |
| 844 | |
| 845 | A _boolean type_ represents the set of Boolean truth values denoted by |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 846 | the keywords `true` and `false`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 847 | The predeclared boolean type is `bool`; it is a defined type and a separate |
| 848 | element in the lattice. |
| 849 | |
| 850 | ``` |
| 851 | boolean_lit = "true" | "false" |
| 852 | ``` |
| 853 | |
| 854 | ``` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 855 | bool & true true |
| 856 | true & true true |
| 857 | true & false _|_ |
| 858 | bool & (false|true) false | true |
| 859 | bool & (true|false) true | false |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 860 | ``` |
| 861 | |
| 862 | |
| 863 | ### Numeric values |
| 864 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 865 | The _integer type_ represents the set of all integral numbers. |
| 866 | The _decimal floating-point type_ represents the set of all decimal floating-point |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 867 | numbers. |
| 868 | They are two distinct types. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 869 | The predeclared integer and decimal floating-point types are `int` and `float`; |
| 870 | they are defined types. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 871 | |
| 872 | A decimal floating-point literal always has type `float`; |
| 873 | it is not an instance of `int` even if it is an integral number. |
| 874 | |
| 875 | An integer literal has both type `int` and `float`, with the integer variant |
| 876 | being the default if no other constraints are applied. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 877 | Expressed in terms of disjunction and [type conversion](#conversions), |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 878 | the literal `1`, for instance, is defined as `int(1) | float(1)`. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 879 | Hexadecimal, octal, and binary integer literals are always of type `int`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 880 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 881 | Numeric literals are exact values of arbitrary precision. |
| 882 | If the operation permits it, numbers should be kept in arbitrary precision. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 883 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 884 | Implementation restriction: although numeric values have arbitrary precision |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 885 | in the language, implementations may implement them using an internal |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 886 | representation with limited precision. |
| 887 | That said, every implementation must: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 888 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 889 | - Represent integer values with at least 256 bits. |
| 890 | - Represent floating-point values, with a mantissa of at least 256 bits and |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 891 | a signed binary exponent of at least 16 bits. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 892 | - Give an error if unable to represent an integer value precisely. |
| 893 | - Give an error if unable to represent a floating-point value due to overflow. |
| 894 | - Round to the nearest representable value if unable to represent |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 895 | a floating-point value due to limits on precision. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 896 | These requirements apply to the result of any expression except for builtin |
| 897 | functions for which an unusual loss of precision must be explicitly documented. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 898 | |
| 899 | |
| 900 | ### Strings |
| 901 | |
| 902 | The _string type_ represents the set of all possible UTF-8 strings, |
| 903 | not allowing surrogates. |
| 904 | The predeclared string type is `string`; it is a defined type. |
| 905 | |
| 906 | Strings are designed to be unicode-safe. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 907 | Comparison is done using canonical forms ("é" == "e\u0301"). |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 908 | A string element is an |
| 909 | [extended grapheme cluster](https://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries), |
| 910 | which is an approximation of a human-readable character. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 911 | |
| 912 | The length of a string `s` (its size in bytes) can be discovered using |
| 913 | the built-in function len. |
| 914 | A string's extended grapheme cluster can be accessed by integer index |
| 915 | 0 through len(s)-1 for any byte that is part of that grapheme cluster. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 916 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 917 | To access the individual bytes of a string one should convert it to |
| 918 | a sequence of bytes first. |
| 919 | |
| 920 | |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 921 | ### Bounds |
| 922 | |
| 923 | A _bound_, syntactically_ a [unary expression](#Operands), defines |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 924 | an infinite disjunction of concrete values than can be represented |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 925 | as a single comparison. |
| 926 | |
| 927 | For any [comparison operator](#Comparison-operators) `op` except `==`, |
| 928 | `op a` is the disjunction of every `x` such that `x op a`. |
| 929 | |
| 930 | ``` |
| 931 | 2 & >=2 & <=5 // 2, where 2 is either an int or float. |
| 932 | 2.5 & >=1 & <=5 // 2.5 |
| 933 | 2 & >=1.0 & <3.0 // 2.0 |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 934 | 2 & >1 & <3.0 // 2.0 |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 935 | 2.5 & int & >1 & <5 // _|_ |
| 936 | 2.5 & float & >1 & <5 // 2.5 |
| 937 | int & 2 & >1.0 & <3.0 // _|_ |
| 938 | 2.5 & >=(int & 1) & <5 // _|_ |
| 939 | >=0 & <=7 & >=3 & <=10 // >=3 & <=7 |
| 940 | !=null & 1 // 1 |
| 941 | >=5 & <=5 // 5 |
| 942 | ``` |
| 943 | |
| 944 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 945 | ### Structs |
| 946 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 947 | A _struct_ is a set of elements called _fields_, each of |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 948 | which has a name, called a _label_, and value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 949 | |
| 950 | We say a label is defined for a struct if the struct has a field with the |
| 951 | corresponding label. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 952 | The value for a label `f` of struct `a` is denoted `f.a`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 953 | A struct `a` is an instance of `b`, or `a ⊑ b`, if for any label `f` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 954 | defined for `b`, label `f` is also defined for `a` and `a.f ⊑ b.f`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 955 | Note that if `a` is an instance of `b` it may have fields with labels that |
| 956 | are not defined for `b`. |
| 957 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 958 | The (unique) struct with no fields, written `{}`, has every struct as an |
| 959 | instance. It can be considered the type of all structs. |
| 960 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 961 | The successful unification of structs `a` and `b` is a new struct `c` which |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 962 | has all fields of both `a` and `b`, where |
| 963 | the value of a field `f` in `c` is `a.f & b.f` if `f` is in both `a` and `b`, |
| 964 | or just `a.f` or `b.f` if `f` is in just `a` or `b`, respectively. |
| 965 | Any [references](#References) to `a` or `b` |
| 966 | in their respective field values need to be replaced with references to `c`. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 967 | The result of a unification is bottom (`_|_`) if any of its fields evaluates |
| 968 | to bottom, recursively. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 969 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 970 | A field name may also be an interpolated string. |
| 971 | Identifiers used in such strings are evaluated within |
| 972 | the scope of the struct in which the label is defined. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 973 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 974 | Syntactically, a struct literal may contain multiple fields with |
| 975 | the same label, the result of which is a single field with a value |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 976 | that is the unification of the values of those fields. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 977 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 978 | A TemplateLabel indicates a template value that is to be unified with |
| 979 | the values of all fields within a struct. |
| 980 | The identifier of a template label binds to the field name of each |
| 981 | field and is visible within the template value. |
| 982 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 983 | ``` |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 984 | StructLit = "{" [ Declaration { "," Declaration } [ "," ] ] "}" . |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 985 | Declaration = FieldDecl | AliasDecl | ComprehensionDecl . |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 986 | FieldDecl = Label { Label } ":" Expression { attribute } . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 987 | |
| 988 | AliasDecl = Label "=" Expression . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 989 | TemplateLabel = "<" identifier ">" . |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 990 | ConcreteLabel = identifier | simple_string_lit |
| 991 | OptionalLabel = ConcreteLabel "?" |
| 992 | Label = ConcreteLabel | OptionalLabel | TemplateLabel . |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 993 | |
| 994 | attribute = "@" identifier "(" attr_elem { "," attr_elem } ")" . |
| 995 | attr_elem = attr_string | identifier "=" attr_string . |
| 996 | attr_string = { attr_char } | string_lit . |
| 997 | attr_char = /* an arbitrary Unicode code point except newline, ',', '"', `'`, '#', '=', '(', and ')' */ . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 998 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 999 | |
| 1000 | ``` |
| 1001 | {a: 1} ⊑ {} |
| 1002 | {a: 1, b: 1} ⊑ {a: 1} |
| 1003 | {a: 1} ⊑ {a: int} |
| 1004 | {a: 1, b: 1} ⊑ {a: int, b: float} |
| 1005 | |
| 1006 | {} ⋢ {a: 1} |
| 1007 | {a: 2} ⋢ {a: 1} |
| 1008 | {a: 1} ⋢ {b: 1} |
| 1009 | ``` |
| 1010 | |
| 1011 | ``` |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1012 | Expression Result |
| 1013 | {a: int, a: 1} {a: int(1)} |
| 1014 | {a: int} & {a: 1} {a: int(1)} |
| 1015 | {a: >=1 & <=7} & {a: >=5 & <=9} {a: >=5 & <=7} |
| 1016 | {a: >=1 & <=7, a: >=5 & <=9} {a: >=5 & <=7} |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1017 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1018 | {a: 1} & {b: 2} {a: 1, b: 2} |
| 1019 | {a: 1, b: int} & {b: 2} {a: 1, b: int(2)} |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1020 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1021 | {a: 1} & {a: 2} _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1022 | ``` |
| 1023 | |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 1024 | Fields may be associated with attributes. |
| 1025 | Attributes define additional information about a field, |
| 1026 | such as a mapping to a protobuf tag or alternative |
| 1027 | name of the field when mapping to a different language. |
| 1028 | |
| 1029 | If a field has multiple attributes their identifiers must be unique. |
| 1030 | Attributes accumulate when unifying two fields, removing duplicate entries. |
| 1031 | It is an error for the resulting field to have two different attributes |
| 1032 | with the same identifier. |
| 1033 | |
| 1034 | Attributes are not directly part of the data model, but may be |
| 1035 | accessed through the API or other means of reflection. |
| 1036 | The interpretation of the attribute value |
| 1037 | (a comma-separated list of attribute elements) depends on the attribute. |
| 1038 | Interpolations are not allowed in attribute strings. |
| 1039 | |
| 1040 | The recommended convention, however, is to interpret the first |
| 1041 | `n` arguments as positional arguments, |
| 1042 | where duplicate conflicting entries are an error, |
| 1043 | and the remaining arguments as a combination of flags |
| 1044 | (an identifier) and key value pairs, separated by a `=`. |
| 1045 | |
| 1046 | ``` |
| 1047 | MyStruct1: { |
| 1048 | field: string @go(Field) |
| 1049 | attr: int @xml(,attr) @go(Attr) |
| 1050 | } |
| 1051 | |
| 1052 | MyStruct2: { |
| 1053 | field: string @go(Field) |
| 1054 | attr: int @xml(a1,attr) @go(Attr) |
| 1055 | } |
| 1056 | |
| 1057 | Combined: MyStruct1 & MyStruct2 |
| 1058 | // field: string @go(Field) |
| 1059 | // attr: int @xml(,attr) @xml(a1,attr) @go(Attr) |
| 1060 | ``` |
| 1061 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1062 | In addition to fields, a struct literal may also define aliases. |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1063 | Aliases name values that can be referred to |
| 1064 | within the [scope](#declarations-and-scopes) of their |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1065 | definition, but are not part of the struct: aliases are irrelevant to |
| 1066 | the partial ordering of values and are not emitted as part of any |
| 1067 | generated data. |
| 1068 | The name of an alias must be unique within the struct literal. |
| 1069 | |
| 1070 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1071 | // The empty struct. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1072 | {} |
| 1073 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1074 | // A struct with 3 fields and 1 alias. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1075 | { |
| 1076 | alias = 3 |
| 1077 | |
| 1078 | foo: 2 |
| 1079 | bar: "a string" |
| 1080 | |
| 1081 | "not an ident": 4 |
| 1082 | } |
| 1083 | ``` |
| 1084 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1085 | A field whose value is a struct with a single field may be written as |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1086 | a sequence of the two field names, |
| 1087 | followed by a colon and the value of that single field. |
| 1088 | |
| 1089 | ``` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1090 | job myTask replicas: 2 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1091 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1092 | expands to |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1093 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1094 | job: { |
| 1095 | myTask: { |
| 1096 | replicas: 2 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | ``` |
| 1100 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1101 | |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 1102 | #### Optional fields |
| 1103 | |
| 1104 | An identifier or string label may be followed by a question mark `?` |
| 1105 | to indicate a field is optional. |
Marcel van Lohuizen | 8bc02e5 | 2019-04-01 13:14:07 +0200 | [diff] [blame] | 1106 | The question mark is not part of the field name. |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 1107 | Constraints defined by an optional field should only be applied when |
| 1108 | a field is present. |
Marcel van Lohuizen | 8bc02e5 | 2019-04-01 13:14:07 +0200 | [diff] [blame] | 1109 | A field with such a marker may be omitted from output and should not cause |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 1110 | an error when emitting a concrete configuration, even if its value is |
| 1111 | not concrete or bottom. |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 1112 | The result of unifying two fields only has an optional marker |
| 1113 | if both fields have such a marker. |
| 1114 | |
| 1115 | <!-- |
| 1116 | The optional marker solves the issue of having to print large amounts of |
| 1117 | boilerplate when dealing with large types with many optional or default |
| 1118 | values (such as Kubernetes). |
| 1119 | Writing such optional values in terms of *null | value is tedious, |
| 1120 | unpleasant to read, and as it is not well defined what can be dropped or not, |
| 1121 | all null values have to be emitted from the output, even if the user |
| 1122 | doesn't override them. |
| 1123 | Part of the issue is how null is defined. We could adopt a Typescript-like |
| 1124 | approach of introducing "void" or "undefined" to mean "not defined and not |
| 1125 | part of the output". But having all of null, undefined, and void can be |
| 1126 | confusing. If these ever are introduced anyway, the ? operator could be |
| 1127 | expressed along the lines of |
| 1128 | foo?: bar |
| 1129 | being a shorthand for |
| 1130 | foo: void | bar |
| 1131 | where void is the default if no other default is given. |
| 1132 | |
| 1133 | The current mechanical definition of "?" is straightforward, though, and |
| 1134 | probably avoids the need for void, while solving a big issue. |
| 1135 | |
| 1136 | Caveats: |
| 1137 | [1] this definition requires explicitly defined fields to be emitted, even |
| 1138 | if they could be elided (for instance if the explicit value is the default |
| 1139 | value defined an optional field). This is probably a good thing. |
| 1140 | |
| 1141 | [2] a default value may still need to be included in an output if it is not |
| 1142 | the zero value for that field and it is not known if any outside system is |
| 1143 | aware of defaults. For instance, which defaults are specified by the user |
| 1144 | and which by the schema understood by the receiving system. |
| 1145 | The use of "?" together with defaults should therefore be used carefully |
| 1146 | in non-schema definitions. |
| 1147 | Problematic cases should be easy to detect by a vet-like check, though. |
| 1148 | |
| 1149 | [3] It should be considered how this affects the trim command. |
| 1150 | Should values implied by optional fields be allowed to be removed? |
| 1151 | Probably not. This restriction is unlikely to limit the usefulness of trim, |
| 1152 | though. |
| 1153 | |
| 1154 | [4] There should be an option to emit all concrete optional values. |
| 1155 | ``` |
| 1156 | --> |
| 1157 | |
| 1158 | ``` |
| 1159 | Input Result |
| 1160 | a: { foo?: string } {} |
| 1161 | b: { foo: "bar" } { foo: "bar" } |
| 1162 | c: { foo?: *"bar" | string } {} |
| 1163 | |
| 1164 | d: a & b { foo: "bar" } |
| 1165 | e: b & c { foo: "bar" } |
| 1166 | f: a & c {} |
| 1167 | g: a & { foo?: number } _|_ |
| 1168 | ``` |
| 1169 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1170 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1171 | ### Lists |
| 1172 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1173 | A list literal defines a new value of type list. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1174 | A list may be open or closed. |
| 1175 | An open list is indicated with a `...` at the end of an element list, |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1176 | optionally followed by a value for the remaining elements. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1177 | |
| 1178 | The length of a closed list is the number of elements it contains. |
| 1179 | The length of an open list is the its number of elements as a lower bound |
| 1180 | and an unlimited number of elements as its upper bound. |
| 1181 | |
| 1182 | ``` |
Marcel van Lohuizen | 2b0e7cd | 2019-03-25 08:28:41 +0100 | [diff] [blame] | 1183 | ListLit = "[" [ ElementList [ "," [ "..." [ Expression ] ] ] "]" . |
| 1184 | ElementList = Expression { "," Expression } . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1185 | ``` |
| 1186 | <!--- |
| 1187 | KeyedElement = Element . |
| 1188 | ---> |
| 1189 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1190 | Lists can be thought of as structs: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1191 | |
| 1192 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1193 | List: *null | { |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1194 | Elem: _ |
| 1195 | Tail: List |
| 1196 | } |
| 1197 | ``` |
| 1198 | |
| 1199 | For closed lists, `Tail` is `null` for the last element, for open lists it is |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1200 | `*null | List`, defaulting to the shortest variant. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1201 | For instance, the open list [ 1, 2, ... ] can be represented as: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1202 | ``` |
| 1203 | open: List & { Elem: 1, Tail: { Elem: 2 } } |
| 1204 | ``` |
| 1205 | and the closed version of this list, [ 1, 2 ], as |
| 1206 | ``` |
| 1207 | closed: List & { Elem: 1, Tail: { Elem: 2, Tail: null } } |
| 1208 | ``` |
| 1209 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1210 | Using this representation, the subsumption rule for lists can |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1211 | be derived from those of structs. |
| 1212 | Implementations are not required to implement lists as structs. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1213 | The `Elem` and `Tail` fields are not special and `len` will not work as |
| 1214 | expected in these cases. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1215 | |
| 1216 | |
| 1217 | ## Declarations and Scopes |
| 1218 | |
| 1219 | |
| 1220 | ### Blocks |
| 1221 | |
| 1222 | A _block_ is a possibly empty sequence of declarations. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1223 | The braces of a struct literal `{ ... }` form a block, but there are |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1224 | others as well: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1225 | |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 1226 | - The _universe block_ encompasses all CUE source text. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1227 | - Each [package](#modules-instances-and-packages) has a _package block_ |
| 1228 | containing all CUE source text in that package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1229 | - Each file has a _file block_ containing all CUE source text in that file. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1230 | - Each `for` and `let` clause in a [comprehension](#comprehensions) |
| 1231 | is considered to be its own implicit block. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1232 | |
| 1233 | Blocks nest and influence [scoping]. |
| 1234 | |
| 1235 | |
| 1236 | ### Declarations and scope |
| 1237 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1238 | A _declaration_ binds an identifier to a field, alias, or package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1239 | Every identifier in a program must be declared. |
| 1240 | Other than for fields, |
| 1241 | no identifier may be declared twice within the same block. |
| 1242 | For fields an identifier may be declared more than once within the same block, |
| 1243 | resulting in a field with a value that is the result of unifying the values |
| 1244 | of all fields with the same identifier. |
| 1245 | |
| 1246 | ``` |
| 1247 | TopLevelDecl = Declaration | Emit . |
| 1248 | Emit = Operand . |
| 1249 | ``` |
| 1250 | |
| 1251 | The _scope_ of a declared identifier is the extent of source text in which the |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1252 | identifier denotes the specified field, alias, or package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1253 | |
| 1254 | CUE is lexically scoped using blocks: |
| 1255 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1256 | 1. The scope of a [predeclared identifier](#predeclared-identifiers) is the universe block. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1257 | 1. The scope of an identifier denoting a field or alias |
| 1258 | declared at top level (outside any struct literal) is the file block. |
| 1259 | 1. The scope of the package name of an imported package is the file block of the |
| 1260 | file containing the import declaration. |
| 1261 | 1. The scope of a field or alias identifier declared inside a struct literal |
| 1262 | is the innermost containing block. |
| 1263 | |
| 1264 | An identifier declared in a block may be redeclared in an inner block. |
| 1265 | While the identifier of the inner declaration is in scope, it denotes the entity |
| 1266 | declared by the inner declaration. |
| 1267 | |
| 1268 | The package clause is not a declaration; |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1269 | the package name does not appear in any scope. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1270 | Its purpose is to identify the files belonging to the same package |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 1271 | and to specify the default name for import declarations. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1272 | |
| 1273 | |
| 1274 | ### Predeclared identifiers |
| 1275 | |
| 1276 | ``` |
| 1277 | Functions |
| 1278 | len required close open |
| 1279 | |
| 1280 | Types |
| 1281 | null The null type and value |
| 1282 | bool All boolean values |
| 1283 | int All integral numbers |
| 1284 | float All decimal floating-point numbers |
| 1285 | string Any valid UTF-8 sequence |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1286 | bytes Any vallid byte sequence |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1287 | |
| 1288 | Derived Value |
| 1289 | number int | float |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1290 | uint >=0 |
| 1291 | uint8 >=0 & <=255 |
| 1292 | int8 >=-128 & <=127 |
| 1293 | uint16 >=0 & <=65536 |
| 1294 | int16 >=-32_768 & <=32_767 |
| 1295 | rune >=0 & <=0x10FFFF |
| 1296 | uint32 >=0 & <=4_294_967_296 |
| 1297 | int32 >=-2_147_483_648 & <=2_147_483_647 |
| 1298 | uint64 >=0 & <=18_446_744_073_709_551_615 |
| 1299 | int64 >=-9_223_372_036_854_775_808 & <=9_223_372_036_854_775_807 |
| 1300 | uint128 >=0 & <=340_282_366_920_938_463_463_374_607_431_768_211_455 |
| 1301 | int128 >=-170_141_183_460_469_231_731_687_303_715_884_105_728 & |
| 1302 | <=170_141_183_460_469_231_731_687_303_715_884_105_727 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1303 | ``` |
| 1304 | |
| 1305 | |
| 1306 | ### Exported and manifested identifiers |
| 1307 | |
| 1308 | An identifier of a package may be exported to permit access to it |
| 1309 | from another package. |
| 1310 | An identifier is exported if both: |
| 1311 | the first character of the identifier's name is not a Unicode lower case letter |
| 1312 | (Unicode class "Ll") or the underscore "_"; and |
| 1313 | the identifier is declared in the file block. |
| 1314 | All other identifiers are not exported. |
| 1315 | |
| 1316 | An identifier that starts with the underscore "_" is not |
| 1317 | emitted in any data output. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1318 | Quoted labels that start with an underscore are emitted, however. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1319 | |
| 1320 | ### Uniqueness of identifiers |
| 1321 | |
| 1322 | Given a set of identifiers, an identifier is called unique if it is different |
| 1323 | from every other in the set, after applying normalization following |
| 1324 | Unicode Annex #31. |
| 1325 | Two identifiers are different if they are spelled differently. |
| 1326 | <!-- |
| 1327 | or if they appear in different packages and are not exported. |
| 1328 | ---> |
| 1329 | Otherwise, they are the same. |
| 1330 | |
| 1331 | |
| 1332 | ### Field declarations |
| 1333 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1334 | A field declaration binds a label (the name of the field) to an expression. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1335 | The name for a quoted string used as label is the string it represents. |
| 1336 | Tne name for an identifier used as a label is the identifier itself. |
| 1337 | Quoted strings and identifiers can be used used interchangeably, with the |
| 1338 | exception of identifiers starting with an underscore '_'. |
| 1339 | The latter represent hidden fields and are treated in a different namespace. |
| 1340 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1341 | If an expression may result in a value associated with a default value |
| 1342 | as described in [default values](#default-values), the field binds to this |
| 1343 | value-default pair. |
| 1344 | |
Marcel van Lohuizen | bcf832f | 2019-04-03 22:50:44 +0200 | [diff] [blame] | 1345 | <!-- TODO: disallow creating identifiers starting with __ |
| 1346 | ...and reserve them for builtin values. |
| 1347 | |
| 1348 | The issue is with code generation. As no guarantee can be given that |
| 1349 | a predeclared identifier is not overridden in one of the enclosing scopes, |
| 1350 | code will have to handle detecting such cases and renaming them. |
| 1351 | An alternative is to have the predeclared identifiers be aliases for namesake |
| 1352 | equivalents starting with a double underscore (e.g. string -> __string), |
| 1353 | allowing generated code (normal code would keep using `string`) to refer |
| 1354 | to these directly. |
| 1355 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1356 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1357 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1358 | ### Alias declarations |
| 1359 | |
| 1360 | An alias declaration binds an identifier to the given expression. |
| 1361 | |
| 1362 | Within the scope of the identifier, it serves as an _alias_ for that |
| 1363 | expression. |
| 1364 | The expression is evaluated in the scope as it was declared. |
| 1365 | |
| 1366 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1367 | ## Expressions |
| 1368 | |
| 1369 | An expression specifies the computation of a value by applying operators and |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1370 | built-in functions to operands. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1371 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1372 | Expressions that require concrete values are called _incomplete_ if any of |
| 1373 | their operands are not concrete, but define a value that would be legal for |
| 1374 | that expression. |
| 1375 | Incomplete expressions may be left unevaluated until a concrete value is |
| 1376 | requested at the application level. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1377 | |
| 1378 | ### Operands |
| 1379 | |
| 1380 | Operands denote the elementary values in an expression. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1381 | An operand may be a literal, a (possibly qualified) identifier denoting |
| 1382 | field, alias, or a parenthesized expression. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1383 | |
| 1384 | ``` |
| 1385 | Operand = Literal | OperandName | ListComprehension | "(" Expression ")" . |
| 1386 | Literal = BasicLit | ListLit | StructLit . |
| 1387 | BasicLit = int_lit | float_lit | string_lit | |
| 1388 | null_lit | bool_lit | bottom_lit | top_lit . |
| 1389 | OperandName = identifier | QualifiedIdent. |
| 1390 | ``` |
| 1391 | |
| 1392 | ### Qualified identifiers |
| 1393 | |
| 1394 | A qualified identifier is an identifier qualified with a package name prefix. |
| 1395 | |
| 1396 | ``` |
| 1397 | QualifiedIdent = PackageName "." identifier . |
| 1398 | ``` |
| 1399 | |
| 1400 | A qualified identifier accesses an identifier in a different package, |
| 1401 | which must be [imported]. |
| 1402 | The identifier must be declared in the [package block] of that package. |
| 1403 | |
| 1404 | ``` |
| 1405 | math.Sin // denotes the Sin function in package math |
| 1406 | ``` |
| 1407 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1408 | ### References |
| 1409 | |
| 1410 | An identifier operand refers to a field and is called a reference. |
| 1411 | The value of a reference is a copy of the expression associated with the field |
| 1412 | that it is bound to, |
| 1413 | with any references within that expression bound to the respective copies of |
| 1414 | the fields they were originally bound to. |
| 1415 | Implementations may use a different mechanism to evaluate as long as |
| 1416 | these semantics are maintained. |
| 1417 | |
| 1418 | ``` |
| 1419 | a: { |
| 1420 | place: string |
| 1421 | greeting: "Hello, \(place)!" |
| 1422 | } |
| 1423 | |
| 1424 | b: a & { place: "world" } |
| 1425 | c: a & { place: "you" } |
| 1426 | |
| 1427 | d: b.greeting // "Hello, world!" |
| 1428 | e: c.greeting // "Hello, you!" |
| 1429 | ``` |
| 1430 | |
| 1431 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1432 | |
| 1433 | ### Primary expressions |
| 1434 | |
| 1435 | Primary expressions are the operands for unary and binary expressions. |
| 1436 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1437 | |
| 1438 | ``` |
| 1439 | |
| 1440 | Slice: indices must be complete |
| 1441 | ([0, 1, 2, 3] | [2, 3])[0:2] => [0, 1] | [2, 3] |
| 1442 | |
| 1443 | ([0, 1, 2, 3] | *[2, 3])[0:2] => [0, 1] | [2, 3] |
| 1444 | ([0,1,2,3]|[2,3], [2,3])[0:2] => ([0,1]|[2,3], [2,3]) |
| 1445 | |
| 1446 | Index |
| 1447 | a: (1|2, 1) |
| 1448 | b: ([0,1,2,3]|[2,3], [2,3])[a] => ([0,1,2,3]|[2,3][a], 3) |
| 1449 | |
| 1450 | Binary operation |
| 1451 | A binary is only evaluated if its operands are complete. |
| 1452 | |
| 1453 | Input Maximum allowed evaluation |
| 1454 | a: string string |
| 1455 | b: 2 2 |
| 1456 | c: a * b a * 2 |
| 1457 | |
| 1458 | An error in a struct is if the evaluation of any expression results in |
| 1459 | bottom, where an incomplete expression is not considered bottom. |
| 1460 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 1461 | <!-- TODO(mpvl) |
| 1462 | Conversion | |
| 1463 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1464 | ``` |
| 1465 | PrimaryExpr = |
| 1466 | Operand | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1467 | PrimaryExpr Selector | |
| 1468 | PrimaryExpr Index | |
| 1469 | PrimaryExpr Slice | |
| 1470 | PrimaryExpr Arguments . |
| 1471 | |
| 1472 | Selector = "." identifier . |
| 1473 | Index = "[" Expression "]" . |
| 1474 | Slice = "[" [ Expression ] ":" [ Expression ] "]" |
| 1475 | Argument = Expression . |
| 1476 | Arguments = "(" [ ( Argument { "," Argument } ) [ "..." ] [ "," ] ] ")" . |
| 1477 | ``` |
| 1478 | <!--- |
| 1479 | Argument = Expression | ( identifer ":" Expression ). |
| 1480 | ---> |
| 1481 | |
| 1482 | ``` |
| 1483 | x |
| 1484 | 2 |
| 1485 | (s + ".txt") |
| 1486 | f(3.1415, true) |
| 1487 | m["foo"] |
| 1488 | s[i : j + 1] |
| 1489 | obj.color |
| 1490 | f.p[i].x |
| 1491 | ``` |
| 1492 | |
| 1493 | |
| 1494 | ### Selectors |
| 1495 | |
| 1496 | For a [primary expression] `x` that is not a [package name], |
| 1497 | the selector expression |
| 1498 | |
| 1499 | ``` |
| 1500 | x.f |
| 1501 | ``` |
| 1502 | |
| 1503 | denotes the field `f` of the value `x`. |
| 1504 | The identifier `f` is called the field selector. |
| 1505 | The type of the selector expression is the type of `f`. |
| 1506 | If `x` is a package name, see the section on [qualified identifiers]. |
| 1507 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1508 | <!-- |
| 1509 | TODO: consider allowing this and also for selectors. It needs to be considered |
| 1510 | how defaults are corried forward in cases like: |
| 1511 | |
| 1512 | x: { a: string | *"foo" } | *{ a: int | *4 } |
| 1513 | y: x.a & string |
| 1514 | |
| 1515 | What is y in this case? |
| 1516 | (x.a & string, _|_) |
| 1517 | (string|"foo", _|_) |
| 1518 | (string|"foo", "foo) |
| 1519 | If the latter, then why? |
| 1520 | |
| 1521 | For a disjunction of the form `x1 | ... | xn`, |
| 1522 | the selector is applied to each element `x1.f | ... | xn.f`. |
| 1523 | --> |
| 1524 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1525 | Otherwise, if `x` is not a struct, or if `f` does not exist in `x`, |
| 1526 | the result of the expression is bottom (an error). |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1527 | In the latter case the expression is incomplete. |
| 1528 | The operand of a selector may be associated with a default. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1529 | |
| 1530 | ``` |
| 1531 | T: { |
| 1532 | x: int |
| 1533 | y: 3 |
| 1534 | } |
| 1535 | |
| 1536 | a: T.x // int |
| 1537 | b: T.y // 3 |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1538 | c: T.z // _|_ // field 'z' not found in T |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1539 | |
| 1540 | e: {a: 1|*2} | *{a: 3|*4} |
| 1541 | f: e.a // 4 (default value) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1542 | ``` |
| 1543 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1544 | <!-- |
| 1545 | ``` |
| 1546 | (v, d).f => (v.f, d.f) |
| 1547 | |
| 1548 | e: {a: 1|*2} | *{a: 3|*4} |
| 1549 | f: e.a // 4 after selecting default from (({a: 1|*2} | {a: 3|*4}).a, 4) |
| 1550 | |
| 1551 | ``` |
| 1552 | --> |
| 1553 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1554 | |
| 1555 | ### Index expressions |
| 1556 | |
| 1557 | A primary expression of the form |
| 1558 | |
| 1559 | ``` |
| 1560 | a[x] |
| 1561 | ``` |
| 1562 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1563 | denotes the element of the list, string, bytes, or struct `a` indexed by `x`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1564 | The value `x` is called the index or field name, respectively. |
| 1565 | The following rules apply: |
| 1566 | |
| 1567 | If `a` is not a struct: |
| 1568 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1569 | - `a` is a concrete string or bytes type or a list (which need not be complete) |
| 1570 | - the index `x` unified with `int` must be concrete. |
| 1571 | - the index `x` is in range if `0 <= x < len(a)`, where only the |
| 1572 | explicitly defined values of an open-ended list are considered, |
| 1573 | otherwise it is out of range |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1574 | |
| 1575 | The result of `a[x]` is |
| 1576 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1577 | for `a` of list or bytes type: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1578 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1579 | - the list or byte element at index `x`, if `x` is within range |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1580 | - bottom (an error), otherwise |
| 1581 | |
| 1582 | for `a` of string type: |
| 1583 | |
| 1584 | - the grapheme cluster at the `x`th byte (type string), if `x` is within range |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1585 | where `x` may match any byte of the grapheme cluster |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1586 | - bottom (an error), otherwise |
| 1587 | |
| 1588 | for `a` of struct type: |
| 1589 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1590 | - the index `x` unified with `string` must be concrete. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1591 | - the value of the field named `x` of struct `a`, if this field exists |
| 1592 | - bottom (an error), otherwise |
| 1593 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1594 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1595 | ``` |
| 1596 | [ 1, 2 ][1] // 2 |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1597 | [ 1, 2 ][2] // _|_ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1598 | [ 1, 2, ...][2] // _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1599 | "He\u0300?"[0] // "H" |
| 1600 | "He\u0300?"[1] // "e\u0300" |
| 1601 | "He\u0300?"[2] // "e\u0300" |
| 1602 | "He\u0300?"[3] // "e\u0300" |
| 1603 | "He\u0300?"[4] // "?" |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1604 | "He\u0300?"[5] // _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1605 | ``` |
| 1606 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1607 | Both the operand and index value may be a value-default pair. |
| 1608 | ``` |
| 1609 | va[vi] => va[vi] |
| 1610 | va[(vi, di)] => (va[vi], va[di]) |
| 1611 | (va, da)[vi] => (va[vi], da[vi]) |
| 1612 | (va, da)[(vi, di)] => (va[vi], da[di]) |
| 1613 | ``` |
| 1614 | |
| 1615 | ``` |
| 1616 | Fields Result |
| 1617 | x: [1, 2] | *[3, 4] ([1,2]|[3,4], [3,4]) |
| 1618 | i: int | *1 (int, 1) |
| 1619 | |
| 1620 | v: x[i] (x[i], 4) |
| 1621 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1622 | |
| 1623 | ### Slice expressions |
| 1624 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1625 | Slice expressions construct a substring or slice from a string, bytes, |
| 1626 | or list value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1627 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1628 | For strings, bytes or lists, the primary expression |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1629 | ``` |
| 1630 | a[low : high] |
| 1631 | ``` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1632 | constructs a substring or slice. The indices `low` and `high` must be |
| 1633 | concrete integers and select |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1634 | which elements of operand `a` appear in the result. |
| 1635 | The result has indices starting at 0 and length equal to `high` - `low`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1636 | After slicing the list `a` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1637 | <!-- TODO(jba): how does slicing open lists work? --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1638 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1639 | <!-- TODO: consider this. |
| 1640 | For `a` is a disjunction of the form `a1 | ... | an`, then the result is |
| 1641 | `a1[low:high] | ... | an[low:high]` observing the above rules. |
| 1642 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1643 | ``` |
| 1644 | a := [1, 2, 3, 4, 5] |
| 1645 | s := a[1:4] |
| 1646 | ``` |
| 1647 | the list s has length 3 and elements |
| 1648 | ``` |
| 1649 | s[0] == 2 |
| 1650 | s[1] == 3 |
| 1651 | s[2] == 4 |
| 1652 | ``` |
| 1653 | For convenience, any of the indices may be omitted. |
| 1654 | A missing `low` index defaults to zero; a missing `high` index defaults |
| 1655 | to the length of the sliced operand: |
| 1656 | ``` |
| 1657 | a[2:] // same as a[2 : len(a)] |
| 1658 | a[:3] // same as a[0 : 3] |
| 1659 | a[:] // same as a[0 : len(a)] |
| 1660 | ``` |
| 1661 | |
| 1662 | Indices are in range if `0 <= low <= high <= len(a)`, |
| 1663 | otherwise they are out of range. |
| 1664 | For strings, the indices selects the start of the extended grapheme cluster |
| 1665 | at byte position indicated by the index. |
| 1666 | If any of the slice values is out of range or if `low > high`, the result of |
| 1667 | a slice is bottom (error). |
| 1668 | |
| 1669 | ``` |
| 1670 | "He\u0300?"[:2] // "He\u0300" |
| 1671 | "He\u0300?"[1:2] // "e\u0300" |
| 1672 | "He\u0300?"[4:5] // "e\u0300?" |
| 1673 | ``` |
| 1674 | |
| 1675 | |
| 1676 | The result of a successful slice operation is a value of the same type |
| 1677 | as the operand. |
| 1678 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1679 | Both the slice operand and the slice indices may be associated with a default. |
| 1680 | |
| 1681 | <!-- |
| 1682 | ``` |
| 1683 | va[vs:ve] => va[vs:ve] |
| 1684 | va[vs:(ve, de)] => (va[vs:ve], va[vs:de]) |
| 1685 | va[(vs, ds):ve] => (va[vs:ve], va[ds:ve]) |
| 1686 | va[(vs, ds):(ve, de)] => (va[vs:ve], va[ds:de]) |
| 1687 | (va, da)[vs:ve] => (va[vs:ve], da[vs:ve]) |
| 1688 | (va, da)[vs:(ve, de)] => (va[vs:ve], da[vs:de]) |
| 1689 | (va, da)[(vs, ds):ve] => (va[vs:ve], da[ds:ve]) |
| 1690 | (va, da)[(vs, ds):(ve, de)] => (va[vs:ve], da[ds:de]) |
| 1691 | ``` |
| 1692 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1693 | |
| 1694 | ### Operators |
| 1695 | |
| 1696 | Operators combine operands into expressions. |
| 1697 | |
| 1698 | ``` |
| 1699 | Expression = UnaryExpr | Expression binary_op Expression . |
| 1700 | UnaryExpr = PrimaryExpr | unary_op UnaryExpr . |
| 1701 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1702 | binary_op = "|" | "&" | "||" | "&&" | "==" | rel_op | add_op | mul_op . |
Marcel van Lohuizen | 2b0e7cd | 2019-03-25 08:28:41 +0100 | [diff] [blame] | 1703 | rel_op = "!=" | "<" | "<=" | ">" | ">=" | "=~" | "!~" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1704 | add_op = "+" | "-" . |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1705 | mul_op = "*" | "/" | "%" | "div" | "mod" | "quo" | "rem" . |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 1706 | unary_op = "+" | "-" | "!" | "*" | rel_op . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1707 | ``` |
| 1708 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1709 | Comparisons are discussed [elsewhere](#Comparison-operators). |
Marcel van Lohuizen | 7da140a | 2019-02-01 09:35:00 +0100 | [diff] [blame] | 1710 | For any binary operators, the operand types must unify. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1711 | <!-- TODO: durations |
| 1712 | unless the operation involves durations. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1713 | |
| 1714 | Except for duration operations, if one operand is an untyped [literal] and the |
| 1715 | other operand is not, the constant is [converted] to the type of the other |
| 1716 | operand. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1717 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1718 | |
Marcel van Lohuizen | fe4abac | 2019-04-06 17:19:03 +0200 | [diff] [blame] | 1719 | Operands of unary and binary expressions may be associated with a default using |
| 1720 | the following |
| 1721 | <!-- |
| 1722 | ``` |
| 1723 | O1: op (v1, d1) => (op v1, op d1) |
| 1724 | |
| 1725 | O2: (v1, d1) op (v2, d2) => (v1 op v2, d1 op d2) |
| 1726 | and because v => (v, v) |
| 1727 | O3: v1 op (v2, d2) => (v1 op v2, v1 op d2) |
| 1728 | O4: (v1, d1) op v2 => (v1 op v2, d1 op v2) |
| 1729 | ``` |
| 1730 | --> |
| 1731 | |
| 1732 | ``` |
| 1733 | Field Resulting Value-Default pair |
| 1734 | a: *1|2 (1|2, 1) |
| 1735 | b: -a (-a, -1) |
| 1736 | |
| 1737 | c: a + 2 (a+2, 3) |
| 1738 | d: a + a (a+a, 2) |
| 1739 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1740 | |
| 1741 | #### Operator precedence |
| 1742 | |
| 1743 | Unary operators have the highest precedence. |
| 1744 | |
| 1745 | There are eight precedence levels for binary operators. |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1746 | Multiplication operators binds strongest, followed by |
| 1747 | addition operators, comparison operators, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1748 | `&&` (logical AND), `||` (logical OR), `&` (unification), |
| 1749 | and finally `|` (disjunction): |
| 1750 | |
| 1751 | ``` |
| 1752 | Precedence Operator |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1753 | 7 * / % div mod quo rem |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1754 | 6 + - |
Marcel van Lohuizen | 2b0e7cd | 2019-03-25 08:28:41 +0100 | [diff] [blame] | 1755 | 5 == != < <= > >= =~ !~ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1756 | 4 && |
| 1757 | 3 || |
| 1758 | 2 & |
| 1759 | 1 | |
| 1760 | ``` |
| 1761 | |
| 1762 | Binary operators of the same precedence associate from left to right. |
| 1763 | For instance, `x / y * z` is the same as `(x / y) * z`. |
| 1764 | |
| 1765 | ``` |
| 1766 | +x |
| 1767 | 23 + 3*x[i] |
| 1768 | x <= f() |
| 1769 | f() || g() |
| 1770 | x == y+1 && y == z-1 |
| 1771 | 2 | int |
| 1772 | { a: 1 } & { b: 2 } |
| 1773 | ``` |
| 1774 | |
| 1775 | #### Arithmetic operators |
| 1776 | |
| 1777 | Arithmetic operators apply to numeric values and yield a result of the same type |
| 1778 | as the first operand. The three of the four standard arithmetic operators |
| 1779 | `(+, -, *)` apply to integer and decimal floating-point types; |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1780 | `+` and `*` also apply to lists and strings. |
| 1781 | `/` and `%` only apply to decimal floating-point types and |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1782 | `div`, `mod`, `quo`, and `rem` only apply to integer types. |
| 1783 | |
| 1784 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1785 | + sum integers, floats, lists, strings, bytes |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1786 | - difference integers, floats |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1787 | * product integers, floats, lists, strings, bytes |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1788 | / quotient floats |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1789 | % remainder floats |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1790 | div division integers |
| 1791 | mod modulo integers |
| 1792 | quo quotient integers |
| 1793 | rem remainder integers |
| 1794 | ``` |
| 1795 | |
| 1796 | #### Integer operators |
| 1797 | |
| 1798 | For two integer values `x` and `y`, |
| 1799 | the integer quotient `q = x div y` and remainder `r = x mod y ` |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 1800 | implement Euclidean division and |
| 1801 | satisfy the following relationship: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1802 | |
| 1803 | ``` |
| 1804 | r = x - y*q with 0 <= r < |y| |
| 1805 | ``` |
| 1806 | where `|y|` denotes the absolute value of `y`. |
| 1807 | |
| 1808 | ``` |
| 1809 | x y x div y x mod y |
| 1810 | 5 3 1 2 |
| 1811 | -5 3 -2 1 |
| 1812 | 5 -3 -1 2 |
| 1813 | -5 -3 2 1 |
| 1814 | ``` |
| 1815 | |
| 1816 | For two integer values `x` and `y`, |
| 1817 | the integer quotient `q = x quo y` and remainder `r = x rem y ` |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 1818 | implement truncated division and |
| 1819 | satisfy the following relationship: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1820 | |
| 1821 | ``` |
| 1822 | x = q*y + r and |r| < |y| |
| 1823 | ``` |
| 1824 | |
| 1825 | with `x quo y` truncated towards zero. |
| 1826 | |
| 1827 | ``` |
| 1828 | x y x quo y x rem y |
| 1829 | 5 3 1 2 |
| 1830 | -5 3 -1 -2 |
| 1831 | 5 -3 -1 2 |
| 1832 | -5 -3 1 -2 |
| 1833 | ``` |
| 1834 | |
| 1835 | A zero divisor in either case results in bottom (an error). |
| 1836 | |
| 1837 | For integer operands, the unary operators `+` and `-` are defined as follows: |
| 1838 | |
| 1839 | ``` |
| 1840 | +x is 0 + x |
| 1841 | -x negation is 0 - x |
| 1842 | ``` |
| 1843 | |
| 1844 | |
| 1845 | #### Decimal floating-point operators |
| 1846 | |
| 1847 | For decimal floating-point numbers, `+x` is the same as `x`, |
| 1848 | while -x is the negation of x. |
| 1849 | The result of a floating-point division by zero is bottom (an error). |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1850 | <!-- TODO: consider making it +/- Inf --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1851 | |
| 1852 | An implementation may combine multiple floating-point operations into a single |
| 1853 | fused operation, possibly across statements, and produce a result that differs |
| 1854 | from the value obtained by executing and rounding the instructions individually. |
| 1855 | |
| 1856 | |
| 1857 | #### List operators |
| 1858 | |
| 1859 | Lists can be concatenated using the `+` operator. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1860 | For lists `a` and `b`, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1861 | ``` |
| 1862 | a + b |
| 1863 | ``` |
| 1864 | will produce an open list if `b` is open. |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1865 | If list `a` is open, its default value, the shortest variant, is selected. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1866 | |
| 1867 | ``` |
| 1868 | [ 1, 2 ] + [ 3, 4 ] // [ 1, 2, 3, 4 ] |
| 1869 | [ 1, 2, ... ] + [ 3, 4 ] // [ 1, 2, 3, 4 ] |
| 1870 | [ 1, 2 ] + [ 3, 4, ... ] // [ 1, 2, 3, 4, ... ] |
Marcel van Lohuizen | 09d814d | 2019-02-22 19:14:33 +0100 | [diff] [blame] | 1871 | [ 1, 2, ... ] + [ 3, 4, ... ] // [ 1, 2, 3, 4, ... ] |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1872 | ``` |
| 1873 | |
Jonathan Amsterdam | 0500c31 | 2019-02-16 18:04:09 -0500 | [diff] [blame] | 1874 | Lists can be multiplied with a non-negative`int` using the `*` operator |
Marcel van Lohuizen | 13e36bd | 2019-02-01 09:59:18 +0100 | [diff] [blame] | 1875 | to create a repeated the list by the indicated number. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1876 | ``` |
| 1877 | 3*[1,2] // [1, 2, 1, 2, 1, 2] |
Jonathan Amsterdam | 0500c31 | 2019-02-16 18:04:09 -0500 | [diff] [blame] | 1878 | 3*[1, 2, ...] // [1, 2, 1, 2, 1 ,2, ...] |
Marcel van Lohuizen | 13e36bd | 2019-02-01 09:59:18 +0100 | [diff] [blame] | 1879 | [byte]*4 // [byte, byte, byte, byte] |
Jonathan Amsterdam | 0500c31 | 2019-02-16 18:04:09 -0500 | [diff] [blame] | 1880 | 0*[1,2] // [] |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1881 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1882 | |
| 1883 | <!-- TODO(mpvl): should we allow multiplication with a range? |
| 1884 | If so, how does one specify a list with a range of possible lengths? |
| 1885 | |
| 1886 | Suggestion from jba: |
| 1887 | Multiplication should distribute over disjunction, |
| 1888 | so int(1)..int(3) * [x] = [x] | [x, x] | [x, x, x]. |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1889 | The hard part is figuring out what (>=1 & <=3) * [x] means, |
| 1890 | since >=1 & <=3 includes many floats. |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1891 | (mpvl: could constrain arguments to parameter types, but needs to be |
| 1892 | done consistently.) |
| 1893 | --> |
| 1894 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1895 | |
| 1896 | #### String operators |
| 1897 | |
| 1898 | Strings can be concatenated using the `+` operator: |
| 1899 | ``` |
| 1900 | s := "hi " + name + " and good bye" |
| 1901 | ``` |
| 1902 | String addition creates a new string by concatenating the operands. |
| 1903 | |
| 1904 | A string can be repeated by multiplying it: |
| 1905 | |
| 1906 | ``` |
| 1907 | s: "etc. "*3 // "etc. etc. etc. " |
| 1908 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1909 | <!-- jba: Do these work for byte sequences? If not, why not? --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1910 | |
| 1911 | ##### Comparison operators |
| 1912 | |
| 1913 | Comparison operators compare two operands and yield an untyped boolean value. |
| 1914 | |
| 1915 | ``` |
| 1916 | == equal |
| 1917 | != not equal |
| 1918 | < less |
| 1919 | <= less or equal |
| 1920 | > greater |
| 1921 | >= greater or equal |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 1922 | =~ matches regular expression |
| 1923 | !~ does not match regular expression |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1924 | ``` |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 1925 | <!-- regular expression operator inspired by Bash, Perl, and Ruby. --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1926 | |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 1927 | In any comparison, the types of the two operands must unify or one of the |
| 1928 | operands must be null. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1929 | |
| 1930 | The equality operators `==` and `!=` apply to operands that are comparable. |
| 1931 | The ordering operators `<`, `<=`, `>`, and `>=` apply to operands that are ordered. |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 1932 | The matching operators `=~` and `!~` apply to a string and regular |
| 1933 | expression operand. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1934 | These terms and the result of the comparisons are defined as follows: |
| 1935 | |
Marcel van Lohuizen | 855243e | 2019-02-07 18:00:55 +0100 | [diff] [blame] | 1936 | - Null is comparable with itself and any other type. |
| 1937 | Two null values are always equal, null is unequal with anything else. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1938 | - Boolean values are comparable. |
| 1939 | Two boolean values are equal if they are either both true or both false. |
| 1940 | - Integer values are comparable and ordered, in the usual way. |
| 1941 | - Floating-point values are comparable and ordered, as per the definitions |
| 1942 | for binary coded decimals in the IEEE-754-2008 standard. |
Marcel van Lohuizen | 4a36099 | 2019-05-11 18:18:31 +0200 | [diff] [blame] | 1943 | - Floating point numbers may be compared with integers. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1944 | - String values are comparable and ordered, lexically byte-wise after |
| 1945 | normalization to Unicode normal form NFC. |
| 1946 | - Struct are not comparable. |
Marcel van Lohuizen | 855243e | 2019-02-07 18:00:55 +0100 | [diff] [blame] | 1947 | - Lists are not comparable. |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 1948 | - The regular expression syntax is the one accepted by RE2, |
| 1949 | described in https://github.com/google/re2/wiki/Syntax, |
| 1950 | except for `\C`. |
| 1951 | - `s =~ r` is true if `s` matches the regular expression `r`. |
| 1952 | - `s !~ r` is true if `s` does not match regular expression `r`. |
| 1953 | <!-- TODO: Implementations should adopt an algorithm that runs in linear time? --> |
Marcel van Lohuizen | 88a8a5f | 2019-02-20 01:26:22 +0100 | [diff] [blame] | 1954 | <!-- Consider implementing Level 2 of Unicode regular expression. --> |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 1955 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1956 | ``` |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 1957 | 3 < 4 // true |
Marcel van Lohuizen | 4a36099 | 2019-05-11 18:18:31 +0200 | [diff] [blame] | 1958 | 3 < 4.0 // true |
Marcel van Lohuizen | 0a0a3ac | 2019-02-10 16:48:53 +0100 | [diff] [blame] | 1959 | null == 2 // false |
| 1960 | null != {} // true |
| 1961 | {} == {} // _|_: structs are not comparable against structs |
| 1962 | |
| 1963 | "Wild cats" =~ "cat" // true |
| 1964 | "Wild cats" !~ "dog" // true |
| 1965 | |
| 1966 | "foo" =~ "^[a-z]{3}$" // true |
| 1967 | "foo" =~ "^[a-z]{4}$" // false |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1968 | ``` |
| 1969 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1970 | <!-- jba |
| 1971 | I think I know what `3 < a` should mean if |
| 1972 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1973 | a: >=1 & <=5 |
| 1974 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1975 | It should be a constraint on `a` that can be evaluated once `a`'s value is known more precisely. |
| 1976 | |
Marcel van Lohuizen | 62b8727 | 2019-02-01 10:07:49 +0100 | [diff] [blame] | 1977 | But what does `3 < (>=1 & <=5)` mean? We'll never get more information, so it must have a definite value. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1978 | --> |
| 1979 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1980 | #### Logical operators |
| 1981 | |
| 1982 | Logical operators apply to boolean values and yield a result of the same type |
| 1983 | as the operands. The right operand is evaluated conditionally. |
| 1984 | |
| 1985 | ``` |
| 1986 | && conditional AND p && q is "if p then q else false" |
| 1987 | || conditional OR p || q is "if p then true else q" |
| 1988 | ! NOT !p is "not p" |
| 1989 | ``` |
| 1990 | |
| 1991 | |
| 1992 | <!-- |
| 1993 | ### TODO TODO TODO |
| 1994 | |
| 1995 | 3.14 / 0.0 // illegal: division by zero |
| 1996 | Illegal conversions always apply to CUE. |
| 1997 | |
| 1998 | Implementation restriction: A compiler may use rounding while computing untyped floating-point or complex constant expressions; see the implementation restriction in the section on constants. This rounding may cause a floating-point constant expression to be invalid in an integer context, even if it would be integral when calculated using infinite precision, and vice versa. |
| 1999 | --> |
| 2000 | |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2001 | <!--- TODO(mpvl): conversions |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2002 | ### Conversions |
| 2003 | Conversions are expressions of the form `T(x)` where `T` and `x` are |
| 2004 | expressions. |
| 2005 | The result is always an instance of `T`. |
| 2006 | |
| 2007 | ``` |
| 2008 | Conversion = Expression "(" Expression [ "," ] ")" . |
| 2009 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2010 | ---> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2011 | <!--- |
| 2012 | |
| 2013 | A literal value `x` can be converted to type T if `x` is representable by a |
| 2014 | value of `T`. |
| 2015 | |
| 2016 | As a special case, an integer literal `x` can be converted to a string type |
| 2017 | using the same rule as for non-constant x. |
| 2018 | |
| 2019 | Converting a literal yields a typed value as result. |
| 2020 | |
| 2021 | ``` |
| 2022 | uint(iota) // iota value of type uint |
| 2023 | float32(2.718281828) // 2.718281828 of type float32 |
| 2024 | complex128(1) // 1.0 + 0.0i of type complex128 |
| 2025 | float32(0.49999999) // 0.5 of type float32 |
| 2026 | float64(-1e-1000) // 0.0 of type float64 |
| 2027 | string('x') // "x" of type string |
| 2028 | string(0x266c) // "♬" of type string |
| 2029 | MyString("foo" + "bar") // "foobar" of type MyString |
| 2030 | string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant |
| 2031 | (*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type |
| 2032 | int(1.2) // illegal: 1.2 cannot be represented as an int |
| 2033 | string(65.0) // illegal: 65.0 is not an integer constant |
| 2034 | ``` |
| 2035 | ---> |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2036 | <!--- |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2037 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2038 | A conversion is always allowed if `x` is an instance of `T`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2039 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2040 | If `T` and `x` of different underlying type, a conversion is allowed if |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2041 | `x` can be converted to a value `x'` of `T`'s type, and |
| 2042 | `x'` is an instance of `T`. |
| 2043 | A value `x` can be converted to the type of `T` in any of these cases: |
| 2044 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2045 | - `x` is a struct and is subsumed by `T`. |
| 2046 | - `x` and `T` are both integer or floating points. |
| 2047 | - `x` is an integer or a byte sequence and `T` is a string. |
| 2048 | - `x` is a string and `T` is a byte sequence. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2049 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2050 | Specific rules apply to conversions between numeric types, structs, |
| 2051 | or to and from a string type. These conversions may change the representation |
| 2052 | of `x`. |
| 2053 | All other conversions only change the type but not the representation of x. |
| 2054 | |
| 2055 | |
| 2056 | #### Conversions between numeric ranges |
| 2057 | For the conversion of numeric values, the following rules apply: |
| 2058 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2059 | 1. Any integer value can be converted into any other integer value |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2060 | provided that it is within range. |
| 2061 | 2. When converting a decimal floating-point number to an integer, the fraction |
| 2062 | is discarded (truncation towards zero). TODO: or disallow truncating? |
| 2063 | |
| 2064 | ``` |
| 2065 | a: uint16(int(1000)) // uint16(1000) |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 2066 | b: uint8(1000) // _|_ // overflow |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2067 | c: int(2.5) // 2 TODO: TBD |
| 2068 | ``` |
| 2069 | |
| 2070 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2071 | #### Conversions to and from a string type |
| 2072 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2073 | Converting a list of bytes to a string type yields a string whose successive |
| 2074 | bytes are the elements of the slice. |
| 2075 | Invalid UTF-8 is converted to `"\uFFFD"`. |
| 2076 | |
| 2077 | ``` |
| 2078 | string('hell\xc3\xb8') // "hellø" |
| 2079 | string(bytes([0x20])) // " " |
| 2080 | ``` |
| 2081 | |
| 2082 | As string value is always convertible to a list of bytes. |
| 2083 | |
| 2084 | ``` |
| 2085 | bytes("hellø") // 'hell\xc3\xb8' |
| 2086 | bytes("") // '' |
| 2087 | ``` |
| 2088 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2089 | #### Conversions between list types |
| 2090 | |
| 2091 | Conversions between list types are possible only if `T` strictly subsumes `x` |
| 2092 | and the result will be the unification of `T` and `x`. |
| 2093 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2094 | If we introduce named types this would be different from IP & [10, ...] |
| 2095 | |
| 2096 | Consider removing this until it has a different meaning. |
| 2097 | |
| 2098 | ``` |
| 2099 | IP: 4*[byte] |
| 2100 | Private10: IP([10, ...]) // [10, byte, byte, byte] |
| 2101 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2102 | |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 2103 | #### Conversions between struct types |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2104 | |
| 2105 | A conversion from `x` to `T` |
| 2106 | is applied using the following rules: |
| 2107 | |
| 2108 | 1. `x` must be an instance of `T`, |
| 2109 | 2. all fields defined for `x` that are not defined for `T` are removed from |
| 2110 | the result of the conversion, recursively. |
| 2111 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2112 | <!-- jba: I don't think you say anywhere that the matching fields are unified. |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2113 | mpvl: they are not, x must be an instance of T, in which case x == T&x, |
| 2114 | so unification would be unnecessary. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2115 | --> |
Marcel van Lohuizen | a3f0097 | 2019-02-01 11:10:39 +0100 | [diff] [blame] | 2116 | <!-- |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2117 | ``` |
| 2118 | T: { |
| 2119 | a: { b: 1..10 } |
| 2120 | } |
| 2121 | |
| 2122 | x1: { |
| 2123 | a: { b: 8, c: 10 } |
| 2124 | d: 9 |
| 2125 | } |
| 2126 | |
| 2127 | c1: T(x1) // { a: { b: 8 } } |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 2128 | c2: T({}) // _|_ // missing field 'a' in '{}' |
| 2129 | c3: T({ a: {b: 0} }) // _|_ // field a.b does not unify (0 & 1..10) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2130 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 2131 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2132 | |
| 2133 | ### Calls |
| 2134 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2135 | Calls can be made to core library functions, called builtins. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2136 | Given an expression `f` of function type F, |
| 2137 | ``` |
| 2138 | f(a1, a2, … an) |
| 2139 | ``` |
| 2140 | calls `f` with arguments a1, a2, … an. Arguments must be expressions |
| 2141 | of which the values are an instance of the parameter types of `F` |
| 2142 | and are evaluated before the function is called. |
| 2143 | |
| 2144 | ``` |
| 2145 | a: math.Atan2(x, y) |
| 2146 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2147 | |
| 2148 | In a function call, the function value and arguments are evaluated in the usual |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 2149 | order. |
| 2150 | After they are evaluated, the parameters of the call are passed by value |
| 2151 | to the function and the called function begins execution. |
| 2152 | The return parameters |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2153 | of the function are passed by value back to the calling function when the |
| 2154 | function returns. |
| 2155 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2156 | |
| 2157 | ### Comprehensions |
| 2158 | |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 2159 | Lists and fields can be constructed using comprehensions. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2160 | |
| 2161 | Each define a clause sequence that consists of a sequence of `for`, `if`, and |
| 2162 | `let` clauses, nesting from left to right. |
| 2163 | The `for` and `let` clauses each define a new scope in which new values are |
| 2164 | bound to be available for the next clause. |
| 2165 | |
| 2166 | The `for` clause binds the defined identifiers, on each iteration, to the next |
| 2167 | value of some iterable value in a new scope. |
| 2168 | A `for` clause may bind one or two identifiers. |
| 2169 | If there is one identifier, it binds it to the value, for instance |
| 2170 | a list element, a struct field value or a range element. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2171 | If there are two identifiers, the first value will be the key or index, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2172 | if available, and the second will be the value. |
| 2173 | |
| 2174 | An `if` clause, or guard, specifies an expression that terminates the current |
| 2175 | iteration if it evaluates to false. |
| 2176 | |
| 2177 | The `let` clause binds the result of an expression to the defined identifier |
| 2178 | in a new scope. |
| 2179 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2180 | A current iteration is said to complete if the innermost block of the clause |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2181 | sequence is reached. |
| 2182 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2183 | _List comprehensions_ specify a single expression that is evaluated and included |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2184 | in the list for each completed iteration. |
| 2185 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2186 | _Field comprehensions_ follow a `Field` with a clause sequence, where the |
| 2187 | label and value of the field are evaluated for each iteration. |
Marcel van Lohuizen | 369e423 | 2019-02-15 10:59:29 +0400 | [diff] [blame] | 2188 | The label must be an identifier or simple_string_lit, where the |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 2189 | later may be a string interpolation that refers to the identifiers defined |
| 2190 | in the clauses. |
| 2191 | Values of iterations that map to the same label unify into a single field. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2192 | |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 2193 | <!-- |
| 2194 | TODO: consider allowing multiple labels for comprehensions |
| 2195 | (current implementation). Generally it is better to define comprehensions |
| 2196 | in the current scope, though, as it may prevent surprises given the |
| 2197 | restrictions on comprehensions. |
| 2198 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2199 | ``` |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 2200 | ComprehensionDecl = Label ":" Expression [ "<-" ] Clauses . |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 2201 | ListComprehension = "[" Expression [ "<-" ] Clauses "]" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2202 | |
| 2203 | Clauses = Clause { Clause } . |
| 2204 | Clause = ForClause | GuardClause | LetClause . |
| 2205 | ForClause = "for" identifier [ ", " identifier] "in" Expression . |
| 2206 | GuardClause = "if" Expression . |
| 2207 | LetClause = "let" identifier "=" Expression . |
| 2208 | ``` |
| 2209 | |
| 2210 | ``` |
| 2211 | a: [1, 2, 3, 4] |
| 2212 | b: [ x+1 for x in a if x > 1] // [3, 4, 5] |
| 2213 | |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 2214 | c: { "\(x)": x + y for x in a if x < 4 let y = 1 } |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2215 | d: { "1": 2, "2": 3, "3": 4 } |
| 2216 | ``` |
| 2217 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2218 | |
| 2219 | ### String interpolation |
| 2220 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2221 | String interpolation allows constructing strings by replacing placeholder |
| 2222 | expressions with their string representation. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2223 | String interpolation may be used in single- and double-quoted strings, as well |
| 2224 | as their multiline equivalent. |
| 2225 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2226 | A placeholder consists of "\(" followed by an expression and a ")". The |
| 2227 | expression is evaluated within the scope within which the string is defined. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2228 | |
| 2229 | ``` |
| 2230 | a: "World" |
| 2231 | b: "Hello \( a )!" // Hello World! |
| 2232 | ``` |
| 2233 | |
| 2234 | |
| 2235 | ## Builtin Functions |
| 2236 | |
| 2237 | Built-in functions are predeclared. They are called like any other function. |
| 2238 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2239 | |
| 2240 | ### `len` |
| 2241 | |
| 2242 | The built-in function `len` takes arguments of various types and return |
| 2243 | a result of type int. |
| 2244 | |
| 2245 | ``` |
| 2246 | Argument type Result |
| 2247 | |
| 2248 | string string length in bytes |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2249 | bytes length of byte sequence |
| 2250 | list list length, smallest length for an open list |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2251 | struct number of distinct fields |
| 2252 | ``` |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 2253 | |
| 2254 | ``` |
| 2255 | Expression Result |
| 2256 | len("Hellø") 6 |
| 2257 | len([1, 2, 3]) 3 |
| 2258 | len([1, 2, ...]) 2 |
| 2259 | len({a:1, b:2}) 2 |
| 2260 | ``` |
| 2261 | |
Marcel van Lohuizen | a460fe8 | 2019-04-26 10:20:51 +0200 | [diff] [blame] | 2262 | ### `and` |
| 2263 | |
| 2264 | The built-in function `and` takes a list and returns the result of applying |
| 2265 | the `&` operator to all elements in the list. |
| 2266 | It returns top for the empty list. |
| 2267 | |
| 2268 | Expression: Result |
| 2269 | and([a, b]) a & b |
| 2270 | and([a]) a |
| 2271 | and([]) _ |
| 2272 | |
| 2273 | ### `or` |
| 2274 | |
| 2275 | The built-in function `or` takes a list and returns the result of applying |
| 2276 | the `|` operator to all elements in the list. |
| 2277 | It returns bottom for the empty list. |
| 2278 | |
| 2279 | Expression: Result |
| 2280 | and([a, b]) a | b |
| 2281 | and([a]) a |
| 2282 | and([]) _|_ |
| 2283 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2284 | |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2285 | ## Cycles |
| 2286 | |
| 2287 | Implementations are required to interpret or reject cycles encountered |
| 2288 | during evaluation according to the rules in this section. |
| 2289 | |
| 2290 | |
| 2291 | ### Reference cycles |
| 2292 | |
| 2293 | A _reference cycle_ occurs if a field references itself, either directly or |
| 2294 | indirectly. |
| 2295 | |
| 2296 | ``` |
| 2297 | // x references itself |
| 2298 | x: x |
| 2299 | |
| 2300 | // indirect cycles |
| 2301 | b: c |
| 2302 | c: d |
| 2303 | d: b |
| 2304 | ``` |
| 2305 | |
| 2306 | Implementations should report these as an error except in the following cases: |
| 2307 | |
| 2308 | |
| 2309 | #### Expressions that unify an atom with an expression |
| 2310 | |
| 2311 | An expression of the form `a & e`, where `a` is an atom |
| 2312 | and `e` is an expression, always evaluates to `a` or bottom. |
| 2313 | As it does not matter how we fail, we can assume the result to be `a` |
| 2314 | and validate after the field in which the expression occurs has been evaluated |
| 2315 | that `a == e`. |
| 2316 | |
| 2317 | ``` |
Marcel van Lohuizen | eac8f9a | 2019-08-03 13:53:56 +0200 | [diff] [blame^] | 2318 | // Config Evaluates to (requiring concrete values) |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2319 | x: { x: { |
| 2320 | a: b + 100 a: _|_ // cycle detected |
| 2321 | b: a - 100 b: _|_ // cycle detected |
| 2322 | } } |
| 2323 | |
| 2324 | y: x & { y: { |
| 2325 | a: 200 a: 200 // asserted that 200 == b + 100 |
| 2326 | b: 100 |
| 2327 | } } |
| 2328 | ``` |
| 2329 | |
| 2330 | |
| 2331 | #### Field values |
| 2332 | |
| 2333 | A field value of the form `r & v`, |
| 2334 | where `r` evaluates to a reference cycle and `v` is a value, |
| 2335 | evaluates to `v`. |
| 2336 | Unification is idempotent and unifying a value with itself ad infinitum, |
| 2337 | which is what the cycle represents, results in this value. |
| 2338 | Implementations should detect cycles of this kind, ignore `r`, |
| 2339 | and take `v` as the result of unification. |
| 2340 | <!-- Tomabechi's graph unification algorithm |
| 2341 | can detect such cycles at near-zero cost. --> |
| 2342 | |
| 2343 | ``` |
| 2344 | Configuration Evaluated |
| 2345 | // c Cycles in nodes of type struct evaluate |
| 2346 | // ↙︎ ↖ to the fixed point of unifying their |
| 2347 | // a → b values ad infinitum. |
| 2348 | |
| 2349 | a: b & { x: 1 } // a: { x: 1, y: 2, z: 3 } |
| 2350 | b: c & { y: 2 } // b: { x: 1, y: 2, z: 3 } |
| 2351 | c: a & { z: 3 } // c: { x: 1, y: 2, z: 3 } |
| 2352 | |
| 2353 | // resolve a b & {x:1} |
| 2354 | // substitute b c & {y:2} & {x:1} |
| 2355 | // substitute c a & {z:3} & {y:2} & {x:1} |
| 2356 | // eliminate a (cycle) {z:3} & {y:2} & {x:1} |
| 2357 | // simplify {x:1,y:2,z:3} |
| 2358 | ``` |
| 2359 | |
| 2360 | This rule also applies to field values that are disjunctions of unification |
| 2361 | operations of the above form. |
| 2362 | |
| 2363 | ``` |
| 2364 | a: b&{x:1} | {y:1} // {x:1,y:3,z:2} | {y:1} |
| 2365 | b: {x:2} | c&{z:2} // {x:2} | {x:1,y:3,z:2} |
| 2366 | c: a&{y:3} | {z:3} // {x:1,y:3,z:2} | {z:3} |
| 2367 | |
| 2368 | |
| 2369 | // resolving a b&{x:1} | {y:1} |
| 2370 | // substitute b ({x:2} | c&{z:2})&{x:1} | {y:1} |
| 2371 | // simplify c&{z:2}&{x:1} | {y:1} |
| 2372 | // substitute c (a&{y:3} | {z:3})&{z:2}&{x:1} | {y:1} |
| 2373 | // simplify a&{y:3}&{z:2}&{x:1} | {y:1} |
| 2374 | // eliminate a (cycle) {y:3}&{z:2}&{x:1} | {y:1} |
| 2375 | // expand {x:1,y:3,z:2} | {y:1} |
| 2376 | ``` |
| 2377 | |
| 2378 | Note that all nodes that form a reference cycle to form a struct will evaluate |
| 2379 | to the same value. |
| 2380 | If a field value is a disjunction, any element that is part of a cycle will |
| 2381 | evaluate to this value. |
| 2382 | |
| 2383 | |
| 2384 | ### Structural cycles |
| 2385 | |
| 2386 | CUE disallows infinite structures. |
| 2387 | Implementations must report an error when encountering such declarations. |
| 2388 | |
| 2389 | <!-- for instance using an occurs check --> |
| 2390 | |
| 2391 | ``` |
| 2392 | // Disallowed: a list of infinite length with all elements being 1. |
| 2393 | list: { |
| 2394 | head: 1 |
| 2395 | tail: list |
| 2396 | } |
| 2397 | |
| 2398 | // Disallowed: another infinite structure (a:{b:{d:{b:{d:{...}}}}}, ...). |
| 2399 | a: { |
| 2400 | b: c |
| 2401 | } |
| 2402 | c: { |
| 2403 | d: a |
| 2404 | } |
| 2405 | ``` |
| 2406 | |
| 2407 | It is allowed for a value to define an infinite set of possibilities |
| 2408 | without evaluating to an infinite structure itself. |
| 2409 | |
| 2410 | ``` |
| 2411 | // List defines a list of arbitrary length (default null). |
| 2412 | List: *null | { |
| 2413 | head: _ |
| 2414 | tail: List |
| 2415 | } |
| 2416 | ``` |
| 2417 | |
| 2418 | <!-- |
Marcel van Lohuizen | 7f48df7 | 2019-02-01 17:24:59 +0100 | [diff] [blame] | 2419 | Consider banning any construct that makes CUE not having a linear |
| 2420 | running time expressed in the number of nodes in the output. |
| 2421 | |
| 2422 | This would require restricting constructs like: |
| 2423 | |
| 2424 | (fib&{n:2}).out |
| 2425 | |
| 2426 | fib: { |
| 2427 | n: int |
| 2428 | |
| 2429 | out: (fib&{n:n-2}).out + (fib&{n:n-1}).out if n >= 2 |
| 2430 | out: fib({n:n-2}).out + fib({n:n-1}).out if n >= 2 |
| 2431 | out: n if n < 2 |
| 2432 | } |
| 2433 | |
| 2434 | --> |
| 2435 | <!-- |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2436 | ### Unused fields |
| 2437 | |
| 2438 | TODO: rules for detection of unused fields |
| 2439 | |
| 2440 | 1. Any alias value must be used |
| 2441 | --> |
| 2442 | |
| 2443 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2444 | ## Modules, instances, and packages |
| 2445 | |
| 2446 | CUE configurations are constructed combining _instances_. |
| 2447 | An instance, in turn, is constructed from one or more source files belonging |
| 2448 | to the same _package_ that together declare the data representation. |
| 2449 | Elements of this data representation may be exported and used |
| 2450 | in other instances. |
| 2451 | |
| 2452 | ### Source file organization |
| 2453 | |
| 2454 | Each source file consists of an optional package clause defining collection |
| 2455 | of files to which it belongs, |
| 2456 | followed by a possibly empty set of import declarations that declare |
| 2457 | packages whose contents it wishes to use, followed by a possibly empty set of |
| 2458 | declarations. |
| 2459 | |
| 2460 | |
| 2461 | ``` |
| 2462 | SourceFile = [ PackageClause "," ] { ImportDecl "," } { TopLevelDecl "," } . |
| 2463 | ``` |
| 2464 | |
| 2465 | ### Package clause |
| 2466 | |
| 2467 | A package clause is an optional clause that defines the package to which |
| 2468 | a source file the file belongs. |
| 2469 | |
| 2470 | ``` |
| 2471 | PackageClause = "package" PackageName . |
| 2472 | PackageName = identifier . |
| 2473 | ``` |
| 2474 | |
| 2475 | The PackageName must not be the blank identifier. |
| 2476 | |
| 2477 | ``` |
| 2478 | package math |
| 2479 | ``` |
| 2480 | |
| 2481 | ### Modules and instances |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2482 | A _module_ defines a tree of directories, rooted at the _module root_. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2483 | |
| 2484 | All source files within a module with the same package belong to the same |
| 2485 | package. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2486 | <!-- jba: I can't make sense of the above sentence. --> |
| 2487 | A module may define multiple packages. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2488 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2489 | An _instance_ of a package is any subset of files belonging |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2490 | to the same package. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2491 | <!-- jba: Are you saying that --> |
| 2492 | <!-- if I have a package with files a, b and c, then there are 8 instances of --> |
| 2493 | <!-- that package, some of which are {a, b}, {c}, {b, c}, and so on? What's the --> |
| 2494 | <!-- purpose of that definition? --> |
| 2495 | It is interpreted as the concatenation of these files. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2496 | |
| 2497 | An implementation may impose conventions on the layout of package files |
| 2498 | to determine which files of a package belongs to an instance. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2499 | For example, an instance may be defined as the subset of package files |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2500 | belonging to a directory and all its ancestors. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2501 | <!-- jba: OK, that helps a little, but I still don't see what the purpose is. --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2502 | |
| 2503 | ### Import declarations |
| 2504 | |
| 2505 | An import declaration states that the source file containing the declaration |
| 2506 | depends on definitions of the _imported_ package (§Program initialization and |
| 2507 | execution) and enables access to exported identifiers of that package. |
| 2508 | The import names an identifier (PackageName) to be used for access and an |
| 2509 | ImportPath that specifies the package to be imported. |
| 2510 | |
| 2511 | ``` |
| 2512 | ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) . |
| 2513 | ImportSpec = [ "." | PackageName ] ImportPath . |
| 2514 | ImportPath = `"` { unicode_value } `"` . |
| 2515 | ``` |
| 2516 | |
| 2517 | The PackageName is used in qualified identifiers to access exported identifiers |
| 2518 | of the package within the importing source file. |
| 2519 | It is declared in the file block. |
| 2520 | If the PackageName is omitted, it defaults to the identifier specified in the |
| 2521 | package clause of the imported instance. |
| 2522 | If an explicit period (.) appears instead of a name, all the instances's |
| 2523 | exported identifiers declared in that instances's package block will be declared |
| 2524 | in the importing source file's file block |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2525 | and must be accessed without a qualifier. |
| 2526 | <!-- jba: Can you omit this feature? It's likely to only decrease readability, |
| 2527 | as we know from Go. --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2528 | |
| 2529 | The interpretation of the ImportPath is implementation-dependent but it is |
| 2530 | typically either the path of a builtin package or a fully qualifying location |
| 2531 | of an instance within a source code repository. |
| 2532 | |
| 2533 | Implementation restriction: An interpreter may restrict ImportPaths to non-empty |
| 2534 | strings using only characters belonging to Unicode's L, M, N, P, and S general |
| 2535 | categories (the Graphic characters without spaces) and may also exclude the |
| 2536 | characters !"#$%&'()*,:;<=>?[\]^`{|} and the Unicode replacement character |
| 2537 | U+FFFD. |
| 2538 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2539 | Assume we have package containing the package clause "package math", |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2540 | which exports function Sin at the path identified by "lib/math". |
| 2541 | This table illustrates how Sin is accessed in files |
| 2542 | that import the package after the various types of import declaration. |
| 2543 | |
| 2544 | ``` |
| 2545 | Import declaration Local name of Sin |
| 2546 | |
| 2547 | import "lib/math" math.Sin |
| 2548 | import m "lib/math" m.Sin |
| 2549 | import . "lib/math" Sin |
| 2550 | ``` |
| 2551 | |
| 2552 | An import declaration declares a dependency relation between the importing and |
| 2553 | imported package. It is illegal for a package to import itself, directly or |
| 2554 | indirectly, or to directly import a package without referring to any of its |
| 2555 | exported identifiers. |
| 2556 | |
| 2557 | |
| 2558 | ### An example package |
| 2559 | |
| 2560 | TODO |
| 2561 | |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2562 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2563 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2564 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2565 | |