Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Implement the Ternary Operator in the DLR
    primarykey
    data
    text
    <p>I am implementing a language interpreter in C# using the DLR, and I'm having some troubles with the ternary operator. At this point, I have basic function declarations/calls implemented, like so:</p> <pre><code>F := (x) -&gt; x + 1 F(1) # returns 2 </code></pre> <p>I've not had a problem with a function body being a sequence of expressions -- the value of the last expression is always returned, and I've made sure all cases in the interpreter return at least <em>something</em> as a side effect. I'm now trying to implement the ternary operator (? :). The Expression tree I'm rendering looks like this:</p> <pre><code>work = Expression.IfThenElse( Expression.IsTrue(Expression.Convert(work, typeof(Boolean))), trueExp, falseExp); </code></pre> <p>where trueExp and falseExp are both valid expressions.</p> <p>The problem seems to be that the IfThenElse expression does not return a value, so basically even though trueExp and falseExp are building expression trees, the end result of the IfThenElse expression is always null. Short of making a Runtime function and explicitly calling it, is there a way to implement the ternary operator using the DLR? (ie: an Expression. that does the IfThenElse and returns the actual values in the true and false clauses?)</p> <p>What I hope to parse is something like:</p> <pre><code>F := (x) -&gt; (x = 1) ? 4 : 5 F(1) #4 F(2) #5 </code></pre> <p>But right now this always returns null when compiled into a program, because of the problem outlined above.</p> <p>I'd appreciate any help, this is quite vexing!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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