Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Expand AST using Macro.Env API #293

Open
wants to merge 218 commits into
base: master
Choose a base branch
from
Open

[WIP] Expand AST using Macro.Env API #293

wants to merge 218 commits into from

Conversation

lukaszsamson
Copy link
Collaborator

This PR replaces MetadataBuilder implementation with a mini compiler that is close to what elixir compiler does when traversing AST. It expands special forms and macros (if possible).
Key differences from elixir compiler:

  • It is designed to be error tolerant. In most places where elixir will throw off the compilation it attempt to continue
  • It does not do any tracing
  • It doesn't evaluate code. It means that it is not able to handle dynamic code and quoted defs
  • it is not able to expand local macros. This would require evaluating and compiling the code
  • it intercepts a few Kernel, Record macros (defmodule, def, defguard, defdelegate, @, defrecord, defprotocol, defimpl), attempts to traverse the code and extract info

# res |> elem(0) |> IO.inspect
# res
{q, q_context, q_prelude} =
__MODULE__.Quote.build(meta, line, file, context, unquote_opt, generated, et)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need to build the quoted expression? Does the compiler care about returned results or do you only care about building the proper environment?

If you really need this functionality, we should find ways to expose it from Elixir. You should not re-implement this part of the compiler. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure yet. In the beginning I did it to verify if my expansion is correct. Now I wonder if at some point we would need true compilation and evaluation


{:function, module, fun} ->
{ar, af} =
case __MODULE__.Rewrite.inline(module, fun, arity) do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to inline as well? It is an optimization, you should not need to care about it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I do. Otherwise I need to deal with both inlined and not inlined versions as Kernel macros return inlined versions when expanded

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question though is: do you care about the result of the expansion? My understanding is that you'd only care about how it affects the environment but you don't need the actual expansion result, which should help reduce some of the complexity here.


defp expand_macro(
meta,
Record,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine to special case Kernel, but it would be nice to make Record and Protocol functions work without special casing entries here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to do that without eval though. In case of Record I plan to extract field info similarly to what I do with defstruct

defp expand_macro(
meta,
Kernel,
:defprotocol,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why regular expansion is not enough here? Anything we can do in Elixir to improve it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't work without actually evaling the module body

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last time we chatted, the theory is that you would be able to expand and track some variables in order to make this work. It would take a bit of complexity but the upside is that it would make the code more generic and make more expansions work. Feel free to ping me about this and we can jump into a chat later?

env
)
when is_atom(name) do
# transform protocol def to def with empty body

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the regular macro expansion do this anyway? Why do we need to intercept?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eval. I also collect some info about protocols and implementations


defp expand_macro(
meta,
ExUnit.Case,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we would find ways of making all ExUnit features work without special cases!

end
end

defmodule Bitstring do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably simplify the bitstring implementation. A lot of the work here is to normalize the bitstrings, which you don't care. You would probably be happy enough by simply traversing the left side of :: (or the entry if there is no ::).

Copy link
Collaborator Author

@lukaszsamson lukaszsamson Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Here I care only for the parts of tree that recurse into expand or define/use variables or call macros

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, variables can only be defined on the left side, so you are good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants