Module documentation
transverse the Ast and call Fn for each node. replace the node with the value returned
replace an exact match with other:
>>> OnePlus = fn (B) { ast.replace([|1 + $(replaceme)|], replaceme, B) }
#Fun<erl_eval.6.13229925>
>>> OnePlus([|A|])
{op,1,'+',{integer,1,1},{var,1,'A'}}
>>> OnePlus([|5|])
{op,1,'+',{integer,1,1},{integer,1,5}}
replace the name of a variable:
>>> ast.rename_var([|A + B|], 'A', 'Foo')
{op,1,'+',{var,1,'Foo'},{var,1,'B'}}
replace the value of an atom:
>>> ast.replace_atom([|foo|], foo, bar)
{atom,1,bar}
>>> ast.replace_atom([|recordname.RecordVar[attribute]|], attribute, name)
{record_field,1,{var,1,'RecordVar'},recordname,{atom,1,name}}
>>> ast.replace_atom([|recordname.RecordVar[attribute="foo"]|], attribute, name)
{record,1,
{var,1,'RecordVar'},
recordname,
[{record_field,1,{atom,1,name},{string,1,"foo"}}]}
replace the parts of a record:
>>> ast.replace_record([|record_name.RecordVar[field_name]|], record_name,
person, 'RecordVar', 'P', field_name, name)
{record_field,1,{var,1,'P'},person,{atom,1,name}}
>>> ast.replace_record([|record_name.RecordVar[field_name=Val]|],
record_name, person, 'RecordVar', 'P', field_name, name)
{record,1,
{var,1,'P'},
person,
[{record_field,1,{atom,1,field_name},{var,1,'Val'}}]}
update the Line field of all the items in the ast:
>>> ast.line(5, [| 1 + 2 * 3 |])
{op,5,'+',{integer,5,1},{op,5,'*',{integer,5,2},{integer,5,3}}}
extract the first clause Ast from a fun definition
extract the first clause Ast from a fun definition and update line numbers:
>>> ast.clause(4, [|fn (X) { X + 1 }|])
{clause,4,[{var,4,'X'}],[],[{op,4,'+',{var,4,'X'},{integer,4,1}}]}
>>> ast.clause(4, [|fn (X) when X > 4 { X + 1 }|])
{clause,4,
[{var,4,'X'}],
[[{op,4,'>',{var,4,'X'},{integer,4,4}}]],
[{op,4,'+',{var,4,'X'},{integer,4,1}}]}
extract the clauses AST from a fun definition
extract the clauses AST from a fun definition and update line numbers:
>>> ast.clauses(42, [|fn (1) { 1 } fn (X) { X }|])
[{clause,42,[{integer,42,1}],[],[{integer,42,1}]},
{clause,42,[{var,42,'X'}],[],[{var,42,'X'}]}]
return the AST representing a fun where the clauses are the ones passed as parameter
return the ast representation of a efene data structure:
return the ast representation of a efene data structure. Replace the atoms that start with $ to a var AST node:
return the AST representation of an attribute definition
return the AST representation of an attribute definition
return the AST representation of a global attribute definition
return the public attribute
return the AST to define a new record type:
>>> ast.rec($line, person, [firstname, lastname, (mail, "none")])
{global_attribute,1,record,
{person,[{record_field,1,{atom,1,firstname}},
{record_field,1,{atom,1,lastname}},
{record_field,1,{atom,1,mail},{string,1,"none"}}]}}
return the AST that matches record attributes, used in instantiation and pattern matching:
>>> ast.rec_match(3, person, [(firstname, '$FirstName'), (lastname, '$LastName'), (mail, '$Mail')])
{record,3,person,
[{record_field,3,{atom,3,firstname},{var,3,'FirstName'}},
{record_field,3,{atom,3,lastname},{var,3,'LastName'}},
{record_field,3,{atom,3,mail},{var,3,'Mail'}}]}
return the AST for a top level function definition from a anonymous function definition:
>>> ast.function_from_fun(4, add, [|fn (A, B) { A + B } |])
{function,4,add,2,
[{clause,4,
[{var,4,'A'},{var,4,'B'}],
[],
[{op,4,'+',{var,4,'A'},{var,4,'B'}}]}]}
replace the arguments from a clause
replace the arguments from a fun