001    package toolbus.process;
002    
003    /**
004     * @author paulk
005     */
006    import java.util.Stack;
007    import toolbus.AtomList;
008    import toolbus.State;
009    import toolbus.TBTermFactory;
010    import toolbus.environment.Environment;
011    import toolbus.exceptions.ToolBusException;
012    import toolbus.parsercup.PositionInformation;
013    
014    /**
015     * Alternative is the abstract syntax operator for the choice operator (+) in Tscripts. It is
016     * compiled away and does not exist during execution.
017     */
018    public class Alternative extends ProcessExpression{
019            private final ProcessExpression left;
020            private final ProcessExpression right;
021            
022            public Alternative(ProcessExpression left, ProcessExpression right, TBTermFactory tbfactory, PositionInformation posInfo){
023                    super(tbfactory, posInfo);
024                    this.left = left;
025                    this.right = right;
026            }
027            
028            protected ProcessExpression copy(){
029                    return new Alternative(left.copy(), right.copy(), tbfactory, getPosInfo());
030            }
031            
032            public String toString(){
033                    return "Alt(" + left.toString() + ", " + right.toString() + ")";
034            }
035            
036            protected void computeFirst(){
037                    left.computeFirst();
038                    right.computeFirst();
039                    setFirst(left.getFirst().union(right.getFirst()));
040            }
041            
042            protected void compile(ProcessInstance P, Stack<String> calls, State follow) throws ToolBusException{
043                    left.compile(P, calls, follow);
044                    right.compile(P, calls, follow);
045                    setFollow(follow);
046            }
047            
048            protected void replaceFormals(Environment env) throws ToolBusException{
049                    left.replaceFormals(env);
050                    right.replaceFormals(env);
051            }
052            
053            public AtomList getAtoms(){
054                    return left.getAtoms().union(right.getAtoms());
055            }
056    }