[GEP] Refine parsing negative numbers

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[GEP] Refine parsing negative numbers

Daniel Sun
Hi all,

       Groovy parses negative numbers as unary expression. For example, `-1`
is parsed as `-``1`, older parser parses negative numbers twice, so does the
Parrot parser for the identical output...

       I suggest to parse negative numbers as REAL numbers instead of unary
expression[1]. It will break the edge case, for example: `-1(a, b, c)` is
parsed as `-(1.call(a, b, c))`, after refining, `-1(a, b, c)` is parsed as
`-1.call(a, b, c)`.

       Any thoughts?  If no PMC votes -1 on this GEP in 72 hours, I will
work on it.

Cheers,
Daniel.Sun

[1]
https://github.com/danielsun1106/groovy-parser/blob/master/src/main/antlr/GroovyParser.g4#L796



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Reply | Threaded
Open this post in threaded view
|

Re: [GEP] Refine parsing negative numbers

paulk_asert
This and similar issues have come up before. It would be worthwhile searching the mail archives.
At least one related change we backed out of last time. I'd be interested to see how it impacts the examples in previous discussions:




On Thu, Feb 22, 2018 at 6:29 PM, Daniel Sun <[hidden email]> wrote:
Hi all,

       Groovy parses negative numbers as unary expression. For example, `-1`
is parsed as `-``1`, older parser parses negative numbers twice, so does the
Parrot parser for the identical output...

       I suggest to parse negative numbers as REAL numbers instead of unary
expression[1]. It will break the edge case, for example: `-1(a, b, c)` is
parsed as `-(1.call(a, b, c))`, after refining, `-1(a, b, c)` is parsed as
`-1.call(a, b, c)`.

       Any thoughts?  If no PMC votes -1 on this GEP in 72 hours, I will
work on it.

Cheers,
Daniel.Sun

[1]
https://github.com/danielsun1106/groovy-parser/blob/master/src/main/antlr/GroovyParser.g4#L796



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Reply | Threaded
Open this post in threaded view
|

Re: [GEP] Refine parsing negative numbers

Daniel Sun
Hi Paul,

     Thanks for your finding these discussion.

     As Jochen said: Ruby and Python calculate `-2**4` and return `-16`,
Groovy returns the same value currently.

     But Scala/Excel  returns `16`...

     Maybe Ruby/Python/Groovy behaves properly for most people...
     

blackdrag wrote
> I just checked Ruby and Python and there -2**4 will return -16, same for
> -2^4 in
> Lua. So now I am wondering if that is the right thing for unary minus
>
>
> bye blackdrag


Cheers,
Daniel.Sun



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Reply | Threaded
Open this post in threaded view
|

Re: [GEP] Refine parsing negative numbers

Guillaume Laforge
Administrator
We have to be careful with such breaking changes, as it could change the results of existing calculations.
The other aspect being the fact that the AST would be different, so it might have a small impact on some AST transformations that would look at such constant numbers.

On Thu, Feb 22, 2018 at 10:53 AM, Daniel Sun <[hidden email]> wrote:
Hi Paul,

     Thanks for your finding these discussion.

     As Jochen said: Ruby and Python calculate `-2**4` and return `-16`,
Groovy returns the same value currently.

     But Scala/Excel  returns `16`...

     Maybe Ruby/Python/Groovy behaves properly for most people...


blackdrag wrote
> I just checked Ruby and Python and there -2**4 will return -16, same for
> -2^4 in
> Lua. So now I am wondering if that is the right thing for unary minus
>
>
> bye blackdrag


Cheers,
Daniel.Sun



--
Guillaume Laforge
Apache Groovy committer & PMC Vice-President
Developer Advocate @ Google Cloud Platform

Reply | Threaded
Open this post in threaded view
|

Re: [GEP] Refine parsing negative numbers

Daniel.Sun
OK. I see ;-)




--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Daniel Sun
Apache Groovy committer

Blog: http://blog.sunlan.me
Twitter: @daniel_sun