Copyright (c) 2006-2007, 2016 AUTHORS
Version: 0.0.1
Simple Metaprogramming for Erlang
Smerl is an Erlang library that simplifies the creation and manipulation of Erlang modules at runtime, using Erlang's capabilities for hot code swapping and abstract syntax tree transformations to do its magic.
New functions can be expressed either as strings of Erlang code or as abstract forms.
For more information, read the Abstract Format section in the ERTS User's guide.
Smerl was inspired by the rdbms_codegen
module in the RDBMS
application (part of Jungerl) written by Ulf Wiger.
The smerl
module was extracted from the abandoned ErlyWeb
and patches from ErlyWeb forks.
Define a module foo
:
M1 = smerl:new(foo).
Define a function foo:bar/0
using a string of Erlang code:
{ok, M2} = smerl:add_func(M1, "bar() -> 1 + 1.").
... or using the Erlang Abstract Format:
{ok, M2} = smerl:add_func(M1, {function, 1, bar, 0,
[{clause, 1, [], [],
[{op, 1, '+',
{integer, 1, 1},
{integer, 1, 1}}]}]}).
The abstract format may look more verbose in this example, but it's also easier to manipulate in code.
Compile the foo
module and confirm the expected results:
smerl:compile(M2),
foo:bar(), % returns 2
smerl:has_func(M2, bar, 0). % returns true
Section order:
- Types
- Macros
- Callbacks
- Public API
- Deprecations
- Private Parts
Install the git
pre-commit hook:
./bin/pre-commit.sh install
The pre-commit check can be skipped by passing --no-verify
to git commit
.
See the AUTHORS file.
Smerl is licensed under The MIT License.
smerl |