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.expr;
22  
23  import org.millscript.millscript.action.ApplyAction;
24  import org.millscript.millscript.vm.CompilerState;
25  
26  /**
27   * This class implements an apply expression.
28   *
29   * @see org.millscript.millscript.syntax.DotSyntax
30   * @see org.millscript.millscript.syntax.ParenSyntax
31   * @see org.millscript.millscript.functions.Function
32   * @see ApplyAction
33   */
34  public final class ApplyExpr extends Expr< ApplyAction > {
35  
36      /**
37       * The expression generating the arguments to apply the function with.
38       */
39      private final Expr< ? > args;
40  
41      /**
42       * The expression generating a function to apply.
43       */
44      private final Expr< ? > fun;
45  
46      /**
47       * Creates a new apply expression for the specified function and arguments
48       * expressions.
49       *
50       * @param   f   the function expression, which must return a function as a
51       *              single result
52       * @param   a   the arguments expression, which can return zero or more
53       *              results
54       */
55      public ApplyExpr( final Expr< ? > f, final Expr< ? > a ) {
56          fun = CheckExpr.make( f );
57          args = a;
58      }
59  
60      /**
61       * @see org.millscript.millscript.expr.Expr#compileIt()
62       */
63      @Override
64      public ApplyAction compileIt() {
65          return new ApplyAction( fun.compile(), args.compile() );
66      }
67  
68      /**
69       * Returns the arguments expression this apply expression will apply the
70       * function with.
71       *
72       * @return  the expression returning the arguments for the function
73       */
74      public Expr getArgs() {
75          return args;
76      }
77  
78      /**
79       * Returns the function expression this apply expression will apply.
80       *
81       * @return  the expression returning a single function
82       */
83      public Expr getFun() {
84          return fun;
85      }
86  
87      /**
88       * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
89       */
90      @Override
91      public void resolve( final CompilerState state ) {
92          this.fun.resolve( state );
93          this.args.resolve( state );
94      }
95  
96      /**
97       * @see org.millscript.millscript.expr.Expr#showComponents(int)
98       */
99      @Override
100     void showComponents( final int n ) {
101         this.fun.show( n );
102         this.args.show( n );
103     }
104 
105 }