Angel \”Java\” Lopez on Blog

April 13, 2011

Dynamic Language Runtime (Part 3) Binary Expressions

Filed under: .NET, C Sharp, Code Generation, Dynamic Language Runtime — ajlopez @ 2:27 pm

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),
    Expression.Constant(20));
Console.WriteLine(expr.GetType().FullName);
Console.WriteLine(expr.Type);
Console.WriteLine(expr.Left.Type);
Console.WriteLine(expr.Right.Type);
Console.WriteLine(expr.NodeType);
Console.WriteLine(expr.Left.NodeType);
Console.WriteLine(expr.Right.NodeType);

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(
    Expression.Add(Expression.Constant(3d),
        Expression.Constant(2d)),
    Expression.Constant(7d)
    );
Func<double> binaryDelegate = Expression.Lambda<Func<double>>(expr2).Compile();
Console.WriteLine(binaryDelegate.Invoke());

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

http://www.ajlopez.com

http://twitter.com/ajlopez

2 Comments »

  1. […] Filed under: .NET, C Sharp, Code Generation, Dynamic Language Runtime — ajlopez @ 10:20 am Next postPrevious […]

    Pingback by Dynamic Language Runtime (Part 2) Hello Expressions! « Angel “Java” Lopez on Blog — April 13, 2011 @ 2:33 pm

  2. Thanks for sharing. The sample is really helpful.

    Comment by barcode 2d creator in java — January 23, 2015 @ 7:01 am


RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: