-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntaxParser.cpp
178 lines (150 loc) · 4.51 KB
/
SyntaxParser.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
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
#include "SyntaxParser.h"
#include "ListToDoubleConverter.h"
#include <iostream>
using namespace std;
SyntaxParser::SyntaxParser() {
_parsedExpression = new LinkedList<FormulaItem*>();
_current = 0;
}
SyntaxParser::~SyntaxParser() {
delete _parsedExpression;
};
bool IsDecimalNumber(char c) {
return (c >= '0' && c <= '9') || c == '.';
}
bool IsSpace(char c) {
return c == ' ' || c == '\n' || c == '\n\r' || c == '\r';
}
bool IsText(char c) {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
}
void Exit(string error) {
cout << error;
exit(-1);
}
bool StringCompare(LinkedList<char>* list, string s) {
list->MoveFirst();
int counter = 0;
while (!list->AtEnd()) {
if (counter >= s.length()) {
return false;
}
if (list->GetCurrent() != s[counter]) {
return false;
}
counter++;
list->MoveNext();
}
return true;
}
void AppendFunction(LinkedList<FormulaItem*>* _parsedExpression, LinkedList<char>* buffer) {
/*sin, cos, tan, log, exp, sqrt, min, max, nsd, nsn*/
if (StringCompare(buffer, "sin")) {
_parsedExpression->Append(new FormulaItem(FunctionType::Sine));
} else if (StringCompare(buffer, "cos")) {
_parsedExpression->Append(new FormulaItem(FunctionType::Cosine));
} else if (StringCompare(buffer, "tan")) {
_parsedExpression->Append(new FormulaItem(FunctionType::Tangent));
} else if (StringCompare(buffer, "log")) {
_parsedExpression->Append(new FormulaItem(FunctionType::Logarithm));
} else if (StringCompare(buffer, "exp")) {
_parsedExpression->Append(new FormulaItem(FunctionType::Exponential));
} else if (StringCompare(buffer, "sqrt")) {
_parsedExpression->Append(new FormulaItem(FunctionType::SquareRoot));
} else if (StringCompare(buffer, "min")) {
_parsedExpression->Append(new FormulaItem(FunctionType::Minimum));
} else if (StringCompare(buffer, "max")) {
_parsedExpression->Append(new FormulaItem(FunctionType::Maximum));
} else if (StringCompare(buffer, "nsd")) {
_parsedExpression->Append(new FormulaItem(FunctionType::GreatestCommonDivisor));
} else if (StringCompare(buffer, "nsn")) {
_parsedExpression->Append(new FormulaItem(FunctionType::LeastCommonDenominator));
} else {
Exit("Function not recognised");
}
}
void SyntaxParser::ParseText() {
LinkedList<char>* buffer = new LinkedList<char>();
RewindByOne();
//Checking for function names terminated by either open parenthesis
char c;
while (true) {
c = GetNextChar();
if (IsText(c)) {
buffer->Append(c);
} else if (c == '(') {
AppendFunction(_parsedExpression, buffer);
break;
} else {
Exit("Function not followed by open parenthesis");
}
}
RewindByOne();
delete buffer;
}
void SyntaxParser::ParseNumber() {
LinkedList<char>* buffer = new LinkedList<char>();
RewindByOne();
char c;
while (true) {
c = GetNextChar();
if (IsDecimalNumber(c)) {
buffer->Append(c);
} else {
break;
}
}
float value = ConvertCharListToDouble(buffer);
_parsedExpression->Append(new FormulaItem(value));
RewindByOne();
delete buffer;
}
LinkedList<FormulaItem*>* SyntaxParser::ParseExpression(string input) {
_input = input;
while (true) {
char c = GetNextChar();
if (c == '\0') {
return _parsedExpression;
} else if (IsText(c)) {
ParseText();
} else if (IsDecimalNumber(c)) {
ParseNumber();
} else if (IsSpace(c)) {
continue;
} else if (c == '(') {
_parsedExpression->Append(new FormulaItem(ParenthesisType::Left));
} else if (c == ')') {
_parsedExpression->Append(new FormulaItem(ParenthesisType::Right));
} else if (c == '+') {
_parsedExpression->Append(new FormulaItem(OperatorType::Addition));
} else if (c == '-') {
if (_parsedExpression->IsEmpty() || _parsedExpression->Last()->FormulaType == FormulaType::Operator) {
_parsedExpression->Append(new FormulaItem(FunctionType::Negation));
} else {
_parsedExpression->Append(new FormulaItem(OperatorType::Substraction));
}
} else if (c == '*') {
_parsedExpression->Append(new FormulaItem(OperatorType::Multiplication));
} else if (c == '/') {
_parsedExpression->Append(new FormulaItem(OperatorType::Division));
} else if (c == ',') {
//max(1+2,3+4) -> max(1+2)(3+4)
_parsedExpression->Append(new FormulaItem(ParenthesisType::Right));
_parsedExpression->Append(new FormulaItem(ParenthesisType::Left));
} else {
Exit("Not recognised character in equation");
}
}
}
char SyntaxParser::GetNextChar() {
if (_current >= _input.length()) {
_current++;
return '\0';
}
char result = _input[_current];
_current++;
return result;
}
void SyntaxParser::RewindByOne() {
_current--;
}