/****************************************************************************
**
** Copyright (C) 2013 Valentina project All Rights Reserved.
**
** This file is part of Valentina.
**
** Tox 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 3 of the License, or
** (at your option) any later version.
**
** Valentina 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 Valentina. If not, see .
**
****************************************************************************/
#include "calculator.h"
#define DELIMITER 1
#define VARIABLE 2
#define NUMBER 3
#define COMMAND 4
#define STRING 5
#define QUOTE 6
#define FINISHED 10
#define EOL 9
qreal Calculator::eval(QString prog, QString *errorMsg)
{
this->errorMsg = errorMsg;
this->errorMsg->clear();
debugFormula.clear();
this->prog = prog;
//qDebug()<<"Формула: "<0; --t)
// *r = (*r) * ex;
break;
}
}
/* Изменение знака */
void Calculator::unary(QChar o, qreal *r)
{
if (o=='-')
{
*r = -(*r);
}
}
/* Поиск значения переменной */
qreal Calculator::find_var(QString s)
{
bool ok = false;
qreal value = data->FindVar(s, &ok);
if (ok == false)
{
qDebug()<clear();
*errorMsg = e[error];
qDebug()</*%^=()", c) || c=='\n' || c=='\r' || c=='\0')
{
return true;
}
return false;
}
/* Возвращает 1, если "с" пробел или табуляция */
bool Calculator::iswhite(QChar c)
{
if (c==' ' || c=='\t')
{
return true;
}
else
{
return false;
}
}
void Calculator::get_token()
{
QString *temp;
token_type=0; tok=0;
token.clear();
temp=&token;
if (prog[index]=='\0')
{ /* Конец файла */
token="\0";
tok=FINISHED;
token_type=DELIMITER;
return;
}
while (iswhite(prog[index]))
{
++index; /* пропуск пробелов */
}
if (prog[index]=='\r')
{ /* crtl */
++index; ++index;
tok= EOL; token='\r';
token.append('\n');token.append("\0");
token_type = DELIMITER;
return;
}
if (StrChr("+-*^/%=;(),><", prog[index]))
{ /* разделитель */
*temp=prog[index];
index++; /* переход на следующую позицию */
temp->append("\0");
token_type=DELIMITER;
debugFormula.append(token);
return;
}
if (prog[index]=='"')
{ /* строка в кавычках */
index++;
while (prog[index] != '"' && prog[index] != '\r')
{
temp->append(prog[index]);
index++;
}
if (prog[index]=='\r')
{
serror(1);
}
index++;temp->append("\0");
token_type=QUOTE;
return;
}
if (prog[index].isDigit())
{ /* число */
while (isdelim(prog[index]) == false)
{
temp->append(prog[index]);
index++;
}
temp->append('\0');
token_type = NUMBER;
return;
}
if (prog[index].isPrint())
{ /* переменная или команда */
while (isdelim(prog[index]) == false)
{
temp->append(prog[index]);
index++;
}
token_type=STRING;
}
temp->append("\0");
/* Просматривается, если строка есть команда или переменная */
if (token_type==STRING)
{
tok=look_up(token); /* преобразование во внутренний
формат */
if (tok == false)
{
token_type = VARIABLE;
}
else
{
token_type = COMMAND; /* это команда */
}
}
return;
}
bool Calculator::StrChr(QString string, QChar c)
{
return string.contains(c, Qt::CaseInsensitive);
}
/* Возвращает лексему обратно во входной поток */
void Calculator::putback()
{
QString t;
t = token;
index = index - t.size();
}