View Javadoc

1   /*
2    * Copyright (C) 2006 uguu@users.sourceforge.jp, All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    *
8    *    1. Redistributions of source code must retain the above copyright
9    *       notice, this list of conditions and the following disclaimer.
10   *
11   *    2. Redistributions in binary form must reproduce the above copyright
12   *       notice, this list of conditions and the following disclaimer in the
13   *       documentation and/or other materials provided with the distribution.
14   *
15   *    3. Neither the name of Clarkware Consulting, Inc. nor the names of its
16   *       contributors may be used to endorse or promote products derived
17   *       from this software without prior written permission. For written
18   *       permission, please contact clarkware@clarkware.com.
19   *
20   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
21   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
23   * CLARKWARE CONSULTING OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28   * NEGLIGENCE OR OTHERWISE) ARISING IN  ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package jp.sourceforge.rpn_computer.node;
33  
34  import jp.sourceforge.rpn_computer.ParseException;
35  import jp.sourceforge.rpn_computer.RpnCompileContext;
36  import jp.sourceforge.rpn_computer.RpnNode;
37  import jp.sourceforge.rpn_computer.token.PercentToken;
38  import jp.sourceforge.rpn_computer.token.PercentTokenInfo;
39  import jp.sourceforge.rpn_computer.token.SkipTokenInfo;
40  import jp.sourceforge.rpn_computer.token.SlashToken;
41  import jp.sourceforge.rpn_computer.token.SlashTokenInfo;
42  import jp.sourceforge.rpn_computer.token.StarToken;
43  import jp.sourceforge.rpn_computer.token.StarTokenInfo;
44  import jp.sourceforge.tokenizer.Token;
45  import jp.sourceforge.tokenizer.TokenInfo;
46  import jp.sourceforge.tokenizer.Tokenizer;
47  
48  /**
49   * <p>
50   * MultiplicativeExpressionを表すノードです。
51   * </p>
52   * 
53   * @author uguu@users.sourceforge.jp
54   */
55  public final class MultiplicativeExpressionNode extends AbstractNode {
56  
57      /**
58       * {@inheritDoc}
59       */
60      public void parse(Tokenizer tokenizer) {
61          RpnNode node = new PrimaryExpressionNode();
62          node.parse(tokenizer);
63          this.addNode(node);
64  
65          TokenInfo[] readTokenInfos = new TokenInfo[] { new StarTokenInfo(), new SlashTokenInfo(), new PercentTokenInfo(), };
66          TokenInfo[] skipTokenInfos = new TokenInfo[] { new SkipTokenInfo(), };
67          if (tokenizer.read(readTokenInfos, skipTokenInfos)) {
68              Token token = tokenizer.current();
69              if (token instanceof StarToken) {
70                  node = new MultiplyNode();
71              } else if (token instanceof SlashToken) {
72                  node = new DivideNode();
73              } else if (token instanceof PercentToken) {
74                  node = new SurplusNode();
75              } else {
76                  throw new ParseException(token, readTokenInfos);
77              }
78              node.parse(tokenizer);
79              this.addNode(node);
80  
81              node = new MultiplicativeExpressionNode();
82              node.parse(tokenizer);
83              this.addNode(node);
84          }
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      public String toString() {
91          return "multiplicative expression\n" + this.toChildrenString();
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      public void compile(RpnCompileContext context) {
98          RpnNode[] nodes = this.getChildren();
99  
100         if (nodes.length == 1) {
101             nodes[0].compile(context);
102         } else {
103             nodes[0].compile(context);
104             nodes[2].compile(context);
105             nodes[1].compile(context);
106         }
107     }
108 }