-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdriver.cpp
More file actions
81 lines (62 loc) · 2.14 KB
/
Copy pathdriver.cpp
File metadata and controls
81 lines (62 loc) · 2.14 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
#include "Grammar/driver.hh"
#include "PrintVisitor.hpp"
#include "SymbolTreeVisitor.hpp"
#include "MethodCallVisitor.h"
#include "parser.hh"
#include <fstream>
#include <Visitors/MethodCallVisitor.h>
Driver::Driver() :
trace_parsing_(false),
trace_scanning_(false),
scanner_(*this), parser_(scanner_, *this) {}
int Driver::Parse(const std::string& filename) {
filename_ = filename;
location_.initialize(&filename_);
BeginScan();
parser_.set_debug_level(trace_parsing_);
int res = parser_();
EndScan();
return res;
}
void Driver::BeginScan() {
scanner_.set_debug(trace_scanning_);
if (filename_.empty() || filename_ == "-") {
std::cout << "Empty filename" << std::endl;
} else {
stream_.open(filename_);
std::cout << filename_ << std::endl;
scanner_.yyrestart(&stream_);
}
}
void Driver::EndScan() {
stream_.close();
}
void Driver::PrintTree(const std::string& filename) {
ast::PrintVisitor visitor(filename);
visitor.Visit(program_);
}
//void Driver::RunInterpreter(const std::string& filename) {
// ast::Interpreter interpreter(filename);
// interpreter.Visit(program_);
// interpreter.PrintReturnValue();
//}
void Driver::Evaluate(const std::string& filename) {
using namespace ast;
SymbolTreeVisitor symbol_tree_visitor(filename);
symbol_tree_visitor.Visit(program_);
std::cout << "SymbolTreeVisitor finished work" << std::endl
<< "Tree layers (tab = depth in tree, BFS):" << std::endl;
// Layers info:
symbol_tree_visitor.OutputTreeLayers();
// Classes info:
symbol_tree_visitor.OutputClassesInfo();
// For correct method_call_visitor work
symbol_tree_visitor.FillClassStorage();
std::shared_ptr<MethodType> main_method = std::dynamic_pointer_cast<MethodType>(
symbol_tree_visitor.GetScopeLayerTree()->GetRoot()->Get("main"));
MethodCallVisitor method_call_visitor(
symbol_tree_visitor.GetScopeLayerTree()->GetScopeLayer("main"), main_method,
new SimpleObject(new PrimitiveSimpleType(new SimpleType("int")), 0));
method_call_visitor.SetTree(symbol_tree_visitor.GetScopeLayerTree());
method_call_visitor.Visit(main_method->GetMethodDeclaration());
}