@@ -162,9 +162,9 @@ CUE programs may omit most of these commas using the following two rules:
162
162
When the input is broken into tokens, a comma is automatically inserted into
163
163
the token stream immediately after a line's final token if that token is
164
164
165
- - an identifier
166
- - null, true, false, bottom, or an integer, floating-point, or string literal
167
- - one of the characters ), ] , or }
165
+ - an identifier, keyword, or bottom
166
+ - a number or string literal, including an interpolation
167
+ - one of the characters ` ) ` , ` ] ` , ` } ` , or ` ? `
168
168
169
169
170
170
Although commas are automatically inserted, the parser will require
@@ -257,23 +257,15 @@ TODO:
257
257
-->
258
258
259
259
260
- #### Arithmetic
261
-
262
- The following pseudo keywords can be used as operators in expressions.
263
-
264
- ```
265
- div mod quo rem
266
- ```
267
-
268
260
### Operators and punctuation
269
261
270
262
The following character sequences represent operators and punctuation:
271
263
272
264
```
273
- + div && == < = ( )
274
- - mod || != > : { }
275
- * quo & =~ <= ? [ ] ,
276
- / rem | !~ >= ! _|_ ... .
265
+ + && == < = ( )
266
+ - || != > : { }
267
+ * & =~ <= ? [ ] ,
268
+ / | !~ >= ! _|_ ... .
277
269
```
278
270
<!--
279
271
Free tokens: ; ~ ^
@@ -286,19 +278,9 @@ Free tokens: ; ~ ^
286
278
-->
287
279
288
280
289
- ### Integer literals
281
+ ### Numeric literals
290
282
291
- An integer literal is a sequence of digits representing an integer value.
292
- An optional prefix sets a non-decimal base: 0o for octal,
293
- 0x or 0X for hexadecimal, and 0b for binary.
294
- In hexadecimal literals, letters a-f and A-F represent values 10 through 15.
295
- All integers allow interstitial underscores "_ ";
296
- these have no meaning and are solely for readability.
297
-
298
- Decimal integers may have a SI or IEC multiplier.
299
- Multipliers can be used with fractional numbers.
300
- When multiplying a fraction by a multiplier, the result is truncated
301
- towards zero if it is not an integer.
283
+ There are several kinds of numeric literals.
302
284
303
285
```
304
286
int_lit = decimal_lit | si_lit | octal_lit | binary_lit | hex_lit .
@@ -316,23 +298,30 @@ float_lit = decimals "." [ decimals ] [ exponent ] |
316
298
"." decimals [ exponent ].
317
299
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
318
300
```
319
- <!--
320
- TODO: consider allowing Exo (and up), if not followed by a sign
321
- or number. Alternatively one could only allow Ei, Yi, and Zi.
322
- -->
301
+
302
+ An _ integer literal_ is a sequence of digits representing an integer value.
303
+ An optional prefix sets a non-decimal base: 0o for octal,
304
+ 0x or 0X for hexadecimal, and 0b for binary.
305
+ In hexadecimal literals, letters a-f and A-F represent values 10 through 15.
306
+ All integers allow interstitial underscores "_ ";
307
+ these have no meaning and are solely for readability.
308
+
309
+ Integer literals may have an SI or IEC multiplier.
310
+ Multipliers can be used with fractional numbers.
311
+ When multiplying a fraction by a multiplier, the result is truncated
312
+ towards zero if it is not an integer.
323
313
324
314
```
325
315
42
326
- 1.5Gi
316
+ 1.5G // 1_000_000_000
317
+ 1.3Ki // 1.3 * 1024 = trunc(1331.2) = 1331
327
318
170_141_183_460_469_231_731_687_303_715_884_105_727
328
319
0xBad_Face
329
320
0o755
330
321
0b0101_0001
331
322
```
332
323
333
- ### Decimal floating-point literals
334
-
335
- A decimal floating-point literal is a representation of
324
+ A _ decimal floating-point literal_ is a representation of
336
325
a decimal floating-point value (a _ float_ ).
337
326
It has an integer part, a decimal point, a fractional part, and an
338
327
exponent part.
@@ -341,13 +330,6 @@ exponent part is an `e` or `E` followed by an optionally signed decimal exponent
341
330
One of the integer part or the fractional part may be elided; one of the decimal
342
331
point or the exponent may be elided.
343
332
344
- ```
345
- decimal_lit = decimals "." [ decimals ] [ exponent ] |
346
- decimals exponent |
347
- "." decimals [ exponent ] .
348
- exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
349
- ```
350
-
351
333
```
352
334
0.
353
335
72.40
@@ -360,6 +342,25 @@ exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
360
342
.12345E+5
361
343
```
362
344
345
+ <!--
346
+ TODO: consider allowing Exo (and up), if not followed by a sign
347
+ or number. Alternatively one could only allow Ei, Yi, and Zi.
348
+ -->
349
+
350
+ Neither a ` float_lit ` nor an ` si_lit ` may not appear after a token that is:
351
+
352
+ - an identifier, keyword, or bottom
353
+ - a number or string literal, including an interpolation
354
+ - one of the characters ` ) ` , ` ] ` , ` } ` , ` ? ` , or ` . ` .
355
+
356
+ <!--
357
+ So
358
+ `a + 3.2Ti` -> `a`, `+`, `3.2Ti`
359
+ `a 3.2Ti` -> `a`, `3`, `.`, `2`, `Ti`
360
+ `a + .5e3` -> `a`, `+`, `.5e3`
361
+ `a .5e3` -> `a`, `.`, `5`, `e3`.
362
+ -->
363
+
363
364
364
365
### String and byte sequence literals
365
366
@@ -2103,7 +2104,7 @@ UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
2103
2104
binary_op = "|" | "&" | "||" | "&&" | "==" | rel_op | add_op | mul_op .
2104
2105
rel_op = "!=" | "<" | "<=" | ">" | ">=" | "=~" | "!~" .
2105
2106
add_op = "+" | "-" .
2106
- mul_op = "*" | "/" | "div" | "mod" | "quo" | "rem" .
2107
+ mul_op = "*" | "/" .
2107
2108
unary_op = "+" | "-" | "!" | "*" | rel_op .
2108
2109
```
2109
2110
@@ -2153,7 +2154,7 @@ and finally `|` (disjunction):
2153
2154
2154
2155
```
2155
2156
Precedence Operator
2156
- 7 * / div mod quo rem
2157
+ 7 * /
2157
2158
6 + -
2158
2159
5 == != < <= > >= =~ !~
2159
2160
4 &&
@@ -2178,91 +2179,35 @@ x == y+1 && y == z-1
2178
2179
#### Arithmetic operators
2179
2180
2180
2181
Arithmetic operators apply to numeric values and yield a result of the same type
2181
- as the first operand. The three of the four standard arithmetic operators
2182
- ` (+, -, *) ` apply to integer and decimal floating-point types;
2182
+ as the first operand. The four standard arithmetic operators
2183
+ ` (+, -, *, / ) ` apply to integer and decimal floating-point types;
2183
2184
` + ` and ` * ` also apply to lists and strings.
2184
- ` / ` only applies to decimal floating-point types and
2185
- ` div ` , ` mod ` , ` quo ` , and ` rem ` only apply to integer types.
2186
2185
2187
2186
```
2188
2187
+ sum integers, floats, lists, strings, bytes
2189
2188
- difference integers, floats
2190
2189
* product integers, floats, lists, strings, bytes
2191
- / quotient floats
2192
- div division integers
2193
- mod modulo integers
2194
- quo quotient integers
2195
- rem remainder integers
2190
+ / quotient integers, floats
2196
2191
```
2197
2192
2198
2193
For any operator that accepts operands of type ` float ` , any operand may be
2199
- of type ` int ` or ` float ` , in which case the result will be ` float ` if any
2200
- of the operands is ` float ` or ` int ` otherwise.
2201
- For ` / ` the result is always ` float ` .
2202
-
2203
-
2204
- #### Integer operators
2205
-
2206
- For two integer values ` x ` and ` y ` ,
2207
- the integer quotient ` q = x div y ` and remainder ` r = x mod y `
2208
- implement Euclidean division and
2209
- satisfy the following relationship:
2210
-
2211
- ```
2212
- r = x - y*q with 0 <= r < |y|
2213
- ```
2214
- where ` |y| ` denotes the absolute value of ` y ` .
2215
-
2216
- ```
2217
- x y x div y x mod y
2218
- 5 3 1 2
2219
- -5 3 -2 1
2220
- 5 -3 -1 2
2221
- -5 -3 2 1
2222
- ```
2223
-
2224
- For two integer values ` x ` and ` y ` ,
2225
- the integer quotient ` q = x quo y ` and remainder ` r = x rem y `
2226
- implement truncated division and
2227
- satisfy the following relationship:
2228
-
2229
- ```
2230
- x = q*y + r and |r| < |y|
2231
- ```
2232
-
2233
- with ` x quo y ` truncated towards zero.
2194
+ of type ` int ` or ` float ` , in which case the result will be ` float `
2195
+ if it cannot be represented as an ` int ` or if any of the operands are ` float ` ,
2196
+ or ` int ` otherwise.
2197
+ So the result of ` 1 / 2 ` is ` 0.5 ` and is of type ` float ` .
2234
2198
2235
- ```
2236
- x y x quo y x rem y
2237
- 5 3 1 2
2238
- -5 3 -1 -2
2239
- 5 -3 -1 2
2240
- -5 -3 1 -2
2241
- ```
2242
-
2243
- A zero divisor in either case results in bottom (an error).
2199
+ The result of division by zero is bottom (an error).
2200
+ <!-- TODO: consider making it +/- Inf -->
2201
+ Integer division is implemented through the builtin functions
2202
+ ` quo ` , ` rem ` , ` div ` , and ` mod ` .
2244
2203
2245
- For integer operands, the unary operators ` + ` and ` - ` are defined as follows:
2204
+ The unary operators ` + ` and ` - ` are defined for numeric values as follows:
2246
2205
2247
2206
```
2248
2207
+x is 0 + x
2249
2208
-x negation is 0 - x
2250
2209
```
2251
2210
2252
-
2253
- #### Decimal floating-point operators
2254
-
2255
- For decimal floating-point numbers, ` +x ` is the same as ` x ` ,
2256
- while -x is the negation of x.
2257
- The result of a floating-point division by zero is bottom (an error).
2258
-
2259
- <!-- TODO: consider making it +/- Inf -->
2260
-
2261
- An implementation may combine multiple floating-point operations into a single
2262
- fused operation, possibly across statements, and produce a result that differs
2263
- from the value obtained by executing and rounding the instructions individually.
2264
-
2265
-
2266
2211
#### List operators
2267
2212
2268
2213
Lists can be concatenated using the ` + ` operator.
@@ -2724,6 +2669,47 @@ or([a]) a
2724
2669
or([]) _|_
2725
2670
```
2726
2671
2672
+ #### ` div ` , ` mod ` , ` quo ` and ` rem `
2673
+
2674
+ For two integer values ` x ` and ` y ` ,
2675
+ the integer quotient ` q = div(x, y) ` and remainder ` r = mod(x, y) `
2676
+ implement Euclidean division and
2677
+ satisfy the following relationship:
2678
+
2679
+ ```
2680
+ r = x - y*q with 0 <= r < |y|
2681
+ ```
2682
+ where ` |y| ` denotes the absolute value of ` y ` .
2683
+
2684
+ ```
2685
+ x y div(x, y) mod(x, y)
2686
+ 5 3 1 2
2687
+ -5 3 -2 1
2688
+ 5 -3 -1 2
2689
+ -5 -3 2 1
2690
+ ```
2691
+
2692
+ For two integer values ` x ` and ` y ` ,
2693
+ the integer quotient ` q = quo(x, y) ` and remainder ` r = rem(x, y) `
2694
+ implement truncated division and
2695
+ satisfy the following relationship:
2696
+
2697
+ ```
2698
+ x = q*y + r and |r| < |y|
2699
+ ```
2700
+
2701
+ with ` quo(x, y) ` truncated towards zero.
2702
+
2703
+ ```
2704
+ x y quo(x, y) rem(x, y)
2705
+ 5 3 1 2
2706
+ -5 3 -1 -2
2707
+ 5 -3 -1 2
2708
+ -5 -3 1 -2
2709
+ ```
2710
+
2711
+ A zero divisor in either case results in bottom (an error).
2712
+
2727
2713
2728
2714
## Cycles
2729
2715
0 commit comments