Skip to content

Instantly share code, notes, and snippets.

@archan937
Last active September 10, 2019 07:06
Show Gist options
  • Save archan937/ce525b59a87b3332c8d69a1ddb1a22c2 to your computer and use it in GitHub Desktop.
Save archan937/ce525b59a87b3332c8d69a1ddb1a22c2 to your computer and use it in GitHub Desktop.
Amsterdam |> Elixir (10-09-2019) (see also: https://github.com/archan937/clustorage)
# iex(1)>
ast = quote do: 1 + 1
# iex(2)>
Code.eval_quoted(ast)
# iex(3)>
ast = quote do: sum(1, 2 + 3)
# iex(4)>
ast = quote do: fn(a, b) -> a * b end
# iex(5)>
{func, []} = Code.eval_quoted(ast)
# iex(6)>
func.(2, 4)
defmodule AST do
def compile(module) do
module
|> generate()
|> Code.compile_quoted()
end
def generate(module) do
quote do
defmodule unquote(module) do
def say_hi(name) do
IO.puts("Hi, " <> name)
end
end
end
end
end
# iex(1)>
# [ Paste AST module ]
# iex(2)>
# Generate AST module for the module `Paul.Engel`
ast = AST.generate(Paul.Engel)
# iex(3)>
# Print the generated AST to Elixir code
ast |> Macro.to_string() |> IO.puts()
# iex(4)>
# Compile the generated AST
[{mod, binary}] = Code.compile_quoted(ast)
# iex(5)>
# Ask Paul.Engel to say hi ;)
Paul.Engel.say_hi("stranger")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment