authorgravatar for gbell12907@gmail.comCMDRZero <gbell12907@gmail.com> 2026-07-14 13:42:31-05:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-10-22 12:21:43+02:00
log2d3eece3f4c6ab67165aab2c6c0bc890ad2338ea
tree2aab9c01841fe9d700b65780fae621ca2e940280
parent12ead9415ca1dfb15803c46d4598141daee314ba

Add WIP langref for new operators


7 files changed, 80 insertions(+), 0 deletions(-)

doc/langref.html.in+2
......@@ -1908,6 +1908,8 @@ or
19081908= *= *%= *|= /= %= += +%= +|= -= -%= -|= <<= <<|= >>= &= ^= |={#endsyntax#}</pre>
19091909 {#header_close#}
19101910 {#header_close#}
1911 {#header_close#}
1912
19111913 {#header_open|Arrays#}
19121914 {#code|test_arrays.zig#}
19131915
doc/langref/ambiguous_precedence_mul_bitand.zig created+13
......@@ -0,0 +1,13 @@
1test "ambiguous precedence" {
2 const x: u8 = 1;
3 const y: u8 = 2;
4 const z: u8 = 3;
5
6 // Its not obvious below if this is multiplication by a masked value
7 // `(x&y) *% z`
8 // or a masking of wrapped multiplication
9 // `x & (y*%z)`
10 _ = x & y *% z;
11}
12
13// obj=ambiguous operator precedence with operators … & … *% …; use parentheses to disambiguate
\ No newline at end of file
doc/langref/ambiguous_precedence_orelse_ne.zig created+21
......@@ -0,0 +1,21 @@
1fn makeBool(x: u32) ?bool {
2 if (x == 0) return false;
3 if (x == 1) return true;
4 return null;
5}
6
7test "ambiguous precedence" {
8 const x: ?u32 = 0;
9 const y: u8 = 0;
10 const z: u8 = 0;
11
12 // We might expect the line below to parse something like:
13 // `makeBool(x) orelse (y != z)`
14 // but we might elsewhere want something like
15 // `(nullable(x) orelse y) != w`
16 // to perform a fallback and then a check, either way its
17 // not obvious the behavior so we get an error
18 _ = makeBool(x) orelse y != z;
19}
20
21// obj=ambiguous operator precedence with operators … orelse … != …; use parentheses to disambiguate
\ No newline at end of file
doc/langref/ambiguous_precedence_shift_sub.zig created+10
......@@ -0,0 +1,10 @@
1test "ambiguous precedence" {
2 const x: u64 = 1;
3 const y: u6 = 63;
4
5 // We probably meant `(x << y) - 1`
6 // But thats not how it would have compiled
7 _ = x << y - 1;
8}
9
10// obj=ambiguous operator precedence with operators … << … - …; use parentheses to disambiguate
\ No newline at end of file
doc/langref/chained_sibling_operators.zig created+5
......@@ -0,0 +1,5 @@
1fn foo(x: anytype, y: anytype, z: anytype) void {
2 _ = x + y - z;
3}
4
5// syntax
\ No newline at end of file
doc/langref/nonchaining_sibling_operators.zig created+5
......@@ -0,0 +1,5 @@
1fn foo(x: anytype, y: anytype, z: anytype) void {
2 _ = x << y << z;
3}
4
5// obj=repetition of non-chainable operators … << … << …; use parentheses to disambiguate
\ No newline at end of file
doc/langref/operator_precedence.zig created+24
......@@ -0,0 +1,24 @@
1const assert = @import("std").debug.assert;
2
3test "operator precedence" {
4 const x = 1;
5 const y = 2;
6 const z = 3;
7
8 // Because comparisons have a lower precedence than any arithmetic
9 // We can safely avoid parenthesizing the expressions
10 assert(x + y * z == x + (y*z));
11 assert(x * y + z == (x*y) + z);
12
13 // We can also use ambiguous operators as long as they are seperated
14 // by a well ordered one such as below
15 assert(x*y + x%y == (x*y) + (x%y));
16 //That includes comparisons
17 assert(x & y != x + y);
18
19 // We can freely chain or's and and's, but not with each other
20 assert(false or x == y or x + y == z);
21 assert(true and x != y and y != z);
22}
23
24// test
\ No newline at end of file