summaryrefslogtreecommitdiffstats
path: root/kommander/widget/expression.cpp
blob: 6ced8689610242b41cfea783729662b042a0fddd (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/***************************************************************************
                    expression.cpp - Expression parser
                             -------------------
    copyright          : (C) 2004      Michal Rudolf <mrudolf@kdewebdwev.org>
    
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "expression.h"

#include <tdelocale.h>

Expression::Expression() : m_start(0), m_error(false)
{
}

Expression::Expression(const TQString& expr)
{
  *this = expr;
}

Expression& Expression::operator=(const TQString& s)
{
  m_start = 0;
  m_error = false;
  m_parts.clear();
  const TQString single = "()<>!+-/*%";
  int start = 0;
  int len = s.length();
  int i = 0;
  while (i < len)
  {
    if (((s[i] == '>' || s[i] == '<' || s[i] == '=' || s[i] == '!') &&
            s[i + 1] == '=') || (s[i] == '<' && s[i + 1] == '>'))
    {
      m_parts.append(TQVariant(s.mid(i, 2)));
      i += 2;
    } else if (s[i].isDigit())
    {
      i++;
      bool decimal = false;
      while (i < len && (s[i].isDigit() || (!decimal && s[i] == TQChar('.'))))
      {
        if (s[i] == '.')
          decimal = true;
        i++;
      }
      if (decimal)
        m_parts.append(TQVariant(s.mid(start, i - start).toDouble()));
      else
        m_parts.append(TQVariant(s.mid(start, i - start).toInt()));
    } else if (single.contains(s[i]))
      m_parts.append(TQVariant(TQString(s[i++])));
    else if (s[i] == '\"')
    {
      i++;
      while (i < len && s[i] != '\"')
        i++;
      m_parts.append(TQVariant(s.mid(start + 1, i - start - 1)));
      i++;
    } else if (s[i].isSpace())
      while (i < len && s[i].isSpace())
        i++;
    else
    {
      while (i < len && !s[i].isSpace())
        i++;
      TQString keyword = s.mid(start, i - start);
      if (keyword == "true")
        m_parts.append(TQVariant(true));
      else if (keyword == "false")
        m_parts.append(TQVariant(false));
      else        /* will be deprecated soon */
        m_parts.append(TQVariant(keyword));
    }
    start = i;
  }
  return *this;
}

TQString Expression::next() const
{
  if (m_start < m_parts.count())
    return m_parts[m_start].toString();
  else 
    return TQString();
}

bool Expression::validate()
{
  if (m_start >= m_parts.count())
    setError();
  return !m_error;
}
Expression::Type Expression::commonType(const TQVariant v1, const TQVariant v2) const
{
  if (v1.type() == TQVariant::String || v2.type() == TQVariant::String)
    return TypeString;
  else if (v1.type() == TQVariant::Double || v2.type() == TQVariant::Double)
    return TypeDouble;
  return TypeInt;
}

static int expression_compareDouble(const double A, const double B)
{ 
  return A<B ? -1 : (A==B ? 0 : 1);
}


int Expression::compare(const TQVariant v1, const TQVariant v2) const
{
  switch (commonType(v1, v2))  {
    case TypeString: return v1.toString().compare(v2.toString());
    case TypeDouble: return expression_compareDouble(v1.toDouble(), v2.toDouble());
    case TypeInt:    return v1.toInt() - v2.toInt();
    default:	 return 0;
  }
}


void Expression::setError(int pos)
{
  m_errorPosition = pos == -1 ? m_start : pos;
  m_error = true;
}

TQVariant Expression::parseNumber()
{
  if (!validate()) 
    return -1;
  return m_parts[m_start++];
}

TQVariant Expression::parseMinus()
{
  if (!validate()) return -1;
  bool sign = next() == "-";
  if (sign)
  {
    m_start++;
    TQVariant value = parseNumber();
    if (value.type() == TQVariant::Double)
      return -value.toDouble();
    else
      return -value.toInt();
  }
  else 
    return parseNumber();
}



