-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
98 lines (77 loc) · 2.94 KB
/
main.cpp
File metadata and controls
98 lines (77 loc) · 2.94 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
#include <iostream>
#include <streambuf>
#include "util.hpp"
#include "Lexer.hpp"
#include "Parser.hpp"
#include "SemanticAnalyzer.hpp"
#include "ScopedSymbolTable.hpp"
#include "Interpreter.hpp"
#include "StandardAPI.hpp"
#include "GraphicsAPI.hpp"
class DummyLogger: private std::streambuf, public std::ostream {
public:
inline DummyLogger(): std::ostream(this) {}
};
void run(const std::string& sourceCode, std::ostream& cout_tokens, std::ostream& cout_parseTree, std::ostream& cout_AST, std::ostream& cout_Interpreter) {
ImmediateLexer lexer(sourceCode);
cout_tokens << lexer << "\n";
cout_parseTree << "ParseTree:\n";
Parser parser(lexer);
std::unique_ptr<const ParseTree::Program> tree = parser.program();
tree->print(cout_parseTree, "", true);
cout_parseTree << "\n";
// std::cout << "\nReconstructed Source:\n" << tree->toString(0) << "\n\n";
std::shared_ptr<ScopedSymbolTable> globalScope = std::make_shared<ScopedSymbolTable>("Global Scope");
registerStandardApiFunctions(*globalScope);
registerGraphicsApiFunctions(*globalScope);
cout_AST << "AST:\n";
std::unique_ptr<const AST::Node> ast = SemanticAnalyzer::visit(tree.get(), globalScope);
ast->print(cout_AST, "", true);
cout_AST << "\n";
// globalScope->print(std::cout);
cout_Interpreter << "Interpreting:\n";
Interpreter interpreter(ast.get(), cout_Interpreter);
interpreter.run();
}
int main(int argc, char** argv) {
if(argc < 2) {
std::cout << "Usage:\n";
std::cout << "\tbcc [FLAGS] [INPUT]\n";
std::cout << "\nFLAGS:\n";
std::cout << "\t--print-tokens print Token Stream\n";
std::cout << "\t--print-parsetree print generated Parse-Tree\n";
std::cout << "\t--print-ast print generated AST\n";
std::cout << "\t--print-interpreter print interpreter Debug Output\n";
std::cout << "\nINPUT:\n";
std::cout << "\tpath of script file\n";
std::cout << "\n";
return 0;
}
// const std::string fileName = argv[1];
DummyLogger dout;
std::ostream* cout_lexer = &dout;
std::ostream* cout_parser = &dout;
std::ostream* cout_semantic = &dout;
std::ostream* cout_interpreter = &dout;
for(int i = 1; i < argc; i++) {
const std::string arg = argv[i];
if(arg == "--print-tokens") cout_lexer = &std::cout;
if(arg == "--print-parsetree") cout_parser = &std::cout;
if(arg == "--print-ast") cout_semantic = &std::cout;
if(arg == "--print-interpreter") cout_interpreter = &std::cout;
}
// const std::string fileName = (argc > 1) ? argv[argc - 1] : "../Demo.bcc";
const std::string fileName = argv[argc - 1];
std::string code = util::readFile(fileName) + "\n\n";
if(code.size() == 0) {
std::cout << "Code konnte nicht eingelesen werden\n";
}
try {
run(code, *cout_lexer, *cout_parser, *cout_semantic, *cout_interpreter);
std::cout << "\nProgram terminated successfully\n";
// std::cout << "Number of remaining Value objects: " << Value::numInstances() << "\n";
} catch(const std::exception& e) {
std::cout << "Exception thrown: " << e.what() << "\n";
}
return 0;
}