lsym_binary_op.c revision 1.10 1 /* $NetBSD: lsym_binary_op.c,v 1.10 2023/06/04 22:36:10 rillig Exp $ */
2
3 /*
4 * Tests for the token lsym_binary_op, which represents a binary operator in
5 * an expression. Examples for binary operators are '>>', '=', '+', '&&'.
6 *
7 * Binary operators are surrounded by blanks.
8 *
9 * Some tokens like '+', '*' or '&' can be either binary or unary operators,
10 * with an entirely different meaning.
11 *
12 * The token '*' is not only a binary or a unary operator, it is used in types
13 * as well, to derive a pointer type.
14 *
15 * See also:
16 * lsym_postfix_op.c for postfix unary operators
17 * lsym_unary_op.c for prefix unary operators
18 * lsym_colon.c for ':'
19 * lsym_question.c for '?'
20 * lsym_comma.c for ','
21 * C99 6.4.6 "Punctuators"
22 */
23
24 //indent input
25 void
26 binary_operators(void)
27 {
28 /* In the order of appearance in C11 6.5. */
29 a = a * a;
30 a = a / a;
31 a = a % a;
32 a = a + a;
33 a = a - a;
34 a = a << a;
35 a = a >> a;
36 a = a < a;
37 a = a > a;
38 a = a <= a;
39 a = a >= a;
40 a = a == a;
41 a = a != a;
42 a = a & a;
43 a = a ^ a;
44 a = a | a;
45 a = a && a;
46 a = a || a;
47 a = a ? a : a;
48 a = a;
49 a *= a;
50 a /= a;
51 a %= a;
52 a += a;
53 a -= a;
54 a <<= a;
55 a >>= a;
56 a &= a;
57 a ^= a;
58 a |= a;
59 a = a, a;
60 }
61 //indent end
62
63 //indent run-equals-input
64
65
66 /*
67 * If a '*' is immediately followed by another '*', they still form separate
68 * operators. The first is a binary operator, the second is unary.
69 */
70 //indent input
71 int var = expr**ptr;
72 //indent end
73
74 //indent run -di0
75 int var = expr * *ptr;
76 //indent end
77
78
79 /*
80 * When indent tokenizes some operators, it allows for
81 * arbitrary repetitions of the operator character, followed by an
82 * arbitrary amount of '='. This is used for operators like '&&' or
83 * '|||==='.
84 *
85 * Before 2021-03-07 22:11:01, the comment '//' was treated as a binary
86 * operator as well, and so was the comment '/////', leading to unexpected
87 * spacing.
88 *
89 * See lexi.c, lexi, "default:".
90 */
91 //indent input
92 void
93 long_run_of_operators(void)
94 {
95 if (a &&&&&&& b)
96 return;
97 if (a |||=== b)
98 return;
99 }
100 //indent end
101
102 //indent run-equals-input
103
104
105 /*
106 * Long chains of '+' and '-' must be split into several operators as the
107 * lexer has to distinguish between '++' and '+' early. The following
108 * sequence is thus tokenized as:
109 *
110 * word "a"
111 * postfix_op "++"
112 * binary_op "++"
113 * unary_op "++"
114 * unary_op "+"
115 * word "b"
116 *
117 * See lexi.c, lexi, "case '+':".
118 */
119 //indent input
120 void
121 joined_unary_and_binary_operators(void)
122 {
123 if (a +++++++ b)
124 return;
125 }
126 //indent end
127
128 //indent run
129 void
130 joined_unary_and_binary_operators(void)
131 {
132 if (a++ ++ ++ +b)
133 return;
134 }
135 //indent end
136
137
138 /*
139 * Ensure that the result of the indentation does not depend on whether a
140 * token from the input starts in column 1 or 9.
141 *
142 * See process_binary_op, ps.curr_col_1.
143 */
144 //indent input
145 int col_1 //
146 = //
147 1;
148
149 int col_9 //
150 = //
151 9;
152 //indent end
153
154 //indent run
155 int col_1 //
156 = //
157 1;
158
159 int col_9 //
160 = //
161 9;
162 //indent end
163
164
165 /*
166 * The ternary conditional operator is not a binary operator, but both its
167 * components '?' and ':' follow the same spacing rules.
168 */
169 //indent input
170 int conditional = condition ? number : number;
171 //indent end
172
173 //indent run-equals-input -di0
174
175
176 // After a ']', a '*' is a binary operator.
177 //indent input
178 int x = arr[3]*y;
179 //indent end
180
181 //indent run -di0
182 int x = arr[3] * y;
183 //indent end
184
185
186 /*
187 * Ensure that after an assignment, a '*=' operator is properly spaced, like
188 * any other binary operator.
189 */
190 //indent input
191 {
192 a = a;
193 a *= b *= c;
194 }
195 //indent end
196
197 //indent run-equals-input -di0
198