summaryrefslogtreecommitdiffstats
path: root/src/evaluator.cpp
blob: 71998cf9d3111c1018a6e6dbf41a1f2c991c8fbf (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
/* This file was part of the SpeedCrunch project
   Copyright (C) 2004 Ariya Hidayat <ariya@kde.org>

   And is now part of abakus.
   Copyright (c) 2005 Michael Pyne <michael.pyne@kdemail.net>

   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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 
#include "evaluator.h"
#include "function.h"
#include "node.h" // For parser_yacc.hpp below
#include "parser_yacc.hpp"

#include <tqapplication.h>
#include <tqmap.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqvaluevector.h>

#include <kdebug.h>

//
// Reimplementation of goodies from Evaluator follows.
//

Evaluator::Evaluator()
{
}

Evaluator::~Evaluator()
{
}

void Evaluator::setExpression(const TQString &expr)
{
    kdError() << k_funcinfo << " not implemented.\n";
}

TQString Evaluator::expression() const
{
    kdError() << k_funcinfo << " not implemented.\n";
    return TQString();
}

void Evaluator::clear()
{
    kdError() << k_funcinfo << " not implemented.\n";
    // Yeah, whatever.
}

bool Evaluator::isValid() const
{
    return true;
}

Tokens Evaluator::tokens() const
{
    kdError() << k_funcinfo << " not implemented.\n";
    return Tokens();
}

Tokens Evaluator::scan(const TQString &expr)
{
    Lexer l(expr);
    Tokens tokens;

    while(l.hasNext())
    {
        int t = l.nextType();
        Token::Type type = Token::Unknown;

        switch(t)
        {
            case POWER:
            case '*':
            case '(':
            case ')':
            case '-':
            case '+':
            case ',':
            case '=':
                type = Token::Operator;
                break;

            case NUM:
                type = Token::Number;
                break;

            case SET:
            case REMOVE:
            case DERIV:
            case FN:
            case ID:
                type = Token::Identifier;
                break;

            default:
                type = Token::Unknown;
                break;
        }

        tokens.append(Token(type, l.tokenValue(), l.tokenPos()));
    }

    return tokens;
}

TQString Evaluator::error() const
{
    kdError() << k_funcinfo << " not implemented.\n";
    return "No Error Yet";
}

///
/// ARIYA'S CLASS CODE FOLLOWS
///

// for null token
const Token Token::null;

// helper function: return operator of given token text
// e.g. "*" yields Operator::Asterisk, and so on
static Token::Op matchOperator( const TQString& text )
{
  Token::Op result = Token::InvalidOp;
  
  if( text.length() == 1 )
  {
    TQChar p = text[0];
    switch( p.tqunicode() )
    {
        case '+': result = Token::Plus; break;
        case '-': result = Token::Minus; break;
        case '*': result = Token::Asterisk; break;
        case '/': result = Token::Slash; break;
        case '^': result = Token::Caret; break;
        case ',': result = Token::Comma; break;
        case '(': result = Token::LeftPar; break;
        case ')': result = Token::RightPar; break;
        case '%': result = Token::Percent; break;
        case '=': result = Token::Equal; break;
        default : result = Token::InvalidOp; break;
    }
  }
  
  if( text.length() == 2 )
  {
    if( text == "**" ) result = Token::Caret;
  }
  
  return result;
}

// creates a token
Token::Token( Type type, const TQString& text, int pos )
{
  m_type = type;
  m_text = text;
  m_pos = pos;
}

// copy constructor
Token::Token( const Token& token )
{
  m_type = token.m_type;
  m_text = token.m_text;
  m_pos = token.m_pos;
}

// assignment operator
Token& Token::operator=( const Token& token )
{
  m_type = token.m_type;
  m_text = token.m_text;
  m_pos = token.m_pos;
  return *this;
}

Abakus::number_t Token::asNumber() const
{
  if( isNumber() ) 
    return Abakus::number_t( m_text.latin1() );
  else 
    return Abakus::number_t();
}

Token::Op Token::asOperator() const
{
  if( isOperator() ) return matchOperator( m_text );
  else return InvalidOp;
}

TQString Token::description() const
{
  TQString desc;

  switch (m_type )
  {
    case  Number:     desc = "Number"; break;
    case  Identifier: desc = "Identifier"; break;
    case  Operator:   desc = "Operator"; break;
    default:          desc = "Unknown"; break;
  }

  while( desc.length() < 10 ) desc.prepend( ' ' );
  desc.prepend( "  " );
  desc.prepend( TQString::number( m_pos ) );
  desc.append( " : " ).append( m_text );

  return desc;
}


TQString Evaluator::autoFix( const TQString& expr )
{
  int par = 0;
  TQString result;
  
  // strip off all funny characters
  for( unsigned c = 0; c < expr.length(); c++ )
    if( expr[c] >= TQChar(32) )
      result.append( expr[c] );
  
  // automagically close all parenthesis
  Tokens tokens = Evaluator::scan( result );
  for( unsigned i=0; i<tokens.count(); i++ )
    if( tokens[i].asOperator() == Token::LeftPar ) par++;
    else if( tokens[i].asOperator() == Token::RightPar ) par--;
  for(; par > 0; par-- )
    result.append( ')' );  
    
  // special treatment for simple function 
  // e.g. "cos" is regarded as "cos(ans)"
  if( !result.isEmpty() ) 
  {
    Tokens tokens = Evaluator::scan( result );
    if( (tokens.count() == 1) &&
        FunctionManager::instance()->isFunction(tokens[0].text())
      )
    {
      result.append( "(ans)" );
    }
  }
  
  return result;  
}

// vim: set et ts=8 sw=4: