Replace the bare-paren tuple grammar with explicit, position-unambiguous
forms, mirroring how structs work:
type `(A, B)` -> `Tuple(A, B)` (named keeps `:`)
value `(a, b)` -> `.(a, b)` (named uses `=`)
typed (new) -> `Tuple(A, B).(a, b)` (like `Point.{...}`)
failable `-> (T, !)` -> `-> T !`
`-> (T1, T2, !)`-> `-> Tuple(T1, T2) !` (channel outside Tuple)
Bare `(...)` is now grouping only, everywhere; a comma in bare parens is a
hard error with a migration hint. Grouping, function types `(A, B) -> R`,
param lists, lambdas, and match bindings are unaffected.
`Tuple(...)` is strictly a TYPE in every position (including `size_of` /
`type_info` args); a tuple VALUE comes only from `.(...)` (anonymous) or
`Tuple(...).(...)` (explicitly typed). A bare `Tuple(1, 2)` is a tuple
type with non-type elements -> rejected.
The ~110 tuple-bearing corpus files were migrated with a one-shot
AST-aware migrator (the `sx migrate` tool from the prior commit, removed
here). New examples: 0130 (new syntax), 0131 (typed construction), 1060
(named-tuple failable return). 1116 golden updated for the new hint text.
43 lines
1.3 KiB
Plaintext
43 lines
1.3 KiB
Plaintext
// New tuple syntax (additive over the legacy `(a, b)` forms):
|
|
// - tuple TYPE `Tuple(A, B)` and named `Tuple(x: A, y: B)`
|
|
// - tuple VALUE `.(a, b)`, named `.(x = a, y = b)`, 1-tuple `.(n)`
|
|
// - element access by index `.0` and by name `.x`
|
|
// - a `-> Tuple(i64, i64)` return type with a `.(b, a)` body
|
|
// - tuple equality operator over two `.(...)` literals
|
|
// The `Tuple(...)` type mirrors the inline `(A, B)` tuple_type_expr and
|
|
// `.(...)` mirrors the inline `(a, b)` tuple_literal, so both self-type
|
|
// structurally and reuse the existing tuple lowering.
|
|
|
|
#import "modules/std.sx";
|
|
|
|
swap :: (a: i64, b: i64) -> Tuple(i64, i64) {
|
|
.(b, a)
|
|
}
|
|
|
|
main :: () -> i32 {
|
|
// Positional value + index access.
|
|
p := .(1, 2);
|
|
print("p {} {}\n", p.0, p.1);
|
|
|
|
// Named value (`=`) + name access.
|
|
n := .(x = 10, y = 20);
|
|
print("n {} {}\n", n.x, n.y);
|
|
|
|
// 1-tuple.
|
|
one := .(7);
|
|
print("one {}\n", one.0);
|
|
|
|
// Tuple return type with a `.(...)` body.
|
|
s := swap(3, 4);
|
|
print("swap {} {}\n", s.0, s.1);
|
|
|
|
// Named tuple TYPE annotation, filled by a named `.(...)` literal.
|
|
nt : Tuple(x: i64, y: i64) = .(x = 5, y = 6);
|
|
print("named-type {} {}\n", nt.x, nt.y);
|
|
|
|
// Tuple equality operator over two `.(...)` literals.
|
|
print("eq {}\n", .(1, 2) == .(1, 2));
|
|
|
|
0
|
|
}
|