Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 1fae053

Browse files
committed
[[ Bug 20430 ]] Swap LCB exponentiation and unary operator precedence
1 parent 93a1601 commit 1fae053

File tree

6 files changed

+46
-10
lines changed

6 files changed

+46
-10
lines changed

builder/release_notes_builder.livecodescript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ private command BaseCreate
407407
BaseCreateOverview
408408
BaseCreateIssues
409409

410+
MarkdownAppend "notes", BaseReadFile("breaking_changes")
410411
MarkdownAppend "notes", BaseReadFile("platforms")
411412
MarkdownAppend "notes", BaseReadFile("setup")
412413
MarkdownAppend "notes", BaseReadFile("proposed_changes")

docs/lcb/notes/20430.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# LiveCode Builder Standard Library
2+
3+
## Mathematical Operator Precedence
4+
5+
* The exponentiation operator `^` now has higher precedence than
6+
unary minus, meaning for example that -1^2 evaluates to -1 rather
7+
than 1, as it does in LiveCode Script.
8+
9+
# [20430] Exponentiation should have higher precedence than unary minus
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Breaking changes
2+
3+
## LiveCode Builder
4+
5+
### Exponentiation operator precedence
6+
Prior to this release, exponentiation had lower precedence that unary
7+
minus. In order to write code that operates as expected in both this
8+
release and previous releases, please use parentheses where appropriate.
9+
10+
Using lc-compile tool in LiveCode 9:
11+
12+
-1^2 = -1
13+
14+
Using lc-compile tool in LiveCode 8:
15+
16+
-1^2 = 1

docs/specs/lcb-precedence.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ Lower precedence numbers are more tightly-binding.
3131
| | subscript | `tList[1]`, `tArray["key"]` |
3232
| 3 | property | `the paint of this canvas` |
3333
| | subscript chunk | `char 2 of tString` |
34-
| 4 | conversion | `tString parsed as number` |
3534
| | function chunk | `the offset of "o" in "foo"` |
36-
| 5 | modifier | `-tNum`, `bitwise not` |
37-
| 6 | exponentiation | `^` |
35+
| 4 | conversion | `tString parsed as number` |
36+
| 5 | exponentiation | `^` |
37+
| 6 | modifier | `-tNum`, `bitwise not` |
3838
| 7 | multiplication | `/`,`*`,`mod`, `div` |
3939
| 8 | addition | `+`, `-` |
4040
| | concatenation | `&`, `&&` |
@@ -49,3 +49,7 @@ Lower precedence numbers are more tightly-binding.
4949
| 16 | logical and | `Foo() and Bar()` |
5050
| 17 | logical or | `Foo() or Bar()` |
5151
| 18 | sequence | `,` |
52+
53+
>**Important:** Prior to LiveCode 9, exponentiation and modifier
54+
> precedence were reversed. In order to write code that operates as
55+
> expected in both LC 9 and 8, please use parentheses where appropriate.

tests/lcb/stdlib/math.lcb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public handler TestRound()
4747
end handler
4848

4949
public handler TestPow()
50-
test "pow (-ve, -ve)" when -2 ^ -2 is 0.25
50+
test "pow (-ve, -ve)" when (-2) ^ -2 is 0.25
5151
test "pow (-ve, +ve)" when (-2) ^ 2 is 4
52-
test "pow (-ve, +ve)" when -2 ^ 3 is -8
52+
test "pow (-ve, +ve)" when (-2) ^ 3 is -8
5353
test "pow (+ve, -ve)" when 2 ^ -2 is 0.25
5454
test "pow (+ve, +ve)" when 2 ^ 2 is 4
5555
test "pow (0, 1)" when 0 ^ 1 is 0
@@ -66,8 +66,14 @@ public handler TestPowDomain()
6666
end handler
6767

6868
public handler TestPowPrecedence()
69-
test "pow vs. unary−" when -2 ^ 2 is 4
70-
test "pow vs. unary−" when -2 ^ 0 is 1
69+
test "pow vs. -" when 0-1^2 is -1
70+
test "pow vs. -" when -1^2 is -1
71+
test "pow vs. -" when 0-(1^2) is -1
72+
test "pow vs. -" when -(1^2) is -1
73+
test "pow vs. -" when 0-(1^2) is -1
74+
test "pow vs. -" when -(1)^2 is -1
75+
test "pow vs. -" when 0-(1)^2 is -1
76+
test "pow vs. -" when (-1)^2 is 1
7177
end handler
7278

7379
public handler TestLog10()

toolchain/lc-compile/src/syntax.g

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@
294294
295295
'rule' MapSyntaxPrecedence(property -> 3)
296296
'rule' MapSyntaxPrecedence(subscriptchunk -> 3)
297+
'rule' MapSyntaxPrecedence(functionchunk -> 3)
297298
298299
'rule' MapSyntaxPrecedence(conversion -> 4)
299-
'rule' MapSyntaxPrecedence(functionchunk -> 3)
300300
301-
'rule' MapSyntaxPrecedence(modifier -> 5)
301+
'rule' MapSyntaxPrecedence(exponentiation -> 5)
302302
303-
'rule' MapSyntaxPrecedence(exponentiation -> 6)
303+
'rule' MapSyntaxPrecedence(modifier -> 6)
304304
305305
'rule' MapSyntaxPrecedence(multiplication -> 7)
306306

0 commit comments

Comments
 (0)