Skip to content

Commit

Permalink
First functional kernel based on clang-repl
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainCorlay committed Apr 4, 2023
1 parent 11a0b9a commit aef99bd
Show file tree
Hide file tree
Showing 37 changed files with 2,357 additions and 293 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
# Jupyter
.ipynb_checkpoints/

# Python
__pycache__/

# Generated kernelspec
share/jupyter/kernels/xcpp/kernel.json

Expand Down
31 changes: 24 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ OPTION(XEUS_CPP_USE_SHARED_XEUS_CPP "Link xcpp with the xeus shared library (in

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


if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
add_compile_definitions(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
message("Build with emscripten")
Expand Down Expand Up @@ -106,6 +105,9 @@ if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
set(EMSCRIPTEN_FEATURES "${EMSCRIPTEN_FEATURES} -s \"EXTRA_EXPORTED_RUNTIME_METHODS=[ENV']\"")
endif()

find_package(Clang REQUIRED)
find_package(argparse REQUIRED)
find_package(pugixml REQUIRED)

# Source files
# ============
Expand All @@ -116,14 +118,17 @@ set(XEUS_CPP_HEADERS
)

set(XEUS_CPP_SRC
src/xholder.cpp
src/xinput.cpp
src/xinterpreter.cpp
src/xoptions.cpp
src/xparser.cpp
)

set(XEUS_CPP_MAIN_SRC
src/main.cpp
)


# Targets and link - Macros
# =========================

Expand Down Expand Up @@ -170,7 +175,7 @@ macro(xeus_cpp_set_common_options target_name)
)
endmacro()

# Common macro kernels (xcpp )
# Common macro kernels (xcpp)
macro(xeus_cpp_set_kernel_options target_name)
if (XEUS_CPP_USE_SHARED_XEUS_CPP)
target_link_libraries(${target_name} PRIVATE xeus-cpp)
Expand Down Expand Up @@ -221,7 +226,7 @@ macro(xeus_cpp_create_target target_name linkage output_name)
set(XEUS_CPP_XEUS_TARGET xeus-static)
endif ()

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

endmacro()

# xeus-cpp-headers
# ================

set(XCPP_HEADERS
include/xcpp/xmime.hpp
include/xcpp/xdisplay.hpp
)
add_library(xeus-cpp-headers INTERFACE)
set_target_properties(xeus-cpp-headers PROPERTIES PUBLIC_HEADER "${XCPP_HEADERS}")

install(TARGETS xeus-cpp-headers PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xcpp)

# xeus-cpp
# ===========
# ========

set(XEUS_CPP_TARGETS "")

Expand All @@ -257,7 +274,8 @@ if (XEUS_CPP_BUILD_STATIC)
endif ()

# xcpp
# =======
# ====

if (XEUS_CPP_BUILD_EXECUTABLE)
find_package(xeus-zmq 1.0.2 REQUIRED)
add_executable(xcpp ${XEUS_CPP_MAIN_SRC})
Expand All @@ -277,7 +295,6 @@ if(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
xeus_wasm_link_options(xcpp_wasm "web,worker")
endif()


# Installation
# ============
include(CMakePackageConfigHelpers)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mamba install`xeus-cpp` notebook -c conda-forge
Or you can install it from the sources, you will first need to install dependencies

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

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

## Contributing

Expand Down
4 changes: 4 additions & 0 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ dependencies:
- nlohmann_json
- cppzmq
- xtl
- clangdev >=16
- pugixml
- cpp-argparse
- zlib
# Test dependencies
- pytest
- jupyter_kernel_test>=0.4.3
Expand Down
61 changes: 61 additions & 0 deletions include/xcpp/xdisplay.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/************************************************************************************
* Copyright (c) 2023, xeus-cpp contributors *
* Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/

#ifndef XCPP_DISPLAY_HPP
#define XCPP_DISPLAY_HPP

#include <nlohmann/json.hpp>

#include "xcpp/xmime.hpp"

namespace nl = nlohmann;

namespace xcpp
{
// Adding a dummy non-template display overload as a workaround to
// Issue https://reviews.llvm.org/D147319
class dummy_display
{
};

void display(dummy_display i)
{
}

template <class T>
void display(const T& t)
{
using ::xcpp::mime_bundle_repr;
xeus::get_interpreter().display_data(mime_bundle_repr(t), nl::json::object(), nl::json::object());
}

template <class T>
void display(const T& t, xeus::xguid id, bool update = false)
{
nl::json transient;
transient["display_id"] = id;
using ::xcpp::mime_bundle_repr;
if (update)
{
xeus::get_interpreter()
.update_display_data(mime_bundle_repr(t), nl::json::object(), std::move(transient));
}
else
{
xeus::get_interpreter().display_data(mime_bundle_repr(t), nl::json::object(), std::move(transient));
}
}

inline void clear_output(bool wait = false)
{
xeus::get_interpreter().clear_output(wait);
}
}

#endif
47 changes: 47 additions & 0 deletions include/xcpp/xmime.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/************************************************************************************
* Copyright (c) 2023, xeus-cpp contributors *
* Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/

#ifndef XCPP_MIME_HPP
#define XCPP_MIME_HPP

#include <sstream>

#include <nlohmann/json.hpp>

namespace nl = nlohmann;

namespace xcpp
{
namespace detail
{
// Generic mime_bundle_repr() implementation
// via std::ostringstream.
template <class T>
nl::json mime_bundle_repr_via_sstream(const T& value)
{
auto bundle = nl::json::object();

std::ostringstream oss;
oss << value;

bundle["text/plain"] = oss.str();
return bundle;
}

}

// Default implementation of mime_bundle_repr
template <class T>
nl::json mime_bundle_repr(const T& value)
{
return detail::mime_bundle_repr_via_sstream(&value);
}
}

#endif
128 changes: 128 additions & 0 deletions include/xeus-cpp/xbuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/************************************************************************************
* Copyright (c) 2023, xeus-cpp contributors *
* Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/

#ifndef XEUS_CPP_BUFFER_HPP
#define XEUS_CPP_BUFFER_HPP

#include <functional>
#include <memory>
#include <mutex>
#include <streambuf>
#include <string>

namespace xcpp
{
/********************
* output streambuf *
********************/

class xoutput_buffer : public std::streambuf
{
public:

using base_type = std::streambuf;
using callback_type = std::function<void(const std::string&)>;
using traits_type = base_type::traits_type;

xoutput_buffer(callback_type callback)
: m_callback(std::move(callback))
{
}

protected:

traits_type::int_type overflow(traits_type::int_type c) override
{
std::lock_guard<std::mutex> lock(m_mutex);
// Called for each output character.
if (!traits_type::eq_int_type(c, traits_type::eof()))
{
m_output.push_back(traits_type::to_char_type(c));
}
return c;
}

std::streamsize xsputn(const char* s, std::streamsize count) override
{
std::lock_guard<std::mutex> lock(m_mutex);
// Called for a string of characters.
m_output.append(s, count);
return count;
}

traits_type::int_type sync() override
{
std::lock_guard<std::mutex> lock(m_mutex);
// Called in case of flush.
if (!m_output.empty())
{
m_callback(m_output);
m_output.clear();
}
return 0;
}

callback_type m_callback;
std::string m_output;
std::mutex m_mutex;
};

/*******************
* input streambuf *
*******************/

class xinput_buffer : public std::streambuf
{
public:

using base_type = std::streambuf;
using callback_type = std::function<void(std::string&)>;
using traits_type = base_type::traits_type;

xinput_buffer(callback_type callback)
: m_callback(std::move(callback))
, m_value()
{
char* data = const_cast<char*>(m_value.data());
this->setg(data, data, data);
}

protected:

traits_type::int_type underflow() override
{
m_callback(m_value);
// Terminate the string to trigger parsing.
m_value += '\n';
char* data = const_cast<char*>(m_value.data());
setg(data, data, data + m_value.size());
return traits_type::to_int_type(*gptr());
}

callback_type m_callback;
std::string m_value;
};

/*************************
* output null streambuf *
*************************/

class xnull : public std::streambuf
{
using base_type = std::streambuf;
using traits_type = base_type::traits_type;

traits_type::int_type overflow(traits_type::int_type c) override
{
return c;
}
};
}

#endif
14 changes: 7 additions & 7 deletions include/xeus-cpp/xeus_cpp_config.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/***************************************************************************
* Copyright (c) 2023, 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.
****************************************************************************/
/************************************************************************************
* Copyright (c) 2023, 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. *
************************************************************************************/

#ifndef XEUS_CPP_CONFIG_HPP
#define XEUS_CPP_CONFIG_HPP
Expand Down
Loading

0 comments on commit aef99bd

Please sign in to comment.