Skip to content

Commit 06eb09b

Browse files
committed
Fix ** right-associativity, and clarify docs
1 parent 6982c8a commit 06eb09b

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

Diff for: man/rgbasm.5

+23-14
Original file line numberDiff line numberDiff line change
@@ -309,26 +309,35 @@ is equivalent to
309309
.Pp
310310
You can also use symbols, which are implicitly replaced with their value.
311311
.Ss Operators
312-
A great number of operators you can use in expressions are available (listed from highest to lowest precedence):
312+
You can use these operators in numeric expressions (listed from highest to lowest precedence):
313313
.Bl -column -offset indent "!= == <= >= < >"
314314
.It Sy Operator Ta Sy Meaning
315-
.It Li \&( \&) Ta Precedence override
315+
.It Li \&( \&) Ta Grouping
316316
.It Li FUNC() Ta Built-in function call
317-
.It Li ** Ta Exponent
318-
.It Li ~ + - Ta Unary complement/plus/minus
319-
.It Li * / % Ta Multiply/divide/modulo
320-
.It Li << Ta Shift left
321-
.It Li >> Ta Signed shift right (sign-extension)
322-
.It Li >>> Ta Unsigned shift right (zero-extension)
323-
.It Li & \&| ^ Ta Binary and/or/xor
324-
.It Li + - Ta Add/subtract
325-
.It Li != == <= >= < > Ta Comparison
326-
.It Li && || Ta Boolean and/or
327-
.It Li \&! Ta Unary not
317+
.It Li ** Ta Exponentiation
318+
.It Li + - ~ \&! Ta Unary plus, minus (negation), complement (bitwise negation), and Boolean negation
319+
.It Li * / % Ta Multiplication, division, and modulo (remainder)
320+
.It Li << >> >>> Ta Bit shifts (left, sign-extended right, zero-extended right)
321+
.It Li & \&| ^ Ta Bitwise AND/OR/XOR
322+
.It Li + - Ta Addition and subtraction
323+
.It Li == != < > <= >= Ta Comparisons
324+
.It Li && Ta Boolean AND
325+
.It Li || Ta Boolean OR
328326
.El
329327
.Pp
328+
.Sq **
329+
raises a number to a non-negative power. It is the only
330+
.Em right-associative
331+
operator, meaning that
332+
.Ql p ** q ** r
333+
is equal to
334+
.Ql p ** (q ** r) ,
335+
not
336+
.Ql (p ** q) ** r .
337+
All other binary operators are left-associative.
338+
.Pp
330339
.Sq ~
331-
complements a value by inverting all its bits.
340+
complements a value by inverting all 32 of its bits.
332341
.Pp
333342
.Sq %
334343
is used to get the remainder of the corresponding division, so that

Diff for: src/asm/parser.y

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
%left OP_SHL OP_SHR OP_USHR
141141
%left OP_MUL OP_DIV OP_MOD
142142
%precedence NEG // applies to unary OP_LOGICNOT, OP_ADD, OP_SUB, OP_NOT
143-
%left OP_EXP
143+
%right OP_EXP
144144

145145
// Assignment operators (only for variables)
146146
%token POP_EQUAL "="

Diff for: test/asm/operator-associativity.asm

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
MACRO setup
2+
def result = (\2) \1 (\3) \1 (\4)
3+
def leftgroup = ((\2) \1 (\3)) \1 (\4)
4+
def rightgroup = (\2) \1 ((\3) \1 (\4))
5+
ENDM
6+
7+
MACRO left
8+
setup \#
9+
ASSERT result == leftgroup && result != rightgroup
10+
ENDM
11+
12+
MACRO right
13+
setup \#
14+
ASSERT result == rightgroup && result != leftgroup
15+
ENDM
16+
17+
left /, 24, 6, 2
18+
left %, 22, 13, 5
19+
20+
right **, 2, 3, 2
21+
22+
left ==, 0, 1, 2
23+
left !=, 1, 1, 2
24+
left <, 1, 2, 2
25+
left >, 2, 2, 1
26+
left <=, 1, 3, 2
27+
left >=, 2, 3, 1
28+
29+
left <<, 1, 2, 2
30+
left >>, 16, 2, 2
31+
left >>>, 16, 2, 2

0 commit comments

Comments
 (0)