1 /*******************************************************************************
2 * Reverse Polish Notation calculator. *
3 * Copyright (c) 2007-2008, Samuel Fredrickson <kinghajj@gmail.com> *
4 * All rights reserved. *
6 * Redistribution and use in source and binary forms, with or without *
7 * modification, are permitted provided that the following conditions are met: *
8 * * Redistributions of source code must retain the above copyright *
9 * notice, this list of conditions and the following disclaimer. *
10 * * Redistributions in binary form must reproduce the above copyright *
11 * notice, this list of conditions and the following disclaimer in the *
12 * documentation and/or other materials provided with the distribution. *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS *
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY *
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
25 ******************************************************************************/
27 /*******************************************************************************
28 * parser.c - parses RPN expressions. *
29 ******************************************************************************/
36 //! Tests if a string can be converted into a number.
38 * @param s The string to test.
39 * @return true or false.
41 static bool isNumber(char *s
)
43 size_t i
, len
= strlen(s
);
46 for(i
= 0; isnum
&& i
< len
; ++i
)
47 if(!isdigit(s
[i
]) && s
[i
] != '.') isnum
= false;
49 // caveat: if length is one and that's just a dot, then it's NOT a number.
50 if(isnum
&& len
== 1 && *s
== '.')
56 //! Evaluates a token on the calculator.
58 * @param calculator The calculator.
59 * @param tok The token.
61 static void evalToken(RPNCalculator
*calculator
, char *tok
)
66 // try to execute an operator; if that fails, then try to execute a command
67 executed
= RPN_executeOperator(calculator
, tok
) ||
68 RPN_executeCommand(calculator
, tok
);
70 // no? then treat this as a variable name.
73 // find variable with this name
74 var
= RPN_findVariable(calculator
->variables
, tok
);
78 // push it's value to the stack
79 RPN_push(calculator
->stack
, var
->value
);
81 // add a new variable to the variable table, whose value is that of
82 // the topmost item in the stack.
83 RPN_addVariable(calculator
->variables
, strdup(tok
),
84 RPN_peek(calculator
->stack
));
88 //! Evaluates a string on a calculator.
90 * @param s The string to evaluate.
91 * @param calculator The calculator on which to evaluate.
92 * @return The topmost item of the calculator's stack.
94 RPNValue
RPN_eval(char *s
, RPNCalculator
*calculator
)
96 /* these are just used as shorthands to make the code more readable. */
100 /* split the string. */
101 tokens
= calculator
->tokens
= RPN_splitString(s
);
103 /* go through the tokens. */
104 for(tokens
->pos
= 0; tokens
->pos
< tokens
->size
; ++tokens
->pos
)
106 token
= tokens
->tokens
[tokens
->pos
];
107 /* push numeric tokens to the stack. */
109 RPN_push(calculator
->stack
, RPN_strtod(token
, NULL
));
110 /* delegate other tokens to evalToken(). */
112 evalToken(calculator
, token
);
116 RPN_freeTokens(tokens
);
117 calculator
->tokens
= NULL
;
119 return RPN_peek(calculator
->stack
);