View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2004-2005 Kevin Rogers
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.commons.util.IList;
24  import org.millscript.millscript.action.Action;
25  import org.millscript.millscript.action.SpiceClassAction;
26  import org.millscript.millscript.functions.SlotFunction;
27  import org.millscript.millscript.vm.CompilerState;
28  
29  /**
30   * This class implements a <code>class</code> expression.
31   *
32   * @see org.millscript.millscript.action.SpiceClassAction
33   */
34  public final class SpiceClassExpr extends Expr< SpiceClassAction > implements OneResult {
35  
36      /**
37       * The name of the class this class expression defines.
38       */
39      private final String className;
40  
41      /**
42       * The expr for the parent Spice class.
43       */
44      private final Expr< ? > parentSpiceClassExpr;
45  
46      /**
47       * A list of the slot initialisers. These need to be kept around to
48       * initialise the slots when an instance of a class is created.
49       */
50      private final IList< SpiceClassSlotExpr > slotInitialisers;
51  
52      /**
53       * Constructs a new <code>class</code> expression.
54       *
55       * @param nm    the name of the class we are defining
56       * @param pe    the expression for the parent Spice class
57       * @param si    a list of slot initialiser expressions
58       */
59      public SpiceClassExpr( final String nm, final Expr< ? > pe, final IList< SpiceClassSlotExpr > si ) {
60          this.className = nm;
61          this.parentSpiceClassExpr = pe;
62          this.slotInitialisers = si;
63      }
64  
65      /**
66       * @see org.millscript.millscript.expr.Expr#compileIt()
67       */
68      @Override
69      public SpiceClassAction compileIt() {
70          // Compile the parent class
71          Action parentAction = null;
72          if ( parentSpiceClassExpr != null ) {
73              parentAction = parentSpiceClassExpr.compile();
74          }
75          // Compile each slot initialiser.
76          SlotFunction[] slotFunctions = new SlotFunction[ slotInitialisers.size() ];
77          Action[] slotInitialValues = new Action[ slotInitialisers.size() ];
78          for ( int i = 0; i < slotInitialisers.size(); i++ ) {
79              SpiceClassSlotExpr e = slotInitialisers.get0( i );
80              slotFunctions[ i ] = e.getSlotFunction();
81              slotInitialValues[ i ] = e.getSlotValue().compile();
82          }
83          return new SpiceClassAction( className, parentAction, slotFunctions, slotInitialValues );
84      }
85  
86      /**
87       * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
88       */
89      @Override
90      public void resolve( final CompilerState state ) {
91          if ( this.parentSpiceClassExpr != null ) {
92              this.parentSpiceClassExpr.resolve( state );
93          }
94          for ( int i = 1; i <= this.slotInitialisers.size(); i++ ) {
95              final SpiceClassSlotExpr e = this.slotInitialisers.get( i );
96              e.resolve( state );
97          }
98      }
99  
100     /**
101      * @see org.millscript.millscript.expr.Expr#showComponents(int)
102      */
103     @Override
104     void showComponents( final int n ) {
105         // We can only push the parent class expression if there is one!
106         if ( this.parentSpiceClassExpr != null ) {
107             this.parentSpiceClassExpr.show( n );
108         }
109         for ( int i = 1; i <= this.slotInitialisers.size(); i++ ) {
110             this.slotInitialisers.get( i ).show( n );
111         }
112     }
113 
114 }