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.command.PushStackCommand; 37 import jp.sourceforge.rpn_computer.token.NumberToken; 38 import jp.sourceforge.rpn_computer.token.NumberTokenInfo; 39 import jp.sourceforge.tokenizer.Token; 40 import jp.sourceforge.tokenizer.TokenInfo; 41 import jp.sourceforge.tokenizer.Tokenizer; 42 43 /** 44 * <p> 45 * 数値を表すノードです。 46 * </p> 47 * 48 * @author uguu@users.sourceforge.jp 49 */ 50 public final class NumberNode extends AbstractNode { 51 52 private double value; 53 54 /** 55 * {@inheritDoc} 56 */ 57 public void parse(Tokenizer tokenizer) { 58 Token token = tokenizer.current(); 59 if (!(token instanceof NumberToken)) { 60 throw new ParseException(token, new TokenInfo[] { new NumberTokenInfo() }); 61 } 62 this.value = Double.parseDouble(token.getToken()); 63 } 64 65 /** 66 * {@inheritDoc} 67 */ 68 public String toString() { 69 return "number[" + this.value + "]"; 70 } 71 72 /** 73 * {@inheritDoc} 74 */ 75 public void compile(RpnCompileContext context) { 76 context.add(new PushStackCommand(this.value)); 77 } 78 }