Class AbstractProtelisAST<T>
- java.lang.Object
-
- org.protelis.lang.interpreter.impl.AbstractProtelisAST<T>
-
- Type Parameters:
T
- annotation type
- All Implemented Interfaces:
java.io.Serializable
,ProtelisAST<T>
,WithBytecode
- Direct Known Subclasses:
AbstractPersistedTree
,AlignedMap
,All
,AssignmentOp
,BinaryOp
,ConditionalSideEffect
,Constant
,CreateTuple
,Env
,FunctionCall
,GenericHoodCall
,HoodCall
,If
,Invoke
,JvmConstant
,NBRCall
,Self
,TernaryOp
,UnaryOp
,Variable
public abstract class AbstractProtelisAST<T> extends java.lang.Object implements ProtelisAST<T>, WithBytecode
Basic implementation of anProtelisAST
.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractProtelisAST(Metadata metadata, java.util.List<ProtelisAST<?>> branch)
protected
AbstractProtelisAST(Metadata metadata, ProtelisAST<?>... branch)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected java.lang.String
branchesToString()
protected java.lang.String
branchesToString(java.lang.CharSequence separator, java.lang.CharSequence prefix, java.lang.CharSequence postfix)
Returns aString
representing the branches of this tree with the specified format.T
eval(ExecutionContext context)
Evaluates the program using the passedExecutionContext
.protected abstract T
evaluate(ExecutionContext context)
Evaluates this AST node.protected void
forEach(java.util.function.Consumer<? super ProtelisAST<?>> action)
Facility to run lambdas across all the branches.protected void
forEachWithIndex(java.util.function.BiConsumer<java.lang.Integer,? super ProtelisAST<?>> action)
Facility to run lambdas across all the branches.ProtelisAST<?>
getBranch(int i)
java.util.List<ProtelisAST<?>>
getBranches()
protected int
getBranchesNumber()
Metadata
getMetadata()
java.lang.String
getName()
protected boolean
isNullable()
protected static java.lang.String
stringFor(ProtelisAST<?> tree)
A String representation of anProtelisAST
.java.lang.String
toString()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.protelis.lang.interpreter.util.WithBytecode
getBytecode
-
-
-
-
Constructor Detail
-
AbstractProtelisAST
protected AbstractProtelisAST(Metadata metadata, ProtelisAST<?>... branch)
- Parameters:
metadata
- AMetadata
object containing information about the code that generated this AST node.branch
- the branches for this tree
-
AbstractProtelisAST
protected AbstractProtelisAST(Metadata metadata, java.util.List<ProtelisAST<?>> branch)
- Parameters:
metadata
- AMetadata
object containing information about the code that generated this AST node.branch
- the branches for this tree
-
-
Method Detail
-
branchesToString
protected final java.lang.String branchesToString()
- Returns:
- a
String
representing the branches of this tree, in the format (b1, b2, ..., bn).
-
branchesToString
protected final java.lang.String branchesToString(java.lang.CharSequence separator, java.lang.CharSequence prefix, java.lang.CharSequence postfix)
Returns aString
representing the branches of this tree with the specified format.- Parameters:
separator
- the separatorCharSequence
prefix
- the prefixpostfix
- the postfix- Returns:
- a
String
representing the branches of this tree with the specified format.
-
eval
public final T eval(ExecutionContext context)
Description copied from interface:ProtelisAST
Evaluates the program using the passedExecutionContext
.- Specified by:
eval
in interfaceProtelisAST<T>
- Parameters:
context
- the execution context- Returns:
- the AST evaluation
-
evaluate
protected abstract T evaluate(ExecutionContext context)
Evaluates this AST node. This method can throw any exception,AbstractProtelisAST
takes care of storing the necessary metadata.- Parameters:
context
- the execution context- Returns:
- the result of the evaluation
-
forEach
protected final void forEach(java.util.function.Consumer<? super ProtelisAST<?>> action)
Facility to run lambdas across all the branches.- Parameters:
action
- the Consumer to execute
-
isNullable
protected boolean isNullable()
- Returns:
- true if this node can get annotated with null values - namely, if it is an interaction with Java
-
forEachWithIndex
protected final void forEachWithIndex(java.util.function.BiConsumer<java.lang.Integer,? super ProtelisAST<?>> action)
Facility to run lambdas across all the branches.- Parameters:
action
- the Consumer to execute
-
getBranch
public final ProtelisAST<?> getBranch(int i)
- Specified by:
getBranch
in interfaceProtelisAST<T>
- Parameters:
i
- the index- Returns:
- the i-th branch of the evaluation tree
-
getBranches
public final java.util.List<ProtelisAST<?>> getBranches()
- Specified by:
getBranches
in interfaceProtelisAST<T>
- Returns:
- a view of the branches of the tree
-
getBranchesNumber
protected final int getBranchesNumber()
- Returns:
- the number of branches
-
getMetadata
public final Metadata getMetadata()
- Specified by:
getMetadata
in interfaceProtelisAST<T>
- Returns:
- A
Metadata
object containing information about the code that generated this AST node.
-
getName
public java.lang.String getName()
- Specified by:
getName
in interfaceProtelisAST<T>
- Returns:
- The name of the operation
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
stringFor
protected static final java.lang.String stringFor(ProtelisAST<?> tree)
A String representation of anProtelisAST
. I- Parameters:
tree
- the tree to stringify- Returns:
- if the tree it is not erased (i.e., contains a value), returns a
stringified version of such value. Otherwise, returns the branch name
via
getName()
-
-