fix: stop lexing negative numbers as single tokens to fix parsing of subtraction

Previously, the lexer greedily consumed a `-` followed by a digit as a single negative number token, causing "1 -1" to be lexed as two number tokens instead of a minus operator and a positive literal. This broke parsing of subtraction expressions without spaces.

Now `-` always emits a TOKEN_MINUS, and the parser handles unary negation. This is a breaking change: `-16.sqrt` is now parsed as `-(16.sqrt)` instead of `(-16).sqrt`, requiring parentheses for negative method receivers. All number method tests (abs, ceil, floor, sqrt, toString) are updated to use explicit parentheses for negative receivers.
This commit is contained in:
Evan Shaw
2015-01-09 07:06:50 +00:00
parent 3f126945df
commit 0526cee655
7 changed files with 34 additions and 35 deletions
+6 -6
View File
@@ -1,6 +1,6 @@
IO.print(123.abs) // expect: 123
IO.print(-123.abs) // expect: 123
IO.print(0.abs) // expect: 0
IO.print(-0.abs) // expect: 0
IO.print(-0.12.abs) // expect: 0.12
IO.print(12.34.abs) // expect: 12.34
IO.print(123.abs) // expect: 123
IO.print((-123).abs) // expect: 123
IO.print(0.abs) // expect: 0
IO.print((-0).abs) // expect: 0
IO.print((-0.12).abs) // expect: 0.12
IO.print(12.34.abs) // expect: 12.34
+8 -8
View File
@@ -1,8 +1,8 @@
IO.print(123.ceil) // expect: 123
IO.print(-123.ceil) // expect: -123
IO.print(0.ceil) // expect: 0
IO.print(-0.ceil) // expect: -0
IO.print(0.123.ceil) // expect: 1
IO.print(12.3.ceil) // expect: 13
IO.print(-0.123.ceil) // expect: -0
IO.print(-12.3.ceil) // expect: -12
IO.print(123.ceil) // expect: 123
IO.print((-123).ceil) // expect: -123
IO.print(0.ceil) // expect: 0
IO.print((-0).ceil) // expect: -0
IO.print(0.123.ceil) // expect: 1
IO.print(12.3.ceil) // expect: 13
IO.print((-0.123).ceil) // expect: -0
IO.print((-12.3).ceil) // expect: -12
+8 -8
View File
@@ -1,8 +1,8 @@
IO.print(123.floor) // expect: 123
IO.print(-123.floor) // expect: -123
IO.print(0.floor) // expect: 0
IO.print(-0.floor) // expect: -0
IO.print(0.123.floor) // expect: 0
IO.print(12.3.floor) // expect: 12
IO.print(-0.123.floor) // expect: -1
IO.print(-12.3.floor) // expect: -13
IO.print(123.floor) // expect: 123
IO.print((-123).floor) // expect: -123
IO.print(0.floor) // expect: 0
IO.print((-0).floor) // expect: -0
IO.print(0.123.floor) // expect: 0
IO.print(12.3.floor) // expect: 12
IO.print((-0.123).floor) // expect: -1
IO.print((-12.3).floor) // expect: -13
+2 -2
View File
@@ -1,10 +1,10 @@
IO.print(4.sqrt) // expect: 2
IO.print(1000000.sqrt) // expect: 1000
IO.print(1.sqrt) // expect: 1
IO.print(-0.sqrt) // expect: -0
IO.print((-0).sqrt) // expect: -0
IO.print(0.sqrt) // expect: 0
IO.print(2.sqrt) // expect: 1.4142135623731
IO.print(-4.sqrt.isNan) // expect: true
IO.print((-4).sqrt.isNan) // expect: true
// TODO: Tests for sin and cos.
+3 -3
View File
@@ -1,5 +1,5 @@
IO.print(123.toString == "123") // expect: true
IO.print(-123.toString == "-123") // expect: true
IO.print(-0.toString == "-0") // expect: true
IO.print((-123).toString == "-123") // expect: true
IO.print((-0).toString == "-0") // expect: true
IO.print(12.34.toString == "12.34") // expect: true
IO.print(-0.0001.toString == "-0.0001") // expect: true
IO.print((-0.0001).toString == "-0.0001") // expect: true