bugGNU Octave - Bugs: bug #56032, Weird "if" parsing...

 
 

bug #56032: Weird "if" parsing behavior ignores stuff past first expression in some cases

Submitter:  Andrew Janke <apjanke>
Submitted:  Sat 30 Mar 2019 10:04:39 AM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Other
Status:  Invalid / Not an Octave Bug Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Sat 30 Mar 2019 08:50:57 PM UTC, comment #8: 

Yep, definitely not the least of the surprising syntax!

Mike Miller <mtmiller>
Group Member
Sat 30 Mar 2019 08:44:58 PM UTC, comment #7: 

Well, my head hurts now, but you can close this out as "just surprising M-code syntax".

Andrew Janke <apjanke>
Sat 30 Mar 2019 08:36:17 PM UTC, comment #6: 

Also this example


v = -10;
x = 10;
y = 1;
if (x < y) or (v<=0)
  some_stuff()
else
  some_other_stuff()
end


calls 'some_other_stuff' in Matlab, same as in Octave.

Mike Miller <mtmiller>
Group Member
Sat 30 Mar 2019 08:26:02 PM UTC, comment #5: 

I believe this is Matlab compatible behavior, the following works the same in Matlab as in Octave


>> if 0 disp('yup'), end
>> if 1 disp('yup'), end
yup


The comma is required in Matlab, but the "if condition expression" on a single line behaves exactly the same, a line break is not significant.

Mike Miller <mtmiller>
Group Member
Sat 30 Mar 2019 10:32:54 AM UTC, comment #4: 

BTW: this first came up because a user mentioned in IRC that they were doing:


if (x < y) or (v<=0)
  some_stuff()
else
  some_other_stuff()
end


And it wasn't doing what they were expected. Turned out to be a weird interaction between this behavior and the fact that "or" isn't a keyword. So that 'or (v<=0)' isn't part of the "if" condition; it's the first line in the "do-if-condition-is-true" block.

Andrew Janke <apjanke>
Sat 30 Mar 2019 10:26:25 AM UTC, comment #3: 

Speaking of which, the "if" statement documentation - https://octave.org/doc/v4.2.1/The-if-Statement.html#The-if-Statement - is written as though parentheses are required. Parentheses aren't actually part of the if/while/for syntax; they're a grouping operation that we choose to use there as a stylistic convention. Might be good to mention that, at least as a footnote.

Andrew Janke <apjanke>
Sat 30 Mar 2019 10:16:25 AM UTC, comment #2: 

Totally degenerate case:


>> if 0 disp('yup') end
>> if 1 disp('yup') end
yup


Andrew Janke <apjanke>
Sat 30 Mar 2019 10:08:43 AM UTC, comment #1: 

Simpler repro:


>> if 1 > 3 klsdjfkldsjfkls; else fprintf('nope\n') end
nope


Maybe the "if" stops executing its conditional as soon as it hits a full expression, and treats stuff following that as part of the code to execute if the conditional is true?

Andrew Janke <apjanke>
Sat 30 Mar 2019 10:04:39 AM UTC, original submission:  

This shouldn't work, right?


function blarg
  x = 1;
  y = 2;
  if x > y sldkfjdskljfskldklsdjf (x>y)
    fprintf('yup\n');
  else
    fprintf('nope\n');
  endif
endfunction


But it does this:


>> blarg
nope


The parentheses in the condition following the bogus keyword are significant. If you remove them, you get a syntax error, like I'd expect.

Happens in 4.2, 4.4.1, and 5.1.0.

Andrew Janke <apjanke>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by apjanke (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-03-30 mtmiller StatusNone Invalid / Not an Octave Bug
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code