-
Notifications
You must be signed in to change notification settings - Fork 41
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
base: master
Are you sure you want to change the base?
Conversation
super resolve
# Conflicts: # lib/elixir_sense/core/metadata_builder.ex # lib/elixir_sense/core/state.ex # test/elixir_sense/core/metadata_builder_test.exs
recover from errors
# res |> elem(0) |> IO.inspect | ||
# res | ||
{q, q_context, q_prelude} = | ||
__MODULE__.Quote.build(meta, line, file, context, unquote_opt, generated, et) |
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 ::
).
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
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: