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.action;
22
23 import org.millscript.millscript.datatypes.SpiceClass;
24 import org.millscript.millscript.functions.Function;
25 import org.millscript.millscript.functions.ManyBodyFunction;
26 import org.millscript.millscript.vm.Machine;
27
28 /**
29 * This class implements the action used when defining a new method. This
30 * action returns a new many body function which can then be bound to the
31 * relevant variable.
32 *
33 * @see org.millscript.millscript.expr.MethodExpr
34 * @see org.millscript.millscript.syntax.DefineSyntax
35 */
36 public final class MethodAction extends Action {
37
38 /**
39 * The action that returns the Function this method action is based on.
40 */
41 private final Action methodFunction;
42
43 /**
44 * The action that returns the enclosing Spice class.
45 */
46 private final Action enclosingSpiceClass;
47
48 /**
49 * Constructs a new method action, using the specified actions to obtain
50 * the default type signature and associated Function.
51 *
52 * @param sc the action that generates the Spice class to dispatch on
53 * @param mf the action, which generates the Function for the method
54 * body
55 */
56 public MethodAction( final Action sc, final Action mf ) {
57 this.enclosingSpiceClass = sc;
58 this.methodFunction = mf;
59 }
60
61 /**
62 * @see org.millscript.millscript.action.Action#action(org.millscript.millscript.vm.Machine)
63 */
64 @Override
65 public void action( final Machine mc ) {
66
67
68 final Function fn = (Function)methodFunction.act1( mc );
69
70
71 final SpiceClass sc = (SpiceClass) enclosingSpiceClass.act1( mc );
72
73 mc.pushObject(
74 new ManyBodyFunction( mc, sc, fn ).modName( fn.getName() )
75 );
76 }
77
78 }