Tree

public abstract class Tree

A node of an Abstract Syntax Tree.

Constructors

Tree

protected Tree(Operator operator)

Build a new Tree with given operator.

Methods

addChild

public void addChild(Tree addedChild, int rank)

Adds a new child to this node. Allowed only when this is a list node. Inserts the added child so that its rank will be “rank” (rank of 1st child is 1). Negative ranks are allowed, designating positions backward from the end, e.g -1 is last child, -2 is last but one.

addChildren

public void addChildren(Tree[] addedChildren, int rank)

Adds a set of new children to this node. Allowed only when this is a list node. Inserts the addedChildren so that the rank of the first of them will be “rank” (rank of 1st child is 1). Negative ranks are allowed, designating positions backward from the end, e.g -1 is last child, -2 is last but one. When rank is negative, it will be the position of the last of addedChildren.

addChildren

public void addChildren(TapList<Tree> addedChildren, int rank)

Adds a set of new children to this node. Allowed only when this is a list node. Inserts the addedChildren so that the rank of the first of them will be “rank” (rank of 1st child is 1). Negative ranks are allowed, designating positions backward from the end, e.g -1 is last child, -2 is last but one. When rank is negative, it will be the position of the last of addedChildren.

allAnnotations

public Enumeration<?> allAnnotations()
Returns

all annotations currently attached to this Tree.

checkSyntax

public boolean checkSyntax(String path)
Parameters
  • path – Some string that will name this Tree in potential error messages.

Returns

true if the syntax of this Tree is correct, i.e. all children trees non null and in correct phylum.

checkSyntax

protected boolean checkSyntax(Phylum phylum, String path)
Returns

true if the syntax of this Tree is correct, i.e. all children trees non null and in correct phylum.

children

public Tree[] children()
Returns

the array of the children of this Tree node. Allowed only when this is either a fixed or list arity node, but not an atom node.

childrenList

public TapList<Tree> childrenList()
Returns

the ordered list of children of this Tree node. Allowed only when this is either a fixed or list arity node, but not an atom node.

connectNewChild

protected void connectNewChild(Tree newChild, Tree[] parentNewChildren, int index)

Utility to branch a child Tree under this Tree, as its new index-th child, with a correct full management of the “parent” links.

Parameters
  • parentNewChildren – the future array of children of this parent tree, which may be different from the previous array, for instance in the case of variable-arity lists.

copy

public abstract Tree copy()
Returns

a copy of this Tree, including a copy of all its annotations. Also manages copies of unfinished identifiers such as #xd#.

copyAnnotations

public void copyAnnotations(Tree model)

Copies the annotations of the model onto this Tree. Also manages copies of unfinished identifiers such as #xd#. TODO: annotations are dangerous: copyAnnotations() share them between copies only when the annotation already exists, Some parts on the code rely on this! This should be cleaned up, but meanwhile, we force explicit sharing upon copy for some specific annotations (see annotationsToShareAlways).

cutChild

public Tree cutChild(int rank)

Cuts off this Tree’s child of rank “rank”, and returns it (rank of 1st child is 1). Allowed only when this is either a fixed or list arity node, but not an atom node. Leaves a null in this Tree, at the location of the cut child.

down

public Tree down(int rank)
Returns

the child of rank “rank” of this Tree node (rank of 1st child is 1). Allowed only when this is either a fixed or list arity node, but not an atom node.

equalsTree

public abstract boolean equalsTree(Tree other)
Returns

True iff this Tree is equal to the other Tree. Doesn’t check for equality of annotations.

getAnnotation

public <T> T getAnnotation(String name)
Returns

the value of annotation “name” of this Tree, null if not present.

getAnnotationPair

public <T> TapPair<String, T> getAnnotationPair(String name)
Parameters
  • name – The name of the annotation.

Returns

the annotation pair found, null otherwise.

getRank

protected int getRank(Tree child)
Returns

the rank of the given child node in this node (rank of 1st child is 1).

getRemoveAnnotation

public <T> T getRemoveAnnotation(String name)
Returns

the value of annotation “name” of this Tree, null if not present, and removes it if it was present.

intValue

public int intValue()
Returns

the integer value contained in this atom node. Allowed only when this is an integer atom node.

isAtom

public abstract boolean isAtom()
Returns

True iff this Tree node is an atom.

isFixed

public abstract boolean isFixed()
Returns

True iff this Tree node is a fixed-arity node.

isIntAtom

public abstract boolean isIntAtom()
Returns

True iff this Tree node is an atom that contains an integer.

isList

public abstract boolean isList()
Returns

True iff this Tree node is a list node.

isStringAtom

public abstract boolean isStringAtom()
Returns

True iff this Tree node is an atom that contains a String.

length

public int length()
Returns

the number of children of this Tree node. Allowed only when this is either a fixed or list arity node, but not an atom node.

opCode

public int opCode()
Returns

The integer code of the Operator of this Tree node.

opName

public String opName()
Returns

The name of the Operator of this Tree node.

operator

public Operator operator()
Returns

The Operator of this Tree node.

parent

public Tree parent()
Returns

the parent node of this Tree node, or null if no parent.

rankInParent

public int rankInParent()
Returns

the rank of this node in its parent node. Rank starts at 1. @return -1 if no parent.

removeAnnotation

public void removeAnnotation(String name)

Removes the annotation “name” of this Tree.

removeChild

public void removeChild(int rank)

Cuts off this Tree’s child of rank “rank” (rank of 1st child is 1). Allowed only when this is a list arity node. Leaves no hole in this Tree in place of the cut child.

setAnnotation

public <T> TapPair<String, T> setAnnotation(String name, T val)

Sets the value of annotation “name” of this Tree. If the annotation is not already present, creates it.

Returns

the annotation pair, after setting is done.

setChild

public void setChild(Tree newChild, int rank)

Replaces this Tree’s child of rank “rank” with the new child (rank of 1st child is 1). Allowed only when this is either a fixed or list arity node, but not an atom node. When this is a list node and rank is larger than its length, this enlarges the list and pads with null children (is this really necessary?)

setOperator

public void setOperator(Operator oper)

setParent

public void setParent(Tree parent)

Set the parent node.

setValue

public void setValue(int value)

Sets the value attached to an integer atom Tree node. Allowed only when this is an integer atom node.

setValue

public void setValue(String value)

Sets the value attached to a string atom Tree node. Allowed only when this is a string atom node.

stringValue

public String stringValue()
Returns

the value contained in this atom node, in the form of a String. Allowed only when this is an atom node.

toString

public String toString()
Returns

a String representation of this Tree.

writeToILFile

protected abstract void writeToILFile(java.io.BufferedWriter fileWriter)

Prints into a file, as an AST.

Throws
  • IOException – if an input or output error is detected.