-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrepl.cpp
More file actions
233 lines (199 loc) · 6.63 KB
/
Copy pathrepl.cpp
File metadata and controls
233 lines (199 loc) · 6.63 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <cstdio>
#include <cstdlib>
#include "linenoise.h"
#include <cxxopts.hpp>
#include <filesystem>
#include <iostream>
#include <optional>
#include <string>
#include "executable/Program.hpp"
#include "executable/llvm/LLVMGenerator.hpp"
#include "interpreter/Interpreter.hpp"
#include "parser/Parser.hpp"
#include "runtime/modules/Modules.hpp"
#include "vm/VM.hpp"
using namespace py;
namespace repl {
std::optional<std::string> getline(const std::string &prompt)
{
if (auto line = linenoise(prompt.c_str())) {
linenoiseHistoryAdd(line);
linenoiseHistorySave("history.txt");
return std::string{ line };
} else {
return {};
}
}
// FIXME
// class InteractivePython
// {
// public:
// InteractivePython() {}
// PyObject *interpret_statement(std::string statement)
// {
// auto lexer = Lexer::create(statement, std::filesystem::current_path());
// parser::Parser parser{ lexer };
// parser.parse();
// if (!m_main_module) {
// m_main_module = parser.module();
// } else {
// for (const auto &node : parser.module()->body()) { m_main_module->emplace(node); }
// }
// auto bytecode =
// codegen::BytecodeGenerator::compile(m_main_module, compiler::OptimizationLevel::None);
// auto &vm = VirtualMachine::the();
// return vm.execute_statement(bytecode);
// }
// private:
// std::shared_ptr<ast::Module> m_main_module{ nullptr };
// };
}// namespace repl
namespace {
int run_and_execute_script(size_t argc,
char **argv,
bool print_bytecode,
bool print_tokens,
bool use_llvm,
bool print_ast,
uint64_t gc_frequency)
{
size_t arg_idx{ 1 };
const char *filename = argv[arg_idx];
std::vector<std::string> argv_vector;
argv_vector.reserve(argc - 1);
while (arg_idx < argc) { argv_vector.emplace_back(argv[arg_idx++]); }
auto &vm = VirtualMachine::the();
vm.heap().garbage_collector().set_frequency(gc_frequency);
initialize_types();
auto lexer = Lexer::create(std::filesystem::absolute(filename));
if (print_tokens) {
auto l = Lexer::create(std::filesystem::absolute(filename));
std::cout << "Generated tokens: \n";
size_t idx = 0;
while (auto token = l.peek_token(idx)) {
std::cout << *token << '\n';
idx++;
}
std::cout << std::endl;
}
parser::Parser p{ lexer };
p.parse();
if (print_ast) {
const auto lvl = spdlog::get_level();
spdlog::set_level(spdlog::level::debug);
p.module()->print_node("");
spdlog::set_level(lvl);
}
std::shared_ptr<Program> bytecode = compiler::compile(
p.module(), argv_vector, compiler::Backend::MLIR, compiler::OptimizationLevel::None);
if (print_bytecode) {
std::cout << "Generated bytecode: \n";
std::cout << bytecode->to_string() << '\n';
std::cout << std::endl;
}
if (use_llvm) {
#ifdef USE_LLVM
auto llvm_code = codegen::LLVMGenerator::compile(
p.module(), argv_vector, compiler::OptimizationLevel::None);
if (!llvm_code) {
std::cout << "Could not compile to LLVM IR\n";
} else {
std::cout << "------------------------------------------------\n";
std::cout << "Generated LLVM IR (experimental feature): \n";
std::cout << llvm_code->to_string() << '\n';
std::cout << "------------------------------------------------\n";
static_cast<BytecodeProgram *>(bytecode.get())->add_backend(llvm_code);
}
#else
std::cout << "Python interpreter was compiled without LLVM\n";
#endif
}
return vm.execute(bytecode);
}
int run_and_execute_module_as_script(size_t argc,
char **argv,
const std::string &,// module_name
bool,// print_bytecode
bool,// print_ast
bool// print_tokens
)
{
size_t arg_idx{ 1 };
std::vector<std::string> argv_vector;
argv_vector.reserve(argc - 1);
while (arg_idx < argc) { argv_vector.emplace_back(argv[arg_idx++]); }
TODO();
return 0;
}
}// namespace
int main(int argc, char **argv)
{
cxxopts::Options options("python", "The C++ Python interpreter");
// clang-format off
options.add_options()
("f,filename", "Script path", cxxopts::value<std::string>())
("m", "run library module as a script", cxxopts::value<std::string>())
("b,bytecode", "Print the script's bytecode to stdout", cxxopts::value<bool>()->default_value("false"))
("a,ast", "Print the script's tokens to stdout", cxxopts::value<bool>()->default_value("false"))
("t,tokenize", "Print the script's tokens to stdout", cxxopts::value<bool>()->default_value("false"))
("d,debug", "Enable debug logging", cxxopts::value<bool>()->default_value("false"))
("trace", "Enable trace logging", cxxopts::value<bool>()->default_value("false"))
("use-llvm", "Enable trace logging", cxxopts::value<bool>()->default_value("false"))
("gc-frequency",
"Frequency at which the garbage collector is run. Unit is number of allocations",
cxxopts::value<uint64_t>()->default_value("10000"))
("h,help", "Print usage");
options
.positional_help("[optional args]")
.show_positional_help();
// clang-format on
options.parse_positional({ "filename" });
auto result = options.parse(argc, argv);
if (result.count("help")) {
std::cout << options.help() << std::endl;
return EXIT_SUCCESS;
}
const bool debug = result["debug"].as<bool>();
const bool trace = result["trace"].as<bool>();
if (debug) { spdlog::set_level(spdlog::level::debug); }
if (trace) { spdlog::set_level(spdlog::level::trace); }
if (result.count("filename")) {
return run_and_execute_script(argc,
argv,
result["bytecode"].as<bool>(),
result["tokenize"].as<bool>(),
result["use-llvm"].as<bool>(),
result["ast"].as<bool>(),
result["gc-frequency"].as<uint64_t>());
}
if (result.count("m")) {
return run_and_execute_module_as_script(argc,
argv,
result["m"].as<std::string>(),
result["bytecode"].as<bool>(),
result["ast"].as<bool>(),
result["tokenize"].as<bool>());
}
TODO();
// FIXME
// linenoiseHistoryLoad("history.txt");
// repl::InteractivePython interactive_interpreter;
// static constexpr std::string_view major_version = "3";
// static constexpr std::string_view minor_version = "10";
// static constexpr std::string_view build_version = "0a";
// static constexpr std::string_view compiler = "Clang 11.0.0";
// static constexpr std::string_view platform = "Linux";
// std::cout << fmt::format("Python {}.{}.{}\n", major_version, minor_version, build_version);
// std::cout << fmt::format("[{}] :: {}\n", compiler, platform);
// std::cout << "Type \"help\", \"copyright\", \"credits\" or \"license\" for more
// information."; std::cout << std::endl;
// while (auto line = repl::getline(">>> ")) {
// (*line) += "\n";
// auto *result = interactive_interpreter.interpret_statement(*line);
// if (result != py_none()) {
// std::cout << result->repr_impl(VirtualMachine::the().interpreter())->to_string()
// << '\n';
// }
// }
return EXIT_SUCCESS;
}