summaryrefslogtreecommitdiff
path: root/plugins/Popup/src/formula.cpp
blob: f2748ae0301363e6a9ff5a51b8c87bba24673e8e (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
/*
Popup Plus plugin for Miranda IM

Copyright	© 2002 Luca Santarelli,
© 2004-2007 Victor Pavlychko
© 2010 MPK
© 2010 Merlin_de

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "stdafx.h"

static inline bool myisspace(char ch)
{
	return (ch >= 0) && (ch <= 32);
}

int Formula::eval_neq(wchar_t *&s, Args *args, bool *vars) const
{
	int left = eval_sum(s, args, vars);
	while (*s) {
		if (myisspace(*s)) {
			++s;
		}
		else if (*s == '<') {
			// this is needed due to side effects caused by min() macro...
			int tmp = eval_sum(++s, args, vars);
			left = min(left, tmp);
		}
		else if (*s == '>') {
			// this is needed due to side effects caused by max() macro...
			int tmp = eval_sum(++s, args, vars);
			left = max(left, tmp);
		}
		else break;
	}
	return left;
}

int Formula::eval_sum(wchar_t *&s, Args *args, bool *vars) const
{
	int left = eval_mul(s, args, vars);
	while (*s) {
		if (myisspace(*s)) {
			++s;
		}
		else if (*s == '+') {
			left += eval_mul(++s, args, vars);
		}
		else if (*s == '-') {
			left -= eval_mul(++s, args, vars);
		}
		else break;
	}
	return left;
}

int Formula::eval_mul(wchar_t *&s, Args *args, bool *vars) const
{
	int left = eval_atom(s, args, vars);
	while (*s) {
		if (myisspace(*s)) {
			++s;
		}
		else if (*s == '*') {
			left *= eval_atom(++s, args, vars);
		}
		else if (*s == '/') {
			if (int right = eval_atom(++s, args, vars))
				left /= right;
		}
		else if (*s == '%') {
			if (int right = eval_atom(++s, args, vars))
				left %= right;
		}
		else break;
	}
	return left;
}

int Formula::eval_atom(wchar_t *&s, Args *args, bool *vars) const
{
	while (*s) {
		if (myisspace(*s)) {
			++s;
		}
		else if (*s == '(') {
			int res = eval_neq(++s, args, vars);
			if (*s == ')')
				++s;
			return res;
		}
		else if ((*s == '+') || (*s == '-')) {
			int sign = 1;
			if (*s == '-')
				sign = -1;
			return sign * eval_neq(++s, args, vars);
		}
		else if (*s == '!') {
			return !eval_neq(++s, args, vars);
		}
		else if ((*s >= '0') && (*s <= '9')) {
			int res = 0;
			while ((*s >= '0') && (*s <= '9'))
				res = res * 10 + *s++ - '0';
			return res;
		}
		else {
			if (!args)
				return 0;
			char buf[1024];
			char *bufptr = buf;
			while (((*s >= '0') && (*s <= '9')) || ((*s >= 'a') && (*s <= 'z')) || ((*s >= 'A') && (*s <= 'Z')) || (*s == '_') || (*s == '.'))
				*bufptr++ = *s++;
			*bufptr = 0;
			int res = args->get(buf);
			if (vars) *vars = true;
			return res;
		}
	}
	return 0;
}

int Formula::eval(Args *args, bool *vars) const
{
	if (vars) *vars = false;
	wchar_t *s = m_str;
	int res = eval_neq(s, args, vars);
	return res;
}