Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Bytecode patterns for <code>&amp;&amp;</code> and <code>||</code></h2> <p>Think about what a short-circuiting operator like <code>&amp;&amp;</code> or <code>||</code> actually does. You've got some conditional branching. Let's consider <code>&amp;&amp;</code>. What you're effectively evaluating is:</p> <pre><code>if (left) if (right) &lt;do something&gt; endIf </code></pre> <p>There is no single bytecode instruction that can describe this behavior. You need need some labels and conditional branching instructions:</p> <pre><code>.start &lt;left expression&gt; IFEQ .endIf // if left evaluates to zero (false), skip to end &lt;right expression&gt; IFEQ .endIf // if right evaluates to zero (false), skip to end .ifTrue &lt;body of 'if' block&gt; .endIf </code></pre> <p>The behavior of the <code>||</code> operator is a bit different; in this case, the logic looks something like this:</p> <pre><code> if (left) goto .ifTrue if (!right) goto .endIf .ifTrue &lt;do something&gt; .endIf </code></pre> <p>Note how the check on the right operand is inverted to avoid an additional branch when the right operand evaluates to <code>true</code>. This behavior could be implemented in bytecode like so:</p> <pre><code> &lt;left operand&gt; IFNE .ifTrue // if left evaluates true, skip right, enter 'if' body &lt;right operand&gt; IFEQ .endIf // if right evaluates false, skip 'if' body .ifTrue &lt;do something&gt; .endIf </code></pre> <hr> <h2>When to push your operands</h2> <p>Note that your original question suggested you already have the left and right operands on the stack; that would be bad. You should only evaluate the right operand <em>after</em> the left operand has evaluated to <code>true</code> (nonzero) for <code>&amp;&amp;</code> or <code>false</code> (zero) for <code>||</code>. If the right operand causes side effects, evaluating it prematurely would violate the defined behavior of these operators.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload