View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.millscript.expr;
22  
23  import org.millscript.millscript.action.If2Action;
24  import org.millscript.millscript.vm.CompilerState;
25  
26  /**
27   * This class implements an if2 expression. An if2 expresion has a predicate and
28   * ifso expression, where the ifso expression is only executed if the predicate
29   * returns <code>true</code>.
30   *
31   * @see org.millscript.millscript.syntax.ConditionalSyntax
32   * @see If3Expr
33   * @see If2Action
34   */
35  public final class If2Expr extends Expr< If2Action > {
36  
37      /**
38       * The ifso expression, to be executed if the predicate returns
39       * <code>true</code>.
40       */
41      private final Expr< ? > ifso;
42  
43      /**
44       * The predicate expression.
45       */
46      private final Expr< ? > pred;
47  
48      /**
49       * Creates a new if2 expression with the specified predicate and ifso
50       * expressions.
51       *
52       * @param   a   the predicate expression which must return a single result
53       * @param   b   the ifso expression
54       */
55      public If2Expr( final Expr< ? > a, final Expr< ? > b ) {
56          pred = CheckExpr.make( a );
57          ifso = b;
58      }
59  
60      /**
61       * @see org.millscript.millscript.expr.Expr#arity()
62       */
63      @Override
64      public int arity() {
65          // An if2 expressions arity is zero if the ifso expression returns
66          // zero, otherwise it is unknown, as it would depend on the result of
67          // the predicate expression.
68          return ifso.arity() == 0 ? 0 : -1;
69      }
70  
71      /**
72       * @see org.millscript.millscript.expr.Expr#compileIt()
73       */
74      @Override
75      public If2Action compileIt() {
76          return new If2Action( pred.compile(), ifso.compile() );
77      }
78  
79      /**
80       * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
81       */
82      @Override
83      public void resolve( final CompilerState state ) {
84          this.pred.resolve( state );
85          this.ifso.resolve( state );
86      }
87  
88      /**
89       * @see org.millscript.millscript.expr.Expr#showComponents(int)
90       */
91      @Override
92      void showComponents( final int n ) {
93          this.pred.show( n );
94          this.ifso.show( n );
95      }
96  
97  }