slepher / astranaut Goto Github PK
View Code? Open in Web Editor NEWtraverse erlang ast and elixir macro in erlang.
License: MIT License
traverse erlang ast and elixir macro in erlang.
License: MIT License
-use_macro({macro/0, [unique_var]}).
macro() ->
quote(
fun(A) ->
B = A + 1
B = A + 3
{C, +B} = {A + 5, A +3}
{B, C}
end).
hello(A) ->
B = A + 5,
macro().
=>
hello(A) ->
B = A + 5,
fun(A_1@Q_1) ->
B_1@Q_1 = A_1@Q_1 + 1
B_2@Q_1 = A_1@Q_1 + 3
{C_1@Q_1, B_2@Q_1} = {A_1@Q_1 + 5, A_1@Q_1 + 3}
{B_2@Q_1, C_1@Q_1}
end.
+before Var is pin operator.
just behaviours like pattern matching and pin operator in elixir
https://elixir-lang.org/getting-started/pattern-matching.html
usually pepole dont bind variable in fuction call, tuple, etc such as
hello(begin A = A +1, A end, begin A = A+ 2, A end).
{begin A = A +1, A end, begin A = A+ 2, A end}
add rebinding option strict
to enable rebinding in such cases.
strict
is default to false.
macro could return error and warning to let compiler know
exception throwed by macro should be catched by parse_transform and let compiler know.
macro expand order should be the same order of -use_macro
add parameter {order => pre | post} for -use_macro
it means which order is used for expand nested macro such as
do([state_m ||
B <- do([reader_m ||
R <- monad_reader:ask(),
return(A + R)
]),
return(B)
]).
Just opening an issue here to have a little chat. You seem to have some interesting features here, and I'm wondering if you have at all considered submitting some PRs to make some of those features directly available as part of syntax_tools?
-module(macro_example).
macro_with_vars_1(Ast) ->
quote(
begin
A = 10,
B = unquote(Ast),
A + B
end
).
macro_with_vars_2(Ast) ->
quote(
begin
A = 10,
B = unquote(Ast),
A + B
end
).
test_macro_with_vars(N) ->
A1 = macro_with_vars_1(N),
A2 = macro_with_vars_2(A1),
A3 = macro_with_vars_2(N),
A4 = macro_with_vars_1(A1),
A1 + A2.
=>
test_macro_with_vars(N) ->
A1 =
begin
A@macro_example@_1 = 10,
B@macro_example@_1 = N,
A@macro_example@_1 + B@macro_example@_1
end,
A2 =
begin
A@macro_example@_3 = 10,
B@macro_example@_3 = A1,
A@macro_example@_3 + B@macro_example@_3
end,
A3 =
begin
A@macro_example@_4 = 10,
B@macro_example@_4 = N,
A@macro_example@_4 + B@macro_example@_4
end
A4 =
begin
A@macro_example@_2 = 10,
B@macro_example@_2 = A1,
A@macro_example@_2 + B@macro_example@_2
end
A1 + A2 + A3 + A4.
-export_macro({[macro_1/0, macro_2/0], Options}).
macro_1() ->
quote(ok).
macro_2() ->
quote(ok).
Options will be merged to -use_macro options.
while import attributes is imported to macro
extra argument Attributes is added to macro function while called.
it includes all attributes of module mentioned in proplist key attrs
special attrs:
module: module use_macro
line: line where macro used
-module(a).
-behaviour(gen_server).
-use_macro({macro_a, macro/2, [{attrs, [module, line, behaviour]}]}).
hello() ->
macro_a:macro(world).
-module(macro_a).
-export([macro/2]).
macro(Ast, #{module => Module, line => Line, behaviour => Behaviours} = Attributes) ->
io:format("module is ~p, behaviour is ~p", [Module, Behaviours]),
Ast.
astranaut:replace_from_nth(Nodes, N, Forms, Options).
insert Nodes to Forms at nth.
if nodes contains -export.
-export will stay before first function definition.
more attribute to stay should be supported.
more stay before target should be supported.
If you want to pattern match against an existing variable, you need to use the ^ operator:
iex> x = 1
1
iex> case 10 do
...> ^x -> "Won't match"
...> _ -> "Will match"
...> end
"Will match"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.