Skip to content

Commit aef99bd

Browse files
committed
First functional kernel based on clang-repl
1 parent 11a0b9a commit aef99bd

37 files changed

+2357
-293
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
# Jupyter
3636
.ipynb_checkpoints/
3737

38+
# Python
39+
__pycache__/
40+
3841
# Generated kernelspec
3942
share/jupyter/kernels/xcpp/kernel.json
4043

CMakeLists.txt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ OPTION(XEUS_CPP_USE_SHARED_XEUS_CPP "Link xcpp with the xeus shared library (in
5252

5353
OPTION(XEUS_CPP_EMSCRIPTEN_WASM_BUILD "Build for wasm with emscripten" OFF)
5454

55-
5655
if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
5756
add_compile_definitions(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
5857
message("Build with emscripten")
@@ -106,6 +105,9 @@ if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
106105
set(EMSCRIPTEN_FEATURES "${EMSCRIPTEN_FEATURES} -s \"EXTRA_EXPORTED_RUNTIME_METHODS=[ENV']\"")
107106
endif()
108107

108+
find_package(Clang REQUIRED)
109+
find_package(argparse REQUIRED)
110+
find_package(pugixml REQUIRED)
109111

110112
# Source files
111113
# ============
@@ -116,14 +118,17 @@ set(XEUS_CPP_HEADERS
116118
)
117119

118120
set(XEUS_CPP_SRC
121+
src/xholder.cpp
122+
src/xinput.cpp
119123
src/xinterpreter.cpp
124+
src/xoptions.cpp
125+
src/xparser.cpp
120126
)
121127

122128
set(XEUS_CPP_MAIN_SRC
123129
src/main.cpp
124130
)
125131

126-
127132
# Targets and link - Macros
128133
# =========================
129134

@@ -170,7 +175,7 @@ macro(xeus_cpp_set_common_options target_name)
170175
)
171176
endmacro()
172177

173-
# Common macro kernels (xcpp )
178+
# Common macro kernels (xcpp)
174179
macro(xeus_cpp_set_kernel_options target_name)
175180
if (XEUS_CPP_USE_SHARED_XEUS_CPP)
176181
target_link_libraries(${target_name} PRIVATE xeus-cpp)
@@ -221,7 +226,7 @@ macro(xeus_cpp_create_target target_name linkage output_name)
221226
set(XEUS_CPP_XEUS_TARGET xeus-static)
222227
endif ()
223228

224-
target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} xtl)
229+
target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangInterpreter pugixml argparse::argparse xtl)
225230
if (WIN32 OR CYGWIN)
226231
#
227232
elseif (APPLE)
@@ -234,8 +239,20 @@ macro(xeus_cpp_create_target target_name linkage output_name)
234239

235240
endmacro()
236241

242+
# xeus-cpp-headers
243+
# ================
244+
245+
set(XCPP_HEADERS
246+
include/xcpp/xmime.hpp
247+
include/xcpp/xdisplay.hpp
248+
)
249+
add_library(xeus-cpp-headers INTERFACE)
250+
set_target_properties(xeus-cpp-headers PROPERTIES PUBLIC_HEADER "${XCPP_HEADERS}")
251+
252+
install(TARGETS xeus-cpp-headers PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xcpp)
253+
237254
# xeus-cpp
238-
# ===========
255+
# ========
239256

240257
set(XEUS_CPP_TARGETS "")
241258

@@ -257,7 +274,8 @@ if (XEUS_CPP_BUILD_STATIC)
257274
endif ()
258275

259276
# xcpp
260-
# =======
277+
# ====
278+
261279
if (XEUS_CPP_BUILD_EXECUTABLE)
262280
find_package(xeus-zmq 1.0.2 REQUIRED)
263281
add_executable(xcpp ${XEUS_CPP_MAIN_SRC})
@@ -277,7 +295,6 @@ if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
277295
xeus_wasm_link_options(xcpp_wasm "web,worker")
278296
endif()
279297

