1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }