cerl_trees (compiler v8.0.2)

Basic functions on Core Erlang abstract syntax trees.

Syntax trees are defined in the module cerl.

DATA TYPES

cerl() = cerl:cerl()

Link to this section Summary

Functions

Returns the length of the longest path in the tree. A leaf node has depth zero, the tree representing "{foo, bar}" has depth one, etc.

Does a fold operation over the nodes of the tree. The result is the value of Function(X1, Function(X2, ... Function(Xn, Unit) ... )), where X1, ..., Xn are the nodes of Tree in a post-order traversal.

Like variables/1, but only includes variables that are free in the tree.

Labels each expression in the tree. A term {label, L} is prefixed to the annotation list of each expression node, where L is a unique number for every node, except for variables (and function name variables) which get the same label if they represent the same variable. Constant literal nodes are not labeled.

Maps a function onto the nodes of a tree. This replaces each node in the tree by the result of applying the given function on the original node, bottom-up.

Does a combined map/fold operation on the nodes of the tree. This is similar to map/2, but also propagates a value from each application of Function to the next, starting with the given value Initial, while doing a post-order traversal of the tree, much like fold/3.

Does a combined map/fold operation on the nodes of the tree. It begins by calling Pre on the tree, using the Initial value. Pre must either return a tree with an updated accumulator or the atom skip.

Returns a integer variable name higher than any other integer variable name in the syntax tree. An exception is thrown if Tree does not represent a well-formed Core Erlang syntax tree.

Returns the number of nodes in Tree.

Returns an ordered-set list of the names of all variables in the syntax tree. (This includes function name variables.) An exception is thrown if Tree does not represent a well-formed Core Erlang syntax tree.

Link to this section Functions

Link to this function

depth(T:: cerl:cerl() ) -> non_neg_integer()

Specs

depth(cerl:cerl()) -> non_neg_integer().

Returns the length of the longest path in the tree. A leaf node has depth zero, the tree representing "{foo, bar}" has depth one, etc.

Link to this function

fold(F::( cerl:cerl() , term()) -> term(), S::term(), T:: cerl:cerl() ) -> term()

Specs

fold(fun((cerl:cerl(), term()) -> term()), term(), cerl:cerl()) -> term().

Does a fold operation over the nodes of the tree. The result is the value of Function(X1, Function(X2, ... Function(Xn, Unit) ... )), where X1, ..., Xn are the nodes of Tree in a post-order traversal.

See also: mapfold/3.

Link to this function

free_variables(T:: cerl:cerl() ) -> [ cerl:var_name() ]

Specs

free_variables(cerl:cerl()) -> [cerl:var_name()].

Like variables/1, but only includes variables that are free in the tree.

See also: next_free_variable_name/1, variables/1.

Link to this function

get_label(T:: cerl:cerl() ) -> top | integer()

Specs

get_label(cerl:cerl()) -> top | integer().
Link to this function

label(T:: cerl:cerl() ) -> { cerl:cerl() , integer()}

Specs

label(cerl:cerl()) -> {cerl:cerl(), integer()}.

Equivalent to label(Tree, 0).

Link to this function

label(T:: cerl:cerl() , N::integer()) -> { cerl:cerl() , integer()}

Specs

label(cerl:cerl(), integer()) -> {cerl:cerl(), integer()}.

Labels each expression in the tree. A term {label, L} is prefixed to the annotation list of each expression node, where L is a unique number for every node, except for variables (and function name variables) which get the same label if they represent the same variable. Constant literal nodes are not labeled.

The returned value is a tuple {NewTree, Max}, where NewTree is the labeled tree and Max is 1 plus the largest label value used. All previous annotation terms on the form {label, X} are deleted.

The values of L used in the tree is a dense range from N to Max - 1, where N =< Max =< N + size(Tree). Note that it is possible that no labels are used at all, i.e., N = Max.

Note: All instances of free variables will be given distinct labels.

See also: label/1, size/1.

Link to this function

map(F::( cerl:cerl() ) -> cerl:cerl() , T:: cerl:cerl() ) -> cerl:cerl()

Specs

map(fun((cerl:cerl()) -> cerl:cerl()), cerl:cerl()) -> cerl:cerl().

Maps a function onto the nodes of a tree. This replaces each node in the tree by the result of applying the given function on the original node, bottom-up.

See also: mapfold/3.

Link to this function

mapfold(F::( cerl:cerl() , term()) -> { cerl:cerl() , term()}, S0::term(), T:: cerl:cerl() ) -> { cerl:cerl() , term()}

Specs

mapfold(fun((cerl:cerl(), term()) -> {cerl:cerl(), term()}), term(), cerl:cerl()) ->
           {cerl:cerl(), term()}.

Does a combined map/fold operation on the nodes of the tree. This is similar to map/2, but also propagates a value from each application of Function to the next, starting with the given value Initial, while doing a post-order traversal of the tree, much like fold/3.

This is the same as mapfold/4, with an identity function as the pre-operation.

See also: fold/3, map/2, mapfold/4.

Link to this function

mapfold(Pre::( cerl:cerl() , term()) -> { cerl:cerl() , term()} | skip, Post::( cerl:cerl() , term()) -> { cerl:cerl() , term()}, S00::term(), T0:: cerl:cerl() ) -> { cerl:cerl() , term()}

Specs

mapfold(fun((cerl:cerl(), term()) -> {cerl:cerl(), term()} | skip),
        fun((cerl:cerl(), term()) -> {cerl:cerl(), term()}),
        term(),
        cerl:cerl()) ->
           {cerl:cerl(), term()}.

Does a combined map/fold operation on the nodes of the tree. It begins by calling Pre on the tree, using the Initial value. Pre must either return a tree with an updated accumulator or the atom skip.

If a tree is returned, this function deconstructs the top node of the returned tree and recurses on the children, using the returned value as the new initial and carrying the returned values from one call to the next. Finally it reassembles the top node from the children, calls Post on it and returns the result.

If skip is returned, it returns the tree and accumulator as is.

Link to this function

next_free_variable_name(T:: cerl:cerl() ) -> integer()

Specs

next_free_variable_name(cerl:cerl()) -> integer().

Returns a integer variable name higher than any other integer variable name in the syntax tree. An exception is thrown if Tree does not represent a well-formed Core Erlang syntax tree.

See also: free_variables/1, variables/1.

Link to this function

size(T:: cerl:cerl() ) -> non_neg_integer()

Specs

size(cerl:cerl()) -> non_neg_integer().

Returns the number of nodes in Tree.

Link to this function

variables(T:: cerl:cerl() ) -> [ cerl:var_name() ]

Specs

variables(cerl:cerl()) -> [cerl:var_name()].

Returns an ordered-set list of the names of all variables in the syntax tree. (This includes function name variables.) An exception is thrown if Tree does not represent a well-formed Core Erlang syntax tree.

See also: free_variables/1, next_free_variable_name/1.