-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.cpp
More file actions
47 lines (40 loc) · 1.16 KB
/
compiler.cpp
File metadata and controls
47 lines (40 loc) · 1.16 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
#include <iostream>
#include <fstream>
#include <sstream>
#include "Lexer.h"
#include "Parser.h"
#include "SemanticAnalyzer.h"
#include "BashGenerator.h"
std::string readProgram(const std::string fileName) {
std::string input;
std::stringstream ss;
std::ifstream ifs(fileName);
ss << ifs.rdbuf();
input = ss.str();
ifs.close();
return input;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
throw std::runtime_error("Source code file required");
}
Lexer lexer;
Parser parser;
SemanticAnalyzer semanticAnalyzer(1);
BashGenerator bashGenerator;
std::string source = readProgram(argv[1]);
source.push_back('\n');
source.push_back(EOF);
const TokenContainer& tokens = lexer.tokenize(source);
ProgramTranslationNode* ast = parser.parse(tokens);
const SemanticAnalysisResult& checkResult = semanticAnalyzer.checkProgram(ast);
if (checkResult.isError()) {
throw std::runtime_error(checkResult.what());
}
std::string bashCode = bashGenerator.generate(ast);
delete ast;
std::ofstream outFile("bash_program.sh");
outFile << bashCode;
outFile.close();
return 0;
}