-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
166 lines (150 loc) · 3.95 KB
/
main.cpp
File metadata and controls
166 lines (150 loc) · 3.95 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
#include "blue.h"
using namespace std;
void console() {
cout << "BlueBetter Console (v1.0 for BlueBetter v1.30)" << endl;
cout << "Type 'call exit' to exit and 'call debug' to set up debug mode for further code." << endl << endl;
string curline, temp;
interpreter keep_ip = interpreter(false, false, { {"debug", interpreter::bcaller(
[](string arg, varmap &env, interpreter& interp) -> intValue {
interp.in_debug = !interp.in_debug;
cout << "Debug switch is now " << (interp.in_debug ? "on" : "off") << endl;
return null;
}
)} });
while (true) {
cout << ">>> ";
curline = "";
getline(cin, curline);
curline += '\n';
if (beginWith(curline, "function") || beginWith(curline, "class") || beginWith(curline, "property") || beginWith(curline, "error_handler:")
|| beginWith(curline, "if") || beginWith(curline, "elif") || beginWith(curline, "else:") || beginWith(curline, "while")
|| beginWith(curline, "for")) {
int tabcount = 1;
string tabstr = "\t";
while (true) {
cout << "... " << tabstr;
getline(cin, temp);
if (!temp.length()) {
curline += tabstr + '\n'; // Windows format ?
if (tabcount <= 0) break;
tabcount--;
tabstr.pop_back();
}
else curline += tabstr + temp + '\n';
if (temp.length() && temp[temp.length() - 1] == ':') {
tabcount++;
tabstr.push_back('\t');
}
}
}
intValue ret = keep_ip.preRun(curline, { {"CONSOLE", intValue(1)} });
if (!ret.isNull) ret.output();
cout << endl;
}
}
int main(int argc, char* argv[]) {
standardPreparation();
interpreter ip;
// Test: Input code here:
#pragma region Compiler Test Option
#if _DEBUG
string code = "", file = "";
ip.in_debug = false;
ip.no_lib = false;
if (code.length()) {
specialout();
cout << code;
cout << endl << "-------" << endl;
endout();
}
#else
// DO NOT CHANGE
string code = "", file = "";
ip.in_debug = false;
ip.no_lib = false;
#endif
string version_info = string("BlueBetter Interpreter\nVersion 1.30\nCompiled on ") + __DATE__ + " " + __TIME__;
#pragma endregion
// End
if (argc <= 1 && !file.length() && !code.length()) {
cout << "Usage: " << argv[0] << " filename [options]" << endl;
// Try entering console mode.
console();
return 0;
}
#pragma region Read Options
if (!file.length() && !code.length()) {
file = argv[1];
}
if (file == "--version") {
// = true;
cout << version_info << endl;
return 0;
}
ip.env_name = file;
map<string, intValue> reqs = { {"FILE_NAME", intValue(file)} };
map<string, interpreter::bcaller> callers; // Insert your requirements here
for (int i = 2; i < argc; i++) {
string opt = argv[i];
if (opt == "--debug") {
ip.in_debug = true;
}
else if (beginWith(opt, "--const:")) {
// String values only
vector<string> spl = split(opt, ':', 1);
if (spl.size() < 2) {
curlout();
cout << "Error: Bad format of --const option" << endl;
endout();
return 1;
}
vector<string> key_value = split(spl[1], '=', 1);
if (key_value.size() < 2) {
reqs[key_value[0]] = null;
}
else {
reqs[key_value[0]] = intValue(key_value[1]);
}
}
else if (beginWith(opt, "--include-from:")) {
vector<string> spl = split(opt, ':', 1);
if (spl.size() < 2) {
curlout();
cout << "Error: Bad format of --include-from option" << endl;
endout();
return 1;
}
ip.include_sources.push_back(spl[1]);
}
else if (opt == "--no-lib") {
ip.no_lib = true;
}
}
#pragma endregion
if (!code.length()) {
FILE *f = fopen(file.c_str(), "r");
if (f != NULL) {
while (!feof(f)) {
fgets(buf1, 65536, f);
code += buf1;
}
}
}
if (ip.in_debug) {
begindout();
cout << "Debug mode" << endl;
string command = "";
do {
cout << "-> ";
//cin >> command;
getline(cin, command);
vector<string> spl = split(command, ' ', 1);
if (spl.size() <= 0) continue;
if (spl[0] == "quit") {
exit(0);
}
} while (command != "run");
endout();
}
return ip.preRun(code, reqs, callers).numeric;
}