-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (94 loc) · 2.37 KB
/
main.cpp
File metadata and controls
105 lines (94 loc) · 2.37 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
#include "Compiler.hpp"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream fin;
ofstream ftable;
ofstream fcode;
ofstream fas;
ofstream fresult;
int main()
{
string fname;
int err_cnt; // count for errors
string input_option;
char ch;
cout << ("Input L25 file path:") << endl;
cin >> fname;
fin.open(fname);
if (!fin)
{
cerr << "The input file cannot be opened: " << fname << endl;
exit(1);
}
// 检查文件是否为空
fin.seekg(0, ios::end);
if (fin.tellg() == 0)
{
cout << "The input file is empty!" << endl;
fin.close();
exit(1);
}
fin.seekg(0, ios::beg);
ftable.open("ftable.txt");
if (!ftable)
{
cerr << "The table file cannot be opened!" << endl;
exit(1);
}
cout << "List object codes?(Y/N)" << endl;
cin >> input_option;
listswitch = (input_option == "y" || input_option == "Y");
cout << listswitch << endl;
cout << "List symbol table?(Y/N)" << endl;
cin >> input_option;
tableswitch = (input_option == "y" || input_option == "Y");
cout << tableswitch << endl; // 设置全局输出文件流
fas.open("foutput.txt");
if (!fas)
{
cerr << "Cannot open output file for general output!" << endl;
exit(1);
}
// 设置代码文件流
fcode.open("fcode.txt");
if (!fcode)
{
cerr << "Cannot open output file for P-code instructions!" << endl;
exit(1);
}
// 设置结果文件流
fresult.open("fresult.txt");
if (!fresult)
{
cerr << "Cannot open output file for execution results!" << endl;
exit(1);
}
// 创建编译器并编译
Compiler L25compiler(fin);
bool result = L25compiler.compile();
if (result)
{
cout << "Compilation successful!" << endl;
// 询问是否执行程序
cout << "Execute the program?(Y/N)" << endl;
cin >> input_option;
bool executeswitch = (input_option == "y" || input_option == "Y");
if (executeswitch)
{
cout << "\n=== Program Output ===" << endl;
L25compiler.execute();
}
}
else
{
cout << "Compilation failed!" << endl;
}
fin.close();
fas.close();
fcode.close();
fresult.close();
ftable.close();
return 0;
}