Re: Parrot and the ++ and -- operators

2017-02-14 Thread Joe Wolf
This isn't a good idea at all, but you can produce the C++ behavior by
overriding next() in a mutable fashion, e.g.

@groovy.transform.TupleConstructor
class Bar {
int value
Bar next() {
++value
this
}
}

Bar b = new Bar(0)
++(++(++b))
assert b.value == 3

But in reality, it's just eliminating the effects/ordering of re-assignment
by only keeping one instance of Bar in play.

-Joe

On Tue, Feb 14, 2017 at 12:10 PM, Jochen Theodorou 
wrote:

>
>
> On 13.02.2017 23:58, Thibault Kruse wrote:
>
>>
>>
>> On Feb 14, 2017 3:52 AM, "Jochen Theodorou" > > wrote:
>>
>>
>> Options:
>>
>> (2) do allow these post- and prefix operators only on
>> VariableExpressions, making the code above no longer compile
>>
>>
>> Just to be sure, a VariableExpression is an expression consisting only
>> if a variable name?
>>
>
> yes, but yes, you are right, there are also PropertyExpressions and
> ArrayExpressions... so I was narrowing it down too much.
>
> And this applies to both the pre and postfix ++ operator?
>> So ++i++ would also stop compiling?
>>
>
> yes
>
> So the question could be rephrased as: is there any good usecase for ++
>> outside the increment of a variable? Like
>> x = (((a * b)++)/c)--
>>
>
> yes... and that even if considering we call next() for ++. I could imagine
> some crazy DSL to use this... but should we still consider doing this
> breaking change?
>
> bye Jochen
>


Re: Parrot and the ++ and -- operators

2017-02-13 Thread Jochen Theodorou



On 12.02.2017 16:11, Mauro Zallocco wrote:

Hi all.

I tried this:
groovy -version
Groovy Version: 2.4.6 JVM: 1.8.0_66 Vendor: Oracle Corporation OS: Windows 7

int i = 0
++(++(++i));
assert i == 1

However with C++, it gives 3.


It took me a while, but I think I start to get an idea why this happens.
"++i" is recognized by the compiler as it should be. But it is legal in 
Groovy to do ++1 for example. Of course we will not change the value of 
the literal 1 here. This is just to show that there is such a logic 
applying to any expression. In that sense (++i) will return a value and 
then we do ++ on that value to do it again on the result. The outer two 
cases do not involve a local variable. Or let me write this a little bit 
different:


++i is expanded to:
tmp = i.next()
i = tmp
return tmp

"++(++i)" is:
tmp = i.next()
i = tmp
return ++tmp

which is:
tmp = i.next()
i = tmp
tmp2 = tmp.next()
return tmp2

and that is because during code generation we work from inside out and 
do not return any expression.


If had this implemented in a macro manner we would probably have done 
something like this:

++i is expanded to (i = i+1)
++(++i) is expanded to ++(i=i+1) with the rule of ++ only applying to 
the local variable on the RHS we expand then to i=i+1+1.
Then of course ++(++(++i)) is i=i+1+1+1, with the compiler most probably 
shortening this to i=i+3


But as I said, we do not do it in a macro manner, instead we work with 
the result of an expression and the result of i=i.next() is the value of 
RHS, thus whatever i.next() returns. There is no chance to increase the 
value in the local variable here.


Options:
(1) do nothing and explain the difference only
(2) do allow these post- and prefix operators only on 
VariableExpressions, making the code above no longer compile

(3) do special casing
(4) combine 2 and 3 to require a VariableExpression as innermost 
expression and allow only prefix or postfix operators outside


Since (3) will include (1) to some extend my feeling here is, that this 
is no good idea at all. (1) is surely underdocumented atm. But the real 
question is if we should go with 1,2 or 4. And preferred solution is to 
go with 2. Anyone able to think of a really really good reason not to?


bye Jochen


Re: Parrot and the ++ and -- operators

