how: info on how to traverse abstract format trees?

View: New views
9 Messages — Rating Filter:   Alert me  

how: info on how to traverse abstract format trees?

by Tim Fletcher :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The ERTS User's Guide describes the abstract format, but is there any
info (official or otherwise) on how best to traverse these trees? It's
possible to get most of the way by just matching on the terms and
guessing which parts to traverse next, but it would be easier if there
was a clearer definition of what should be handled.
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: how: info on how to traverse abstract format trees?

by Gleb Peregud :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Jun 24, 2008 at 1:57 PM, Tim Fletcher <twoggle@...> wrote:
> The ERTS User's Guide describes the abstract format, but is there any
> info (official or otherwise) on how best to traverse these trees? It's
> possible to get most of the way by just matching on the terms and
> guessing which parts to traverse next, but it would be easier if there
> was a clearer definition of what should be handled.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@...
> http://www.erlang.org/mailman/listinfo/erlang-questions

You may take a look at newly created "frabjous" project by Kevin Smith

Here it is:
http://weblog.hypotheticalabs.com/?p=275

<quote>
First, it interfaces directly with the compiler and handles all of the
low-level details around iterating over the AST and other parse
transform muck. Frabjous knows all about multi-pass transforms and
provides a persistent state mechanism for parse transformers so they
can remember where they've been and what they're doing.
</quote>

--
Gleb Peregud
http://gleber.pl/

Every minute is to be grasped.
Time waits for nobody.
-- Inscription on a Zen Gong
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: how: info on how to traverse abstract format trees?

by Tim Fletcher :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> You may take a look at newly created "frabjous" project by Kevin Smith

That's where my question came from :)

AFAIK frabjous doesn't yet handle deep structures like function
definitions. So if I wanted to transform expressions that can be
contained in function definitions (e.g. function calls), I would have
to write the additional traversal code myself.

I'm just checking to see if there might be some recommended/documented
way of doing such a traversal. Trying to work it out from parse
transform code directly is a bit intimidating (which is why frabjous
is very appealing).
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: how: info on how to traverse abstract format trees?

by Vlad Dumitrescu-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

On Tue, Jun 24, 2008 at 3:46 PM, Tim Fletcher <twoggle@...> wrote:
I'm just checking to see if there might be some recommended/documented
way of doing such a traversal. Trying to work it out from parse
transform code directly is a bit intimidating (which is why frabjous
is very appealing).

You can use syntax_tools. They let you construct and take apart ASTs without having to know the exact representation.

regards,
Vlad
 

_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: how: info on how to traverse abstract format trees?

by Tim Fletcher :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> You can use syntax_tools. They let you construct and take apart ASTs without
> having to know the exact representation.

That makes my code considerably clearer. Thanks for the pointer.
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: how: info on how to traverse abstract format trees?

by Thomas Lindgren :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message




--- On Tue, 6/24/08, Tim Fletcher <twoggle@...> wrote:

> I'm just checking to see if there might be some
> recommended/documented
> way of doing such a traversal. Trying to work it out from
> parse
> transform code directly is a bit intimidating (which is why
> frabjous
> is very appealing).

Personally, I've always tended to write my own, among other things because the appropriate traversal differs depending on what you want to do. But the erlang distribution itself includes a few hints:

* erl_id_trans.erl contains a skeleton parse transform
* The syntax_tools application provides some stuff you may like
* I think the abstract forms format is documented somewhere too

Best,
Thomas



     
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: how: info on how to traverse abstract format trees?

by Robert Virding-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/7/3 Thomas Lindgren <thomasl_erlang@...>:

--- On Tue, 6/24/08, Tim Fletcher <twoggle@...> wrote:

> I'm just checking to see if there might be some
> recommended/documented
> way of doing such a traversal. Trying to work it out from
> parse
> transform code directly is a bit intimidating (which is why
> frabjous
> is very appealing).

Personally, I've always tended to write my own, among other things because the appropriate traversal differs depending on what you want to do. But the erlang distribution itself includes a few hints:

* erl_id_trans.erl contains a skeleton parse transform

I agree. I have a modified version of erl_id_trans.erl which threads a (in this case dummy) state structure through all the calls as well as rebuilding the tree. It is a little easier to extend to be useful. It also uses some different styles when traversing the structure as examples of when you might need to handle them differently. For example passing over a list using mapfoldl (which fits this perfectly) when you know you can handle each element separately as opposed to processing the list explicitly when you need that.

It is not really documented but you cam "see" it when you know what to look for.

Robert


_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: how: info on how to traverse abstract format trees?

by Tim Fletcher :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Personally, I've always tended to write my own, among other things because the
> appropriate traversal differs depending on what you want to do.

Sure, but for the case where you just want to rewrite a particular
function call a full parse transform seems a bit too much.


> It is not really documented but you cam "see" it when you know what to look for.

Do you mean that your modified version is somewhere in the Erlang
distribution, or am I misunderstanding you?
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: how: info on how to traverse abstract format trees?

by Robert Virding-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/7/24 Tim Fletcher <twoggle@...>:
> Personally, I've always tended to write my own, among other things because the
> appropriate traversal differs depending on what you want to do.

Sure, but for the case where you just want to rewrite a particular
function call a full parse transform seems a bit too much.

Perhaps, but then feeping creaturism usually ensures you want to more soon. :-)
 
> It is not really documented but you cam "see" it when you know what to look for.

Do you mean that your modified version is somewhere in the Erlang
distribution, or am I misunderstanding you?

No, it's not in the distribution. I will send it to anyone who is interested. What I meant that there is no extra documentation describing the different styles, but that you can see it if you are looking for it. It is quite trivial really.

Robert


_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions