mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Don't treat negative numbers as literals
Fixes #93. Previously, "1 -1" was lexed as two number tokens: a positive literal and a negative literal. This caused problems when it came to parsing. Now the '-' and the second number are separate tokens. Note this is a breaking change, since `-16.sqrt` is now parsed as `-(16.sqrt)`, as opposed to `(-16).sqrt`. There is a small bit of overhead to doing it this way, but it might be possible to optimize that out in the compiler at some point in the future.
This commit is contained in:
@ -752,14 +752,7 @@ static void nextToken(Parser* parser)
|
||||
return;
|
||||
|
||||
case '-':
|
||||
if (isDigit(peekChar(parser)))
|
||||
{
|
||||
readNumber(parser);
|
||||
}
|
||||
else
|
||||
{
|
||||
makeToken(parser, TOKEN_MINUS);
|
||||
}
|
||||
makeToken(parser, TOKEN_MINUS);
|
||||
return;
|
||||
|
||||
case '|':
|
||||
|
||||
@ -25,6 +25,12 @@ IO.print(false == 1 >= 2) // expect: true
|
||||
// Unary - has lower precedence than ..
|
||||
IO.print(-"abc".count) // expect: -3
|
||||
|
||||
// 1 - 1 is not space-sensitive.
|
||||
IO.print(1 - 1) // expect: 0
|
||||
IO.print(1 -1) // expect: 0
|
||||
IO.print(1- 1) // expect: 0
|
||||
IO.print(1-1) // expect: 0
|
||||
|
||||
// TODO: %, associativity.
|
||||
|
||||
// Using () for grouping.
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user