Old 08-19-2019, 06:08 AM   #1
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
Default JSFX - While syntax checking.

Please, please, make this throw up a compilation error:

Code:
    while (midirecv(mpos, msg1, msg23)) ? (
        midisend(mpos, msg1, msg23);
// .... more valid statements
     );
I've just spent an hour tracking it down
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is offline   Reply With Quote
Old 08-19-2019, 11:27 AM   #2
sai'ke
Human being with feelings
 
sai'ke's Avatar
 
Join Date: Aug 2009
Location: NL
Posts: 1,453
Default

It could be a valid approach in some cases though as long as the body of the if statement returns zero at some point.
__________________
[Tracker Plugin: Thread|Github|Reapack] | [Routing Plugin: Thread|Reapack] | [More JSFX: Thread|Descriptions|Reapack]
sai'ke is offline   Reply With Quote
Old 08-20-2019, 03:51 PM   #3
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
Default

Are you sure? Could you explain, step by step, what that construct would do?

As I understand it the two valid constructs, to get each MIDI message and process it, are:

Code:
    while (
        midirecv(mpos, msg1, msg23) ? (
// .... more valid statements
            midisend(mpos, msg1, msg23);
        );   // .... end of midirecv() test
    );       // .,.. end of while
or

Code:
    while (midirecv(mpos, msg1, msg23)) (  // .... no ? operator
// .... more valid statements
            midisend(mpos, msg1, msg23);
     );  // .... end of while/midirecv() 
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is offline   Reply With Quote
Old 08-21-2019, 10:07 AM   #4
sai'ke
Human being with feelings
 
sai'ke's Avatar
 
Join Date: Aug 2009
Location: NL
Posts: 1,453
Default

I didn't test it to be honest (not at home), but I'd expect it to keep executing the true case of that if-statement as long as midirecv returns non-zero.

If that "true"-case body ends on zero (the last statement being zero or not returning a value), I expect the while statement to terminate early (before running out of midi messages).

If midirecv returns zero, I would expect the body to not be called at all.

What actually happens?

I mean, I don't disagree that that construct is usually not what you want, but by excluding certain syntaxes, you risk removing other valid uses of them.
__________________
[Tracker Plugin: Thread|Github|Reapack] | [Routing Plugin: Thread|Reapack] | [More JSFX: Thread|Descriptions|Reapack]
sai'ke is offline   Reply With Quote
Old 08-21-2019, 10:10 AM   #5
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
Default

The problem was that I had the syntax I posted in my original post and that was giving me some odd (not really explainable) results.
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is offline   Reply With Quote
Old 08-21-2019, 10:12 AM   #6
sai'ke
Human being with feelings
 
sai'ke's Avatar
 
Join Date: Aug 2009
Location: NL
Posts: 1,453
Default

It didn't do what I just posted? What I wrote is what I would've expected for the syntax. Which is probably not what you wanted since it'd early out quite often before receiving all midi messages, thereby dropping some or all depending on the last statement in the body of the if-statement.

Now I'm curious! Maybe I'll investigate on the weekend.
__________________
[Tracker Plugin: Thread|Github|Reapack] | [Routing Plugin: Thread|Reapack] | [More JSFX: Thread|Descriptions|Reapack]
sai'ke is offline   Reply With Quote
Old 08-22-2019, 01:59 AM   #7
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,645
Default

Here's DarkStar's while construction rewritten to make it clear what's actually going on:

Code:
while(
  // Repeats midirecv() until it returns zero. Note that this is all that happens
  // inside the while loop.
  midirecv(mpos, msg1, msg23)
)

? // Evaluates the result of the while loop after it has finished.

(
  // This never gets executed, because a finished while loop always returns zero.
  midisend(mpos, msg1, msg23);
  // .... more valid statements
);
So yeah, this is valid JSFX, even though it includes unreachable code. Maybe an optional warning would be nice, but IMHO this shouldn't throw an error.
Tale is offline   Reply With Quote
Old 08-22-2019, 05:23 AM   #8
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
Default

Guys, thank you for the clarifications. I'll have to check my typing more closely.

Also, this seems to work:
Code:
    while (midirecv(mpos, msg1, msg23) ? (
        midisend(mpos, msg1, msg23);
// .... more valid statements
     ); );
I've highlighted the parenthesis pairs.
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is offline   Reply With Quote
Old 08-22-2019, 06:02 AM   #9
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,645
Default

Quote:
Originally Posted by DarkStar View Post
Also, this seems to work:
Yeah, that's my favorite JSFX while variant.

Do note that in this case you will have to end your while loop with a non-zero statement, because else it stop looping if the last statement inside the loop happens to return zero.

Code:
    while (midirecv(mpos, msg1, msg23) ? (
        midisend(mpos, msg1, msg23);
// .... more valid statements
        1; // Force while to loop
     ); );
Tale is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 05:56 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.