ast

Module documentation

transform/2

transverse the Ast and call Fn for each node. replace the node with the value returned

replace/3

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}}

rename_var/3

replace the name of a variable:

>>> ast.rename_var([|A + B|], 'A', 'Foo')
{op,1,'+',{var,1,'Foo'},{var,1,'B'}}

replace_atom/3

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_record/7

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'}}]}

line/2

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}}}

clause/1

extract the first clause Ast from a fun definition

clause/2

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}}]}

clauses/1

extract the clauses AST from a fun definition

clauses/2

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'}]}]

clauses_to_fun/2

return the AST representing a fun where the clauses are the ones passed as parameter

to_ast/2

return the ast representation of a efene data structure:

to_ast/3

return the ast representation of a efene data structure. Replace the atoms that start with $ to a var AST node:

attribute/2

return the AST representation of an attribute definition

attribute/3

return the AST representation of an attribute definition

global_attribute/3

return the AST representation of a global attribute definition

public/1

return the public attribute

rec/3

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"}}]}}

rec_match/3

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'}}]}

function_from_fun/3

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'}}]}]}

set_clause_args/2

replace the arguments from a clause

set_fun_args/2

replace the arguments from a fun

Table Of Contents

Previous topic

Module Index

Next topic

dct

blog comments powered by Disqus