View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
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.action;
22  
23  import org.millscript.millscript.functions.Function;
24  import org.millscript.millscript.functions.SpiceClassConstructorFunction;
25  import org.millscript.millscript.tools.CastLibrary;
26  import org.millscript.millscript.vm.Machine;
27  
28  /**
29   * This class implements the action for a <code>init</code> expression. This
30   * behaves much like a normal apply action, but it examines the function action
31   * to see if it is a constructor function and if so it extracts the underlying
32   * initialiser and applies that.
33   *
34   * @see org.millscript.millscript.expr.InitExpr
35   * @see org.millscript.millscript.syntax.InitSyntax
36   */
37  public final class InitAction extends Action {
38  
39      /**
40       * The constructor function action.
41       */
42      private Action constructorAction;
43  
44      /**
45       * The constructor arguments action.
46       */
47      private Action argsAction;
48  
49      /**
50       * Constructs a new <code>init</code> action to run the supplied
51       * initialiser action.
52       *
53       * @param c the initialiser action
54       */
55      public InitAction( final Action ca, final Action aa ) {
56          this.argsAction = aa;
57          this.constructorAction = ca;
58      }
59  
60      /**
61       * @see org.millscript.millscript.action.Action#action(org.millscript.millscript.vm.Machine)
62       */
63      @Override
64      public void action( final Machine mc ) {
65          // Store the number of values on the stack, before generating the
66          // arguments
67          int before = mc.getCount();
68          // Generate the arguments for the function
69          argsAction.act( mc );
70          // Store the number of values on the stack, after generating the
71          // arguments
72          int after = mc.getCount();
73          // Perform the constructor function action
74          this.constructorAction.act( mc );
75          // Get the class constructor function off the stack
76          final SpiceClassConstructorFunction constructorFunction = CastLibrary.toSpiceClassConstructorFunction( mc.popObject() );
77          // Get the initialiser out of the constructor function
78          final Function initialiserFunction = constructorFunction.getInitialiserFunction();
79          // Now apply the initialiser function
80          // Record the function entry
81          mc.enterFunction( initialiserFunction );
82          // Apply the function, telling it how many arguments it has
83          initialiserFunction.apply( mc, after - before );
84          // Record the function's exit
85          mc.exitFunction( initialiserFunction );
86      }
87  
88  }