summaryrefslogtreecommitdiffstats
path: root/utils/regtools/lib/formula.cpp
blob: 1a6b16c773b86399ddacc6925519e0c27b1eb3c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "soc_desc.hpp"
#include <cstdarg>
#include <cstdio>

using namespace soc_desc;

namespace soc_desc
{

namespace
{

struct formula_evaluator
{
    std::string formula;
    size_t pos;
    error_context_t& ctx;
    std::string m_loc;

    bool err(const char *fmt, ...)
    {
        char buffer[256];
        va_list args;
        va_start(args, fmt);
        vsnprintf(buffer,sizeof(buffer), fmt, args);
        va_end(args);
        ctx.add(error_t(error_t::FATAL, m_loc, buffer));
        return false;
    }

    formula_evaluator(const std::string& s, error_context_t& ctx):pos(0),ctx(ctx)
    {
        for(size_t i = 0; i < s.size(); i++)
            if(!isspace(s[i]))
                formula.push_back(s[i]);
    }

    void set_location(const std::string& loc)
    {
        m_loc = loc;
    }

    void adv()
    {
        pos++;
    }

    char cur()
    {
        return end() ? 0 : formula[pos];
    }

    bool end()
    {
        return pos >= formula.size();
    }

    bool parse_digit(char c, int basis, soc_word_t& res)
    {
        c = tolower(c);
        if(isdigit(c))
        {
            res = c - '0';
            return true;
        }
        if(basis == 16 && isxdigit(c))
        {
            res = c + 10 - 'a';
            return true;
        }
        return false;
    }

    bool parse_signed(soc_word_t& res)
    {
        char op = cur();
        if(op == '+' || op == '-')
        {
            adv();
            if(!parse_signed(res))
                return false;
            if(op == '-')
                res *= -1;
            return true;
        }
        else if(op == '(')
        {
            adv();
            if(!parse_expression(res))
                return false;
            if(cur() != ')')
                return err("expected ')', got '%c'", cur());
            adv();
            return true;
        }
        else if(isdigit(op))
        {
            res = op - '0';
            adv();
            int basis = 10;
            if(op == '0' && cur() == 'x')
            {
                basis = 16;
                adv();
            }
            soc_word_t digit = 0;
            while(parse_digit(cur(), basis, digit))
            {
                res = res * basis + digit;
                adv();
            }
            return true;
        }
        else if(isalpha(op) || op == '_')
        {
            std::string name;
            while(isalnum(cur()) || cur() == '_')
            {
                name.push_back(cur());
                adv();
            }
            return get_variable(name, res);
        }
        else
            return err("express signed expression, got '%c'", op);
    }

    bool parse_term(soc_word_t& res)
    {
        if(!parse_signed(res))
            return false;
        while(cur() == '*' || cur() == '/' || cur() == '%')
        {
            char op = cur();
            adv();
            soc_word_t tmp;
            if(!parse_signed(tmp))
                return false;
            if(op == '*')
                res *= tmp;
            else if(tmp != 0)
                res = op == '/' ? res / tmp : res % tmp;
            else
                return err("division by 0");
        }
        return true;
    }

    bool parse_expression(soc_word_t& res)
    {
        if(!parse_term(res))
            return false;
        while(!end() && (cur() == '+' || cur() == '-'))
        {
            char op = cur();
            adv();
            soc_word_t tmp;
            if(!parse_term(tmp))
                return false;
            if(op == '+')
                res += tmp;
            else
                res -= tmp;
        }
        return true;
    }

    bool parse(soc_word_t& res)
    {
        bool ok = parse_expression(res);
        if(ok && !end())
            err("unexpected character '%c'", cur());
        return ok && end();
    }

    virtual bool get_variable(std::string name, soc_word_t& res)
    {
        return err("unknown variable '%s'", name.c_str());
    }
};

struct my_evaluator : public formula_evaluator
{
    const std::map< std::string, soc_word_t>& var;

    my_evaluator(const std::string& formula, const std::map< std::string, soc_word_t>& _var,
        error_context_t& ctx)
        :formula_evaluator(formula, ctx), var(_var) {}

    virtual bool get_variable(std::string name, soc_word_t& res)
    {
        std::map< std::string, soc_word_t>::const_iterator it = var.find(name);
        if(it == var.end())
            return formula_evaluator::get_variable(name, res);
        else
        {
            res = it->second;
            return true;
        }
    }
};

}

bool evaluate_formula(const std::string& formula,
    const std::map< std::string, soc_word_t>& var, soc_word_t& result,
    const std::string& loc, error_context_t& error)
{
    my_evaluator e(formula, var, error);
    e.set_location(loc);
    return e.parse(result);
}

} // soc_desc