2017-02-12 Thread Roman Shaposhnik
Well, for better of for worse this is the Groovy behavior as of today
(sorry for quoting myself -- but the thread there is interesting):
   https://twitter.com/rhatr/status/824033519684829185

Personally I find it surprising.

Thanks,
Roman.




On Sun, Feb 12, 2017 at 7:11 AM, Mauro Zallocco <mzallo...@gmail.com> wrote:
> Hi all.
>
> I tried this:
> groovy -version
> Groovy Version: 2.4.6 JVM: 1.8.0_66 Vendor: Oracle Corporation OS: Windows 7
>
> int i = 0
> ++(++(++i));
> assert i == 1
>
> However with C++, it gives 3.
>
> #include 
>
> int main() {
>   int i = 0;
>   ++(++(++i));
>   assert(i==3);
>
>   return 0;
> }
>
> I think 3 is correct.
> True?
>
>
>
> On Tue, Jan 24, 2017 at 12:46 PM, Joe Wolf <joew...@gmail.com> wrote:
>>
>> Good to know I'll be able to use these patterns in Groovy 3...I think :)
>>
>> -Joe
>>
>> On Tue, Jan 24, 2017 at 11:00 AM, Daniel Sun <realblue...@hotmail.com>
>> wrote:
>>>
>>> Hi Joe,
>>>
>>>   I've added your sample code as test
>>>
>>> cases(https://github.com/danielsun1106/groovy-parser/commit/9914682e53fb2fe3d4bb335c8153e61066cea317).
>>> Parrot has same result with the old parser ;)
>>>
>>> Cheers,
>>> Daniel.Sun
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://groovy.329449.n5.nabble.com/Parrot-and-the-and-operators-tp5737960p5737973.html
>>> Sent from the Groovy Dev mailing list archive at Nabble.com.
>>
>>
>


Re: Parrot and the ++ and -- operators

2017-01-24 Thread Joe Wolf
Good to know I'll be able to use these patterns in Groovy 3...I think :)

-Joe

On Tue, Jan 24, 2017 at 11:00 AM, Daniel Sun <realblue...@hotmail.com>
wrote:

> Hi Joe,
>
>   I've added your sample code as test
> cases(https://github.com/danielsun1106/groovy-parser/commit/
> 9914682e53fb2fe3d4bb335c8153e61066cea317).
> Parrot has same result with the old parser ;)
>
> Cheers,
> Daniel.Sun
>
>
>
> --
> View this message in context: http://groovy.329449.n5.
> nabble.com/Parrot-and-the-and-operators-tp5737960p5737973.html
> Sent from the Groovy Dev mailing list archive at Nabble.com.
>


Re: Parrot and the ++ and -- operators

2017-01-24 Thread Daniel Sun
Hi Joe,

  I've added your sample code as test
cases(https://github.com/danielsun1106/groovy-parser/commit/9914682e53fb2fe3d4bb335c8153e61066cea317).
Parrot has same result with the old parser ;)

Cheers,
Daniel.Sun



--
View this message in context: 
http://groovy.329449.n5.nabble.com/Parrot-and-the-and-operators-tp5737960p5737973.html
Sent from the Groovy Dev mailing list archive at Nabble.com.


Parrot and the ++ and -- operators

2017-01-24 Thread Joe Wolf
I accidentally discovered that Groovy 2, in contrast to Java, allows you to
apply the ++ and -- operators to the left and right sides of variables
simultaneously.

int i = 0
++i++
assert i == 1

and you can also chain them in a single statement provided you apply parens

int i = 0
((i++)++)++
assert i == 1
++(++(++i))
assert i == 2
++(++(++i++)++)++
assert i == 3

I can understand allowing the chaining syntax, considering that it's
roughly equivalent to next().next().next(), but as far as I reckon, being
able to use these operators in such a way has no practical purpose.

I haven't tried this code with the Groovy 3 parser yet--I was wondering if
it supported the ++ and -- operators similarly.

-Joe