Skip to content

Instantly share code, notes, and snippets.

@SHyx0rmZ
Created August 28, 2015 21:04
Show Gist options
  • Select an option

  • Save SHyx0rmZ/62885218a93bb190ebb0 to your computer and use it in GitHub Desktop.

Select an option

Save SHyx0rmZ/62885218a93bb190ebb0 to your computer and use it in GitHub Desktop.
-module(demo).
-export([ main/0, test/1 ]).
main() ->
Module = erl_syntax:attribute(
erl_syntax:atom(module),
[
erl_syntax:atom("lang")
]
),
Export = erl_syntax:attribute(
erl_syntax:atom(export),
[
erl_syntax:list(
[
erl_syntax:arity_qualifier(
erl_syntax:atom("lang"),
erl_syntax:integer(0)
)
]
)
]
),
Function = erl_syntax:function(
erl_syntax:atom("lang"),
[
erl_syntax:clause(
[],
none,
[
erl_syntax:infix_expr(
erl_syntax:application(
erl_syntax:atom("demo"),
erl_syntax:atom("test"),
[
erl_syntax:integer(1337)
]
),
erl_syntax:operator('+'),
erl_syntax:integer(1)
)
]
)
]
),
Forms = [ erl_syntax:revert(Ast) || Ast <- [ Module, Export, Function ] ],
case compile:forms(Forms) of
{ ok, ModuleName, Binary } ->
code:load_binary(ModuleName, "lang.demo", Binary);
{ ok, ModuleName, Binary, _Warnings } ->
code:load_binary(ModuleName, "lang.demo", Binary)
end,
io:format("~p~n", [ lang:lang() ]),
ok.
test(Int) ->
Int * 2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment