From 75d28c083cf5e66d250b451b0ce9f4ea122010f8 Mon Sep 17 00:00:00 2001 From: Evan Shaw Date: Fri, 9 Jan 2015 20:06:50 +1300 Subject: [PATCH] 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. --- src/wren_compiler.c | 9 +-------- test/grammar.wren | 6 ++++++ test/number/abs.wren | 12 ++++++------ test/number/ceil.wren | 16 ++++++++-------- test/number/floor.wren | 16 ++++++++-------- test/number/sqrt.wren | 4 ++-- test/number/to_string.wren | 6 +++--- 7 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/wren_compiler.c b/src/wren_compiler.c index 92a7e5c9..9a41c980 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -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 '|': diff --git a/test/grammar.wren b/test/grammar.wren index 36590f70..cca6ec4e 100644 --- a/test/grammar.wren +++ b/test/grammar.wren @@ -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. diff --git a/test/number/abs.wren b/test/number/abs.wren index 31c1e444..171b3ebb 100644 --- a/test/number/abs.wren +++ b/test/number/abs.wren @@ -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 diff --git a/test/number/ceil.wren b/test/number/ceil.wren index 8e180f2a..fe510dbd 100644 --- a/test/number/ceil.wren +++ b/test/number/ceil.wren @@ -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 diff --git a/test/number/floor.wren b/test/number/floor.wren index b519ace3..063f0361 100644 --- a/test/number/floor.wren +++ b/test/number/floor.wren @@ -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 diff --git a/test/number/sqrt.wren b/test/number/sqrt.wren index a4c2a668..f14cf979 100644 --- a/test/number/sqrt.wren +++ b/test/number/sqrt.wren @@ -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. diff --git a/test/number/to_string.wren b/test/number/to_string.wren index b02ca7e2..4ba6f896 100644 --- a/test/number/to_string.wren +++ b/test/number/to_string.wren @@ -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