-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackSyntaxAnalyzer.cpp
More file actions
44 lines (36 loc) · 1.35 KB
/
HackSyntaxAnalyzer.cpp
File metadata and controls
44 lines (36 loc) · 1.35 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
#include "HackSyntaxAnalyzer.h"
#include "OutputEngine.h"
#include "XMLOutputEngine.h"
#include "compilation_engine.h"
#include "error.h"
#include <filesystem>
HackSyntaxAnalyzer::HackSyntaxAnalyzer (std::filesystem::path inp)
: inp (inp), engine (hack_map) {
if (!std::filesystem::exists (inp))
throw Error ("Invalid file path");
}
void HackSyntaxAnalyzer::analyze () {
if (!std::filesystem::is_directory (inp)) {
if (inp.extension () != ".jack")
throw Error ("Invalid file extension");
analyze_file (inp);
return;
}
for (auto& entry : std::filesystem::recursive_directory_iterator (inp)) {
if (entry.path ().extension () == ".jack")
analyze_file (entry.path ());
}
}
std::string HackSyntaxAnalyzer::generate_out_path (std::filesystem::path inp) {
return inp.replace_extension ("xml");
}
void HackSyntaxAnalyzer::analyze_file (std::filesystem::path inp) {
std::filesystem::path out (generate_out_path (inp));
std::cout << "writing to " << out.string () << std::endl;
std::shared_ptr<Tokenizer> tokenizer = std::make_shared<Tokenizer> (inp, hack_map);
std::shared_ptr<XMLOutputEngine> output_engine =
std::make_shared<XMLOutputEngine> (out, hack_map, 1);
engine.set_output_engine (output_engine);
engine.set_tokenizer (tokenizer);
engine.compile ();
}