-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcls4.cpp
85 lines (74 loc) · 1.98 KB
/
cls4.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
#include "Error.h"
#include "Token.h"
#include "TokenList.h"
#include "Node.h"
#include "SyntaxTree.h"
#include "Type.h"
#include "Object.h"
#include "Interpreter.h"
#include "Argument.h"
#include <fstream>
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
string shape_path(string path){
for(int i = 0; i < path.size(); i++){
if(path[i] == '\\') path[i] = '/';
}
return path;
}
string tolower(string text){
std::transform(text.begin(), text.end(), text.begin(), [](unsigned char c){ return std::tolower(c);});
return text;
}
const string get_current_directory(){
constexpr int BUF_SIZE = 1024;
char buf[BUF_SIZE];
getcwd(buf, BUF_SIZE);
return buf;
}
string get_base_name(string path){
path = shape_path(path);
string current_path = shape_path(get_current_directory());
// 絶対パスの場合に相対パスに変換
if(
path.size() > current_path.size() &&
tolower(path.substr(0, current_path.size())) == tolower(current_path)
){
path = path.substr(current_path.size());
while(path[0] == '/'){
path = path.substr(1);
}
}
return path;
}
int main(int argc, char* argv[]){
Argument argument;
try{
argument = Argument(argc, argv);
}
catch(const ArgumentError& e){
cout << e.what() << endl;
return 1;
}
TokenList tokens;
SyntaxTree tree;
Interpreter interpreter;
try{
tokens = TokenList(argument.filename); // 字句解析
tree.parse(tokens); // 構文解析
if(argument.tree_filename != ""){
ofstream tree_file(argument.tree_filename);
tree_file << tree;
}
interpreter.run(tree); // 実行
}
catch(const Error& e){
const string base_file_name = get_base_name(argument.filename);
if(base_file_name.size() > 0){
cout << base_file_name << ":";
}
cout << e.what() << endl;
}
}