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 updater
28   * expression.
29   *
30   * @see org.millscript.millscript.expr.ApplyUpdaterExpr
31   * @see org.millscript.millscript.syntax.AssignSyntax
32   * @see org.millscript.millscript.syntax.ParenSyntax
33   */
34  public final class ApplyUpdaterAction extends Action {
35  
36      /**
37       * The action generating the function to apply and update.
38       */
39      private Action fun;
40  
41      /**
42       * The action for the arguments to the function.
43       */
44      private Action args;
45  
46      /**
47       * The action for the values to update the function with.
48       */
49      private Action uargs;
50  
51      /**
52       * Constructs a new function application updater action, with the specified
53       * actions for the function, it's arguments and update values.
54       *
55       * @param f the action generating the function to apply
56       * @param a the action for the arguments to the function
57       * @param u the action for the values to update the function with
58       */
59      public ApplyUpdaterAction( final Action f, final Action a, final Action u ) {
60          this.fun = f;
61          this.args = a;
62          this.uargs = u;
63      }
64  
65      /**
66       * @see org.millscript.millscript.action.Action#action(org.millscript.millscript.vm.Machine)
67       */
68      @Override
69      public void action( final Machine mc ) {
70          // Store the number of values on the stack, before generating the
71          // arguments
72          int start = mc.getCount();
73          // Generate the arguments for the function
74          args.act( mc );
75          // Store the number of values on the stack, after generating the
76          // arguments
77          int middle = mc.getCount();
78          // Generate the values to update the function with
79          uargs.act( mc );
80          // Store the number of values on the stack, after generating the update
81          // values
82          int end = mc.getCount();
83          // Generate the function we want to apply
84          fun.act( mc );
85          // Get it off the stack
86          Function f = mc.popFunction();
87          // Record the function updaters entry
88          mc.enterUpdater( f );
89          // Apply the function, telling it how many arguments and update values
90          // it has
91          f.applyUpdater( mc, middle - start, end - middle );
92          // Record the function updaters exit
93          mc.exitUpdater( f );
94      }
95  }