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.LeftBracketToken;
38  import jp.sourceforge.rpn_computer.token.LeftBracketTokenInfo;
39  import jp.sourceforge.rpn_computer.token.MinusToken;
40  import jp.sourceforge.rpn_computer.token.MinusTokenInfo;
41  import jp.sourceforge.rpn_computer.token.NumberToken;
42  import jp.sourceforge.rpn_computer.token.NumberTokenInfo;
43  import jp.sourceforge.rpn_computer.token.PlusToken;
44  import jp.sourceforge.rpn_computer.token.PlusTokenInfo;
45  import jp.sourceforge.rpn_computer.token.RightBracketTokenInfo;
46  import jp.sourceforge.rpn_computer.token.SkipTokenInfo;
47  import jp.sourceforge.tokenizer.Token;
48  import jp.sourceforge.tokenizer.TokenInfo;
49  import jp.sourceforge.tokenizer.Tokenizer;
50  
51  /**
52   * <p>
53   * PrimaryExpressionを表すノードです。
54   * </p>
55   * 
56   * @author uguu@users.sourceforge.jp
57   */
58  public final class PrimaryExpressionNode extends AbstractNode {
59  
60      /**
61       * {@inheritDoc}
62       */
63      public void parse(Tokenizer tokenizer) {
64          RpnNode node;
65  
66          TokenInfo[] readTokenInfos = new TokenInfo[] { new PlusTokenInfo(), new MinusTokenInfo(), };
67          TokenInfo[] skipTokenInfos = new TokenInfo[] { new SkipTokenInfo() };
68          if (tokenizer.read(readTokenInfos, skipTokenInfos)) {
69              Token token = tokenizer.current();
70              if (token instanceof PlusToken) {
71                  node = new PreAddNode();
72              } else if (token instanceof MinusToken) {
73                  node = new PreSubtractNode();
74              } else {
75                  throw new ParseException(token, readTokenInfos);
76              }
77              node.parse(tokenizer);
78              this.addNode(node);
79          }
80  
81          readTokenInfos = new TokenInfo[] { new NumberTokenInfo(), new LeftBracketTokenInfo(), };
82          if (!tokenizer.read(readTokenInfos, skipTokenInfos)) {
83              throw new ParseException(null, readTokenInfos);
84          }
85          Token token = tokenizer.current();
86          if (token instanceof NumberToken) {
87              node = new NumberNode();
88              node.parse(tokenizer);
89              this.addNode(node);
90          } else if (token instanceof LeftBracketToken) {
91              node = new LeftBracketNode();
92              node.parse(tokenizer);
93              this.addNode(node);
94  
95              node = new AdditiveExpressionNode();
96              node.parse(tokenizer);
97              this.addNode(node);
98  
99              readTokenInfos = new TokenInfo[] { new RightBracketTokenInfo() };
100             if (!tokenizer.read(readTokenInfos, skipTokenInfos)) {
101                 throw new ParseException(null, readTokenInfos);
102             }
103             node = new RightBracketNode();
104             node.parse(tokenizer);
105             this.addNode(node);
106         } else {
107             throw new ParseException(token, readTokenInfos);
108         }
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     public String toString() {
115         return "primary expression\n" + this.toChildrenString();
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     public void compile(RpnCompileContext context) {
122         RpnNode[] nodes = this.getChildren();
123 
124         RpnNode preOpeNode = null;
125 
126         int i = 0;
127         if ((nodes[i] instanceof PreAddNode) || (nodes[i] instanceof PreSubtractNode)) {
128             preOpeNode = nodes[i];
129             i++;
130         }
131 
132         if (nodes[i] instanceof NumberNode) {
133             nodes[i].compile(context);
134         } else {
135             nodes[i + 1].compile(context);
136         }
137 
138         if (preOpeNode != null) {
139             preOpeNode.compile(context);
140         }
141     }
142 }