-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.cpp
More file actions
130 lines (108 loc) · 3.52 KB
/
repl.cpp
File metadata and controls
130 lines (108 loc) · 3.52 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include "Lexer.h"
#include "Parser.h"
#include "Evaluator.h"
#include "EvalResult.h"
#include "SemanticAnalyzer.h"
#include "SemanticAnalysisResult.h"
bool isInputForLoop(const std::string& input) {
return input.find("for") != std::string::npos;
}
bool isInputIfStmt(const std::string& input) {
return input.find("if") != std::string::npos || input.find("else") != std::string::npos;
}
bool isInputFuncDecl(const std::string& input) {
return input.find("func") != std::string::npos;
}
long compoundCnt;
void countOpenBrackets(const std::string stmt) {
for (const auto& currentCh : stmt) {
if (currentCh == '{') {
compoundCnt++;
}
}
}
void countEndBrackets(const std::string stmt) {
for (const auto& currentCh : stmt) {
if (currentCh == '}') {
compoundCnt--;
}
}
}
std::string readCompoundStatement(const std::string& input) {
std::string stmt = input;
std::string currentInput;
while (true) {
if (compoundCnt == 0) {
break;
}
getline(std::cin, currentInput);
if (std::cin.eof()) {
exit(EXIT_SUCCESS);
}
countOpenBrackets(currentInput);
countEndBrackets(currentInput);
if (isInputIfStmt(currentInput) || isInputForLoop(currentInput) || isInputFuncDecl(currentInput)) {
stmt += readCompoundStatement(currentInput);
} else if (currentInput.empty()) {
continue;
} else {
if (compoundCnt) {
currentInput.push_back('\n');
}
stmt += currentInput;
}
}
return stmt;
}
void printResult(const EvalResult& result, int tabCount = 0) {
ValueType::Type resultType = result.getResultType();
if (resultType == ValueType::Number) {
std::cout << result.getResultDouble() << std::endl;
} else if (resultType == ValueType::Bool) {
std::cout << (result.getResultBool() ? "true" : "false") << std::endl;
} else if (resultType == ValueType::Compound) {
for (const auto& currentResult : result.getResultBlock()) {
printResult(currentResult, tabCount);
}
} else if (resultType == ValueType::String) {
std::cout << result.getResultString() << std::endl;
}
}
int main() {
Lexer lexer;
Parser parser;
SemanticAnalyzer semanticAnalyzer(0);
Evaluator evaluator;
std::vector<ProgramTranslationNode*> nodes;
while (true) {
std::string input;
getline(std::cin, input);
if (std::cin.eof()) {
break;
}
if (input.size() != 0) {
if (isInputIfStmt(input) || isInputForLoop(input) || isInputFuncDecl(input)) {
countOpenBrackets(input);
countEndBrackets(input);
input = readCompoundStatement(input);
}
input.push_back('\n');
input.push_back(EOF);
const TokenContainer& tokens = lexer.tokenize(input);
ProgramTranslationNode* root = parser.parse(tokens);
SemanticAnalysisResult checkResult = semanticAnalyzer.checkProgram(root);
if (checkResult.isError()) {
std::cerr << checkResult.what() << std::endl;
} else {
EvalResult result = evaluator.Evaluate(root->statements[0]);
printResult(result);
}
nodes.emplace_back(root);
}
}
for (const auto& currentNode : nodes) {
delete currentNode;
}
return 0;
}