-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
77 lines (67 loc) · 2.49 KB
/
main.cpp
File metadata and controls
77 lines (67 loc) · 2.49 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
#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>
#include <streambuf>
#include "scanner.h"
#include "token.h"
#include "parser.h"
#include "semanticAnalyzer.h"
#include "astNode.h"
#include "symbolTable.h"
#include "irGenerator.h"
#include "codeGenerator.h"
int main(int argc, char * argv[]) {
if (argc < 3) {
std::cout << "Did not provide enough arguments. Usage: AgNES input_file.c output_file.s\n";
return 1;
}
char * inputFileName = argv[1];
char * outputFileName = argv[2];
// First we run Lexical Analysis, to turn the stream of characters into a list of tokens
// Input: file
// Output: list of tokens
std::ifstream file (inputFileName);
std::list<Token> tokens;
if (file.is_open()) {
Scanner scanner = Scanner(file);
tokens = scanner.scan();
} else {
std::cout << "Could not open file \n";
return 1;
}
// Next we run Syntax Analysis/Parsing, where we generate a parse tree
// Input: list of tokens
// Output: parse tree
Parser parser = Parser(tokens);
ASTNode * astRoot = parser.parse();
//Now we make a SemanticAnalyzer!
std::cout << "Running semantic analysis..." << std::endl;
SemanticAnalyzer analyzer = SemanticAnalyzer(astRoot);
SymbolTable * symbolTable = analyzer.analyze();
// Generate intermediate code
std::cout << "Running IR Generator..." << std::endl;
IRGenerator irGenerator = IRGenerator(astRoot);
std::list<TAC> intermediateCode = irGenerator.generate();
//Generate machine code
std::cout << "Running Code Generator..." << std::endl;
CodeGenerator codeGenerator = CodeGenerator(intermediateCode, symbolTable);
std::string machineCode = codeGenerator.generate();
std::cout << machineCode;
// Sandwich our machine code in the middle of the header and footer
std::cout << "Writing to " << outputFileName << std::endl;
std::ifstream header("NES_HEADER.s");
std::string headerStr((std::istreambuf_iterator<char>(header)), std::istreambuf_iterator<char>());
std::ifstream footer("NES_FOOTER.s");
std::string footerStr((std::istreambuf_iterator<char>(footer)), std::istreambuf_iterator<char>());
std::remove(outputFileName);
std::ofstream outputFile;
outputFile.open(outputFileName);
outputFile << headerStr;
outputFile << machineCode;
outputFile << footerStr;
header.close();
footer.close();
outputFile.close();
std::cout << "Finished Compilation." << std::endl;
}