-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConditionParser.cpp
96 lines (90 loc) · 2.78 KB
/
ConditionParser.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
#include "ConditionParser.h"
#include "Evaluator.h"
#include "StringHelper.h"
#include <iostream>
#define MIN(a,b) (((a)<(b))?(a):(b))
#define DELIM "\n"
using namespace std;
ConditionParser::ConditionParser(map<string, double>* symbolTable,
map<string, Expression*>* expressionsMap,
Interpreter* interpreter) {
_symbolTable = symbolTable;
_expressionsMap = expressionsMap;
_interpreter = interpreter;
// sets operators map:
_operatorsMap = {{"<", 1}, {"<=", 2}, {">", 3}, {">=", 4}, {"==", 5}, {"!=", 6}};
}
/*
* function that calculates whether the condition
* held by the condition parser is accurate.
* */
bool ConditionParser::conditionIsTrue(vector<string>& arguments) {
// finds out where is the condition in the arguments list.
int ifIndex = StringHelper::nextIndexOf(arguments.begin(), "if", arguments.end());
int whileIndex = StringHelper::nextIndexOf(arguments.begin(), "while", arguments.end());
int startingIndex = MIN(ifIndex, whileIndex);
vector<string>::iterator itr = ++(arguments.begin()) + startingIndex;
string operators = ",<=,<,>=,>,==,!=,";
string seperator = ",";
// reads expression until an operator is found -> first operant.
vector<string> firstOp;
while (operators.find(seperator + *itr + seperator) == string::npos) {
firstOp.push_back(*itr);
++itr;
}
string oper = *itr;
*itr++;
// reads expression until an { is found -> second operant.
vector<string> secondOp;
while ((*itr).compare("{") != 0) {
secondOp.push_back(*itr);
++itr;
}
unsigned int dummyIndex = 0;
double firstVal = Evaluator::evaluate(firstOp, &dummyIndex, _symbolTable);
dummyIndex = 0;
double secondVal = Evaluator::evaluate(secondOp, &dummyIndex, _symbolTable);
switch (_operatorsMap.at(oper)) {
case 1:
return firstVal < secondVal;
break;
case 2:
return firstVal <= secondVal;
break;
case 3:
return firstVal > secondVal;
break;
case 4:
return firstVal >= secondVal;
break;
case 5:
return firstVal == secondVal;
break;
case 6:
return firstVal != secondVal;
break;
default:
throw "Undefined comparison in condition";
};
}
// function that calls parser on condition body's block.
void ConditionParser::runBlock(vector<string>& arguments) {
_interpreter->parser(arguments, 0);
}
// extracts the block of commands from the condition.
vector<string> ConditionParser::extractBlock(vector<string>& vec) {
int i = StringHelper::nextIndexOf(vec.begin(), "{", vec.end());
auto itr = vec.begin() + i;
int indentation = 0;
vector<string> body;
while (itr != vec.end()) {
if (itr->find("{") != string::npos)
++indentation;
if (itr->find("}") != string::npos)
if (--indentation == 0)
break;
body.push_back(*(++itr));
}
body.pop_back();
return body;
}