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. |
| 46 | Others are Swift, Javascript, Prolog, NCL (internal to Google), Jsonnet, HCL, |
| 47 | Flabbergast, JSONPath, Haskell, Objective-C, and Python. |
| 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 | 08466f8 | 2019-02-01 09:09:09 +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 | |
| 280 | ### Integer literals |
| 281 | |
| 282 | An integer literal is a sequence of digits representing an integer value. |
| 283 | An optional prefix sets a non-decimal base: 0 for octal, |
| 284 | 0x or 0X for hexadecimal, and 0b for binary. |
| 285 | 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] | 286 | All integers allow interstitial underscores "_"; |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 287 | these have no meaning and are solely for readability. |
| 288 | |
| 289 | Decimal integers may have a SI or IEC multiplier. |
| 290 | Multipliers can be used with fractional numbers. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 291 | When multiplying a fraction by a multiplier, the result is truncated |
| 292 | towards zero if it is not an integer. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 293 | |
| 294 | ``` |
| 295 | int_lit = decimal_lit | octal_lit | binary_lit | hex_lit . |
| 296 | decimals = ( "0" … "9" ) { [ "_" ] decimal_digit } . |
| 297 | decimal_lit = ( "1" … "9" ) { [ "_" ] decimal_digit } [ [ "." decimals ] multiplier ] | |
| 298 | "." decimals multiplier. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 299 | binary_lit = "0b" binary_digit { binary_digit } . |
| 300 | hex_lit = "0" ( "x" | "X" ) hex_digit { [ "_" ] hex_digit } . |
Jonathan Amsterdam | abeffa4 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 301 | multiplier = ( "K" | "M" | "G" | "T" | "P" | "E" | "Y" | "Z" ) [ "i" ] |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 302 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 303 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 304 | <!-- |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 305 | octal_lit = "0" octal_digit { [ "_" ] octal_digit } . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 306 | TODO: consider 0o766 notation for octal. |
| 307 | ---> |
| 308 | |
| 309 | ``` |
| 310 | 42 |
| 311 | 1.5Gi |
| 312 | 0600 |
| 313 | 0xBad_Face |
| 314 | 170_141_183_460_469_231_731_687_303_715_884_105_727 |
| 315 | ``` |
| 316 | |
| 317 | ### Decimal floating-point literals |
| 318 | |
| 319 | A decimal floating-point literal is a representation of |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 320 | a decimal floating-point value (a _float_). |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 321 | It has an integer part, a decimal point, a fractional part, and an |
| 322 | exponent part. |
| 323 | The integer and fractional part comprise decimal digits; the |
| 324 | exponent part is an `e` or `E` followed by an optionally signed decimal exponent. |
| 325 | One of the integer part or the fractional part may be elided; one of the decimal |
| 326 | point or the exponent may be elided. |
| 327 | |
| 328 | ``` |
| 329 | decimal_lit = decimals "." [ decimals ] [ exponent ] | |
| 330 | decimals exponent | |
| 331 | "." decimals [ exponent ] . |
| 332 | exponent = ( "e" | "E" ) [ "+" | "-" ] decimals . |
| 333 | ``` |
| 334 | |
| 335 | ``` |
| 336 | 0. |
| 337 | 72.40 |
| 338 | 072.40 // == 72.40 |
| 339 | 2.71828 |
| 340 | 1.e+0 |
| 341 | 6.67428e-11 |
| 342 | 1E6 |
| 343 | .25 |
| 344 | .12345E+5 |
| 345 | ``` |
| 346 | |
| 347 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 348 | ### String and byte sequence literals |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 349 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 350 | A string literal represents a string constant obtained from concatenating a |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 351 | sequence of characters. |
| 352 | Byte sequences are a sequence of bytes. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 353 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 354 | String and byte sequence literals are character sequences between, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 355 | respectively, double and single quotes, as in `"bar"` and `'bar'`. |
| 356 | Within the quotes, any character may appear except newline and, |
| 357 | respectively, unescaped double or single quote. |
| 358 | String literals may only be valid UTF-8. |
| 359 | Byte sequences may contain any sequence of bytes. |
| 360 | |
| 361 | Several backslash escapes allow arbitrary values to be encoded as ASCII text |
| 362 | in interpreted strings. |
| 363 | There are four ways to represent the integer value as a numeric constant: `\x` |
| 364 | followed by exactly two hexadecimal digits; \u followed by exactly four |
| 365 | hexadecimal digits; `\U` followed by exactly eight hexadecimal digits, and a |
| 366 | plain backslash `\` followed by exactly three octal digits. |
| 367 | In each case the value of the literal is the value represented by the |
| 368 | digits in the corresponding base. |
| 369 | Hexadecimal and octal escapes are only allowed within byte sequences |
| 370 | (single quotes). |
| 371 | |
| 372 | Although these representations all result in an integer, they have different |
| 373 | valid ranges. |
| 374 | Octal escapes must represent a value between 0 and 255 inclusive. |
| 375 | Hexadecimal escapes satisfy this condition by construction. |
| 376 | The escapes `\u` and `\U` represent Unicode code points so within them |
| 377 | some values are illegal, in particular those above `0x10FFFF`. |
| 378 | Surrogate halves are allowed to be compatible with JSON, |
| 379 | but are translated into their non-surrogate equivalent internally. |
| 380 | |
| 381 | The three-digit octal (`\nnn`) and two-digit hexadecimal (`\xnn`) escapes |
| 382 | represent individual bytes of the resulting string; all other escapes represent |
| 383 | the (possibly multi-byte) UTF-8 encoding of individual characters. |
| 384 | Thus inside a string literal `\377` and `\xFF` represent a single byte of |
| 385 | value `0xFF=255`, while `ÿ`, `\u00FF`, `\U000000FF` and `\xc3\xbf` represent |
| 386 | the two bytes `0xc3 0xbf` of the UTF-8 |
| 387 | encoding of character `U+00FF`. |
| 388 | |
| 389 | After a backslash, certain single-character escapes represent special values: |
| 390 | |
| 391 | ``` |
| 392 | \a U+0007 alert or bell |
| 393 | \b U+0008 backspace |
| 394 | \f U+000C form feed |
| 395 | \n U+000A line feed or newline |
| 396 | \r U+000D carriage return |
| 397 | \t U+0009 horizontal tab |
| 398 | \v U+000b vertical tab |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 399 | \/ U+002f slash (solidus) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 400 | \\ U+005c backslash |
| 401 | \' U+0027 single quote (valid escape only within single quoted literals) |
| 402 | \" U+0022 double quote (valid escape only within double quoted literals) |
| 403 | ``` |
| 404 | |
| 405 | The escape `\(` is used as an escape for string interpolation. |
| 406 | A `\(` must be followed by a valid CUE Expression, followed by a `)`. |
| 407 | |
| 408 | All other sequences starting with a backslash are illegal inside literals. |
| 409 | |
| 410 | ``` |
| 411 | escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) . |
| 412 | unicode_value = unicode_char | little_u_value | big_u_value | escaped_char . |
| 413 | byte_value = octal_byte_value | hex_byte_value . |
| 414 | octal_byte_value = `\` octal_digit octal_digit octal_digit . |
| 415 | hex_byte_value = `\` "x" hex_digit hex_digit . |
| 416 | little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit . |
| 417 | big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit |
| 418 | hex_digit hex_digit hex_digit hex_digit . |
| 419 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 420 | string_lit = interpreted_string_lit | |
| 421 | interpreted_bytes_lit | |
| 422 | multiline_lit . |
| 423 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 424 | interpolation = "\(" Expression ")" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 425 | interpreted_string_lit = `"` { unicode_value | interpolation } `"` . |
| 426 | interpreted_bytes_lit = `"` { unicode_value | interpolation | byte_value } `"` . |
| 427 | ``` |
| 428 | |
| 429 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 430 | 'a\000\xab' |
| 431 | '\007' |
| 432 | '\377' |
| 433 | '\xa' // illegal: too few hexadecimal digits |
| 434 | "\n" |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 435 | "\"" |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 436 | 'Hello, world!\n' |
| 437 | "Hello, \( name )!" |
| 438 | "日本語" |
| 439 | "\u65e5本\U00008a9e" |
| 440 | "\xff\u00FF" |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 441 | "\uD800" // illegal: surrogate half (TODO: probably should allow) |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 442 | "\U00110000" // illegal: invalid Unicode code point |
| 443 | ``` |
| 444 | |
| 445 | These examples all represent the same string: |
| 446 | |
| 447 | ``` |
| 448 | "日本語" // UTF-8 input text |
| 449 | '日本語' // UTF-8 input text as byte sequence |
| 450 | `日本語` // UTF-8 input text as a raw literal |
| 451 | "\u65e5\u672c\u8a9e" // the explicit Unicode code points |
| 452 | "\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points |
| 453 | "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes |
| 454 | ``` |
| 455 | |
| 456 | If the source code represents a character as two code points, such as a |
| 457 | combining form involving an accent and a letter, the result will appear as two |
| 458 | code points if placed in a string literal. |
| 459 | |
| 460 | Each of the interpreted string variants have a multiline equivalent. |
| 461 | Multiline interpreted strings are like their single-line equivalent, |
| 462 | but allow newline characters. |
| 463 | Carriage return characters (`\r`) inside raw string literals are discarded from |
| 464 | the raw string value. |
| 465 | |
| 466 | Multiline interpreted strings and byte sequences respectively start with |
| 467 | a triple double quote (`"""`) or triple single quote (`'''`), |
| 468 | immediately followed by a newline, which is discarded from the string contents. |
| 469 | The string is closed by a matching triple quote, which must be by itself |
| 470 | on a newline, preceded by optional whitespace. |
| 471 | The whitespace before a closing triple quote must appear before any non-empty |
| 472 | line after the opening quote and will be removed from each of these |
| 473 | lines in the string literal. |
| 474 | A closing triple quote may not appear in the string. |
| 475 | To include it is suffices to escape one of the quotes. |
| 476 | |
| 477 | ``` |
| 478 | multiline_lit = multiline_string_lit | multiline_bytes_lit . |
| 479 | multiline_string_lit = `"""` newline |
| 480 | { unicode_char | interpolation | newline } |
| 481 | newline `"""` . |
| 482 | multiline_bytes_lit = "'''" newline |
| 483 | { unicode_char | interpolation | newline | byte_value } |
| 484 | newline "'''" . |
| 485 | ``` |
| 486 | |
| 487 | ``` |
| 488 | """ |
| 489 | lily: |
| 490 | out of the water |
| 491 | out of itself |
| 492 | |
| 493 | bass |
| 494 | picking bugs |
| 495 | off the moon |
| 496 | — Nick Virgilio, Selected Haiku, 1988 |
| 497 | """ |
| 498 | ``` |
| 499 | |
| 500 | This represents the same string as: |
| 501 | |
| 502 | ``` |
| 503 | "lily:\nout of the water\nout of itself\n\n" + |
| 504 | "bass\npicking bugs\noff the moon\n" + |
| 505 | " — Nick Virgilio, Selected Haiku, 1988" |
| 506 | ``` |
| 507 | |
| 508 | <!-- TODO: other values |
| 509 | |
| 510 | Support for other values: |
| 511 | - Duration literals |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 512 | - regular expessions: `re("[a-z]")` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 513 | --> |
| 514 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 515 | |
| 516 | ## Values |
| 517 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 518 | In addition to simple values like `"hello"` and `42.0`, CUE has _structs_. |
| 519 | A struct is a map from labels to values, like `{a: 42.0, b: "hello"}`. |
| 520 | Structs are CUE's only way of building up complex values; |
| 521 | lists, which we will see later, |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 522 | are defined in terms of structs. |
| 523 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 524 | All possible values are ordered in a lattice, |
| 525 | 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] | 526 | A value `a` is an _instance_ of a value `b`, |
| 527 | denoted `a ⊑ b`, if `b == a` or `b` is more general than `a`, |
| 528 | that is if `a` orders before `b` in the partial order |
| 529 | (`⊑` is _not_ a CUE operator). |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 530 | We also say that `b` _subsumes_ `a` in this case. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 531 | In graphical terms, `b` is "above" `a` in the lattice. |
| 532 | |
| 533 | 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] | 534 | _top_, denoted `_` in CUE. |
| 535 | Every value is an instance of top. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 536 | |
| 537 | 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] | 538 | A bottom value usually indicates an error. |
| 539 | Bottom is an instance of every value. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 540 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 541 | An _atom_ is any value whose only instances are itself and bottom. |
| 542 | Examples of atoms are `42.0`, `"hello"`, `true`, `null`. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 543 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 544 | A value is _concrete_ if it is either an atom, or a struct all of whose |
| 545 | field values are themselves concrete, recursively. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 546 | |
| 547 | 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] | 548 | `float`. |
| 549 | But CUE does not distinguish between types and values; only the |
| 550 | relationship of values in the lattice is important. |
| 551 | Each CUE "type" subsumes the concrete values that one would normally think |
| 552 | of as part of that type. |
| 553 | For example, "hello" is an instance of `string`, and `42.0` is an instance of |
| 554 | `float`. |
| 555 | In addition to `string` and `float`, CUE has `null`, `int`, `bool` and `bytes`. |
| 556 | We informally call these CUE's "basic types". |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 557 | |
| 558 | |
| 559 | ``` |
| 560 | false ⊑ bool |
| 561 | true ⊑ bool |
| 562 | true ⊑ true |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 563 | 5.0 ⊑ float |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 564 | bool ⊑ _ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 565 | _|_ ⊑ _ |
| 566 | _|_ ⊑ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 567 | |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 568 | _ ⋢ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 569 | _ ⋢ bool |
| 570 | int ⋢ bool |
| 571 | bool ⋢ int |
| 572 | false ⋢ true |
| 573 | true ⋢ false |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 574 | float ⋢ 5.0 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 575 | 5 ⋢ 6 |
| 576 | ``` |
| 577 | |
| 578 | |
| 579 | ### Unification |
| 580 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame^] | 581 | The _unification_ of values `a` and `b` |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 582 | 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] | 583 | value `u` such that `u ⊑ a` and `u ⊑ b`, |
| 584 | and for any other value `v` for which `v ⊑ a` and `v ⊑ b` |
| 585 | it holds that `v ⊑ u`.) |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame^] | 586 | Since CUE values form a lattice, the unification of two CUE values is |
| 587 | always unique. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 588 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame^] | 589 | These all follow from the definition of unification: |
| 590 | - The unification of `a` with itself is always `a`. |
| 591 | - The unification of values `a` and `b` where `a ⊑ b` is always `a`. |
| 592 | - The unification of a value with bottom is always bottom. |
| 593 | |
| 594 | Unification in CUE is a [binary expression](#Operands), written `a & b`. |
| 595 | It is commutative and associative. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 596 | As a consequence, order of evaluation is irrelevant, a property that is key |
| 597 | to many of the constructs in the CUE language as well as the tooling layered |
| 598 | on top of it. |
| 599 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame^] | 600 | |
| 601 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 602 | <!-- TODO: explicitly mention that disjunction is not a binary operation |
| 603 | but a definition of a single value?--> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 604 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 605 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 606 | ### Disjunction |
| 607 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame^] | 608 | The _disjunction_ of values `a` and `b` |
| 609 | is defined as the least upper bound of `a` and `b`. |
| 610 | (That is, the value `d` such that `a ⊑ d` and `b ⊑ d`, |
| 611 | and for any other value `e` for which `a ⊑ e` and `b ⊑ e`, |
| 612 | it holds that `d ⊑ e`.) |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 613 | This style of disjunctions is sometimes also referred to as sum types. |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame^] | 614 | Since CUE values form a lattice, the disjunction of two CUE values is always unique. |
| 615 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 616 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 617 | These all follow from the definition of disjunction: |
| 618 | - The disjunction of `a` with itself is always `a`. |
| 619 | - The disjunction of a value `a` and `b` where `a ⊑ b` is always `b`. |
| 620 | - The disjunction of a value `a` with bottom is always `a`. |
| 621 | - The disjunction of two bottom values is bottom. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 622 | |
Jonathan Amsterdam | a8d8a3c | 2019-02-03 07:53:55 -0500 | [diff] [blame^] | 623 | Disjunction in CUE is a [binary expression](#Operands), written `a | b`. |
| 624 | It is commutative and associative. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 625 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 626 | The unification of a disjunction with another value is equal to the disjunction |
| 627 | composed of the unification of this value with all of the original elements |
| 628 | of the disjunction. |
| 629 | In other words, unification distributes over disjunction. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 630 | |
| 631 | ``` |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 632 | (a_0 | ... |a_n) & b ==> a_0&b | ... | a_n&b. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 633 | ``` |
| 634 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 635 | ``` |
| 636 | Expression Result |
| 637 | ({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] | 638 | (int | string) & "foo" "foo" |
| 639 | ("a" | "b") & "c" _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 640 | ``` |
| 641 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 642 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 643 | #### Default values |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 644 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 645 | One or more values in a disjunction can be _marked_ |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 646 | by prefixing it with a `*` ([a unary expression](#Operators)). |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 647 | A bottom value cannot be marked. |
| 648 | When a marked value is unified, the result is also marked. |
| 649 | (When unification results in a single value, |
| 650 | the mark is dropped, as single values cannot be marked.) |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 651 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 652 | A disjunction is _normalized_ if there is no unmarked element |
| 653 | `a` for which there is an element `b` such that `a ⊑ b` |
| 654 | and no marked element `c` for which there is a marked element |
| 655 | `d` such that `c ⊑ d`. |
| 656 | A disjunction literal must be normalized. |
| 657 | |
| 658 | <!-- |
| 659 | (non-normalized entries could also be implicitly marked, allowing writing |
| 660 | int | 1, instead of int | *1, but that can be done in a backwards |
| 661 | compatible way later if really desirable). |
| 662 | |
| 663 | Normalization is important, as we need to account for spurious elements |
| 664 | For instance |
| 665 | "tcp" | "tcp", or |
| 666 | ({a:1} | {b:1}) & ({a:1} | {b:2}) -> {a:1} | {a:1,b:1} | {a:1,b:2}, |
| 667 | |
| 668 | In the latter case, elements {a:1,b:1} and {a:1,b:2} are subsumed by {a:1}. |
| 669 | Note that without defaults, {a:1} | {a:1,b:1} | {a:1,b:2} is logically |
| 670 | identical to {a:1}. |
| 671 | More to the point, without normalization unifying {a:1} | {b:1} with {a:1,b:2} |
| 672 | results in a single value and thus resolves, |
| 673 | whereas unifying {a:1} | {a:1,b:1} | {a:1,b:2} with {a:1,b:2} |
| 674 | results in two values, and thus does not resolve. |
| 675 | With normalization: |
| 676 | ({a:1} | {a:1,b:1} | {a:1,b:2}) & {a:1} {a:1}, instead of _|_, |
| 677 | ({a:1} | {b:1}) & {a:1} {a:1} (instead of _|_), as {a:1,b:1} ⊑ {a:1} |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 678 | --> |
| 679 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 680 | If a disjunction appears where a concrete value is required |
| 681 | (that is, as an operand or in a location where it will be emitted), |
| 682 | the result is, after normalization and after dropping non-marked elements |
| 683 | if some elements are marked, |
| 684 | the resulting value itself if only a single value remains or bottom otherwise. |
| 685 | |
| 686 | <!-- |
| 687 | We treat remaining marked and unmarked elements the same to have less surprises: |
| 688 | |
| 689 | Unifying {a:1}|{b:1} with *{}|string produces *{a:1}|*{b:1}. It would be |
| 690 | surprising to have a different default for {a:1}|{b:1} and *{a:1}|*{b:1} in |
| 691 | this case. |
| 692 | |
| 693 | Similarly, we do not unify the remaining elements to minimize the difference |
| 694 | between using a disjunction in cases where concrete values are required |
| 695 | versus otherwise. |
| 696 | --> |
| 697 | |
| 698 | <!-- TODO: is the above definition precise enough, or perhaps too abstract? |
| 699 | Previously: |
| 700 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 701 | A default value is chosen if the disjunction is not used |
| 702 | in a unification or disjunction operation. |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 703 | This means that, in practice, a default is chosen for almost any expression |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 704 | that does not involve `&` and `|`, including slices, indices, selectors, |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 705 | and all but a few explicitly marked builtin functions. --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 706 | |
| 707 | ``` |
| 708 | Expression Default |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 709 | "tcp" | "udp" _|_ // more than one element remaining |
| 710 | *"tcp" | "udp" "tcp" |
| 711 | float | *1 1 |
| 712 | *string | 1.0 string |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 713 | |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 714 | (*"tcp"|"udp") & ("udp"|*"tcp") "tcp" |
| 715 | (*"tcp"|"udp") & ("udp"|"tcp") "tcp" |
| 716 | (*"tcp"|"udp") & "tcp" "tcp" |
| 717 | (*"tcp"|"udp") & (*"udp"|"tcp") _|_ // "tcp" & "udp" |
| 718 | |
| 719 | (*true | false) & bool true |
| 720 | (*true | false) & (true | false) true |
| 721 | |
| 722 | {a: 1} | {b: 1} _|_ // more than one element remaining |
| 723 | {a: 1} | *{b: 1} {b:1} |
| 724 | *{a: 1} | *{b: 1} _|_ // more than one marked element remaining |
| 725 | ({a: 1} | {b: 1}) & {a:1} {a:1} // after eliminating {a:1,b:1} |
| 726 | ({a:1}|*{b:1}) & ({a:1}|*{b:1}) {b:1} // after eliminating *{a:1,b:1} |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 727 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 728 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 729 | A disjunction always evaluates to the same default value, regardless of |
| 730 | the context in which the value is used. |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 731 | For instance, `[1, 3][*"a" | 1]` will result in an error, as `"a"` will be |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 732 | selected as the default value. |
| 733 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 734 | ``` |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 735 | [1, 2][*"a" | 1] // _|_ // "a" is not an integer value |
| 736 | [1, 2][(*"a" | 1) & int] // 2, as "a" is eliminated before choosing a default. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 737 | ``` |
| 738 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 739 | |
| 740 | ### Bottom and errors |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 741 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 742 | 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] | 743 | the token '_|_'. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 744 | Bottom is an instance of every other value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 745 | Any evaluation error is represented as bottom. |
| 746 | |
| 747 | Implementations may associate error strings with different instances of bottom; |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 748 | logically they all remain the same value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 749 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 750 | |
| 751 | ### Top |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 752 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 753 | Top is represented by the underscore character '_', lexically an identifier. |
| 754 | Unifying any value `v` with top results `v` itself. |
| 755 | |
| 756 | ``` |
| 757 | Expr Result |
| 758 | _ & 5 5 |
| 759 | _ & _ _ |
| 760 | _ & _|_ _|_ |
| 761 | _ | _|_ _ |
| 762 | ``` |
| 763 | |
| 764 | |
| 765 | ### Null |
| 766 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 767 | The _null value_ is represented with the keyword `null`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 768 | It has only one parent, top, and one child, bottom. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 769 | It is unordered with respect to any other value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 770 | |
| 771 | ``` |
| 772 | null_lit = "null" |
| 773 | ``` |
| 774 | |
| 775 | ``` |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 776 | null & 8 _|_ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 777 | null & _ null |
| 778 | null & _|_ _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 779 | ``` |
| 780 | |
| 781 | |
| 782 | ### Boolean values |
| 783 | |
| 784 | 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] | 785 | the keywords `true` and `false`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 786 | The predeclared boolean type is `bool`; it is a defined type and a separate |
| 787 | element in the lattice. |
| 788 | |
| 789 | ``` |
| 790 | boolean_lit = "true" | "false" |
| 791 | ``` |
| 792 | |
| 793 | ``` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 794 | bool & true true |
| 795 | true & true true |
| 796 | true & false _|_ |
| 797 | bool & (false|true) false | true |
| 798 | bool & (true|false) true | false |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 799 | ``` |
| 800 | |
| 801 | |
| 802 | ### Numeric values |
| 803 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 804 | The _integer type_ represents the set of all integral numbers. |
| 805 | 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] | 806 | numbers. |
| 807 | They are two distinct types. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 808 | The predeclared integer and decimal floating-point types are `int` and `float`; |
| 809 | they are defined types. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 810 | |
| 811 | A decimal floating-point literal always has type `float`; |
| 812 | it is not an instance of `int` even if it is an integral number. |
| 813 | |
| 814 | An integer literal has both type `int` and `float`, with the integer variant |
| 815 | being the default if no other constraints are applied. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 816 | Expressed in terms of disjunction and [type conversion](#conversions), |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 817 | 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] | 818 | Hexadecimal, octal, and binary integer literals are always of type `int`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 819 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 820 | Numeric literals are exact values of arbitrary precision. |
| 821 | 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] | 822 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 823 | Implementation restriction: although numeric values have arbitrary precision |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 824 | in the language, implementations may implement them using an internal |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 825 | representation with limited precision. |
| 826 | That said, every implementation must: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 827 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 828 | - Represent integer values with at least 256 bits. |
| 829 | - 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] | 830 | a signed binary exponent of at least 16 bits. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 831 | - Give an error if unable to represent an integer value precisely. |
| 832 | - Give an error if unable to represent a floating-point value due to overflow. |
| 833 | - Round to the nearest representable value if unable to represent |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 834 | a floating-point value due to limits on precision. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 835 | These requirements apply to the result of any expression except for builtin |
| 836 | 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] | 837 | |
| 838 | |
| 839 | ### Strings |
| 840 | |
| 841 | The _string type_ represents the set of all possible UTF-8 strings, |
| 842 | not allowing surrogates. |
| 843 | The predeclared string type is `string`; it is a defined type. |
| 844 | |
| 845 | Strings are designed to be unicode-safe. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 846 | Comparison is done using canonical forms ("é" == "e\u0301"). |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 847 | A string element is an |
| 848 | [extended grapheme cluster](https://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries), |
| 849 | which is an approximation of a human-readable character. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 850 | |
| 851 | The length of a string `s` (its size in bytes) can be discovered using |
| 852 | the built-in function len. |
| 853 | A string's extended grapheme cluster can be accessed by integer index |
| 854 | 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] | 855 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 856 | To access the individual bytes of a string one should convert it to |
| 857 | a sequence of bytes first. |
| 858 | |
| 859 | |
| 860 | ### Ranges |
| 861 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 862 | A _range type_, syntactically a [binary expression](#Operands), defines |
| 863 | a (possibly infinite) disjunction of concrete values that can be represented |
| 864 | as a contiguous range. |
| 865 | A concrete value `c` unifies with `a..b` if `a <= c` and `c <= b`. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 866 | Ranges can be defined on numbers and strings. |
| 867 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 868 | A range of numbers `a..b` defines an inclusive range for integers and |
| 869 | floating-point numbers. |
| 870 | |
| 871 | Remember that an integer literal represents both an `int` and `float`: |
| 872 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 873 | 2 & 1..5 // 2, where 2 is either an int or float. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 874 | 2.5 & 1..5 // 2.5 |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 875 | 2 & 1.0..3.0 // 2.0 |
| 876 | 2 & 1..3.0 // 2.0 |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 877 | 2.5 & int & 1..5 // _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 878 | 2.5 & float & 1..5 // 2.5 |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 879 | int & 2 & 1.0..3.0 // _|_ |
| 880 | 2.5 & (int & 1)..5 // _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 881 | 0..7 & 3..10 // 3..7 |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 882 | "foo" & "a".."n" // "foo" |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 883 | ``` |
| 884 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 885 | |
| 886 | ### Structs |
| 887 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 888 | A _struct_ is a set of elements called _fields_, each of |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 889 | which has a name, called a _label_, and value. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 890 | |
| 891 | We say a label is defined for a struct if the struct has a field with the |
| 892 | corresponding label. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 893 | 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] | 894 | 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] | 895 | 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] | 896 | Note that if `a` is an instance of `b` it may have fields with labels that |
| 897 | are not defined for `b`. |
| 898 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 899 | The (unique) struct with no fields, written `{}`, has every struct as an |
| 900 | instance. It can be considered the type of all structs. |
| 901 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 902 | 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] | 903 | has all fields of both `a` and `b`, where |
| 904 | the value of a field `f` in `c` is `a.f & b.f` if `f` is in both `a` and `b`, |
| 905 | or just `a.f` or `b.f` if `f` is in just `a` or `b`, respectively. |
| 906 | Any [references](#References) to `a` or `b` |
| 907 | 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] | 908 | The result of a unification is bottom (`_|_`) if any of its fields evaluates |
| 909 | to bottom, recursively. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 910 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 911 | A field name may also be an interpolated string. |
| 912 | Identifiers used in such strings are evaluated within |
| 913 | the scope of the struct in which the label is defined. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 914 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 915 | Syntactically, a struct literal may contain multiple fields with |
| 916 | 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] | 917 | that is the unification of the values of those fields. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 918 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 919 | A TemplateLabel indicates a template value that is to be unified with |
| 920 | the values of all fields within a struct. |
| 921 | The identifier of a template label binds to the field name of each |
| 922 | field and is visible within the template value. |
| 923 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 924 | ``` |
| 925 | StructLit = "{" [ { Declaration "," } Declaration ] "}" . |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 926 | Declaration = FieldDecl | AliasDecl | ComprehensionDecl . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 927 | FieldDecl = Label { Label } ":" Expression . |
| 928 | |
| 929 | AliasDecl = Label "=" Expression . |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 930 | Label = identifier | interpreted_string_lit | TemplateLabel . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 931 | TemplateLabel = "<" identifier ">" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 932 | Tag = "#" identifier [ ":" json_string ] . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 933 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 934 | |
| 935 | ``` |
| 936 | {a: 1} ⊑ {} |
| 937 | {a: 1, b: 1} ⊑ {a: 1} |
| 938 | {a: 1} ⊑ {a: int} |
| 939 | {a: 1, b: 1} ⊑ {a: int, b: float} |
| 940 | |
| 941 | {} ⋢ {a: 1} |
| 942 | {a: 2} ⋢ {a: 1} |
| 943 | {a: 1} ⋢ {b: 1} |
| 944 | ``` |
| 945 | |
| 946 | ``` |
| 947 | Expression Result |
| 948 | {a: int, a: 1} {a: int(1)} |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 949 | {a: int} & {a: 1} {a: int(1)} |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 950 | {a: 1..7} & {a: 5..9} {a: 5..7} |
| 951 | {a: 1..7, a: 5..9} {a: 5..7} |
| 952 | |
| 953 | {a: 1} & {b: 2} {a: 1, b: 2} |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 954 | {a: 1, b: int} & {b: 2} {a: 1, b: int(2)} |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 955 | |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 956 | {a: 1} & {a: 2} _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 957 | ``` |
| 958 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 959 | In addition to fields, a struct literal may also define aliases. |
| 960 | Aliases name values that can be referred to within the [scope](#declarations-and-scopes) of their |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 961 | definition, but are not part of the struct: aliases are irrelevant to |
| 962 | the partial ordering of values and are not emitted as part of any |
| 963 | generated data. |
| 964 | The name of an alias must be unique within the struct literal. |
| 965 | |
| 966 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 967 | // The empty struct. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 968 | {} |
| 969 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 970 | // A struct with 3 fields and 1 alias. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 971 | { |
| 972 | alias = 3 |
| 973 | |
| 974 | foo: 2 |
| 975 | bar: "a string" |
| 976 | |
| 977 | "not an ident": 4 |
| 978 | } |
| 979 | ``` |
| 980 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 981 | 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] | 982 | a sequence of the two field names, |
| 983 | followed by a colon and the value of that single field. |
| 984 | |
| 985 | ``` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 986 | job myTask replicas: 2 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 987 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 988 | expands to |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 989 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 990 | job: { |
| 991 | myTask: { |
| 992 | replicas: 2 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | ``` |
| 996 | |
| 997 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 998 | ### Lists |
| 999 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1000 | A list literal defines a new value of type list. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1001 | A list may be open or closed. |
| 1002 | 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] | 1003 | optionally followed by a value for the remaining elements. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1004 | |
| 1005 | The length of a closed list is the number of elements it contains. |
| 1006 | The length of an open list is the its number of elements as a lower bound |
| 1007 | and an unlimited number of elements as its upper bound. |
| 1008 | |
| 1009 | ``` |
| 1010 | ListLit = "[" [ ElementList [ "," [ "..." [ Element ] ] ] "]" . |
| 1011 | ElementList = Element { "," Element } . |
| 1012 | Element = Expression | LiteralValue . |
| 1013 | ``` |
| 1014 | <!--- |
| 1015 | KeyedElement = Element . |
| 1016 | ---> |
| 1017 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1018 | Lists can be thought of as structs: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1019 | |
| 1020 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1021 | List: *null | { |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1022 | Elem: _ |
| 1023 | Tail: List |
| 1024 | } |
| 1025 | ``` |
| 1026 | |
| 1027 | 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] | 1028 | `*null | List`, defaulting to the shortest variant. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1029 | For instance, the open list [ 1, 2, ... ] can be represented as: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1030 | ``` |
| 1031 | open: List & { Elem: 1, Tail: { Elem: 2 } } |
| 1032 | ``` |
| 1033 | and the closed version of this list, [ 1, 2 ], as |
| 1034 | ``` |
| 1035 | closed: List & { Elem: 1, Tail: { Elem: 2, Tail: null } } |
| 1036 | ``` |
| 1037 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1038 | Using this representation, the subsumption rule for lists can |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1039 | be derived from those of structs. |
| 1040 | Implementations are not required to implement lists as structs. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1041 | The `Elem` and `Tail` fields are not special and `len` will not work as |
| 1042 | expected in these cases. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1043 | |
| 1044 | |
| 1045 | ## Declarations and Scopes |
| 1046 | |
| 1047 | |
| 1048 | ### Blocks |
| 1049 | |
| 1050 | A _block_ is a possibly empty sequence of declarations. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1051 | The braces of a struct literal `{ ... }` form a block, but there are |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1052 | others as well: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1053 | |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 1054 | - The _universe block_ encompasses all CUE source text. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1055 | - Each [package](#modules-instances-and-packages) has a _package block_ |
| 1056 | containing all CUE source text in that package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1057 | - 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] | 1058 | - Each `for` and `let` clause in a [comprehension](#comprehensions) |
| 1059 | is considered to be its own implicit block. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1060 | |
| 1061 | Blocks nest and influence [scoping]. |
| 1062 | |
| 1063 | |
| 1064 | ### Declarations and scope |
| 1065 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1066 | A _declaration_ binds an identifier to a field, alias, or package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1067 | Every identifier in a program must be declared. |
| 1068 | Other than for fields, |
| 1069 | no identifier may be declared twice within the same block. |
| 1070 | For fields an identifier may be declared more than once within the same block, |
| 1071 | resulting in a field with a value that is the result of unifying the values |
| 1072 | of all fields with the same identifier. |
| 1073 | |
| 1074 | ``` |
| 1075 | TopLevelDecl = Declaration | Emit . |
| 1076 | Emit = Operand . |
| 1077 | ``` |
| 1078 | |
| 1079 | 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] | 1080 | identifier denotes the specified field, alias, or package. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1081 | |
| 1082 | CUE is lexically scoped using blocks: |
| 1083 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1084 | 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] | 1085 | 1. The scope of an identifier denoting a field or alias |
| 1086 | declared at top level (outside any struct literal) is the file block. |
| 1087 | 1. The scope of the package name of an imported package is the file block of the |
| 1088 | file containing the import declaration. |
| 1089 | 1. The scope of a field or alias identifier declared inside a struct literal |
| 1090 | is the innermost containing block. |
| 1091 | |
| 1092 | An identifier declared in a block may be redeclared in an inner block. |
| 1093 | While the identifier of the inner declaration is in scope, it denotes the entity |
| 1094 | declared by the inner declaration. |
| 1095 | |
| 1096 | The package clause is not a declaration; |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1097 | the package name does not appear in any scope. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1098 | 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] | 1099 | and to specify the default name for import declarations. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1100 | |
| 1101 | |
| 1102 | ### Predeclared identifiers |
| 1103 | |
| 1104 | ``` |
| 1105 | Functions |
| 1106 | len required close open |
| 1107 | |
| 1108 | Types |
| 1109 | null The null type and value |
| 1110 | bool All boolean values |
| 1111 | int All integral numbers |
| 1112 | float All decimal floating-point numbers |
| 1113 | string Any valid UTF-8 sequence |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1114 | bytes Any vallid byte sequence |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1115 | |
| 1116 | Derived Value |
| 1117 | number int | float |
| 1118 | uint 0..int |
| 1119 | uint8 0..255 |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1120 | int8 -128..127 |
| 1121 | uint16 0..65536 |
| 1122 | int16 -32_768...32_767 |
| 1123 | rune 0..0x10FFFF |
| 1124 | uint32 0..4_294_967_296 |
| 1125 | int32 -2_147_483_648..2_147_483_647 |
| 1126 | uint64 0..18_446_744_073_709_551_615 |
| 1127 | int64 -9_223_372_036_854_775_808..9_223_372_036_854_775_807 |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1128 | uint128 340_282_366_920_938_463_463_374_607_431_768_211_455 |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1129 | int128 -170_141_183_460_469_231_731_687_303_715_884_105_728.. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1130 | 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] | 1131 | ``` |
| 1132 | |
| 1133 | |
| 1134 | ### Exported and manifested identifiers |
| 1135 | |
| 1136 | An identifier of a package may be exported to permit access to it |
| 1137 | from another package. |
| 1138 | An identifier is exported if both: |
| 1139 | the first character of the identifier's name is not a Unicode lower case letter |
| 1140 | (Unicode class "Ll") or the underscore "_"; and |
| 1141 | the identifier is declared in the file block. |
| 1142 | All other identifiers are not exported. |
| 1143 | |
| 1144 | An identifier that starts with the underscore "_" is not |
| 1145 | emitted in any data output. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1146 | Quoted labels that start with an underscore are emitted, however. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1147 | |
| 1148 | ### Uniqueness of identifiers |
| 1149 | |
| 1150 | Given a set of identifiers, an identifier is called unique if it is different |
| 1151 | from every other in the set, after applying normalization following |
| 1152 | Unicode Annex #31. |
| 1153 | Two identifiers are different if they are spelled differently. |
| 1154 | <!-- |
| 1155 | or if they appear in different packages and are not exported. |
| 1156 | ---> |
| 1157 | Otherwise, they are the same. |
| 1158 | |
| 1159 | |
| 1160 | ### Field declarations |
| 1161 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1162 | 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] | 1163 | The name for a quoted string used as label is the string it represents. |
| 1164 | Tne name for an identifier used as a label is the identifier itself. |
| 1165 | Quoted strings and identifiers can be used used interchangeably, with the |
| 1166 | exception of identifiers starting with an underscore '_'. |
| 1167 | The latter represent hidden fields and are treated in a different namespace. |
| 1168 | |
| 1169 | |
| 1170 | ### Alias declarations |
| 1171 | |
| 1172 | An alias declaration binds an identifier to the given expression. |
| 1173 | |
| 1174 | Within the scope of the identifier, it serves as an _alias_ for that |
| 1175 | expression. |
| 1176 | The expression is evaluated in the scope as it was declared. |
| 1177 | |
| 1178 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1179 | ## Expressions |
| 1180 | |
| 1181 | 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] | 1182 | built-in functions to operands. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1183 | |
| 1184 | |
| 1185 | ### Operands |
| 1186 | |
| 1187 | Operands denote the elementary values in an expression. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1188 | An operand may be a literal, a (possibly qualified) identifier denoting |
| 1189 | field, alias, or a parenthesized expression. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1190 | |
| 1191 | ``` |
| 1192 | Operand = Literal | OperandName | ListComprehension | "(" Expression ")" . |
| 1193 | Literal = BasicLit | ListLit | StructLit . |
| 1194 | BasicLit = int_lit | float_lit | string_lit | |
| 1195 | null_lit | bool_lit | bottom_lit | top_lit . |
| 1196 | OperandName = identifier | QualifiedIdent. |
| 1197 | ``` |
| 1198 | |
| 1199 | ### Qualified identifiers |
| 1200 | |
| 1201 | A qualified identifier is an identifier qualified with a package name prefix. |
| 1202 | |
| 1203 | ``` |
| 1204 | QualifiedIdent = PackageName "." identifier . |
| 1205 | ``` |
| 1206 | |
| 1207 | A qualified identifier accesses an identifier in a different package, |
| 1208 | which must be [imported]. |
| 1209 | The identifier must be declared in the [package block] of that package. |
| 1210 | |
| 1211 | ``` |
| 1212 | math.Sin // denotes the Sin function in package math |
| 1213 | ``` |
| 1214 | |
| 1215 | |
| 1216 | ### Primary expressions |
| 1217 | |
| 1218 | Primary expressions are the operands for unary and binary expressions. |
Marcel van Lohuizen | 69139d6 | 2019-01-24 13:46:51 +0100 | [diff] [blame] | 1219 | A default expression is only valid as an operand to a disjunction. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1220 | |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 1221 | <!-- TODO(mpvl) |
| 1222 | Conversion | |
| 1223 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1224 | ``` |
| 1225 | PrimaryExpr = |
| 1226 | Operand | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1227 | PrimaryExpr Selector | |
| 1228 | PrimaryExpr Index | |
| 1229 | PrimaryExpr Slice | |
| 1230 | PrimaryExpr Arguments . |
| 1231 | |
| 1232 | Selector = "." identifier . |
| 1233 | Index = "[" Expression "]" . |
| 1234 | Slice = "[" [ Expression ] ":" [ Expression ] "]" |
| 1235 | Argument = Expression . |
| 1236 | Arguments = "(" [ ( Argument { "," Argument } ) [ "..." ] [ "," ] ] ")" . |
| 1237 | ``` |
| 1238 | <!--- |
| 1239 | Argument = Expression | ( identifer ":" Expression ). |
| 1240 | ---> |
| 1241 | |
| 1242 | ``` |
| 1243 | x |
| 1244 | 2 |
| 1245 | (s + ".txt") |
| 1246 | f(3.1415, true) |
| 1247 | m["foo"] |
| 1248 | s[i : j + 1] |
| 1249 | obj.color |
| 1250 | f.p[i].x |
| 1251 | ``` |
| 1252 | |
| 1253 | |
| 1254 | ### Selectors |
| 1255 | |
| 1256 | For a [primary expression] `x` that is not a [package name], |
| 1257 | the selector expression |
| 1258 | |
| 1259 | ``` |
| 1260 | x.f |
| 1261 | ``` |
| 1262 | |
| 1263 | denotes the field `f` of the value `x`. |
| 1264 | The identifier `f` is called the field selector. |
| 1265 | The type of the selector expression is the type of `f`. |
| 1266 | If `x` is a package name, see the section on [qualified identifiers]. |
| 1267 | |
| 1268 | Otherwise, if `x` is not a struct, or if `f` does not exist in `x`, |
| 1269 | the result of the expression is bottom (an error). |
| 1270 | |
| 1271 | ``` |
| 1272 | T: { |
| 1273 | x: int |
| 1274 | y: 3 |
| 1275 | } |
| 1276 | |
| 1277 | a: T.x // int |
| 1278 | b: T.y // 3 |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1279 | c: T.z // _|_ // field 'z' not found in T |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1280 | ``` |
| 1281 | |
| 1282 | |
| 1283 | ### Index expressions |
| 1284 | |
| 1285 | A primary expression of the form |
| 1286 | |
| 1287 | ``` |
| 1288 | a[x] |
| 1289 | ``` |
| 1290 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1291 | 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] | 1292 | The value `x` is called the index or field name, respectively. |
| 1293 | The following rules apply: |
| 1294 | |
| 1295 | If `a` is not a struct: |
| 1296 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1297 | - the index `x` must be a concrete integer. |
| 1298 | If `x` is a disjunction, the default, if any will be selected without unifying |
| 1299 | `x` with `int` beforehand. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1300 | - the index `x` is in range if `0 <= x < len(a)`, otherwise it is out of range |
| 1301 | |
| 1302 | The result of `a[x]` is |
| 1303 | |
| 1304 | for `a` of list type (including single quoted strings, which are lists of bytes): |
| 1305 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1306 | - the list element at index `x`, if `x` is within range, where only the |
| 1307 | explicitly defined values of an open-ended list are considered |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1308 | - bottom (an error), otherwise |
| 1309 | |
| 1310 | for `a` of string type: |
| 1311 | |
| 1312 | - 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] | 1313 | where `x` may match any byte of the grapheme cluster |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1314 | - bottom (an error), otherwise |
| 1315 | |
| 1316 | for `a` of struct type: |
| 1317 | |
| 1318 | - the value of the field named `x` of struct `a`, if this field exists |
| 1319 | - bottom (an error), otherwise |
| 1320 | |
| 1321 | ``` |
| 1322 | [ 1, 2 ][1] // 2 |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1323 | [ 1, 2 ][2] // _|_ |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1324 | [ 1, 2, ...][2] // _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1325 | "He\u0300?"[0] // "H" |
| 1326 | "He\u0300?"[1] // "e\u0300" |
| 1327 | "He\u0300?"[2] // "e\u0300" |
| 1328 | "He\u0300?"[3] // "e\u0300" |
| 1329 | "He\u0300?"[4] // "?" |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1330 | "He\u0300?"[5] // _|_ |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1331 | ``` |
| 1332 | |
| 1333 | |
| 1334 | ### Slice expressions |
| 1335 | |
| 1336 | Slice expressions construct a substring or slice from a string or list. |
| 1337 | |
| 1338 | For strings or lists, the primary expression |
| 1339 | ``` |
| 1340 | a[low : high] |
| 1341 | ``` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1342 | constructs a substring or slice. The indices `low` and `high` must be |
| 1343 | concrete integers and select |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1344 | which elements of operand `a` appear in the result. |
| 1345 | 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] | 1346 | After slicing the list `a` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1347 | <!-- TODO(jba): how does slicing open lists work? --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1348 | |
| 1349 | ``` |
| 1350 | a := [1, 2, 3, 4, 5] |
| 1351 | s := a[1:4] |
| 1352 | ``` |
| 1353 | the list s has length 3 and elements |
| 1354 | ``` |
| 1355 | s[0] == 2 |
| 1356 | s[1] == 3 |
| 1357 | s[2] == 4 |
| 1358 | ``` |
| 1359 | For convenience, any of the indices may be omitted. |
| 1360 | A missing `low` index defaults to zero; a missing `high` index defaults |
| 1361 | to the length of the sliced operand: |
| 1362 | ``` |
| 1363 | a[2:] // same as a[2 : len(a)] |
| 1364 | a[:3] // same as a[0 : 3] |
| 1365 | a[:] // same as a[0 : len(a)] |
| 1366 | ``` |
| 1367 | |
| 1368 | Indices are in range if `0 <= low <= high <= len(a)`, |
| 1369 | otherwise they are out of range. |
| 1370 | For strings, the indices selects the start of the extended grapheme cluster |
| 1371 | at byte position indicated by the index. |
| 1372 | If any of the slice values is out of range or if `low > high`, the result of |
| 1373 | a slice is bottom (error). |
| 1374 | |
| 1375 | ``` |
| 1376 | "He\u0300?"[:2] // "He\u0300" |
| 1377 | "He\u0300?"[1:2] // "e\u0300" |
| 1378 | "He\u0300?"[4:5] // "e\u0300?" |
| 1379 | ``` |
| 1380 | |
| 1381 | |
| 1382 | The result of a successful slice operation is a value of the same type |
| 1383 | as the operand. |
| 1384 | |
| 1385 | |
| 1386 | ### Operators |
| 1387 | |
| 1388 | Operators combine operands into expressions. |
| 1389 | |
| 1390 | ``` |
| 1391 | Expression = UnaryExpr | Expression binary_op Expression . |
| 1392 | UnaryExpr = PrimaryExpr | unary_op UnaryExpr . |
| 1393 | |
| 1394 | binary_op = "|" | "&" | "||" | "&&" | rel_op | add_op | mul_op | ".." . |
| 1395 | rel_op = "==" | "!=" | "<" | "<=" | ">" | ">=" . |
| 1396 | add_op = "+" | "-" . |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1397 | mul_op = "*" | "/" | "%" | "div" | "mod" | "quo" | "rem" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1398 | |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1399 | unary_op = "+" | "-" | "!" | "*" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1400 | ``` |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1401 | <!-- TODO: consider adding unary_op: "<" | "<=" | ">" | ">=" --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1402 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1403 | Comparisons are discussed [elsewhere](#Comparison-operators). |
| 1404 | For other binary operators, the operand |
| 1405 | types must unify. |
| 1406 | <!-- TODO: durations |
| 1407 | unless the operation involves durations. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1408 | |
| 1409 | Except for duration operations, if one operand is an untyped [literal] and the |
| 1410 | other operand is not, the constant is [converted] to the type of the other |
| 1411 | operand. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1412 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1413 | |
| 1414 | |
| 1415 | #### Operator precedence |
| 1416 | |
| 1417 | Unary operators have the highest precedence. |
| 1418 | |
| 1419 | There are eight precedence levels for binary operators. |
| 1420 | The `..` operator (range) binds strongest, followed by |
| 1421 | multiplication operators, addition operators, comparison operators, |
| 1422 | `&&` (logical AND), `||` (logical OR), `&` (unification), |
| 1423 | and finally `|` (disjunction): |
| 1424 | |
| 1425 | ``` |
| 1426 | Precedence Operator |
| 1427 | 8 .. |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1428 | 7 * / % div mod quo rem |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1429 | 6 + - |
| 1430 | 5 == != < <= > >= |
| 1431 | 4 && |
| 1432 | 3 || |
| 1433 | 2 & |
| 1434 | 1 | |
| 1435 | ``` |
| 1436 | |
| 1437 | Binary operators of the same precedence associate from left to right. |
| 1438 | For instance, `x / y * z` is the same as `(x / y) * z`. |
| 1439 | |
| 1440 | ``` |
| 1441 | +x |
| 1442 | 23 + 3*x[i] |
| 1443 | x <= f() |
| 1444 | f() || g() |
| 1445 | x == y+1 && y == z-1 |
| 1446 | 2 | int |
| 1447 | { a: 1 } & { b: 2 } |
| 1448 | ``` |
| 1449 | |
| 1450 | #### Arithmetic operators |
| 1451 | |
| 1452 | Arithmetic operators apply to numeric values and yield a result of the same type |
| 1453 | as the first operand. The three of the four standard arithmetic operators |
| 1454 | `(+, -, *)` apply to integer and decimal floating-point types; |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1455 | `+` and `*` also apply to lists and strings. |
| 1456 | `/` and `%` only apply to decimal floating-point types and |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1457 | `div`, `mod`, `quo`, and `rem` only apply to integer types. |
| 1458 | |
| 1459 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1460 | + sum integers, floats, lists, strings, bytes |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1461 | - difference integers, floats |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1462 | * product integers, floats, lists, strings, bytes |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1463 | / quotient floats |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1464 | % remainder floats |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1465 | div division integers |
| 1466 | mod modulo integers |
| 1467 | quo quotient integers |
| 1468 | rem remainder integers |
| 1469 | ``` |
| 1470 | |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1471 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1472 | #### Integer operators |
| 1473 | |
| 1474 | For two integer values `x` and `y`, |
| 1475 | 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] | 1476 | implement Euclidean division and |
| 1477 | satisfy the following relationship: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1478 | |
| 1479 | ``` |
| 1480 | r = x - y*q with 0 <= r < |y| |
| 1481 | ``` |
| 1482 | where `|y|` denotes the absolute value of `y`. |
| 1483 | |
| 1484 | ``` |
| 1485 | x y x div y x mod y |
| 1486 | 5 3 1 2 |
| 1487 | -5 3 -2 1 |
| 1488 | 5 -3 -1 2 |
| 1489 | -5 -3 2 1 |
| 1490 | ``` |
| 1491 | |
| 1492 | For two integer values `x` and `y`, |
| 1493 | 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] | 1494 | implement truncated division and |
| 1495 | satisfy the following relationship: |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1496 | |
| 1497 | ``` |
| 1498 | x = q*y + r and |r| < |y| |
| 1499 | ``` |
| 1500 | |
| 1501 | with `x quo y` truncated towards zero. |
| 1502 | |
| 1503 | ``` |
| 1504 | x y x quo y x rem y |
| 1505 | 5 3 1 2 |
| 1506 | -5 3 -1 -2 |
| 1507 | 5 -3 -1 2 |
| 1508 | -5 -3 1 -2 |
| 1509 | ``` |
| 1510 | |
| 1511 | A zero divisor in either case results in bottom (an error). |
| 1512 | |
| 1513 | For integer operands, the unary operators `+` and `-` are defined as follows: |
| 1514 | |
| 1515 | ``` |
| 1516 | +x is 0 + x |
| 1517 | -x negation is 0 - x |
| 1518 | ``` |
| 1519 | |
| 1520 | |
| 1521 | #### Decimal floating-point operators |
| 1522 | |
| 1523 | For decimal floating-point numbers, `+x` is the same as `x`, |
| 1524 | while -x is the negation of x. |
| 1525 | 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] | 1526 | <!-- TODO: consider making it +/- Inf --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1527 | |
| 1528 | An implementation may combine multiple floating-point operations into a single |
| 1529 | fused operation, possibly across statements, and produce a result that differs |
| 1530 | from the value obtained by executing and rounding the instructions individually. |
| 1531 | |
| 1532 | |
| 1533 | #### List operators |
| 1534 | |
| 1535 | Lists can be concatenated using the `+` operator. |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1536 | For lists `a` and `b`, |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1537 | ``` |
| 1538 | a + b |
| 1539 | ``` |
| 1540 | will produce an open list if `b` is open. |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1541 | 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] | 1542 | |
| 1543 | ``` |
| 1544 | [ 1, 2 ] + [ 3, 4 ] // [ 1, 2, 3, 4 ] |
| 1545 | [ 1, 2, ... ] + [ 3, 4 ] // [ 1, 2, 3, 4 ] |
| 1546 | [ 1, 2 ] + [ 3, 4, ... ] // [ 1, 2, 3, 4, ... ] |
| 1547 | ``` |
| 1548 | |
Marcel van Lohuizen | 13e36bd | 2019-02-01 09:59:18 +0100 | [diff] [blame] | 1549 | Lists can be multiplied with a positive `int` using the `*` operator |
| 1550 | to create a repeated the list by the indicated number. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1551 | ``` |
| 1552 | 3*[1,2] // [1, 2, 1, 2, 1, 2] |
Marcel van Lohuizen | 13e36bd | 2019-02-01 09:59:18 +0100 | [diff] [blame] | 1553 | 3*[1, 2, ...] // [1, 2, 1, 2, 1 ,2] |
| 1554 | [byte]*4 // [byte, byte, byte, byte] |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1555 | ``` |
Marcel van Lohuizen | 08466f8 | 2019-02-01 09:09:09 +0100 | [diff] [blame] | 1556 | |
| 1557 | <!-- TODO(mpvl): should we allow multiplication with a range? |
| 1558 | If so, how does one specify a list with a range of possible lengths? |
| 1559 | |
| 1560 | Suggestion from jba: |
| 1561 | Multiplication should distribute over disjunction, |
| 1562 | so int(1)..int(3) * [x] = [x] | [x, x] | [x, x, x]. |
| 1563 | The hard part is figuring out what 1..3 * [x] means, |
| 1564 | since 1..3 includes many floats. |
| 1565 | (mpvl: could constrain arguments to parameter types, but needs to be |
| 1566 | done consistently.) |
| 1567 | --> |
| 1568 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1569 | |
| 1570 | #### String operators |
| 1571 | |
| 1572 | Strings can be concatenated using the `+` operator: |
| 1573 | ``` |
| 1574 | s := "hi " + name + " and good bye" |
| 1575 | ``` |
| 1576 | String addition creates a new string by concatenating the operands. |
| 1577 | |
| 1578 | A string can be repeated by multiplying it: |
| 1579 | |
| 1580 | ``` |
| 1581 | s: "etc. "*3 // "etc. etc. etc. " |
| 1582 | ``` |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1583 | <!-- jba: Do these work for byte sequences? If not, why not? --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1584 | |
| 1585 | ##### Comparison operators |
| 1586 | |
| 1587 | Comparison operators compare two operands and yield an untyped boolean value. |
| 1588 | |
| 1589 | ``` |
| 1590 | == equal |
| 1591 | != not equal |
| 1592 | < less |
| 1593 | <= less or equal |
| 1594 | > greater |
| 1595 | >= greater or equal |
| 1596 | ``` |
| 1597 | |
| 1598 | In any comparison, the types of the two operands must unify. |
| 1599 | |
| 1600 | The equality operators `==` and `!=` apply to operands that are comparable. |
| 1601 | The ordering operators `<`, `<=`, `>`, and `>=` apply to operands that are ordered. |
| 1602 | These terms and the result of the comparisons are defined as follows: |
| 1603 | |
| 1604 | - Boolean values are comparable. |
| 1605 | Two boolean values are equal if they are either both true or both false. |
| 1606 | - Integer values are comparable and ordered, in the usual way. |
| 1607 | - Floating-point values are comparable and ordered, as per the definitions |
| 1608 | for binary coded decimals in the IEEE-754-2008 standard. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1609 | - String values are comparable and ordered, lexically byte-wise after |
| 1610 | normalization to Unicode normal form NFC. |
| 1611 | - Struct are not comparable. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1612 | Two struct values are equal if their corresponding non-blank fields are equal. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1613 | - Lists are comparable. |
| 1614 | Two list values are equal if their corresponding elements are equal. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1615 | ``` |
| 1616 | c: 3 < 4 |
| 1617 | |
| 1618 | x: int |
| 1619 | y: int |
| 1620 | |
| 1621 | b3: x == y // b3 has type bool |
| 1622 | ``` |
| 1623 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1624 | <!-- jba |
| 1625 | I think I know what `3 < a` should mean if |
| 1626 | |
| 1627 | a: 1..5 |
| 1628 | |
| 1629 | It should be a constraint on `a` that can be evaluated once `a`'s value is known more precisely. |
| 1630 | |
| 1631 | But what does `3 < 1..5` mean? We'll never get more information, so it must have a definite value. |
| 1632 | --> |
| 1633 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1634 | #### Logical operators |
| 1635 | |
| 1636 | Logical operators apply to boolean values and yield a result of the same type |
| 1637 | as the operands. The right operand is evaluated conditionally. |
| 1638 | |
| 1639 | ``` |
| 1640 | && conditional AND p && q is "if p then q else false" |
| 1641 | || conditional OR p || q is "if p then true else q" |
| 1642 | ! NOT !p is "not p" |
| 1643 | ``` |
| 1644 | |
| 1645 | |
| 1646 | <!-- |
| 1647 | ### TODO TODO TODO |
| 1648 | |
| 1649 | 3.14 / 0.0 // illegal: division by zero |
| 1650 | Illegal conversions always apply to CUE. |
| 1651 | |
| 1652 | 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. |
| 1653 | --> |
| 1654 | |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 1655 | <!--- TODO(mpvl): conversions |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1656 | ### Conversions |
| 1657 | Conversions are expressions of the form `T(x)` where `T` and `x` are |
| 1658 | expressions. |
| 1659 | The result is always an instance of `T`. |
| 1660 | |
| 1661 | ``` |
| 1662 | Conversion = Expression "(" Expression [ "," ] ")" . |
| 1663 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 1664 | ---> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1665 | <!--- |
| 1666 | |
| 1667 | A literal value `x` can be converted to type T if `x` is representable by a |
| 1668 | value of `T`. |
| 1669 | |
| 1670 | As a special case, an integer literal `x` can be converted to a string type |
| 1671 | using the same rule as for non-constant x. |
| 1672 | |
| 1673 | Converting a literal yields a typed value as result. |
| 1674 | |
| 1675 | ``` |
| 1676 | uint(iota) // iota value of type uint |
| 1677 | float32(2.718281828) // 2.718281828 of type float32 |
| 1678 | complex128(1) // 1.0 + 0.0i of type complex128 |
| 1679 | float32(0.49999999) // 0.5 of type float32 |
| 1680 | float64(-1e-1000) // 0.0 of type float64 |
| 1681 | string('x') // "x" of type string |
| 1682 | string(0x266c) // "♬" of type string |
| 1683 | MyString("foo" + "bar") // "foobar" of type MyString |
| 1684 | string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant |
| 1685 | (*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type |
| 1686 | int(1.2) // illegal: 1.2 cannot be represented as an int |
| 1687 | string(65.0) // illegal: 65.0 is not an integer constant |
| 1688 | ``` |
| 1689 | ---> |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 1690 | <!--- |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1691 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1692 | 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] | 1693 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1694 | 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] | 1695 | `x` can be converted to a value `x'` of `T`'s type, and |
| 1696 | `x'` is an instance of `T`. |
| 1697 | A value `x` can be converted to the type of `T` in any of these cases: |
| 1698 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1699 | - `x` is a struct and is subsumed by `T`. |
| 1700 | - `x` and `T` are both integer or floating points. |
| 1701 | - `x` is an integer or a byte sequence and `T` is a string. |
| 1702 | - `x` is a string and `T` is a byte sequence. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1703 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1704 | Specific rules apply to conversions between numeric types, structs, |
| 1705 | or to and from a string type. These conversions may change the representation |
| 1706 | of `x`. |
| 1707 | All other conversions only change the type but not the representation of x. |
| 1708 | |
| 1709 | |
| 1710 | #### Conversions between numeric ranges |
| 1711 | For the conversion of numeric values, the following rules apply: |
| 1712 | |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1713 | 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] | 1714 | provided that it is within range. |
| 1715 | 2. When converting a decimal floating-point number to an integer, the fraction |
| 1716 | is discarded (truncation towards zero). TODO: or disallow truncating? |
| 1717 | |
| 1718 | ``` |
| 1719 | a: uint16(int(1000)) // uint16(1000) |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1720 | b: uint8(1000) // _|_ // overflow |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1721 | c: int(2.5) // 2 TODO: TBD |
| 1722 | ``` |
| 1723 | |
| 1724 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1725 | #### Conversions to and from a string type |
| 1726 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1727 | Converting a list of bytes to a string type yields a string whose successive |
| 1728 | bytes are the elements of the slice. |
| 1729 | Invalid UTF-8 is converted to `"\uFFFD"`. |
| 1730 | |
| 1731 | ``` |
| 1732 | string('hell\xc3\xb8') // "hellø" |
| 1733 | string(bytes([0x20])) // " " |
| 1734 | ``` |
| 1735 | |
| 1736 | As string value is always convertible to a list of bytes. |
| 1737 | |
| 1738 | ``` |
| 1739 | bytes("hellø") // 'hell\xc3\xb8' |
| 1740 | bytes("") // '' |
| 1741 | ``` |
| 1742 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1743 | #### Conversions between list types |
| 1744 | |
| 1745 | Conversions between list types are possible only if `T` strictly subsumes `x` |
| 1746 | and the result will be the unification of `T` and `x`. |
| 1747 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1748 | If we introduce named types this would be different from IP & [10, ...] |
| 1749 | |
| 1750 | Consider removing this until it has a different meaning. |
| 1751 | |
| 1752 | ``` |
| 1753 | IP: 4*[byte] |
| 1754 | Private10: IP([10, ...]) // [10, byte, byte, byte] |
| 1755 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1756 | |
Marcel van Lohuizen | 75cb003 | 2019-01-11 12:10:48 +0100 | [diff] [blame] | 1757 | #### Conversions between struct types |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1758 | |
| 1759 | A conversion from `x` to `T` |
| 1760 | is applied using the following rules: |
| 1761 | |
| 1762 | 1. `x` must be an instance of `T`, |
| 1763 | 2. all fields defined for `x` that are not defined for `T` are removed from |
| 1764 | the result of the conversion, recursively. |
| 1765 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1766 | <!-- 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] | 1767 | mpvl: they are not, x must be an instance of T, in which case x == T&x, |
| 1768 | so unification would be unnecessary. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1769 | --> |
Marcel van Lohuizen | a3f0097 | 2019-02-01 11:10:39 +0100 | [diff] [blame] | 1770 | <!-- |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1771 | ``` |
| 1772 | T: { |
| 1773 | a: { b: 1..10 } |
| 1774 | } |
| 1775 | |
| 1776 | x1: { |
| 1777 | a: { b: 8, c: 10 } |
| 1778 | d: 9 |
| 1779 | } |
| 1780 | |
| 1781 | c1: T(x1) // { a: { b: 8 } } |
Marcel van Lohuizen | 6f0faec | 2018-12-16 10:42:42 +0100 | [diff] [blame] | 1782 | c2: T({}) // _|_ // missing field 'a' in '{}' |
| 1783 | 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] | 1784 | ``` |
Marcel van Lohuizen | d340e8d | 2019-01-30 16:57:39 +0100 | [diff] [blame] | 1785 | --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1786 | |
| 1787 | ### Calls |
| 1788 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1789 | Calls can be made to core library functions, called builtins. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1790 | Given an expression `f` of function type F, |
| 1791 | ``` |
| 1792 | f(a1, a2, … an) |
| 1793 | ``` |
| 1794 | calls `f` with arguments a1, a2, … an. Arguments must be expressions |
| 1795 | of which the values are an instance of the parameter types of `F` |
| 1796 | and are evaluated before the function is called. |
| 1797 | |
| 1798 | ``` |
| 1799 | a: math.Atan2(x, y) |
| 1800 | ``` |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1801 | |
| 1802 | 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] | 1803 | order. |
| 1804 | After they are evaluated, the parameters of the call are passed by value |
| 1805 | to the function and the called function begins execution. |
| 1806 | The return parameters |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1807 | of the function are passed by value back to the calling function when the |
| 1808 | function returns. |
| 1809 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1810 | |
| 1811 | ### Comprehensions |
| 1812 | |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 1813 | Lists and fields can be constructed using comprehensions. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1814 | |
| 1815 | Each define a clause sequence that consists of a sequence of `for`, `if`, and |
| 1816 | `let` clauses, nesting from left to right. |
| 1817 | The `for` and `let` clauses each define a new scope in which new values are |
| 1818 | bound to be available for the next clause. |
| 1819 | |
| 1820 | The `for` clause binds the defined identifiers, on each iteration, to the next |
| 1821 | value of some iterable value in a new scope. |
| 1822 | A `for` clause may bind one or two identifiers. |
| 1823 | If there is one identifier, it binds it to the value, for instance |
| 1824 | a list element, a struct field value or a range element. |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1825 | 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] | 1826 | if available, and the second will be the value. |
| 1827 | |
| 1828 | An `if` clause, or guard, specifies an expression that terminates the current |
| 1829 | iteration if it evaluates to false. |
| 1830 | |
| 1831 | The `let` clause binds the result of an expression to the defined identifier |
| 1832 | in a new scope. |
| 1833 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1834 | 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] | 1835 | sequence is reached. |
| 1836 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1837 | _List comprehensions_ specify a single expression that is evaluated and included |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1838 | in the list for each completed iteration. |
| 1839 | |
Marcel van Lohuizen | 5fee32f | 2019-01-21 22:18:48 +0100 | [diff] [blame] | 1840 | _Field comprehensions_ follow a `Field` with a clause sequence, where the |
| 1841 | label and value of the field are evaluated for each iteration. |
| 1842 | The label must be an identifier or interpreted_string_lit, where the |
| 1843 | later may be a string interpolation that refers to the identifiers defined |
| 1844 | in the clauses. |
| 1845 | 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] | 1846 | |
| 1847 | ``` |
| 1848 | ComprehensionDecl = Field [ "<-" ] Clauses . |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1849 | ListComprehension = "[" Expression [ "<-" ] Clauses "]" . |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1850 | |
| 1851 | Clauses = Clause { Clause } . |
| 1852 | Clause = ForClause | GuardClause | LetClause . |
| 1853 | ForClause = "for" identifier [ ", " identifier] "in" Expression . |
| 1854 | GuardClause = "if" Expression . |
| 1855 | LetClause = "let" identifier "=" Expression . |
| 1856 | ``` |
| 1857 | |
| 1858 | ``` |
| 1859 | a: [1, 2, 3, 4] |
| 1860 | b: [ x+1 for x in a if x > 1] // [3, 4, 5] |
| 1861 | |
Marcel van Lohuizen | 66db920 | 2018-12-17 19:02:08 +0100 | [diff] [blame] | 1862 | 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] | 1863 | d: { "1": 2, "2": 3, "3": 4 } |
| 1864 | ``` |
| 1865 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1866 | |
| 1867 | ### String interpolation |
| 1868 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1869 | String interpolation allows constructing strings by replacing placeholder |
| 1870 | expressions with their string representation. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1871 | String interpolation may be used in single- and double-quoted strings, as well |
| 1872 | as their multiline equivalent. |
| 1873 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 1874 | A placeholder consists of "\(" followed by an expression and a ")". The |
| 1875 | 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] | 1876 | |
| 1877 | ``` |
| 1878 | a: "World" |
| 1879 | b: "Hello \( a )!" // Hello World! |
| 1880 | ``` |
| 1881 | |
| 1882 | |
| 1883 | ## Builtin Functions |
| 1884 | |
| 1885 | Built-in functions are predeclared. They are called like any other function. |
| 1886 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1887 | |
| 1888 | ### `len` |
| 1889 | |
| 1890 | The built-in function `len` takes arguments of various types and return |
| 1891 | a result of type int. |
| 1892 | |
| 1893 | ``` |
| 1894 | Argument type Result |
| 1895 | |
| 1896 | string string length in bytes |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1897 | bytes length of byte sequence |
| 1898 | list list length, smallest length for an open list |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1899 | struct number of distinct fields |
| 1900 | ``` |
Marcel van Lohuizen | 45163fa | 2019-01-22 15:53:32 +0100 | [diff] [blame] | 1901 | |
| 1902 | ``` |
| 1903 | Expression Result |
| 1904 | len("Hellø") 6 |
| 1905 | len([1, 2, 3]) 3 |
| 1906 | len([1, 2, ...]) 2 |
| 1907 | len({a:1, b:2}) 2 |
| 1908 | ``` |
| 1909 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1910 | |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 1911 | ## Cycles |
| 1912 | |
| 1913 | Implementations are required to interpret or reject cycles encountered |
| 1914 | during evaluation according to the rules in this section. |
| 1915 | |
| 1916 | |
| 1917 | ### Reference cycles |
| 1918 | |
| 1919 | A _reference cycle_ occurs if a field references itself, either directly or |
| 1920 | indirectly. |
| 1921 | |
| 1922 | ``` |
| 1923 | // x references itself |
| 1924 | x: x |
| 1925 | |
| 1926 | // indirect cycles |
| 1927 | b: c |
| 1928 | c: d |
| 1929 | d: b |
| 1930 | ``` |
| 1931 | |
| 1932 | Implementations should report these as an error except in the following cases: |
| 1933 | |
| 1934 | |
| 1935 | #### Expressions that unify an atom with an expression |
| 1936 | |
| 1937 | An expression of the form `a & e`, where `a` is an atom |
| 1938 | and `e` is an expression, always evaluates to `a` or bottom. |
| 1939 | As it does not matter how we fail, we can assume the result to be `a` |
| 1940 | and validate after the field in which the expression occurs has been evaluated |
| 1941 | that `a == e`. |
| 1942 | |
| 1943 | ``` |
| 1944 | // Config Evaluates to |
| 1945 | x: { x: { |
| 1946 | a: b + 100 a: _|_ // cycle detected |
| 1947 | b: a - 100 b: _|_ // cycle detected |
| 1948 | } } |
| 1949 | |
| 1950 | y: x & { y: { |
| 1951 | a: 200 a: 200 // asserted that 200 == b + 100 |
| 1952 | b: 100 |
| 1953 | } } |
| 1954 | ``` |
| 1955 | |
| 1956 | |
| 1957 | #### Field values |
| 1958 | |
| 1959 | A field value of the form `r & v`, |
| 1960 | where `r` evaluates to a reference cycle and `v` is a value, |
| 1961 | evaluates to `v`. |
| 1962 | Unification is idempotent and unifying a value with itself ad infinitum, |
| 1963 | which is what the cycle represents, results in this value. |
| 1964 | Implementations should detect cycles of this kind, ignore `r`, |
| 1965 | and take `v` as the result of unification. |
| 1966 | <!-- Tomabechi's graph unification algorithm |
| 1967 | can detect such cycles at near-zero cost. --> |
| 1968 | |
| 1969 | ``` |
| 1970 | Configuration Evaluated |
| 1971 | // c Cycles in nodes of type struct evaluate |
| 1972 | // ↙︎ ↖ to the fixed point of unifying their |
| 1973 | // a → b values ad infinitum. |
| 1974 | |
| 1975 | a: b & { x: 1 } // a: { x: 1, y: 2, z: 3 } |
| 1976 | b: c & { y: 2 } // b: { x: 1, y: 2, z: 3 } |
| 1977 | c: a & { z: 3 } // c: { x: 1, y: 2, z: 3 } |
| 1978 | |
| 1979 | // resolve a b & {x:1} |
| 1980 | // substitute b c & {y:2} & {x:1} |
| 1981 | // substitute c a & {z:3} & {y:2} & {x:1} |
| 1982 | // eliminate a (cycle) {z:3} & {y:2} & {x:1} |
| 1983 | // simplify {x:1,y:2,z:3} |
| 1984 | ``` |
| 1985 | |
| 1986 | This rule also applies to field values that are disjunctions of unification |
| 1987 | operations of the above form. |
| 1988 | |
| 1989 | ``` |
| 1990 | a: b&{x:1} | {y:1} // {x:1,y:3,z:2} | {y:1} |
| 1991 | b: {x:2} | c&{z:2} // {x:2} | {x:1,y:3,z:2} |
| 1992 | c: a&{y:3} | {z:3} // {x:1,y:3,z:2} | {z:3} |
| 1993 | |
| 1994 | |
| 1995 | // resolving a b&{x:1} | {y:1} |
| 1996 | // substitute b ({x:2} | c&{z:2})&{x:1} | {y:1} |
| 1997 | // simplify c&{z:2}&{x:1} | {y:1} |
| 1998 | // substitute c (a&{y:3} | {z:3})&{z:2}&{x:1} | {y:1} |
| 1999 | // simplify a&{y:3}&{z:2}&{x:1} | {y:1} |
| 2000 | // eliminate a (cycle) {y:3}&{z:2}&{x:1} | {y:1} |
| 2001 | // expand {x:1,y:3,z:2} | {y:1} |
| 2002 | ``` |
| 2003 | |
| 2004 | Note that all nodes that form a reference cycle to form a struct will evaluate |
| 2005 | to the same value. |
| 2006 | If a field value is a disjunction, any element that is part of a cycle will |
| 2007 | evaluate to this value. |
| 2008 | |
| 2009 | |
| 2010 | ### Structural cycles |
| 2011 | |
| 2012 | CUE disallows infinite structures. |
| 2013 | Implementations must report an error when encountering such declarations. |
| 2014 | |
| 2015 | <!-- for instance using an occurs check --> |
| 2016 | |
| 2017 | ``` |
| 2018 | // Disallowed: a list of infinite length with all elements being 1. |
| 2019 | list: { |
| 2020 | head: 1 |
| 2021 | tail: list |
| 2022 | } |
| 2023 | |
| 2024 | // Disallowed: another infinite structure (a:{b:{d:{b:{d:{...}}}}}, ...). |
| 2025 | a: { |
| 2026 | b: c |
| 2027 | } |
| 2028 | c: { |
| 2029 | d: a |
| 2030 | } |
| 2031 | ``` |
| 2032 | |
| 2033 | It is allowed for a value to define an infinite set of possibilities |
| 2034 | without evaluating to an infinite structure itself. |
| 2035 | |
| 2036 | ``` |
| 2037 | // List defines a list of arbitrary length (default null). |
| 2038 | List: *null | { |
| 2039 | head: _ |
| 2040 | tail: List |
| 2041 | } |
| 2042 | ``` |
| 2043 | |
| 2044 | <!-- |
Marcel van Lohuizen | 7f48df7 | 2019-02-01 17:24:59 +0100 | [diff] [blame] | 2045 | Consider banning any construct that makes CUE not having a linear |
| 2046 | running time expressed in the number of nodes in the output. |
| 2047 | |
| 2048 | This would require restricting constructs like: |
| 2049 | |
| 2050 | (fib&{n:2}).out |
| 2051 | |
| 2052 | fib: { |
| 2053 | n: int |
| 2054 | |
| 2055 | out: (fib&{n:n-2}).out + (fib&{n:n-1}).out if n >= 2 |
| 2056 | out: fib({n:n-2}).out + fib({n:n-1}).out if n >= 2 |
| 2057 | out: n if n < 2 |
| 2058 | } |
| 2059 | |
| 2060 | --> |
| 2061 | <!-- |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2062 | ### Unused fields |
| 2063 | |
| 2064 | TODO: rules for detection of unused fields |
| 2065 | |
| 2066 | 1. Any alias value must be used |
| 2067 | --> |
| 2068 | |
| 2069 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2070 | ## Modules, instances, and packages |
| 2071 | |
| 2072 | CUE configurations are constructed combining _instances_. |
| 2073 | An instance, in turn, is constructed from one or more source files belonging |
| 2074 | to the same _package_ that together declare the data representation. |
| 2075 | Elements of this data representation may be exported and used |
| 2076 | in other instances. |
| 2077 | |
| 2078 | ### Source file organization |
| 2079 | |
| 2080 | Each source file consists of an optional package clause defining collection |
| 2081 | of files to which it belongs, |
| 2082 | followed by a possibly empty set of import declarations that declare |
| 2083 | packages whose contents it wishes to use, followed by a possibly empty set of |
| 2084 | declarations. |
| 2085 | |
| 2086 | |
| 2087 | ``` |
| 2088 | SourceFile = [ PackageClause "," ] { ImportDecl "," } { TopLevelDecl "," } . |
| 2089 | ``` |
| 2090 | |
| 2091 | ### Package clause |
| 2092 | |
| 2093 | A package clause is an optional clause that defines the package to which |
| 2094 | a source file the file belongs. |
| 2095 | |
| 2096 | ``` |
| 2097 | PackageClause = "package" PackageName . |
| 2098 | PackageName = identifier . |
| 2099 | ``` |
| 2100 | |
| 2101 | The PackageName must not be the blank identifier. |
| 2102 | |
| 2103 | ``` |
| 2104 | package math |
| 2105 | ``` |
| 2106 | |
| 2107 | ### Modules and instances |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2108 | 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] | 2109 | |
| 2110 | All source files within a module with the same package belong to the same |
| 2111 | package. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2112 | <!-- jba: I can't make sense of the above sentence. --> |
| 2113 | A module may define multiple packages. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2114 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2115 | An _instance_ of a package is any subset of files belonging |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2116 | to the same package. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2117 | <!-- jba: Are you saying that --> |
| 2118 | <!-- if I have a package with files a, b and c, then there are 8 instances of --> |
| 2119 | <!-- that package, some of which are {a, b}, {c}, {b, c}, and so on? What's the --> |
| 2120 | <!-- purpose of that definition? --> |
| 2121 | It is interpreted as the concatenation of these files. |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2122 | |
| 2123 | An implementation may impose conventions on the layout of package files |
| 2124 | to determine which files of a package belongs to an instance. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2125 | 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] | 2126 | belonging to a directory and all its ancestors. |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2127 | <!-- 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] | 2128 | |
| 2129 | ### Import declarations |
| 2130 | |
| 2131 | An import declaration states that the source file containing the declaration |
| 2132 | depends on definitions of the _imported_ package (§Program initialization and |
| 2133 | execution) and enables access to exported identifiers of that package. |
| 2134 | The import names an identifier (PackageName) to be used for access and an |
| 2135 | ImportPath that specifies the package to be imported. |
| 2136 | |
| 2137 | ``` |
| 2138 | ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) . |
| 2139 | ImportSpec = [ "." | PackageName ] ImportPath . |
| 2140 | ImportPath = `"` { unicode_value } `"` . |
| 2141 | ``` |
| 2142 | |
| 2143 | The PackageName is used in qualified identifiers to access exported identifiers |
| 2144 | of the package within the importing source file. |
| 2145 | It is declared in the file block. |
| 2146 | If the PackageName is omitted, it defaults to the identifier specified in the |
| 2147 | package clause of the imported instance. |
| 2148 | If an explicit period (.) appears instead of a name, all the instances's |
| 2149 | exported identifiers declared in that instances's package block will be declared |
| 2150 | in the importing source file's file block |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2151 | and must be accessed without a qualifier. |
| 2152 | <!-- jba: Can you omit this feature? It's likely to only decrease readability, |
| 2153 | as we know from Go. --> |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2154 | |
| 2155 | The interpretation of the ImportPath is implementation-dependent but it is |
| 2156 | typically either the path of a builtin package or a fully qualifying location |
| 2157 | of an instance within a source code repository. |
| 2158 | |
| 2159 | Implementation restriction: An interpreter may restrict ImportPaths to non-empty |
| 2160 | strings using only characters belonging to Unicode's L, M, N, P, and S general |
| 2161 | categories (the Graphic characters without spaces) and may also exclude the |
| 2162 | characters !"#$%&'()*,:;<=>?[\]^`{|} and the Unicode replacement character |
| 2163 | U+FFFD. |
| 2164 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 2165 | Assume we have package containing the package clause "package math", |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2166 | which exports function Sin at the path identified by "lib/math". |
| 2167 | This table illustrates how Sin is accessed in files |
| 2168 | that import the package after the various types of import declaration. |
| 2169 | |
| 2170 | ``` |
| 2171 | Import declaration Local name of Sin |
| 2172 | |
| 2173 | import "lib/math" math.Sin |
| 2174 | import m "lib/math" m.Sin |
| 2175 | import . "lib/math" Sin |
| 2176 | ``` |
| 2177 | |
| 2178 | An import declaration declares a dependency relation between the importing and |
| 2179 | imported package. It is illegal for a package to import itself, directly or |
| 2180 | indirectly, or to directly import a package without referring to any of its |
| 2181 | exported identifiers. |
| 2182 | |
| 2183 | |
| 2184 | ### An example package |
| 2185 | |
| 2186 | TODO |
| 2187 | |
Marcel van Lohuizen | 6713ae2 | 2019-01-26 14:42:25 +0100 | [diff] [blame] | 2188 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2189 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2190 | |
Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 2191 | |