Calculator.h revision 10448:bc1a3b7ab5ef
1/* Copyright (c) 2012 Massachusetts Institute of Technology
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22#ifndef __LIBUTIL_CALCULATOR_H__
23#define __LIBUTIL_CALCULATOR_H__
24
25#include <sstream>
26
27#include "model/Model.h"
28#include "String.h"
29#include "Map.h"
30#include "Assert.h"
31
32namespace LibUtil
33{
34    using std::istringstream;
35    using std::ostringstream;
36
37    /*
38     *  program:
39     *      END                         // END is end-of-input
40     *      expr_list END
41     *
42     *  expr_list:
43     *      expression SEP expr_list    // SEP is semicolon
44     *      expression
45     *      print expression
46     *      print STRING
47     *      print STRING expression
48     *      print STRING expression SEP expr_list
49     *
50     *
51     *  expression:
52     *      expression + term
53     *      expression - term
54     *      term
55     *
56     *  term:
57     *      term / primary
58     *      term * primary
59     *      primary
60     *
61     *  primary:
62     *      NUMBER
63     *      NAME
64     *      NAME = expression
65     *      NAME string expression      // NAME is print
66     *      - primary
67     *      ( expression )
68     *
69     *  string:
70     *
71     **/
72
73    class Calculator
74    {
75        protected:
76            enum Token
77            {
78                NAME, NAME2, NUMBER, STRING, END,
79                PLUS = '+', MINUS = '-', MUL = '*', DIV = '/',
80                SEP = ';', ASSIGN = '=', LP = '(', RP = ')'
81            };
82
83        public:
84            Calculator();
85            virtual ~Calculator();
86
87        public:
88            void reset();
89            void evaluateString(const String& str_,
90                                const std::map<String, String> &config,
91                                DSENT::Model *ms_model,
92                                std::map<std::string, double> &outputs);
93
94        protected:
95            Token getToken(istringstream& ist_);
96
97            double prim(istringstream& ist_, bool is_get_,
98                        const std::map<String, String> &config,
99                        DSENT::Model *ms_model);
100
101            double term(istringstream& ist_, bool is_get_,
102                        const std::map<String, String> &config,
103                        DSENT::Model *ms_model);
104
105            double expr(istringstream& ist_, bool is_get_,
106                        const std::map<String, String> &config,
107                        DSENT::Model *ms_model);
108
109            virtual double getEnvVar(
110                    const String& var_name_,
111                    const std::map<String, String> &config,
112                    DSENT::Model *ms_model) const;
113
114        protected:
115            String m_reserved_chars_;
116            Map<double> m_var_;
117
118            Token m_curr_token_;
119            double m_value_number_;
120            String m_value_string_;
121    };
122} // namespace LibUtil
123
124#endif // __LIBUTIL_CALCULATOR_H__
125
126