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.commons.util.IList;
25  import org.millscript.commons.util.list.IArrayList;
26  import org.millscript.millscript.action.Action;
27  import org.millscript.millscript.alert.Alerts;
28  import org.millscript.millscript.vm.CompilerState;
29  
30  /**
31   * This class implements a <code>case</code> expression. An individual case
32   * expression can match multiple patterns.
33   *
34   * @see org.millscript.millscript.syntax.SwitchSyntax
35   * @see org.millscript.millscript.action.SwitchAction
36   */
37  public final class CaseExpr extends Expr< Action > {
38  
39      /**
40       * The set of patterns this case expression matches. An individual case
41       * expression can match multiple patterns.
42       */
43      private final IList< Expr< ? > > patterns;
44  
45      /**
46       * The body expression for this case. This will be executed when one of the
47       * patterns is satisfied.
48       */
49      private final Expr< ? > body;
50  
51      /**
52       * Creates a new <code>case</code> expression from the specified patterns
53       * and body expression.
54       *
55       * @param   a   a list of pattern expressions
56       * @param   b   the body expression
57       */
58      public CaseExpr( final IList< Expr > a, final Expr< ? > b ) {
59          final Expr< ? >[] unchecked = a.toArray( new Expr< ? >[ a.size() ] );
60          // Iterate over the specified set of patterns, checking each one returns
61          // a single result and adding them to this case expression
62          for ( int i = 0; i < unchecked.length; i++ ) {
63              unchecked[ i ] = CheckExpr.make( unchecked[ i ] );
64          }
65          this.patterns = new IArrayList< Expr< ? > >( unchecked, true );
66          // Set the body expression
67          this.body = b;
68      }
69  
70      /**
71       * @see org.millscript.millscript.expr.Expr#compileIt()
72       */
73      @Override
74      public Action compileIt() {
75          //    It is the responsibility of the SwitchExpr to
76          //    make use of CaseExpr's.
77          throw(
78              Alerts.fault( "Trying to compile a naked CaseExpr" ).mishap()
79          );
80      }
81  
82      /**
83       * Returns this case expressions body expression.
84       *
85       * @return  this expressions body expression
86       */
87      public Expr< ? > getAct() {
88          return body;
89      }
90  
91      /**
92       * Returns this case expressions patterns.
93       *
94       * @return  an immutable list containing this expressions pattern
95       * expressions
96       */
97      public IList< Expr< ? > > getPatterns() {
98          return patterns;
99      }
100 
101     /**
102      * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
103      */
104     @Override
105     public void resolve( final CompilerState state ) {
106         Expr.resolveList( state, this.patterns );
107         this.body.resolve( state );
108     }
109 
110     /**
111      * @see org.millscript.millscript.expr.Expr#showComponents(int)
112      */
113     @Override
114     void showComponents( final int n ) {
115         for ( int i = 1; i <= this.patterns.size(); i++ ) {
116             this.patterns.get( i ).show( n );
117         }
118         this.body.show( n );
119     }
120 
121 }