Visualising programs and program transformations

Hello

Are there any existing tools that can take racket code and visualise it as the tree it is? I would like to visualise program transformations effected by macros.

So, for example, given:

(define a (+ 5 6))

The graph could have a node "a" with an incoming edge from a node "+" with two incoming edges from literals "5" and "6".

I (think I) could write something like this using the graph module, converting a program to a list and then parsing the list recursively , but I am wondering if something like this already exists (?)

hello,

i do not know in Racket, but this make me think of a project in Kawa to display graphically Scheme code:

after Macro expansion is very different than procedures evaluation, and i'm not sure the project above, that i have not tested, just see a video demo, is doing the same with macro too.

Indeed it is really a wonderful project,see video:

This is not what I am after but it looks interesting overall, thank you.

For macros, try the macro stepper in DrRacket. There’s a text utility, too, but I haven’t looked at what it takes to wrap it in a nice interface for use outside a REPL.

For drawing trees, try tree-layout with custom nodes? I thought there was already work on this for S-expressions but I didn’t find it quickly.

I agree. Moreover, the Macro Debugger: Inspecting Macro Expansion exports a lot of the functionality. So it's possible to reuse part of it to get a more visual reprresentation (but I never tried myself). Also, the syntaxs have location information that may be useful to display the code as similar as the original as possible, but after a few macro expansions the mix of original parts and macro introduced parts may cause a lot of problems.

3 Likes

tree-layout is fantastic, will keep it in mind for future work, thank you. At the moment, I am happy with the round-trip to graphviz :slight_smile:

Yep, I guess the macro-stepper is another way of doing this but I was wondering if there was something that could be done programmatically so that you can show various levels of transformations on arbitrary source code input.

The Macro Debugger: Inspecting Macro Expansion suggested by gus-masa is what I was talking about.

@aanastasiou

Is it box-and-pointer diagrams you are interested in?

One tool:

A nice one by Jack Rosenthal:
https://docs.racket-lang.org/sdraw/

There might be a couple more on the package server.

2 Likes

Oh this is going to come in so handy! Thank you very much :slight_smile: