ERights Home elang / kernel 
Back to: Hide Expression On to: Escape Expression

If Expression


Has a generalization of the conventional meaning -- evaluates the first expression (the condition) to a boolean. If it's true, evaluates the second expression (the consequent). If it's last, evaluates the third expression (the alternative). The value of the if expression as a whole is the value of the consequent or alternative -- whichever was executed.

BNF:
"if" "(" eExpr ")" "{"
    eExpr
"}" "else" "{"
    eExpr
"}"
XML DTD:
<!ELEMENT ifExpr (%eExpr;, %eExpr;, %eExpr;)>
Java:

If the condition doesn't define any variables used in the then-part, and if the ifExpr as a whole is not used in a value-context (ie, it is used in a for-effects-only context), then it translates directly to the corresponding Java.

If it's used in a value-context, then a result variable is declared ahead of time, and both branches assign to it.

If the condition does define variables used in the then-part, XXX

Example:
if (getChar() =~ c ? (c != EOF)) {
    println(c)
}
in Kernel-E:
if (getChar.run() =~ c :any ? __equalizer.sameEver(c, EOF).not()) {
    println.run(c)
} else {
    null
}
in XML:
<ifExpr>
    <matchBindExpr>
        <callExpr>
            <Noun>getChar</Noun>
            <Verb>run</Verb>
        </callExpr>
        <suchThatPattern>
            <finalPattern>
                <Noun>c</Noun>
                <Noun>any</Noun>
            </finalPattern>
            <callExpr>
                <callExpr>
                    <Noun>E</Noun>
                    <Noun>c</Noun>
                    <Verb>same</Verb>
                    <Noun>EOF</Noun>
                </callExpr>
                <Verb>not</Verb>
            </callExpr>
        </suchThatPattern>
    </matchBindPattern>
    <callExpr>
        <Noun>println</Noun>
        <Verb>run</Verb>
        <Noun>c</Noun>
    </callExpr>
    <Noun>null</Noun>
</ifExpr>
in Java:
Object result_7;
boolean result_6 = false;
Object c_tmp = Ref.broken(...);
fail_6: {
    Object c = E.call(getChar, "run");
    if (! E.toBoolean(E.call(E.same(c, EOF), "not"))) {
        break fail_6;
    }
    //commit
    result_6 = true;
    c_tmp = c;
}
if (result_6) {
    Object c = c_tmp;
    result7 = E.call(println, "run", c);
} else {
    result_7 = null;
}

There is one scope box containing both condition and consequent. There is another scope box containing only the alternative. This means that both condition and alternative see (have in scope) the same variable definition visible to the if expression as a whole, but that the consequent sees the variables defined in the condition. None of the variables defined in the if expression are visible in the succeeding scope of the if expression.
 
Unless stated otherwise, all text on this page which is either unattributed or by Mark S. Miller is hereby placed in the public domain.
ERights Home elang / kernel 
Back to: Hide Expression On to: Escape Expression
Download    FAQ    API    Mail Archive    Donate

report bug (including invalid html)

Golden Key Campaign Blue Ribbon Campaign