-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineParser.cpp
More file actions
115 lines (102 loc) · 5.13 KB
/
Copy pathLineParser.cpp
File metadata and controls
115 lines (102 loc) · 5.13 KB
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
//
// Created by roy on 12/18/18.
//
#include "LineParser.h"
/**
* Constructor of LineParser, used for parsing single line.
* @param symbolTable symbol table for variables access.
* @param dataReaderServer data reader server for the check if in bind table.
* @param dataSender data sender for the execution of sending commands to the client.
*/
LineParser::LineParser(SymbolTable *symbolTable, DataReaderServer *dataReaderServer, DataSender * newDataSender) {
this->symbolTable = symbolTable;
this->dataReaderServer = dataReaderServer;
this->util.set(this->symbolTable, this->dataReaderServer);
this->dataSender = newDataSender;
}
/**
* This method parses the given strings vector and executes the matching commands
* by their order. The block will be of two types: If block or While block.
* @param stringVector vector of strings representing the commands and parameters.
* @param startIndex starting index for the iteration on the vector.
* @return int value of the jump needed in while/if commands.
*/
int LineParser::parse(vector<string> stringVector, int startIndex) {
string commandString = stringVector.at(startIndex); // first string will represent the first command in line.
Commands command = commandMap.getCommand(commandString);
if (stringVector.at(startIndex + 1) == "=") {
command = Equal;
}
int indexJumpValue = 0;
// for each command option - execute the fitting operation:
switch (command) {
case OpenDataServer : {
string port = util.shuntingYard(smallLexer.lexer(stringVector.at(startIndex + 1)));
string herz = util.shuntingYard(smallLexer.lexer(stringVector.at(startIndex + 2)));
OpenServerCommand openServer(port, herz, this->dataReaderServer, this->symbolTable);
indexJumpValue = openServer.execute();
break;
}
case Connect : {
ConnectCommand connect(stringVector.at(startIndex + 1), util.shuntingYard(smallLexer.lexer(stringVector.at(startIndex + 2))),
this->dataSender);
indexJumpValue = connect.execute();
break;
}
case Var : {
if (stringVector.size() == 5) {
VarCommand varCommand(this->dataReaderServer, this->symbolTable, stringVector.at(startIndex + 1),
stringVector.at(startIndex + 2), stringVector.at(startIndex + 3), stringVector.at(startIndex + 4));
indexJumpValue = varCommand.execute();
} else {
VarCommand varCommand(this->dataReaderServer, this->symbolTable, stringVector.at(startIndex + 1),
stringVector.at(startIndex + 2), stringVector.at(startIndex + 3));
indexJumpValue = varCommand.execute();
}
break;
}
case Equal : {
EqualCommand equalCommand(this->dataReaderServer, this->dataSender, stringVector.at(startIndex),
stringVector.at(startIndex + 2), this->symbolTable);
indexJumpValue = equalCommand.execute();
break;
}
case Print : {
string printString;
// if the next string starts with quotes - string will be the string inside quotes.
if (stringVector.at(startIndex + 1).at(0) == '"') {
printString = stringVector.at(startIndex + 1).substr(1, stringVector.at(startIndex + 1).length() - 2);
}
// else, get the string value from symbol map.
//if the next string starts as a number consider it a mathematical expression
else if (stringVector.at(startIndex + 1).at(0) == '-' || stringVector.at(startIndex + 1).at(0) == '(' ||
isdigit(stringVector.at(startIndex + 1).at(0))) {
printString = util.shuntingYard(smallLexer.lexer(stringVector.at(startIndex + 1)));
}
else {
//check if its a local variable
if (this->symbolTable->isInMap(stringVector.at(startIndex + 1))) {
printString = to_string(this->symbolTable->get(stringVector.at(startIndex + 1)));
//check if its a global variable
} else if (this->symbolTable->isInMap(this->dataReaderServer->getBindAddress(stringVector.at(startIndex + 1)))) {
printString = to_string(
this->symbolTable->get(this->dataReaderServer->getBindAddress(stringVector.at(startIndex + 1))));
}
}
PrintCommand printCommand(printString);
indexJumpValue = printCommand.execute();
break;
}
case Sleep : {
// sleeps for the given amount of time.
vector<string> stringExp = this->smallLexer.lexer(stringVector.at(startIndex + 1));
SleepCommand sleepCommand(stod(this->util.shuntingYard(stringExp)));
indexJumpValue = sleepCommand.execute();
break;
}
// if the command is invalid command, throw.
default:
throw "No such Command";
}
return indexJumpValue;
}