001    /**
002     * @author paulk, Jul 21, 2002
003     */
004    
005    package toolbus.process;
006    
007    import java.util.Stack;
008    import toolbus.AtomList;
009    import toolbus.State;
010    import toolbus.TBTermFactory;
011    import toolbus.environment.Environment;
012    import toolbus.exceptions.ToolBusException;
013    import toolbus.parsercup.PositionInformation;
014    import aterm.ATerm;
015    
016    /**
017     * IfElse is the abstract syntax operator for the if-then-else-fi operator in Tscripts. It is
018     * compiled away and does not exist during execution.
019     */
020    public class IfElse extends ProcessExpression{
021            private final ATerm test;
022            private final ProcessExpression left;
023            private final ProcessExpression right;
024            private Environment env;
025            
026            public IfElse(ATerm test, ProcessExpression Pthen, ProcessExpression Pelse, TBTermFactory tbfactory, PositionInformation posInfo){
027                    super(tbfactory, posInfo);
028                    this.test = test;
029                    this.left = Pthen;
030                    this.right = Pelse;
031            }
032            
033            protected ProcessExpression copy(){
034                    return new IfElse(test, left.copy(), right.copy(), tbfactory, getPosInfo());
035            }
036            
037            protected void computeFirst(){
038                    left.computeFirst();
039                    right.computeFirst();
040                    setFirst(left.getFirst().union(right.getFirst()));
041            }
042            
043            protected void replaceFormals(Environment e) throws ToolBusException{
044                    env = e;
045                    left.replaceFormals(env);
046                    right.replaceFormals(env);
047            }
048            
049            protected void compile(ProcessInstance P, Stack<String> calls, State follows) throws ToolBusException{
050                    left.compile(P, calls, follows);
051                    left.getFirst().setTest(test, env);
052                    right.compile(P, calls, follows);
053                    
054                    ATerm notTest = test.getFactory().make("not(<term>)", test);
055                    
056                    right.getFirst().setTest(notTest, env);
057                    
058                    setFollow(follows);
059                    // System.err.println("rtest = " + rtest);
060                    // System.err.println("notTest = " + notTest);
061                    // System.err.println("env = " + env);
062            }
063            
064            public AtomList getAtoms(){
065                    return left.getAtoms().union(right.getAtoms());
066            }
067            
068            public String toString(){
069                    return "IfElse(" + test + ", " + left + ", " + right + ")";
070            }
071    }