hst337

hst337

How to change AST before Elixir compilation

XY Notice

I know about macros, __before_compile__ and other attributes, compiler tracers and such stuff. The question is exactly about implementing wrapper for Elixir compiler which be compatible with Mix.Task.Compiler, nothing else.

The Question

How do I write the Mix.Task.Compiler which will do some global changes to the AST before calling Elixir’s compiler to generate .beam files.

Current solution

Right now my compiler

  1. Searches for .ex files in the path specified by Mix.Project.config()[:elixirc_paths]
  2. Calls Code.string_to_quoted() on each file
  3. Changes the AST
  4. Calls Code.compile_quoted with File.write!() to the build_path of the binaries returned by compile_quoted

Current solution drawbacks

  1. I think it is possible to compile straight to file (without File.write!)
  2. I think this kind of compilation does not call compiler tracers
  3. I think it can have some problems with protocol consolidation
  4. It needs to read/write elixir’s compiler manifest, which is some kind of a private structure.
  5. It is difficult to change the AST because modules can have compile-time code in their bodies, which I need to preevaluate, to change the code inside functions.

Marked As Solved

josevalim

josevalim

Creator of Elixir

Not possible. By design. We want to avoid anything that goes and globally changes code, without a trace. You can hand roll, as in your current solution, but it will have pitfalls such as losing incremental compilation.

I would suggest taking a step back and discuss what you want to solve and try to find a way that would be considered idiomatic Elixir.

Sorry if this is not the answer you were looking for. :slight_smile:

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement