Dynamic Language Runtime (Part 3) Binary Expressions

Next post
Previous post

I implement simple examples using DLR. I showed how to create and evaluate expressions. Every Expression has some exposed properties and methods:

Note that each Expression object has an NodeType, and separated Type. Let explore that values:

BinaryExpression expr = Expression.Add(Expression.Constant(10),

This is the first time I wrote an Expression.Add. This is a binary expression. Every binary expression has to leafs:

The output of the above code:

Note that the .GetType() (the .NET type) of the created Expression, is SimpleBinaryExpression: it’s not only an Expression object. But there is no AddExpression in DLR. The expr.NodeType exposes that the created expression is an Add one. DLR creators decided not to write a dedicated class for Expression.Add.

The BinaryExpression has a Type: System.Int32, an integer. This is the type that indicates what is the type of the returned value. It is derived from the types of the left and right part of the binary expression.

As in C#, we can combine binary expressions:

// double result = (3d + 2d) / 7d;
// Console.WriteLine(result);
BinaryExpression expr2 = Expression.Divide(
Func<double> binaryDelegate = Expression.Lambda<Func<double>>(expr2).Compile();

Main source for this example: “Pro DLR in .NET 4” by Chaur Wu. The code of this post series is at  http://code.google.com/p/ajcodekatas/ under trunk/Dlr.

Next steps: IfThenElse expressions, switch expression, what is a Visitor? and a Reduce?.

Keep tuned!

Angel “Java” Lopez



2 thoughts on “Dynamic Language Runtime (Part 3) Binary Expressions

  1. Pingback: Dynamic Language Runtime (Part 2) Hello Expressions! « Angel “Java” Lopez on Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s