-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlexer.cpp
executable file
·60 lines (50 loc) · 1.4 KB
/
lexer.cpp
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
#include "lexer.h"
#include <QDebug>
double CLexer::getNumber() const
{
return m_dNumber;
}
ETokenType CLexer::getNextToken()
{
if (m_sExpression.isEmpty())
return m_eCurrentToken = ETokenType::END;
QChar ch = m_sExpression.front();
m_sExpression.remove(0, 1);
switch (ch.cell())
{
case '/': return m_eCurrentToken = ETokenType::DIV;
case '*': return m_eCurrentToken = ETokenType::MUL;
case '+': return m_eCurrentToken = ETokenType::PLUS;
case '-': return m_eCurrentToken = ETokenType::MINUS;
case '(': return m_eCurrentToken = ETokenType::LEFT_PAREN;
case ')': return m_eCurrentToken = ETokenType::RIGHT_PAREN;
}
QString sNumber = "";
if (ch.isDigit())
{
sNumber += ch;
while (!m_sExpression.isEmpty()
&& (m_sExpression.front().isDigit()
|| m_sExpression.front() == '.'))
{
sNumber += m_sExpression.front();
m_sExpression.remove(0, 1);
}
m_dNumber = sNumber.toDouble();
return m_eCurrentToken = ETokenType::NUMBER;
}
else
{
qDebug() << "Error: Invalid Token.";
exit(-1);
}
}
ETokenType CLexer::getCurrentToken() const
{
return m_eCurrentToken;
}
void CLexer::setExpression(const QString &sExpression)
{
m_sExpression = sExpression.simplified();
m_sExpression.replace(" ", "");
}