Skip to content

Commit 19f6f3f

Browse files
committed
doc/ref/spec.md: perpare for allowing a.4.f
- Update the comma elision rule to reflect the addition of new keywords and other syntax. - Also limit the places where floating points are allowed. - Remove integer division operators and reintroduce as builtins - This also makes the first step to unify integral and floating point numbers into a single type by unifying the section on literals. This allows integer numbers to be used as selectors, as in a.b.3 instead of a.b[3] The only ambiguity that would remain is in if .5 == foo {} Which would tokenize as `if`, `.`, `5`, `==`, ... But that seems worth the potential gain. This will be mitigated if we let `cue fmt` rewrite `.5` to `0.5`, not unlike popular spreadsheets do. The integer division operators also have to be removed to make this work. In this CL they are redefined as builtins. This seems far nicer anyway and removes some complexity from a feature that is otherwise not used much. Defining them as builtins also seems to improve discoverablity / documentation. Allowing `a.5` results in a far more regular syntax. The main drawback of this syntax is is that it will not be possible to elide commas in lists. This is already not allowed. Adding this restriction will still allows to either allow `[ a .5 b ]` or `a.b.5`, or neither down the road. This change makes it clear, however, that allowing numbers as selectors can be achieved without disallowing `.5` to be a valid floating point. Change-Id: Iabd8f7fd88a3e5ec602aa1f110f75aa5cffe3256 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7726 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 4db8ffb commit 19f6f3f

File tree

1 file changed

+98
-112
lines changed

1 file changed

+98
-112
lines changed

doc/ref/spec.md

Lines changed: 98 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ CUE programs may omit most of these commas using the following two rules:
162162
When the input is broken into tokens, a comma is automatically inserted into
163163
the token stream immediately after a line's final token if that token is
164164

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 `?`
168168

169169

170170
Although commas are automatically inserted, the parser will require
@@ -257,23 +257,15 @@ TODO:
257257
-->
258258

259259

260-
#### Arithmetic
261-
262-
The following pseudo keywords can be used as operators in expressions.
263-
264-
```
265-
div mod quo rem
266-
```
267-
268260
### Operators and punctuation
269261

270262
The following character sequences represent operators and punctuation:
271263

272264
```
273-
+ div && == < = ( )
274-
- mod || != > : { }
275-
* quo & =~ <= ? [ ] ,
276-
/ rem | !~ >= ! _|_ ... .
265+
+ && == < = ( )
266+
- || != > : { }
267+
* & =~ <= ? [ ] ,
268+
/ | !~ >= ! _|_ ... .
277269
```
278270
<!--
279271
Free tokens: ; ~ ^
@@ -286,19 +278,9 @@ Free tokens: ; ~ ^
286278
-->
287279

288280

289-
### Integer literals
281+
### Numeric literals
290282

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.
302284

303285
```
304286
int_lit = decimal_lit | si_lit | octal_lit | binary_lit | hex_lit .
@@ -316,23 +298,30 @@ float_lit = decimals "." [ decimals ] [ exponent ] |
316298
"." decimals [ exponent ].
317299
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
318300
```
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.
323313

324314
```
325315
42
326-
1.5Gi
316+
1.5G // 1_000_000_000
317+
1.3Ki // 1.3 * 1024 = trunc(1331.2) = 1331
327318
170_141_183_460_469_231_731_687_303_715_884_105_727
328319
0xBad_Face
329320
0o755
330321
0b0101_0001
331322
```
332323

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
336325
a decimal floating-point value (a _float_).
337326
It has an integer part, a decimal point, a fractional part, and an
338327
exponent part.
@@ -341,13 +330,6 @@ exponent part is an `e` or `E` followed by an optionally signed decimal exponent
341330
One of the integer part or the fractional part may be elided; one of the decimal
342331
point or the exponent may be elided.
343332

344-
```
345-
decimal_lit = decimals "." [ decimals ] [ exponent ] |
346-
decimals exponent |
347-
"." decimals [ exponent ] .
348-
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
349-
```
350-
351333
```
352334
0.
353335
72.40
@@ -360,6 +342,25 @@ exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
360342
.12345E+5
361343
```
362344

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+
363364

364365
### String and byte sequence literals
365366

@@ -2103,7 +2104,7 @@ UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
21032104
binary_op = "|" | "&" | "||" | "&&" | "==" | rel_op | add_op | mul_op .
21042105
rel_op = "!=" | "<" | "<=" | ">" | ">=" | "=~" | "!~" .
21052106
add_op = "+" | "-" .
2106-
mul_op = "*" | "/" | "div" | "mod" | "quo" | "rem" .
2107+
mul_op = "*" | "/" .
21072108
unary_op = "+" | "-" | "!" | "*" | rel_op .
21082109
```
21092110

@@ -2153,7 +2154,7 @@ and finally `|` (disjunction):
21532154

21542155
```
21552156
Precedence Operator
2156-
7 * / div mod quo rem
2157+
7 * /
21572158
6 + -
21582159
5 == != < <= > >= =~ !~
21592160
4 &&
@@ -2178,91 +2179,35 @@ x == y+1 && y == z-1
21782179
#### Arithmetic operators
21792180

21802181
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;
21832184
`+` 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.
21862185

21872186
```
21882187
+ sum integers, floats, lists, strings, bytes
21892188
- difference integers, floats
21902189
* 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
21962191
```
21972192

21982193
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`.
22342198

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`.
22442203

2245-
For integer operands, the unary operators `+` and `-` are defined as follows:
2204+
The unary operators `+` and `-` are defined for numeric values as follows:
22462205

22472206
```
22482207
+x is 0 + x
22492208
-x negation is 0 - x
22502209
```
22512210

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-
22662211
#### List operators
22672212

22682213
Lists can be concatenated using the `+` operator.
@@ -2724,6 +2669,47 @@ or([a]) a
27242669
or([]) _|_
27252670
```
27262671

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+
27272713

27282714
## Cycles
27292715

0 commit comments

Comments
 (0)