Last active
September 10, 2019 07:06
-
-
Save archan937/ce525b59a87b3332c8d69a1ddb1a22c2 to your computer and use it in GitHub Desktop.
Amsterdam |> Elixir (10-09-2019) (see also: https://github.com/archan937/clustorage)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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