|
| 1 | +/************************************************************************************ |
| 2 | + * Copyright (c) 2025, xeus-cpp contributors * |
| 3 | + * * |
| 4 | + * Distributed under the terms of the BSD 3-Clause License. * |
| 5 | + * * |
| 6 | + * The full license is in the file LICENSE, distributed with this software. * |
| 7 | + ************************************************************************************/ |
| 8 | + |
| 9 | +#include "multi_interpreter.hpp" |
| 10 | + |
| 11 | +#include <iostream> |
| 12 | +#include <iterator> |
| 13 | +#include <sstream> |
| 14 | +#include <string> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include "CppInterOp/CppInterOp.h" |
| 18 | + |
| 19 | +static std::vector<std::string> split(const std::string& input) |
| 20 | +{ |
| 21 | + std::istringstream buffer(input); |
| 22 | + std::vector<std::string> ret( |
| 23 | + (std::istream_iterator<std::string>(buffer)), |
| 24 | + std::istream_iterator<std::string>() |
| 25 | + ); |
| 26 | + return ret; |
| 27 | +} |
| 28 | + |
| 29 | +static constexpr size_t len(std::string_view n) |
| 30 | +{ |
| 31 | + return n.size(); |
| 32 | +} |
| 33 | + |
| 34 | +namespace xcpp |
| 35 | +{ |
| 36 | + void multi_interpreter::operator()(const std::string& line, const std::string& cell) |
| 37 | + { |
| 38 | + auto Args0 = split(line.substr(len("subinterp"))); |
| 39 | + |
| 40 | + Cpp::TInterp_t OldI = Cpp::GetInterpreter(); // we need to restore the old interpreter |
| 41 | + Cpp::TInterp_t I = nullptr; |
| 42 | + std::string name; |
| 43 | + if (Args0[0] == "--use") |
| 44 | + { |
| 45 | + I = interpreters[Args0[1]]; |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + auto named = std::find(Args0.begin(), Args0.end(), "--name"); |
| 50 | + if (named != Args0.end()) |
| 51 | + { |
| 52 | + name = *(named + 1); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + std::vector<const char*> Args(I ? 0 : Args0.size()); |
| 57 | + if (!I) |
| 58 | + { |
| 59 | + for (auto start = Args0.begin(), end = Args0.end(); start != end; start++) |
| 60 | + { |
| 61 | + if (*start == "--name") |
| 62 | + { |
| 63 | + start++; |
| 64 | + continue; |
| 65 | + } |
| 66 | + Args.push_back((*start).c_str()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Cpp::BeginStdStreamCapture(Cpp::kStdErr); |
| 71 | + Cpp::BeginStdStreamCapture(Cpp::kStdOut); |
| 72 | + if (!I) |
| 73 | + { |
| 74 | + I = Cpp::CreateInterpreter(Args); // TODO: error handling |
| 75 | + } |
| 76 | + if (I) |
| 77 | + { |
| 78 | + Cpp::Declare(cell.c_str(), false, I); |
| 79 | + } |
| 80 | + std::cout << Cpp::EndStdStreamCapture(); |
| 81 | + std::cerr << Cpp::EndStdStreamCapture(); |
| 82 | + |
| 83 | + if (!name.empty()) |
| 84 | + { |
| 85 | + interpreters[name] = I; // TODO: check if this is redefinition |
| 86 | + Cpp::ActivateInterpreter(OldI); // restoring old interpreter |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + Cpp::DeleteInterpreter(I); |
| 91 | + } |
| 92 | + } |
| 93 | +} // namespace xcpp |
0 commit comments