José, thank you as always for your engagement. I am basically hand-crafting the AST, mostly via leex/yecc with a few after-parsing transformations. So I’m left with an AST in literal form (i.e. as a list of tuples - is that the right expression?). What I’ve worked on in the mean time that does what I want is:
# Generate the functions to process plural rules @spec do_cardinal(binary, number, number, number, number, number, number) :: :one | :two | :few | :many | :other Enum.each Cldr.known_locales, fn (locale) -> function_body = cardinal_rules[locale] |> rules_to_condition_statement(__MODULE__) function = quote do defp do_cardinal(unquote(locale), n, i, v, w, f, t), do: unquote(function_body) end Code.eval_quoted(function, [], __ENV__) end In this case there’s no macro involved but I feel a little “unclean” about doing the Code.eval_quoted/3 part. > On 29 Jul 2016, at 5:50 PM, José Valim <jose.va...@plataformatec.com.br> > wrote: > > Well, quoted expressions are meant to represent the code unless it is > escaped, then you get the AST literal. So maybe the question is where the AST > is being escaped? > > On Friday, July 29, 2016, Kip Cole <kipco...@gmail.com > <mailto:kipco...@gmail.com>> wrote: > I have a macro to define a function where one of the parameters is the AST of > the function body. But I can't work out how to insert the AST. Example: > > defmacro define_do_cardinal(rules, locale) do > quote do > defp do_cardinal(unquote(locale), n, i, v, w, f, t), do: > unquote(rules) > end > end > > unquote(rules) won't do it since unquoting doesn't add the code to the > function it just inserts the AST as a literal. I could, I suppose, hand > craft the function header AST and insert the function body AST but that seems > more work than I would expect. > > Is there a way to insert an AST as the code for a function body? > > -- > You received this message because you are subscribed to the Google Groups > "elixir-lang-talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to <>elixir-lang-talk+unsubscr...@googlegroups.com > <mailto:elixir-lang-talk+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-talk/34402692-3d20-447f-aca4-7e815d7badcc%40googlegroups.com > > <https://groups.google.com/d/msgid/elixir-lang-talk/34402692-3d20-447f-aca4-7e815d7badcc%40googlegroups.com?utm_medium=email&utm_source=footer>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. > > > -- > > > José Valim > www.plataformatec.com.br <http://www.plataformatec.com.br/> > Skype: jv.ptec > Founder and Director of R&D > > > -- > You received this message because you are subscribed to a topic in the Google > Groups "elixir-lang-talk" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/elixir-lang-talk/kaJJGQVWdMs/unsubscribe > <https://groups.google.com/d/topic/elixir-lang-talk/kaJJGQVWdMs/unsubscribe>. > To unsubscribe from this group and all its topics, send an email to > elixir-lang-talk+unsubscr...@googlegroups.com > <mailto:elixir-lang-talk+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-talk/CAGnRm4%2B_Nn2_01a-2FJHSDrwdLayWzQdyD1i%2BNmxdUrrjL%3D8xg%40mail.gmail.com > > <https://groups.google.com/d/msgid/elixir-lang-talk/CAGnRm4%2B_Nn2_01a-2FJHSDrwdLayWzQdyD1i%2BNmxdUrrjL%3D8xg%40mail.gmail.com?utm_medium=email&utm_source=footer>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-talk+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/7F2FCE8E-2770-493A-AB16-F969B9985BAF%40gmail.com. For more options, visit https://groups.google.com/d/optout.