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.datatypes;
22  
23  import org.millscript.millscript.action.Action;
24  import org.millscript.millscript.functions.SlotFunction;
25  import org.millscript.millscript.vm.Machine;
26  
27  /**
28   * This class represents a Spice class.
29   */
30  public final class SpiceClass {
31  
32      /**
33       * The class's name.
34       */
35      private final String name;
36  
37      /**
38       * This Spice class's parent
39       */
40      private final SpiceClass parent;
41  
42      /**
43       * An array of the slot functions used to access each slot in this spice
44       * class. The elements in this array are used in conjunction with the
45       * relevant element in the slot initial value array.
46       */
47      private final SlotFunction[] slotFunctions;
48  
49      /**
50       * An array of the slot initial value actions. We need these here as we
51       * can't initialise the slots until we've got an instance of the class. The
52       * elements in this array are used in conjunction with the relevant element
53       * in the slot initial value array.
54       */
55      private final Action[] slotInitialValues;
56  
57      /**
58       * Constructs a new SpiceClass with the specified name.
59       *
60       * @param n the name of this class
61       * @param p the parent Spice class, if any
62       * @param siv   an array of slot initial value actions
63       */
64      public SpiceClass( final String n, final SpiceClass p, final SlotFunction[] sf, final Action[] siv ) {
65          this.name = n;
66          this.parent = p;
67          this.slotFunctions = sf;
68          this.slotInitialValues = siv;
69      }
70  
71      /**
72       * @see org.millscript.millscript.action.Action#action(org.millscript.millscript.vm.Machine)
73       */
74      public void initialiseSlots( final SpiceObject protoObject, final Machine mc ) {
75          if ( parent != null ) {
76              parent.initialiseSlots( protoObject, mc );
77          }
78          for (int i = 0; i < slotInitialValues.length; i++) {
79              // Initialise this slot with the slot value
80              protoObject.initialiseSlotValue( slotFunctions[ i ], slotInitialValues[ i ].act1( mc ) );
81          }
82      }
83  
84      /**
85       * Returns the name of this class.
86       *
87       * @return  a String holding the name of this class
88       */
89      public String getName() {
90          return name;
91      }
92  
93      /**
94       * Returns the parent Spice class that this class inherits from.
95       *
96       * @return  the SpiceClass this class inherits from
97       */
98      public SpiceClass getParentSpiceClass() {
99          return parent;
100     }
101 
102     /**
103      * @see java.lang.Object#toString()
104      */
105     @Override
106     public String toString() {
107         final StringBuffer buff = new StringBuffer( "<class " );
108         buff.append( name );
109         if ( parent != null ) {
110             buff.append( " extends " );
111             buff.append( parent.getName() );
112         }
113         buff.append( ">" );
114         return buff.toString();
115     }
116 
117 }