How to implement ternary operator in DLR

I am using a C # language interpreter using DLR and I am having problems with the ternary operator. At the moment, I have basic declarations / function calls, for example:

F := (x) -> x + 1
F(1)   # returns 2

I had no problem with the fact that the body of the function is a sequence of expressions - the value of the last expression is always returned, and I made sure that all cases in the interpreter return at least something as a side effect. Now I'm trying to implement a ternary operator (? :). The expression tree that I create looks like this:

work = Expression.IfThenElse(                                   
    Expression.IsTrue(Expression.Convert(work, typeof(Boolean))), 
    trueExp, 
    falseExp);

where trueExp and falseExp are valid expressions.

The problem is that the IfThenElse expression does not return a value, so basically, although trueExp and falseExp build expression trees, the final result of the IfThenElse expression is always zero. If you are not executing the Runtime function and explicitly calling it, is there a way to implement the ternary operator using DLR? (i.e.: an expression that IfThenElse does and returns the actual values ​​in the sentences true and false?)

What I hope to make out is something like:

F := (x) -> (x = 1) ? 4 : 5
F(1)   #4
F(2)   #5

But right now, it always returns null when compiling into a program due to the problem described above.

I would be grateful for any help, this is pretty unpleasant!

+5
source share
1 answer

Expression.IfThenElseis a construct if (...) ... else ...;, not a ternary operator.

Ternary operator Expression.Condition

+14
source

All Articles