-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
92 lines (79 loc) · 2.19 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include "Analyser.tab.c"
#include "Visitor.h"
#include "CodeGeneration/CodeGenerator.h"
#include "CILCodeGeneration/CodeGenerator.h"
using namespace std;
string inputFile = "tests/test_0.txt";
void drawTree(Node *root, int depth, bool lastChild[])
{
if (root == nullptr)
return;
cout << string(depth * 2, ' ');
if (lastChild[depth])
{
cout << "\\- ";
lastChild[depth] = false;
}
else
cout << "|-";
cout << root->token.lexeme << endl;
int numChildren = root->nodes.size();
for (int i = 0; i < numChildren; i++)
{
lastChild[depth + 1] = (i == numChildren - 1);
drawTree(root->nodes[i], depth + 1, lastChild);
}
}
int main(int argc, char *argv[])
{
cout << "#####################################\n";
if (argc > 1)
{
inputFile = argv[1];
}
cout << "\nInputfile: " << inputFile << endl;
// Check if input file exists
ifstream input(inputFile);
if (!input.good())
{
cerr << "Error: input file \"" << inputFile << "\" does not exist or cannot be opened.\n";
return 1;
}
input.close();
// Generation AST from the given code
Node *root_1 = generateAST(inputFile);
// Printing generated AST
cout << "\nAST:\n";
bool lastChild[1000] = {false};
drawTree(root_1, 0, lastChild);
// Semantic analysis
cout << endl
<< "Start typechecking" << endl;
Visitor v = Visitor();
v.DEBUG = false;
v.visitProgram(root_1);
cout << "Typecheck ends" << endl
<< endl;
// C# Code Generation
cout << "C# Code Generation starts" << endl;
CodeGenerator v2 = CodeGenerator();
v2.DEBUG = false;
v2.visitProgram(root_1);
v2.printAll();
v2.safeCSFile();
cout << "Code Generation ends" << endl
<< endl;
// .NET CIL Code Generation
cout << ".NET CIL Code Generation starts" << endl;
CILCodeGenerator v3 = CILCodeGenerator();
v3.DEBUG = false;
v3.visitProgram(root_1);
v3.printAll();
v3.safeCSFile();
cout << ".NET CIL Code Generation ends" << endl
<< endl;
cout << "Result of the running:" << endl;
}