-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
199 lines (195 loc) · 6.08 KB
/
main.cpp
File metadata and controls
199 lines (195 loc) · 6.08 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <cstdlib>
#include <BerryMath.h>
#include <vector>
#include <fstream>
#define DEBUG 0
using std::cout;
using std::endl;
using std::cin;
using std::fstream;
using std::vector;
//void* operator new(size_t n) {
// auto o = malloc(n);
// std::cout << "n: " << o << std::endl;
// return o;
//}
//void operator delete(void* o) {
// std::cout << "d: " << o << std::endl;
// free(o);
//}
#if DEBUG
void debug() {
BM::Lexer lexer("print 1;");
auto token = lexer.get();
while (token.t != BM::Lexer::END_TOKEN) {
std::cout << token.s << std::endl;
token = lexer.get();
}
}
#endif
void terminal() {
cout << "BerryMath Terminal" << endl;
cout << BMVersion << endl;
cout << " \033[32m_____\033[0m" << endl;
cout << " \033[32m/\\\033[0m" << endl;
cout << " \033[32m/ |\033[0m" << endl;
cout << " \033[41m \033[0m \033[45m \033[0m" << endl;
cout << " \033[41m \033[45m \033[0m" << endl;
cout << " \033[41m \033[35mB\033[0m\033[41m \033[45m \033[31mM\033[0m\033[45m \033[0m" << endl;
cout << " \033[41m \033[45m \033[0m" << endl;
cout << " \033[41m \033[45m \033[0m" << endl;
BM::Interpreter ip("", "terminal");
// vector<BM::Object*> gPool;
while (true) {
string tmp;
cout << ">> ";
getline(cin, tmp);
if (tmp == ".exit") break;
UL BBC = 0;
UL MBC = 0;
UL SBC = 0;
bool trans = false;
bool inString = false;
for (UL i = 0; i < tmp.length(); i++) {
if ((tmp[i] == '"' || tmp[i] == '\'') && !trans) {
inString = !inString;
}
if (tmp[i] == '\\') {
trans = !trans;
} else {
trans = false;
}
if (!inString) {
switch (tmp[i]) {
case '{': BBC++;break;
case '}': BBC--;break;
case '[': MBC++;break;
case ']': MBC--;break;
case '(': SBC++;break;
case ')': SBC--;break;
}
}
}
if (BBC || MBC || SBC) {// 未输入完
string s(tmp);
while (true) {
cout << ">> ";
getline(cin, tmp);
BBC = 0;
MBC = 0;
SBC = 0;
for (UL i = 0; i < tmp.length(); i++) {
switch (tmp[i]) {
case '{': BBC++;break;
case '}': BBC--;break;
case '[': MBC++;break;
case ']': MBC--;break;
case '(': SBC++;break;
case ')': SBC--;break;
}
}
if (!(BBC || MBC || SBC)) break;
}
ip.open(s, "terminal");
auto e = ip.run();
auto ret = e->get(PASS_RETURN);
if (ret) cout << (*ret) << endl;
// gPool.push_back(e);
} else {
ip.open(tmp, "terminal");
auto e = ip.run();
auto ret = e->get(PASS_RETURN);
if (ret) cout << (*ret) << endl;
// gPool.push_back(e);
}
}
ip.clear();
// for (auto i = gPool.begin(); i != gPool.end(); i++) {
// delete *i;
// }
cout << "bye" << endl;
exit(0);
}
int main(int argc, char* argv[]) {
#if !DEBUG
if (argc == 1) terminal();
string opt(argv[1]);
if (opt == "--version" || opt == "-v") {// 版本
cout << BMVersion << endl;
} else if (opt == "--help" || opt == "-h") {// 帮助
cout << "See https://github.com/BerryMathDevelopmentTeam/BerryMath" << endl;
} else if (opt == "--bc" || opt == "-b") {// 运行字节码
if (argc < 3) {
std::cerr << "SystemError: Filename not found at <null:\033[33msystem\033[0m>:0" << endl;
exit(1);
}
fstream file;
string filename(argv[2]);
file.open(filename);
if (!file) {
std::cerr << "SystemError: Cannot open bytecode file " << filename << " at <" << filename << ":\033[33msystem\033[0m>:0" << endl;
file.close();
exit(1);
}
string line;
string bytecode;
while (getline(file, line)) {
bytecode += line + "\n";
}
BM::VM vm(bytecode);
vm.run();
file.close();
} else if (opt == "--compile" || opt == "-c") {// 运行字节码
if (argc < 3) {
std::cerr << "SystemError: Filename not found at <null:\033[33msystem\033[0m>:0" << endl;
exit(1);
}
fstream file;
string filename(argv[2]);
file.open(filename);
if (!file) {
std::cerr << "SystemError: Cannot open source file " << filename << " at <" << filename << ":\033[33msystem\033[0m>:0" << endl;
file.close();
exit(1);
}
string line;
string source;
while (getline(file, line)) {
source += line + "\n";
}
file.close();
BM::Compiler compiler(source);
string bytecode;
if (!compiler.compile(bytecode)) {
std::cerr << "[COMPILE FAILED]" << std::endl;
}
string outerName(filename);
outerName.replace(outerName.find(".bm"), 3, ".bmc");
file.open(outerName);
file << bytecode;
file.close();
} else {// 运行源码
fstream file;
string& filename = opt;
file.open(filename);
if (!file) {
std::cerr << "SystemError: Cannot open src file " << filename << " at <" << filename << ":\033[33msystem\033[0m>:0" << endl;
file.close();
exit(1);
}
string line;
string script;
while (getline(file, line)) {
script += line + "\n";
}
file.close();
BM::Interpreter ip(script, filename);
delete ip.run();
}
BM::Dylib::clear();
#else
debug();
#endif
return 0;
}