TQVariant Expression::parseBracket()
{
  if (!validate()) return -1;
  if (next() == "(")
  {
    m_start++;
    TQVariant value = parse();
    if (next() == ")")
      m_start++;
    else
      setError();
    return value;  
  }
  else 
    return parseMinus();
}


TQVariant Expression::parseMultiply()
{
  if (!validate()) return -1;
  TQVariant value = parseBracket();
  TQString op = next();
  while (op == "*" || op == "/" || op == "%")
  {
    m_start++;
    TQVariant value2 = parseBracket();
    Type mode = commonType(value, value2);
    if (op == "*")
    {
      if (mode == TypeDouble)
        value = value.toDouble() * value2.toDouble();
      else
        value = value.toInt() * value2.toInt();
    }
    else if (op == "/")
    {
      if (value2.toInt() == 0)
        return i18n("error");
      if (mode == TypeDouble || value.toInt() != value.toInt() / value2.toInt() * value2.toInt())
        value = value.toDouble() / value2.toDouble();
      else
        value = value.toInt() / value2.toInt();
    }
    else 
    {
      if (value2.toInt() == 0)
        return i18n("error");
      if (mode == TypeDouble)
        value = value.toDouble() / value2.toInt();
      else
        value = value.toInt() / value2.toInt();
    }
    op = next();
  }
  return value;
}

TQVariant Expression::parseAdd()
{
  if (!validate()) return -1;
  TQVariant value = parseMultiply();
  TQString op = next();
  while (op == "+" || op == "-")
  {
    m_start++;
    TQVariant value2 = parseMultiply();
    Type mode = commonType(value, value2);
    if (op == "+")
      if (mode == TypeDouble)
        value = value.toDouble() + value2.toDouble();
      else
        value = value.toInt() + value2.toInt();
    else
      if (mode == TypeDouble)
        value = value.toDouble() - value2.toDouble();
      else
        value = value.toInt() - value2.toInt();
    op = next();
  }
  return value;
}

TQVariant Expression::parseComparison()
{
  if (!validate()) return -1;
  TQVariant value = parseAdd();
  TQString cmp = next();
  if (cmp == "<" || cmp == "<=" || cmp == "==" || cmp == ">=" || cmp == ">" || cmp == "<>" || cmp == "!=")
  {
    m_start++;
    TQVariant value2 = parseAdd();
    if (cmp == "<")
      return compare(value, value2) < 0;
    else if (cmp == "<=")
      return compare(value, value2) <= 0;
    else if (cmp == "==")
      return compare(value, value2) == 0;
    else if (cmp == ">=")
      return compare(value, value2) >= 0;
    else if (cmp == "<>" || cmp == "!=")
      return compare(value, value2) != 0;
    else 
      return compare(value, value2) > 0;
  }
  return value;
}

TQVariant Expression::parseNot()
{
  if (next() == "!" || next() == "not")
  {
    m_start++;    
    return !parseComparison().asBool();
  }
  else 
    return parseComparison();
}

TQVariant Expression::parseAnd()
{
  if (!validate()) return -1;
  TQVariant value = parseNot();
  while (next() == "&&" || next() == "and")
  {
    m_start++;    
    value = parseNot().toBool() && value.toBool();
  }
  
  return value;
}

TQVariant Expression::parseOr()
{
  if (!validate()) return -1;
  TQVariant value = parseAnd();
  while (next() == "||" || next() == "or")
  {
    m_start++;    
    value = parseAnd().toBool() || value.toBool();
  }
  return value;
}

TQVariant Expression::parse()
{ 
  return parseOr();
}

TQVariant Expression::value(bool* valid)
{
  m_start = 0;
  m_error = false;
  TQVariant val = parse();
  if (valid)
     *valid = !m_error && m_start == m_parts.count();
  return val;
}

TQVariant Expression::value(const TQString& s, bool* valid)
{
  *this = s;
  return value(valid); 
}

bool Expression::isTrue(const TQString& s, bool* valid)
{
  TQVariant v = value(s, valid);
  return (v.type() == TQVariant::String && !v.toString().isNull()) || 
      (v.type() != TQVariant::String && v.toInt() != 0);
}