diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..3d3a7826 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# emacs workspace config +.projectile +# clangd working space (see emacs+lsp) +.cache +# typical cmake build directory (source-tree-nephew) +.build* +# symlink to builddir/compile_commands.json; should be set manually in dev sandbox +compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..31f734f4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,28 @@ +# xo-pyexpression/CMakeLists.txt + +cmake_minimum_required(VERSION 3.10) + +project(xo_pyexpression VERSION 0.1) + +include(GNUInstallDirs) +include(cmake/xo-bootstrap-macros.cmake) + +xo_cxx_toplevel_options3() + +# ---------------------------------------------------------------- +# c++ settings (usually temporary) + +set(PROJECT_CXX_FLAGS "") +add_definitions(${PROJECT_CXX_FLAGS}) + +# ---------------------------------------------------------------- + +add_subdirectory(src/pyexpression) +#add_subdirectory(utest) + +# ---------------------------------------------------------------- +# provide find_package() support + +xo_export_cmake_config(${PROJECT_NAME} ${PROJECT_VERSION} ${PROJECT_NAME}Targets) + +# end CMakeLists.txt diff --git a/cmake/xo-bootstrap-macros.cmake b/cmake/xo-bootstrap-macros.cmake new file mode 100644 index 00000000..aba31169 --- /dev/null +++ b/cmake/xo-bootstrap-macros.cmake @@ -0,0 +1,35 @@ +# ---------------------------------------------------------------- +# for example: +# $ PREFIX=/usr/local # for example +# $ cmake -DCMAKE_MODULE_PATH=prefix -DCMAKE_INSTALL_PREFIX=$PREFIX -B .build +# +# will get +# CMAKE_MODULE_PATH +# from xo-cmake-config --cmake-module-path +# +# and expect .cmake macros in +# CMAKE_MODULE_PATH/xo_macros/xo_cxx.cmake +# ---------------------------------------------------------------- + +find_program(XO_CMAKE_CONFIG_EXECUTABLE NAMES xo-cmake-config REQUIRED) + +if ("${XO_CMAKE_CONFIG_EXECUTABLE}" STREQUAL "XO_CMAKE_CONFIG_EXECUTABLE-NOT_FOUND") + message(FATAL "could not find xo-cmake-config executable") +endif() + +message(STATUS "XO_CMAKE_CONFIG_EXECUTABLE=${XO_CMAKE_CONFIG_EXECUTABLE}") + +if (NOT XO_SUBMODULE_BUILD) + if (("${CMAKE_MODULE_PATH}" STREQUAL "") OR ("${CMAKE_MODULE_PATH}" STREQUAL prefix)) + # default to typical install location for xo-project-macros + execute_process(COMMAND ${XO_CMAKE_CONFIG_EXECUTABLE} --cmake-module-path OUTPUT_VARIABLE CMAKE_MODULE_PATH) + message(STATUS "CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}") + endif() +endif() + +# needs to have been installed somewhere on CMAKE_MODULE_PATH, +# (e.g. from xo-cmake with the same value for CMAKE_INSTALL_PREFIX) +# +include(xo_macros/xo_cxx) + +xo_cxx_bootstrap_message() diff --git a/cmake/xo_pyexpressionConfig.cmake.in b/cmake/xo_pyexpressionConfig.cmake.in new file mode 100644 index 00000000..dc1d5e3d --- /dev/null +++ b/cmake/xo_pyexpressionConfig.cmake.in @@ -0,0 +1,7 @@ +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) +find_dependency(xo_expression) +find_dependency(xo_pyreflect) +include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") +check_required_components("@PROJECT_NAME@") diff --git a/include/REAMDE.md b/include/REAMDE.md new file mode 100644 index 00000000..9db0605f --- /dev/null +++ b/include/REAMDE.md @@ -0,0 +1 @@ +placeholder for future xo-pymatrix header files diff --git a/src/pyexpression/CMakeLists.txt b/src/pyexpression/CMakeLists.txt new file mode 100644 index 00000000..6e81f021 --- /dev/null +++ b/src/pyexpression/CMakeLists.txt @@ -0,0 +1,9 @@ +# xo-pyexpression/src/pyexpression/CMakeLists.txt + +set(SELF_LIB xo_pyexpression) +set(SELF_SRCS pyexpression.cpp) + +xo_pybind11_library(${SELF_LIB} ${PROJECT_NAME}Targets ${SELF_SRCS}) +xo_pybind11_dependency(${SELF_LIB} xo_expression) +xo_pybind11_dependency(${SELF_LIB} xo_pyreflect) +xo_dependency(${SELF_LIB} refcnt) diff --git a/src/pyexpression/pyexpression.cpp b/src/pyexpression/pyexpression.cpp new file mode 100644 index 00000000..a87f6b06 --- /dev/null +++ b/src/pyexpression/pyexpression.cpp @@ -0,0 +1,76 @@ +/* @file pyexpression.cpp */ + +#include "pyexpression.hpp" +#include "xo/pyreflect/pyreflect.hpp" +#include "xo/expression/Expression.hpp" +#include "xo/expression/ConstantInterface.hpp" +#include "xo/expression/Constant.hpp" +#include "xo/pyutil/pyutil.hpp" + +namespace xo { + namespace ast { + using xo::ast::exprtype; + using xo::ast::Expression; + using xo::ast::ConstantInterface; + using xo::ast::Constant; + using xo::reflect::TaggedPtr; + using xo::ref::rp; + namespace py = pybind11; + + PYBIND11_MODULE(XO_PYEXPRESSION_MODULE_NAME(), m) { + // e.g. for xo::reflect::TypeDescr + PYREFLECT_IMPORT_MODULE(); // py::module_::import("pyreflect"); + + m.doc() = "pybind11 plugin for xo-expression"; + + py::enum_(m, "exprtype") + .value("invalid", exprtype::invalid) + .value("constant", exprtype::constant) + .value("primitive", exprtype::primitive) + .value("apply", exprtype::apply) + .value("lambda", exprtype::lambda) + .value("variable", exprtype::variable) + ; + + py::class_>(m, "Expression") + .def("extype", &Expression::extype) + .def("__repr__", &Expression::display_string); + ; + + py::class_>(m, "ConstantInterface") + .def("value_td", &ConstantInterface::value_td, + py::doc("type description for literal value represented by this Constant")) + .def("value", + [](const ConstantInterface & expr) { + TaggedPtr tp = expr.value_tp(); + + auto * p = tp.recover_native(); + + /* TODO: promote to pyobject, so we can do polymorphism */ + if (p) + return *p; + else + return std::numeric_limits::quiet_NaN(); + }, + py::doc("recover constant expression's wrapped value [wip - only works for double]")) + ; + + py::class_, ConstantInterface, rp>>(m, "Constant_double") + ; + + m.def("make_constant", + [](double x) { + return make_constant(x); + }, + py::arg("x"), + py::doc("make_constant(x) creates constant expression holding x [wip - only works for double")) + ; + + } /*pyexpresion*/ + } /*namespace ast*/ +} /*namespace xo*/ + +/* end pyexpression.cpp */ diff --git a/src/pyexpression/pyexpression.hpp.in b/src/pyexpression/pyexpression.hpp.in new file mode 100644 index 00000000..7f4f4660 --- /dev/null +++ b/src/pyexpression/pyexpression.hpp.in @@ -0,0 +1,25 @@ +/* @file pyexpression.hpp + * + * automatically generated from src/xo_xo_pyexpression/xo_pyexpression.hpp.in + * see src/xo_xo_pyexpression/CMakeLists.txt + */ + +/* python requires module name = library name + * example: + * PYBIND11_MODULE(XO_PYEXPRESSION_MODULE_NAME(), m) { ... } + */ +#define XO_PYEXPRESSION_MODULE_NAME() @SELF_LIB@ + +/* example: + * py::module_::import(XO_PYEXPRESSION_MODULE_NAME_STR) + */ +#define XO_PYEXPRESSION_MODULE_NAME_STR "@SELF_LIB@" + +/* example: + * XO_PYEXPRESSION_IMPORT_MODULE() + * replaces + * py::module_::import("xo_pyexpression") + */ +#define XO_PYEXPRESSION_IMPORT_MODULE() py::module_::import("@SELF_LIB@") + +/* end pyexpression.hpp */