Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 10 | Comments: 37

filter by tags archive

Inline Anonymous Visitors

time to read 2 min | 291 words

One of the most common chores when working with compilers is the need to create special visitors. I mean, I just need to get a list of all the variables in the code, but I need to create a visitor class and execute it in order to get the information out. This is not hard, the code is something like this:

public class ReferenceVisitor : DepthFirstVisitor
{
     public List<string> References = new List<string>();
 
     public void OnReferenceExpression(ReferenceExpression re)
     {
            References.Add(re.Name); 
     }
}
public bool IsCallingEmployeeProperty(Expression condition)
{ 
    var visitor = new ReferenceVisitor();
    visitor.Visit(condition);
    return visitor.References.Contains("Employee"); 
}

Doing this is just annoying. Especially when you have to create several of those, and they make no sense outside of their call site. In many ways, they are to compilers what event handlers are to UI components.

What would happen if we could create a special visitor inline, without going through the "create whole new type" crap? I think that this would be as valuable as anonymous delegates and lambdas turned out to be. With that in mind, let us see if I can make this work, shall we?

public bool IsCallingEmployeeProperty(Expression condition)
{
	var references = new List<string>();
	new InlineVisitor
	{
		OnRefefenceExpression = re => references.Add(re.Name) 
	}.Visit(condition);
	return references.Contains("Employee"); 
}

Especially in the more moderately complex scenarios, such a thing is extremely useful.


Comments

ceilidhboy

To make it even more concise, add a static utility method:

...

InlineVisitor.Visit(condition, re => references.Add(re.Name));

Ayende Rahien

Not really, this would work if and only if I wanted to run only over ReferenceExpression, and I want this over the entire AST.

configurator

[NOTE: I have no idea what you are talking about. I have not built compilers (yet) and I do now know what visitors are. But...]

I don't see why you can't make such a class:

public class InlineVisitor : DepthFirstVisitor

{

 public Action<ReferenceExpression> OnReferenceExpressionHandler { private get; set; }


 public void OnReferenceExpression(ReferenceExpression re)

 {

        OnReferenceExpressionHandler(re);

 }

}

which you would use exactly as you specified.

Or is that exactly what you intended to do?

Ayende Rahien

That is exactly what I was referring to.

configurator

How is this used, if you don't mind me asking?

I mean, who calls that OnReferenceExpression method?

Ayende Rahien

Just like your code is doing, by the actual DepthFirstVisitor

Andrey Shchekin

I just understood what it reminds me of:

Mock(mock => {

    mock.Expect(x => x.OnReferenceExpression(...))

              .Callback(re => references.Add(re.Name))

}).Visit(condition)

which is much uglier than what you propose, bt is not limited to visitors. Just rename Mock to something fitting, like Inline<>.

As for uglyness,

new Inline {

     { x => x.OnReferenceExpression(null), re => references.Add(re.Name) }

};

I think with a ton of overloads for Add(,) it may be viable.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. Production postmortem: The case of the memory eater and high load - 4 hours from now
  2. Production postmortem: The case of the lying configuration file - about one day from now
  3. Production postmortem: The industry at large - 2 days from now
  4. The insidious cost of allocations - 3 days from now
  5. Find the bug: The concurrent memory buster - 4 days from now

And 4 more posts are pending...

There are posts all the way to Sep 10, 2015

RECENT SERIES

  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    14 Aug 2015 - The case of the man in the middle
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats