-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslator.cpp
More file actions
136 lines (119 loc) · 5.44 KB
/
Translator.cpp
File metadata and controls
136 lines (119 loc) · 5.44 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
131
132
133
134
135
136
//
// Created by root on 11/7/17.
//
#include <fstream>
#include "Translator.h"
#include "StringUtils.h"
void Translator::translateToCpp(const string &sourceFileName) {
auto syntaxAnalyzer = new SyntaxAnalyzer();
ifstream file(sourceFileName);
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
//cout << line << endl;
translateLine(line, *syntaxAnalyzer);
}
file.close();
} else {
string errorMessage;
errorMessage = "Could not read source file " + sourceFileName;
throw runtime_error(errorMessage);
}
}
string Translator::translatePows(const string &text, SyntaxAnalyzer &syntaxAnalyzer) {
string result = text;
int index;
while ((index = StringUtils::indexOf(result, "^")) != -1) {
int openedParentheses = 0;
string leftOperand, rightOperand;
int i, j;
for (i = index - 1; i >= 0; i--) {
if (openedParentheses == 0 && syntaxAnalyzer.isMathOperation(string(1, result.at(i)), i).isSuccessful()) break;
if (result.at(i) == '(') openedParentheses++;
else if (result.at(i) == ')') openedParentheses--;
leftOperand = string(1, result.at(i)) + leftOperand;
}
i++;
for (j = index + 1; j < result.length(); j++) {
if (openedParentheses == 0 && syntaxAnalyzer.isMathOperation(string(1, result.at(j)), j).isSuccessful()) break;
if (result.at(j) == '(') openedParentheses++;
else if (result.at(j) == ')') openedParentheses--;
rightOperand += string(1, result.at(j));
}
result = result.substr(0, i) + " pow(" + leftOperand + ", " + rightOperand + ") " + result.substr(j, result.length() - j);
}
return result;
}
CheckingResult Translator::translateLine(const string &line, SyntaxAnalyzer &syntaxAnalyzer) {
if (StringUtils::trim_copy(line).empty()) return CheckingResult(true);
string comment;
string lineWithoutComment;
unsigned int i = 0;
while (line.at(i) == ' ') i++;
int firstNonWhitespaceIdx = i;
if (line.at(i) == SyntaxAnalyzer::COMMENT_CHAR) {
comment = StringUtils::replaceFirst(line, "#", "//");
mainLines.push_back(comment);
return CheckingResult(true);
}
int commentStartIdx = StringUtils::indexOf(line, "#");
if (commentStartIdx != -1) {
comment = line.substr(commentStartIdx + 1, line.length() - (commentStartIdx + 1));
comment = "//" + comment;
lineWithoutComment = line.substr(0, commentStartIdx);
} else {
lineWithoutComment = line;
}
string substring;
bool assignment = false;
for (i; i < lineWithoutComment.length(); i++) {
if ((assignment = lineWithoutComment.at(i) == SyntaxAnalyzer::ASSIGNMENT_OPERATOR) || lineWithoutComment.at(i) == '(') break;
substring += string(1, lineWithoutComment.at(i));
}
string restString = lineWithoutComment.substr(i + 1, lineWithoutComment.length() - (i + 1));
CheckingResult check;
if (assignment) {
string trimmedId = StringUtils::trim_copy(substring);
if (trimmedId == "dx") {
check = syntaxAnalyzer.isFirstDerivativeX(restString, i + 1);
if (!check.isSuccessful()) return check;
syntaxAnalyzer.getIdentifiers().addIdentifier(trimmedId, DERIVATIVE);
string outputString = "#define " + trimmedId + " (" + restString + ") " + comment;
initLines.push_back(outputString);
return CheckingResult(true);
}
if (trimmedId == "dy") {
check = syntaxAnalyzer.isFirstDerivativeY(restString, i + 1);
if (!check.isSuccessful()) return check;
syntaxAnalyzer.getIdentifiers().addIdentifier(trimmedId, DERIVATIVE);
string outputString = "#define " + trimmedId + " (" + restString + ") " + comment;
initLines.push_back(outputString);
return CheckingResult(true);
}
if (trimmedId == "dz") {
check = syntaxAnalyzer.isFirstDerivativeZ(restString, i + 1);
if (!check.isSuccessful()) return check;
syntaxAnalyzer.getIdentifiers().addIdentifier(trimmedId, DERIVATIVE);
string outputString = "#define " + trimmedId + " (" + restString + ") " + comment;
initLines.push_back(outputString);
return CheckingResult(true);
}
check = syntaxAnalyzer.isIdentifier(trimmedId, firstNonWhitespaceIdx);
if (!check.isSuccessful()) return check;
if (syntaxAnalyzer.isExpression(restString, i + 1).isSuccessful()) {
string outputString =
"double " + trimmedId + " = " + translatePows(restString, syntaxAnalyzer) + "; " + comment;
syntaxAnalyzer.getIdentifiers().addIdentifier(trimmedId, DOUBLE);
mainLines.push_back(outputString);
} else if (true || syntaxAnalyzer.isMethodReturningType(restString, i + 1, TABLE).isSuccessful()) {
// TODO: Check and translate eulers, rungekutta and so on
string outputString = "auto " + trimmedId + " = " + restString + "; " + comment;
syntaxAnalyzer.getIdentifiers().addIdentifier(trimmedId, TABLE);
mainLines.push_back(outputString);
} else {
return CheckingResult(false, i + 1, "Invalid right hand side expression");
}
} else {// This must be method call without assignment
}
return CheckingResult();
}