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.

Revisions

  1. archan937 revised this gist Sep 10, 2019. No changes.
  2. archan937 renamed this gist Mar 1, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. archan937 revised this gist Feb 28, 2019. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  4. archan937 revised this gist Feb 28, 2019. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  5. archan937 revised this gist Feb 28, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ast.example.ex
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # iex(1)>
    # [ Paste AST ]
    # [ Paste AST module ]

    # iex(2)>
    # Generate AST module for the module `Paul.Engel`
  6. archan937 revised this gist Feb 28, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ast.example.ex
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ ast |> Macro.to_string() |> IO.puts()

    # iex(4)>
    # Compile the generated AST
    [{mod, bytecode}] = Code.compile_quoted(ast)
    [{mod, binary}] = Code.compile_quoted(ast)

    # iex(5)>
    # Ask Paul.Engel to say hi ;)
  7. archan937 revised this gist Feb 28, 2019. No changes.
  8. archan937 created this gist Feb 28, 2019.
    19 changes: 19 additions & 0 deletions ast.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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
    18 changes: 18 additions & 0 deletions ast.example.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    # iex(1)>
    # [ Paste AST ]

    # 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, bytecode}] = Code.compile_quoted(ast)

    # iex(5)>
    # Ask Paul.Engel to say hi ;)
    Paul.Engel.say_hi("stranger")
    17 changes: 17 additions & 0 deletions examples.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # 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)