View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.expr;
23  
24  import org.millscript.millscript.action.Action;
25  import org.millscript.millscript.action.UnaryOpAction;
26  import org.millscript.millscript.action.arithmetic.ConstantLeftAddAction;
27  import org.millscript.millscript.action.arithmetic.ConstantLeftDivAction;
28  import org.millscript.millscript.action.arithmetic.ConstantLeftModAction;
29  import org.millscript.millscript.action.arithmetic.ConstantLeftMulAction;
30  import org.millscript.millscript.action.arithmetic.ConstantLeftSubAction;
31  import org.millscript.millscript.alert.Alerts;
32  import org.millscript.millscript.tools.IntegerTools;
33  
34  /**
35   * This class implements an arithmetic expression with a constant integer value
36   * for the left hand side. This expression always returns one result.
37   *
38   * @see IntegerTools
39   */
40  public final class ConstantLeftArithExpr extends UnaryOpExpr< UnaryOpAction > {
41  
42      /**
43       * The arithmetic symbol this expression is for.
44       */
45      private String sym;
46  
47      /**
48       * The left hand side integer.
49       */
50      private Integer lhs;
51  
52      /**
53       * Creates a new arithmetic expression for the specified symbol and left and
54       * right hand side expressions.
55       *
56       * @param   s   the arithmetic symbol to make an expression for
57       * @param   a   the left hand side integer
58       * @param   b   the right hand side expression, which should return a single
59       *              result
60       */
61      public ConstantLeftArithExpr( final String s, final Integer a, final Expr< ? > b ) {
62          super( b );
63          this.sym = s;
64          this.lhs = a;
65      }
66  
67      /**
68       * @see org.millscript.millscript.expr.UnaryOpExpr#newAction(org.millscript.millscript.action.Action)
69       */
70      @Override
71      public UnaryOpAction newAction( final Action a ) {
72          // Further compile-time optimisation is performed here for the
73          // situation where either the left or right hand side expression is for
74          // a constant integer.
75          final int intleft = lhs.intValue();
76          if ( sym == "+" ) {
77              return new ConstantLeftAddAction( intleft, a );
78          } else if ( sym == "-" ) {
79              return new ConstantLeftSubAction( intleft, a );
80          } else if ( sym == "*" ) {
81              return new ConstantLeftMulAction( intleft, a );
82          } else if ( sym == "div" ) {
83              return new ConstantLeftDivAction( intleft, a );
84          } else if ( sym == "mod" ) {
85              return new ConstantLeftModAction( intleft, a );
86          } else {
87              throw(
88                  Alerts.fault(
89                      "Unrecognized arithmetic symbol: " + sym
90                  ).mishap()
91              );
92          }
93      }
94  
95  }