View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.expr;
23  
24  import org.millscript.millscript.action.CheckAction;
25  import org.millscript.millscript.alert.Alerts;
26  import org.millscript.millscript.vm.CompilerState;
27  
28  /**
29   * This class implements a check expression. A check expression is guaranteed to
30   * return one result.
31   *
32   * @see CheckAction
33   */
34  public final class CheckExpr extends Expr< CheckAction > {
35  
36      /**
37       * Returns an expression that is guaranteed to return a single result. This
38       * method provides some compile-time optimisation, as it avoids creating
39       * additional unnecessary objects in the expression tree. If the specified
40       * expression already returns a single result, it is simply returned,
41       * otherwise if it returns an unknown number of results, a new check
42       * expression is created.
43       *
44       * @param   e   the expression which must return a single result
45       * @return  the specified expression if it already returns a single
46       *          result, otherwise a new {@link CheckExpr}
47       */
48      public static Expr< ? > make( final Expr< ? > e ) {
49          int n = e.arity();
50          if ( n == 1 ) {
51              return e;
52          } else if ( n < 0 ) {
53              return new CheckExpr( e );
54          } else {
55              throw(
56                  Alerts.compile(
57                      "Arity mismatch (" + n + " results)",
58                      "This expression must return one result"
59                  ).culprit( "expression", e ).mishap()
60              );
61          }
62      }
63  
64      /**
65       * The expression which must return a single result.
66       */
67      private final Expr< ? > cont;
68  
69      /**
70       * Creates a new check expression, to check the specified expression returns
71       * a single result.
72       *
73       * @param   e   the expression which must return a single result
74       */
75      public CheckExpr( final Expr< ? > e ) {
76          cont = e;
77      }
78  
79      /**
80       * @see org.millscript.millscript.expr.Expr#compileIt()
81       */
82      @Override
83      public CheckAction compileIt() {
84          return new CheckAction( cont.compile() );
85      }
86  
87      /**
88       * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
89       */
90      @Override
91      public void resolve( final CompilerState state ) {
92          this.cont.resolve( state );
93      }
94  
95      /**
96       * @see org.millscript.millscript.expr.Expr#showComponents(int)
97       */
98      @Override
99      void showComponents( final int n ) {
100         this.cont.show( n );
101     }
102 
103 }