280-
281298
# Installation
282299
# ============
283300
include(CMakePackageConfigHelpers)

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mamba install`xeus-cpp` notebook -c conda-forge
3838
Or you can install it from the sources, you will first need to install dependencies
3939

4040
```bash
41-
mamba install cmake cxx-compiler xeus-zmq nlohmann_json cppzmq xtl jupyterlab -c conda-forge
41+
mamba install cmake cxx-compiler xeus-zmq nlohmann_json cppzmq xtl jupyterlab clangdev cpp-argparse -c conda-forge
4242
```
4343

4444
Then you can compile the sources (replace `$CONDA_PREFIX` with a custom installation
@@ -72,6 +72,8 @@ http://xeus-cpp.readthedocs.io
7272
- [xtl](https://github.com/xtensor-stack/xtl)
7373
- [nlohmann_json](https://github.com/nlohmann/json)
7474
- [cppzmq](https://github.com/zeromq/cppzmq)
75+
- [clang](https://github.com/llvm/llvm-project/)
76+
- [argparse](https://github.com/p-ranav/argparse)
7577

7678
## Contributing
7779

environment-dev.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ dependencies:
1111
- nlohmann_json
1212
- cppzmq
1313
- xtl
14+
- clangdev >=16
15+
- pugixml
16+
- cpp-argparse
17+
- zlib
1418
# Test dependencies
1519
- pytest
1620
- jupyter_kernel_test>=0.4.3

include/xcpp/xdisplay.hpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/************************************************************************************
2+
* Copyright (c) 2023, xeus-cpp contributors *
3+
* Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
************************************************************************************/
9+
10+
#ifndef XCPP_DISPLAY_HPP
11+
#define XCPP_DISPLAY_HPP
12+
13+
#include <nlohmann/json.hpp>
14+
15+
#include "xcpp/xmime.hpp"
16+
17+
namespace nl = nlohmann;
18+
19+
namespace xcpp
20+
{
21+
// Adding a dummy non-template display overload as a workaround to
22+
// Issue https://reviews.llvm.org/D147319
23+
class dummy_display
24+
{
25+
};
26+
27+
void display(dummy_display i)
28+
{
29+
}
30+
31+
template <class T>
32+
void display(const T& t)
33+
{
34+
using ::xcpp::mime_bundle_repr;
35+
xeus::get_interpreter().display_data(mime_bundle_repr(t), nl::json::object(), nl::json::object());
36+
}
37+
38+
template <class T>
39+
void display(const T& t, xeus::xguid id, bool update = false)
40+
{
41+
nl::json transient;
42+
transient["display_id"] = id;
43+
using ::xcpp::mime_bundle_repr;
44+
if (update)
45+
{
46+
xeus::get_interpreter()
47+
.update_display_data(mime_bundle_repr(t), nl::json::object(), std::move(transient));
48+
}
49+
else
50+
{
51+
xeus::get_interpreter().display_data(mime_bundle_repr(t), nl::json::object(), std::move(transient));
52+
}
53+
}
54+
55+
inline void clear_output(bool wait = false)
56+
{
57+
xeus::get_interpreter().clear_output(wait);
58+
}
59+
}
60+
61+
#endif

include/xcpp/xmime.hpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/************************************************************************************
2+
* Copyright (c) 2023, xeus-cpp contributors *
3+
* Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
************************************************************************************/
9+
10+
#ifndef XCPP_MIME_HPP
11+
#define XCPP_MIME_HPP
12+
13+
#include <sstream>
14+
15+
#include <nlohmann/json.hpp>
16+
17+
namespace nl = nlohmann;
18+
19+
namespace xcpp
20+
{
21+
namespace detail
22+
{
23+
// Generic mime_bundle_repr() implementation
24+
// via std::ostringstream.
25+
template <class T>
26+
nl::json mime_bundle_repr_via_sstream(const T& value)
27+
{
28+
auto bundle = nl::json::object();
29+
30+
std::ostringstream oss;
31+
oss << value;
32+
33+
bundle["text/plain"] = oss.str();
34+
return bundle;
35+
}
36+
37+
}
38+
39+
// Default implementation of mime_bundle_repr
40+
template <class T>
41+
nl::json mime_bundle_repr(const T& value)
42+
{
43+
return detail::mime_bundle_repr_via_sstream(&value);
44+
}
45+
}
46+
47+
#endif

include/xeus-cpp/xbuffer.hpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/************************************************************************************
2+
* Copyright (c) 2023, xeus-cpp contributors *
3+
* Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
************************************************************************************/
9+
10+
#ifndef XEUS_CPP_BUFFER_HPP
11+
#define XEUS_CPP_BUFFER_HPP
12+
13+
#include <functional>
14+
#include <memory>
15+
#include <mutex>
16+
#include <streambuf>
17+
#include <string>
18+
19+
namespace xcpp
20+
{
21+
/********************
22+
* output streambuf *
23+
********************/
24+
25+
class xoutput_buffer : public std::streambuf
26+
{
27+
public:
28+
29+
using base_type = std::streambuf;
30+
using callback_type = std::function<void(const std::string&)>;
31+
using traits_type = base_type::traits_type;
32+
33+
xoutput_buffer(callback_type callback)
34+
: m_callback(std::move(callback))
35+
{
36+
}
37+
38+
protected:
39+
40+
traits_type::int_type overflow(traits_type::int_type c) override
41+
{
42+
std::lock_guard<std::mutex> lock(m_mutex);
43+
// Called for each output character.
44+
if (!traits_type::eq_int_type(c, traits_type::eof()))
45+
{
46+
m_output.push_back(traits_type::to_char_type(c));
47+
}
48+
return c;
49+
}
50+
51+
std::streamsize xsputn(const char* s, std::streamsize count) override
52+
{
53+
std::lock_guard<std::mutex> lock(m_mutex);
54+
// Called for a string of characters.
55+
m_output.append(s, count);
56+
return count;
57+
}
58+
59+
traits_type::int_type sync() override
60+
{
61+
std::lock_guard<std::mutex> lock(m_mutex);
62+
// Called in case of flush.
63+
if (!m_output.empty())
64+
{
65+
m_callback(m_output);
66+
m_output.clear();
67+
}
68+
return 0;
69+
}
70+
71+
callback_type m_callback;
72+
std::string m_output;
73+
std::mutex m_mutex;
74+
};
75+
76+
/*******************
77+
* input streambuf *
78+
*******************/
79+
80+
class xinput_buffer : public std::streambuf
81+
{
82+
public:
83+
84+
using base_type = std::streambuf;
85+
using callback_type = std::function<void(std::string&)>;
86+
using traits_type = base_type::traits_type;
87+
88+
xinput_buffer(callback_type callback)
89+
: m_callback(std::move(callback))
90+
, m_value()
91+
{
92+
char* data = const_cast<char*>(m_value.data());
93+
this->setg(data, data, data);
94+
}
95+
96+
protected:
97+
98+
traits_type::int_type underflow() override
99+
{
100+
m_callback(m_value);
101+
// Terminate the string to trigger parsing.
102+
m_value += '\n';
103+
char* data = const_cast<char*>(m_value.data());
104+
setg(data, data, data + m_value.size());
105+
return traits_type::to_int_type(*gptr());
106+
}
107+
108+
callback_type m_callback;
109+
std::string m_value;
110+
};
111+
112+
/*************************
113+
* output null streambuf *
114+
*************************/
115+
116+
class xnull : public std::streambuf
117+
{
118+
using base_type = std::streambuf;
119+
using traits_type = base_type::traits_type;
120+
121+
traits_type::int_type overflow(traits_type::int_type c) override
122+
{
123+
return c;
124+
}
125+
};
126+
}
127+
128+
#endif

include/xeus-cpp/xeus_cpp_config.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/***************************************************************************
2-
* Copyright (c) 2023, 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-
****************************************************************************/
1+
/************************************************************************************
2+
* Copyright (c) 2023, 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+
************************************************************************************/
88

99
#ifndef XEUS_CPP_CONFIG_HPP
1010
#define XEUS_CPP_CONFIG_HPP

0 commit comments

Comments
 (0)