roehst

roehst

Hi, I have been playing around with the Elixir AST (trying to help with an Issue in Credo) and I found two things that I would like to know more about.

The issue was that Credo has an ABC complexity metric check and it has to detect function calls, but the AST (such as the one that Code.string_to_quoted gives) has very little syntatic information, i.e., I expected that terms would be marked with their syntatic category (variables, functions, etc).

I tried reading the Elixir compiler source or the Code and Macro modules to find if there was a way to get that information but I could not find it.

I also failed to find how to maintain a stack using the Macro.traverse/prewalk/postwalk calls (to keep information about the current lexical scope).

The book on Elixir Metaprogramming is wonderful on writing macros, but does anyone know a resource about analyzing the AST, even if that means diving deeper in the compiler and using lower-level representations? I would be immensely grateful for that!

Showing Posts 1 to 8

OvermindDL1

OvermindDL1

You can do it via the accumulator argument. Honestly though I just encode my own processing as just normal function calls, Elixir’s AST is really simple. :slight_smile:

roehst

roehst OP

I could push values to the accumulator in the prewalk function, but how can I pop them at the right time? Because for example in the prewalk it will read a node and then its children, and then it will go to its sibling node. How can I tell I’m at the last child so that I can pop information from the stack? For example suppose the stack is tracking nested function calls

OvermindDL1

OvermindDL1

Same way as you would anywhere else, just pop the values and return the new structure with the popped values removed. :slight_smile:

Use traverse so you get both ‘up’ and ‘down’ calls.

roehst

roehst OP

Are pre and post arguments in traverse the push/pop down/up calls?

OvermindDL1

OvermindDL1

Yep, pre means it is run ‘before’ recursing down, post means it is run ‘after’ recursing down, so you will push environment in on pre and pop the environment off in post. :slight_smile:

OvermindDL1

OvermindDL1

Also, the prewalk and postwalk are just like using traverse but doing nothing in the other one, so prewalk is like using the pre in traverse while doing nothing in post (fn x -> x end) and likewise for the other way. :slight_smile:

roehst

roehst OP

Cool thanks! But you roll your own traversals usually?

OvermindDL1

OvermindDL1

I tend to do slightly more complex work than most though… ^.^;

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews