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.ConstantRightAddAction;
27  import org.millscript.millscript.action.arithmetic.ConstantRightDivAction;
28  import org.millscript.millscript.action.arithmetic.ConstantRightModAction;
29  import org.millscript.millscript.action.arithmetic.ConstantRightMulAction;
30  import org.millscript.millscript.action.arithmetic.ConstantRightSubAction;
31  import org.millscript.millscript.alert.Alerts;
32  import org.millscript.millscript.tools.IntegerTools;
33  
34  /**
35   * This class implements an arithmetic expression. This expression always
36   * returns one result.
37   *
38   * @see IntegerTools
39   */
40  public final class ConstantRightArithExpr extends UnaryOpExpr< UnaryOpAction > {
41  
42      /**
43       * The arithmetic symbol this expression is for.
44       */
45      private String sym;
46  
47      /**
48       * The right hand side integer.
49       */
50      private Integer rhs;
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 expression, which should return a single
58       * result
59       * @param   b   the right hand side integer
60       */
61      public ConstantRightArithExpr( final String s, final Expr< ? > a, final Integer b ) {
62          super( a );
63          this.sym = s;
64          this.rhs = b;
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 intright = rhs.intValue();
76          if ( sym == "+" ) {
77              return new ConstantRightAddAction( a, intright );
78          } else if ( sym == "-" ) {
79              return new ConstantRightSubAction( a, intright );
80          } else if ( sym == "*" ) {
81              return new ConstantRightMulAction( a, intright );
82          } else if ( sym == "div" ) {
83              return new ConstantRightDivAction( a, intright );
84          } else if ( sym == "mod" ) {
85              return new ConstantRightModAction( a, intright );
86          } else {
87              throw(
88                  Alerts.fault(
89                      "Unrecognized arithmetic symbol: " + sym
90                  ).mishap()
91              );
92          }
93      }
94  
95  }