aboutsummaryrefslogtreecommitdiff
path: root/parser.cpp
blob: 757f8c58f4be35b95d61cadd0cb2c0d1e0e25aba (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
#include "parser.h"

int Parser::getPriority(char tmp)
{
	switch ( tmp ) {
		case '-':
			return 10;
		case '+':
			return 10;
		case '/':
			return 20;
		case '*':
			return 20;
		case '^':
			return 30;
		case '(':
			return 90;
		case ')':
			return 90;	
		case ',':
			return -1;
		default:
			return -1;
	}
}

vector<string>* Parser::lexer(string term)
{
	int priority = 0;
	int last_priority = 0;
	int level = 0;
	string tmp, tmp_num;
	
	vector<string> *output = new vector<string>();
	
	string::iterator termIter;
	
	for ( termIter = term.begin(); termIter != term.end(); termIter++ ) {
		priority = this->getPriority( *termIter );
		
		if ( priority == -1 || ( termIter == term.begin() && priority == 10 ) ) {
			if ( level > 0 ) {
				tmp += *termIter;
			}
			else {
				tmp_num += *termIter;
			}
		}
		else {
			if ( last_priority == -1 && level == 0 ) {
				output->push_back( tmp_num );
				tmp_num = "";
			}
			
			switch ( *termIter ) {
				case '(': {
					if ( level > 0 ) {
						tmp += *termIter;
					}
					
					level++;
					break;
				}
				case ')': {
					level--;
					
					if ( level == 0 ) {
						output->push_back( tmp );
						tmp = "";
					}
					else {
						tmp += *termIter;
					}
					break;
				}
				default: {				
					if ( level == 0 ) {
						output->push_back( boost::lexical_cast<string>(*termIter) );
					}
					else {
						tmp += *termIter;
					}
					break;
				}
			}
		}
		
		last_priority = priority;	
	}
	
	if ( last_priority == -1 ) {
		output->push_back( tmp_num );
	}
	else if ( last_priority != 90 ) {
		throw operator_exception();
	}
	
	if (level != 0) {
		throw parenthese_exception();
	}
	
	return output;
}

Node* Parser::buildTree(Tree **tree, string term)
{
	stack<Node*> *operandStack = new stack<Node*>();
	stack<Node*> *operatorStack = new stack<Node*>();
	
	int priority;
	Node *newNode;
	
	vector<string> *tmpLexer;
	
	vector<string> *lexerOutput = lexer(term);
	
	for ( vector<string>::iterator termIter = lexerOutput->begin(); termIter != lexerOutput->end(); termIter++ ) {
		priority = this->getPriority( (*termIter)[0] );
		
		if ( priority != -1 && (*termIter).size() == 1 ) {
			if ( !operatorStack->empty() ) {
				OperatorNode *lastNode = static_cast<OperatorNode*>( operatorStack->top() );
			
				if ( this->getPriority( lastNode->function ) < priority ) {
					newNode = (*tree)->addOperator( NULL, (*termIter)[0] );
					operatorStack->push( newNode );
				}
				else {
					Node *currOperator = operatorStack->top();
					operatorStack->pop();
					
					currOperator->rightChild = operandStack->top();
					operandStack->pop();
					
					currOperator->leftChild = operandStack->top();
					operandStack->pop();
					
					operandStack->push( currOperator );
					
					termIter--;
				}
			}
			else {
				newNode = (*tree)->addOperator( NULL, (*termIter)[0] );
				operatorStack->push( newNode );
			}
		}
		else {
			tmpLexer = lexer( *termIter );
			
			if ( tmpLexer->size() == 1 ) {
				newNode = (*tree)->addOperand( NULL, boost::lexical_cast<double>(*termIter) );
				operandStack->push( newNode );
			}
			else if ( tmpLexer->size() > 1 ) {
				newNode = buildTree(tree, *termIter);
				operandStack->push( newNode );
			}
		}
	}
	
	while ( !operatorStack->empty() ) {
		OperatorNode *currOperator = static_cast<OperatorNode*>( operatorStack->top() );
		operatorStack->pop();
					
		currOperator->rightChild = operandStack->top();
		operandStack->pop();
					
		currOperator->leftChild = operandStack->top();
		operandStack->pop();
				
		operandStack->push( currOperator );
	}
	
	return operandStack->top();
}

ParserResult Parser::calculate(string term, bool getTreeAsDot)
{
	Tree *termTree = new Tree();
	termTree->root = buildTree(&termTree, term);
	
	ParserResult returnVal;
	
	returnVal.result = termTree->root->solve();
	
	if ( getTreeAsDot ) {
		returnVal.tree = termTree->print(term);
	}
	
	return returnVal;
}