-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathmulti_interpreter.cpp
More file actions
93 lines (83 loc) · 2.77 KB
/
Copy pathmulti_interpreter.cpp
File metadata and controls
93 lines (83 loc) · 2.77 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
/************************************************************************************
* Copyright (c) 2025, xeus-cpp contributors *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/
#include "multi_interpreter.hpp"
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include "CppInterOp/CppInterOp.h"
static std::vector<std::string> split(const std::string& input)
{
std::istringstream buffer(input);
std::vector<std::string> ret(
(std::istream_iterator<std::string>(buffer)),
std::istream_iterator<std::string>()
);
return ret;
}
static constexpr size_t len(std::string_view n)
{
return n.size();
}
namespace xcpp
{
void multi_interpreter::operator()(const std::string& line, const std::string& cell)
{
auto Args0 = split(line.substr(len("subinterp")));
Cpp::TInterp_t OldI = Cpp::GetInterpreter(); // we need to restore the old interpreter
Cpp::TInterp_t I = nullptr;
std::string name;
if (Args0[0] == "--use")
{
I = interpreters[Args0[1]];
}
else
{
auto named = std::find(Args0.begin(), Args0.end(), "--name");
if (named != Args0.end())
{
name = *(named + 1);
}
}
std::vector<const char*> Args(I ? 0 : Args0.size());
if (!I)
{
for (auto start = Args0.begin(), end = Args0.end(); start != end; start++)
{
if (*start == "--name")
{
start++;
continue;
}
Args.push_back((*start).c_str());
}
}
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
Cpp::BeginStdStreamCapture(Cpp::kStdOut);
if (!I)
{
I = Cpp::CreateInterpreter(Args); // TODO: error handling
}
if (I)
{
Cpp::Declare(cell.c_str(), false, I);
}
std::cout << Cpp::EndStdStreamCapture();
std::cerr << Cpp::EndStdStreamCapture();
if (!name.empty())
{
interpreters[name] = I; // TODO: check if this is redefinition
Cpp::ActivateInterpreter(OldI); // restoring old interpreter
}
else
{
Cpp::DeleteInterpreter(I);
}
}
} // namespace xcpp