Variable scope and order of parsing vs. operations: Assignment in an “if”

后端 未结 3 967
既然无缘
既然无缘 2020-12-06 14:15

My understanding is that the if statements at the end of the line are evaluated before the code at the front of the line:

\'never shown\' if (fa         


        
3条回答
  •  一整个雨季
    2020-12-06 14:43

    My guess is that the order of parsing is different from the (logical) order of execution. In particular, given

    array << error if (error = some_function)
    

    Then logically, execution should go something like

    1. call some_function
    2. if error not defined, define error
    3. assign return value of some_function to error
    4. evaluate if
    5. if if evaluates to true, append value of error to array

    However, parsing wise (assuming typical LR parser), it goes

    1. Got token array (identifier). Is this defined? Yes. Is it a variable? Yes.
    2. Got token << (operator). Does array respond to <<? Yes (otherwise, output "undefined method" error).
    3. Got token error (identifier). Is this defined? No. Output "undefined local variable or method".

提交回复
热议问题