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.vm.Machine;
25  
26  /**
27   * This class implements the action for a function application expression.
28   *
29   * @see org.millscript.millscript.expr.ApplyExpr
30   * @see org.millscript.millscript.syntax.ParenSyntax
31   */
32  public final class ApplyAction extends Action {
33  
34      /**
35       * The action generating the function to apply.
36       */
37      private Action fun;
38  
39      /**
40       * The action for the arguments to the function.
41       */
42      private Action args;
43  
44      /**
45       * Constructs a new function application action, with the specified actions
46       * for the function and it's arguments.
47       *
48       * @param f the action generating the function to apply
49       * @param a the action for the arguments to the function
50       */
51      public ApplyAction( final Action f, final Action a ) {
52          this.fun = f;
53          this.args = a;
54      }
55  
56      /**
57       * @see org.millscript.millscript.action.Action#action(org.millscript.millscript.vm.Machine)
58       */
59      @Override
60      public void action( final Machine mc ) {
61          // Store the number of values on the stack, before generating the
62          // arguments
63          int before = mc.getCount();
64          // Generate the arguments for the function
65          args.act( mc );
66          // Store the number of values on the stack, after generating the
67          // arguments
68          int after = mc.getCount();
69          // Generate the function we want to apply
70          fun.act( mc );
71          // Get it off the stack
72          Function f = mc.popFunction();
73          // Record the function entry
74          mc.enterFunction( f );
75          // Apply the function, telling it how many arguments it has
76          f.apply( mc, after - before );
77          // Record the function's exit
78          mc.exitFunction( f );
79      }
80  }