-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascal.cpp
More file actions
152 lines (127 loc) · 4.26 KB
/
Pascal.cpp
File metadata and controls
152 lines (127 loc) · 4.26 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <chrono>
#include "antlr4-runtime.h"
#include "PascalLexer.h"
#include "PascalParser.h"
#include "frontend/Listing.h"
#include "frontend/SyntaxErrorHandler.h"
#include "frontend/Semantics.h"
#include "intermediate/symtab/Predefined.h"
#include "intermediate/type/Typespec.h"
#include "intermediate/util/BackendMode.h"
#include "backend/converter/Converter.h"
#include "backend/interpreter/Executor.h"
#include "backend/compiler/Compiler.h"
using namespace std;
using namespace antlrcpp;
using namespace antlr4;
using namespace frontend;
using namespace intermediate::type;
using namespace intermediate::symtab;
using namespace backend::converter;
using namespace backend::interpreter;
using namespace backend::compiler;
int main(int argc, const char *args[])
{
if (argc != 3)
{
cout << "USAGE: PascalJava option sourceFileName" << endl;
cout << " option: -convert, -execute, or -compile" << endl;
return -1;
}
string option = toLowerCase(args[1]);
string sourceFileName = args[2];
BackendMode mode = EXECUTOR;
if (option == "-convert") mode = CONVERTER;
else if (option == "-execute") mode = EXECUTOR;
else if (option == "-compile") mode = COMPILER;
else
{
cout << "ERROR: Invalid option." << endl;
cout << " Valid options: -convert, -execute, or -compile" << endl;
return -2;
}
// Generate a source file listing.
Listing listing(sourceFileName);
ifstream ins;
ins.open(args[2]);
// Create the input stream.
ANTLRInputStream input(ins);
// Custom syntax error handler.
SyntaxErrorHandler syntaxErrorHandler;
// Create a lexer which scans the character stream
// to create a token stream.
PascalLexer lexer(&input);
lexer.removeErrorListeners();
lexer.addErrorListener(&syntaxErrorHandler);
CommonTokenStream tokens(&lexer);
// Create a parser which parses the token stream.
PascalParser parser(&tokens);
// Pass 1: Check syntax and create the parse tree.
cout << endl << "PASS 1 Syntax: ";
parser.removeErrorListeners();
parser.addErrorListener(&syntaxErrorHandler);
tree::ParseTree *tree = parser.program();
// Allow any syntax error messages to print.
this_thread::sleep_for(chrono::milliseconds(100));
int errorCount = syntaxErrorHandler.getCount();
if (errorCount > 0)
{
printf("\nThere were %d syntax errors.\n", errorCount);
cout << "Object file not created or modified." << endl;
return errorCount;
}
else
{
cout << "There were no syntax errors." << endl;
}
// Pass 2: Create symbol tables and set parse tree node datatypes.
cout << endl << "PASS 2 Semantics:" << endl ;
Semantics *pass2 = new Semantics(mode);
pass2->visit(tree);
int error_count = pass2->getErrorCount();
if (error_count > 0)
{
cout << endl << "There were " << error_count << " semantic errors."
<< " Object file not created or modified." << endl;
return errorCount;
}
// Pass 3: Translation.
switch (mode)
{
case EXECUTOR:
{
// Pass 3: Execute the Pascal program.
cout << endl << "PASS 3 Execution:" << endl << endl;
SymtabEntry *programId = pass2->getProgramId();
Executor *pass3 = new Executor(programId);
pass3->visit(tree);
break;
}
case CONVERTER:
{
// Pass 3: Convert from Pascal to Java.
cout << endl << "PASS 3 Translation:" << endl;
Converter *pass3 = new Converter();
pass3->visit(tree);
cout << endl << "Object file \"" << pass3->getObjectFileName()
<< "\" created." << endl;
break;
}
case COMPILER:
{
// Pass 3: Compile the Pascal program.
cout << endl << "PASS 3 Compilation: ";
SymtabEntry *programId = pass2->getProgramId();
Compiler *pass3 = new Compiler(programId);
pass3->visit(tree);
cout << "Object file \"" << pass3->getObjectFileName()
<< "\" created." << endl;
break;
}
}
return 0;
}