Add 'xo-pyexpression/' from commit '734ead4807'
git-subtree-dir: xo-pyexpression git-subtree-mainline:3309a6b8c0git-subtree-split:734ead4807
This commit is contained in:
commit
91048486aa
9 changed files with 402 additions and 0 deletions
8
xo-pyexpression/.gitignore
vendored
Normal file
8
xo-pyexpression/.gitignore
vendored
Normal file
|
|
@ -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
|
||||
28
xo-pyexpression/CMakeLists.txt
Normal file
28
xo-pyexpression/CMakeLists.txt
Normal file
|
|
@ -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
|
||||
94
xo-pyexpression/README.md
Normal file
94
xo-pyexpression/README.md
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
# python bindings for Egad abstract syntax tree library (xo-expression)
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Build + install `xo-cmake` dependency
|
||||
|
||||
- [github/Rconybea/xo-cmake](https://github.com/Rconybea/xo-cmake)
|
||||
|
||||
Installs a few cmake ingredients, along with a build assistant `xo-build` for XO projects such as this one.
|
||||
|
||||
### Build + install other necessary XO dependencies
|
||||
|
||||
```
|
||||
$ xo-build --clone --configure --build --install xo-indentlog
|
||||
$ xo-build --clone --configure --build --install xo-refnct
|
||||
$ xo-build --clone --configure --build --install xo-subsys
|
||||
$ xo-build --clone --configure --build --install xo-reflect
|
||||
$ xo-build --clone --configure --build --install xo-expression
|
||||
$ xo-build --clone --configure --build --install xo-jit
|
||||
$ xo-build --clone --configure --build --install xo-pyutil
|
||||
```
|
||||
|
||||
### copy `xo-pyexpression` repository locally
|
||||
```
|
||||
$ xo-build --clone xo-pyexpression`
|
||||
```
|
||||
|
||||
or equivalently
|
||||
```
|
||||
$ git clone git@github.com:Rconybea/xo-pyexpression.git
|
||||
```
|
||||
|
||||
### build + install xo-pyexpression
|
||||
```
|
||||
$ xo-build --configure --build --install xo-pyexpression
|
||||
```
|
||||
|
||||
or equivalently:
|
||||
```
|
||||
$ PREFIX=/usr/local # or preferred install location
|
||||
$ cmake -DCMAKE_INSTALL_PREFIX=$PREFIX -S xo-pyexpression -B xo-pyyexpression/.build
|
||||
$ cmake --build xo-pyexpression/.build -j
|
||||
$ cmake --install xo-pyexpression/.build
|
||||
```
|
||||
(also see .github/workflows/main.yml)
|
||||
|
||||
## Examples
|
||||
|
||||
Assumes `xo-pyexpression` installed to `~/local2/lib`,
|
||||
i.e. built with `PREFIX=~/local2`.
|
||||
```
|
||||
PYTHONPATH=~/locasl2/lib:$PYTHONPATH python
|
||||
>>> import xo_pyexpression
|
||||
>>> dir(xo_pyexpression)
|
||||
['Expression', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'exprtype']
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
### use from build tree
|
||||
Requires that supporting libraries (e.g. `xo_pyreflect`) appear in PYTHONPATH
|
||||
```
|
||||
$ cd xo-pyexpression/.build/src/pyexpression
|
||||
$ python
|
||||
>>> import xo_pyexpression
|
||||
```
|
||||
|
||||
### build for unit test coverage
|
||||
```
|
||||
$ cd xo-pyexpression
|
||||
$ cmake -DCMAKE_BUILD_TYPE=coverage -DENABLE_TESTING=on -S . -B .build-ccov
|
||||
$ cmake --build .build-ccov -j
|
||||
```
|
||||
|
||||
### LSP (language server) support
|
||||
|
||||
LSP looks for compile commands in the root of the source tree;
|
||||
while Cmake creates them in the root of its build directory.
|
||||
|
||||
```
|
||||
$ cd xo-pyexpression
|
||||
$ ln -s .build/compile_commands.json # supply compile commands to LSP
|
||||
```
|
||||
|
||||
### display cmake variables
|
||||
|
||||
- `-L` list variables
|
||||
- `-A` include 'advanced' variables
|
||||
- `-H` include help text
|
||||
|
||||
```
|
||||
$ cd xo-pyexpression/.build
|
||||
$ cmake -LAH
|
||||
```
|
||||
35
xo-pyexpression/cmake/xo-bootstrap-macros.cmake
Normal file
35
xo-pyexpression/cmake/xo-bootstrap-macros.cmake
Normal file
|
|
@ -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()
|
||||
7
xo-pyexpression/cmake/xo_pyexpressionConfig.cmake.in
Normal file
7
xo-pyexpression/cmake/xo_pyexpressionConfig.cmake.in
Normal file
|
|
@ -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@")
|
||||
1
xo-pyexpression/include/REAMDE.md
Normal file
1
xo-pyexpression/include/REAMDE.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
placeholder for future xo-pymatrix header files
|
||||
10
xo-pyexpression/src/pyexpression/CMakeLists.txt
Normal file
10
xo-pyexpression/src/pyexpression/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# 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)
|
||||
# always use this when xo_pyfoo depends on xo_pybar
|
||||
xo_pybind11_header_dependency(${SELF_LIB} xo_pyreflect)
|
||||
xo_dependency(${SELF_LIB} refcnt)
|
||||
194
xo-pyexpression/src/pyexpression/pyexpression.cpp
Normal file
194
xo-pyexpression/src/pyexpression/pyexpression.cpp
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
/* @file pyexpression.cpp */
|
||||
|
||||
#include "pyexpression.hpp"
|
||||
#include "xo/pyreflect/pyreflect.hpp"
|
||||
#include "xo/expression/Expression.hpp"
|
||||
#include "xo/expression/Apply.hpp"
|
||||
#include "xo/expression/PrimitiveInterface.hpp"
|
||||
#include "xo/expression/Primitive.hpp"
|
||||
#include "xo/expression/ConstantInterface.hpp"
|
||||
#include "xo/expression/Constant.hpp"
|
||||
#include "xo/expression/Variable.hpp"
|
||||
#include "xo/expression/Lambda.hpp"
|
||||
#include "xo/expression/IfExpr.hpp"
|
||||
#include "xo/pyutil/pyutil.hpp"
|
||||
#include <pybind11/functional.h>
|
||||
#include <pybind11/stl.h>
|
||||
#include <cmath>
|
||||
|
||||
namespace xo {
|
||||
namespace ast {
|
||||
using xo::ast::exprtype;
|
||||
using xo::ast::Expression;
|
||||
using xo::ast::make_apply;
|
||||
using xo::ast::PrimitiveInterface;
|
||||
using xo::ast::Primitive;
|
||||
using xo::ast::make_primitive;
|
||||
using xo::ast::ConstantInterface;
|
||||
using xo::ast::Constant;
|
||||
using xo::ast::Variable;
|
||||
using xo::ast::make_var;
|
||||
using xo::ast::Lambda;
|
||||
using xo::ast::make_lambda;
|
||||
using xo::ast::IfExpr;
|
||||
using xo::ast::make_ifexpr;
|
||||
using xo::reflect::TaggedPtr;
|
||||
using xo::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_<exprtype>(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)
|
||||
.value("ifexpr", exprtype::ifexpr)
|
||||
;
|
||||
|
||||
py::class_<Expression,
|
||||
rp<Expression>>(m, "Expression")
|
||||
.def_property_readonly("extype", &Expression::extype)
|
||||
.def_property_readonly("valuetype", &Expression::valuetype)
|
||||
.def("get_free_variables", &Expression::get_free_variables)
|
||||
.def("__repr__", &Expression::display_string);
|
||||
;
|
||||
|
||||
// ----- Constants -----
|
||||
|
||||
py::class_<ConstantInterface,
|
||||
Expression,
|
||||
rp<ConstantInterface>>(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<double>();
|
||||
|
||||
/* TODO: promote to pyobject, so we can do polymorphism */
|
||||
if (p)
|
||||
return *p;
|
||||
else
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
},
|
||||
py::doc("recover constant expression's wrapped value [wip - only works for double]"))
|
||||
;
|
||||
|
||||
py::class_<Constant<double>, ConstantInterface, rp<Constant<double>>>(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"))
|
||||
;
|
||||
|
||||
// ----- Primitives -----
|
||||
|
||||
py::class_<PrimitiveInterface,
|
||||
Expression,
|
||||
rp<PrimitiveInterface>>(m, "PrimitiveInterface")
|
||||
.def("name", &PrimitiveInterface::name,
|
||||
py::doc("name of this primitive function; use this name to invoke the function"))
|
||||
.def("n_arg", &PrimitiveInterface::n_arg,
|
||||
py::doc("number of arguments to this function (not counting return value)"))
|
||||
;
|
||||
|
||||
using int32_t = std::int32_t;
|
||||
|
||||
py::class_<Primitive<int32_t (*)(int32_t, int32_t)>,
|
||||
PrimitiveInterface,
|
||||
rp<Primitive<int32_t (*)(int32_t, int32_t)>>>(m, "Primitive_i32_i32")
|
||||
;
|
||||
|
||||
py::class_<Primitive<double (*)(double)>,
|
||||
PrimitiveInterface,
|
||||
rp<Primitive<double (*)(double)>>>(m, "Primitive_double_double")
|
||||
;
|
||||
using Fn_dbl_dbl_type = double (*)(double);
|
||||
|
||||
m.def("make_sqrt_pm", []() { return make_primitive<double (*)(double)>("sqrt",
|
||||
::sqrt,
|
||||
false /*!explicit_symbol_def*/,
|
||||
llvmintrinsic::fp_sqrt); },
|
||||
py::doc("create primitive representing the ::sqrt() function"));
|
||||
m.def("make_sin_pm",
|
||||
[]() { return make_primitive<Fn_dbl_dbl_type>("sin",
|
||||
::sin,
|
||||
false /*!explicit_symbol_def*/,
|
||||
llvmintrinsic::fp_sin); },
|
||||
py::doc("create primitive representing the ::sin() function"));
|
||||
m.def("make_cos_pm",
|
||||
[]() { return make_primitive<Fn_dbl_dbl_type>("cos",
|
||||
::cos,
|
||||
false /*!explicit_symbol_def*/,
|
||||
llvmintrinsic::fp_cos); },
|
||||
py::doc("create primitive representing the ::cos() function"));
|
||||
|
||||
py::class_<Primitive<double (*)(double, double)>,
|
||||
PrimitiveInterface,
|
||||
rp<Primitive<double (*)(double, double)>>>(m, "Primitive_double_double_double")
|
||||
;
|
||||
|
||||
m.def("make_pow_pm",
|
||||
[]() { return make_primitive<double (*)(double, double)>("pow",
|
||||
::pow,
|
||||
false /*!explicit_symbol_def*/,
|
||||
llvmintrinsic::fp_pow); },
|
||||
py::doc("create primitive representing the ::pow() function"));
|
||||
|
||||
// ----- Apply -----
|
||||
|
||||
py::class_<Apply, Expression, rp<Apply>>(m, "Apply")
|
||||
.def_property_readonly("fn", &Apply::fn, py::doc("function to be invoked"))
|
||||
.def_property_readonly("argv", &Apply::argv, py::doc("expressions (in position order) for function arguments"))
|
||||
;
|
||||
|
||||
m.def("make_apply", &Apply::make);
|
||||
|
||||
// ----- Variables -----
|
||||
|
||||
py::class_<Variable, Expression, rp<Variable>>(m, "Variable")
|
||||
.def_property_readonly("name", &Variable::name, py::doc("variable name"))
|
||||
;
|
||||
|
||||
m.def("make_var", &make_var,
|
||||
py::arg("name"),
|
||||
py::arg("var_type"));
|
||||
|
||||
// ----- Lambdas -----
|
||||
|
||||
py::class_<Lambda, Expression, rp<Lambda>>(m, "Lambda")
|
||||
.def_property_readonly("name", &Lambda::name, py::doc("lambda name (maybe automatically generated?)"))
|
||||
.def_property_readonly("type_str", &Lambda::type_str, py::doc("string specifying lambda type. e.g. \"double(double,double)\""))
|
||||
.def_property_readonly("argv", &Lambda::argv, py::doc("lambda formal parameters"))
|
||||
.def_property_readonly("body", &Lambda::body, py::doc("lambda body expression"))
|
||||
.def_property_readonly("n_arg", &Lambda::n_arg, py::doc("number of format parameters to this lambda function"))
|
||||
;
|
||||
|
||||
m.def("make_lambda", &make_lambda);
|
||||
|
||||
// ----- IfExpr -----
|
||||
|
||||
py::class_<IfExpr, Expression, rp<IfExpr>>(m, "IfExpr")
|
||||
.def_property_readonly("test", &IfExpr::test, py::doc("test expression"))
|
||||
.def_property_readonly("when_true", &IfExpr::when_true, py::doc("execute this expression when (and only when) test succeeds"))
|
||||
.def_property_readonly("when_false", &IfExpr::when_false, py::doc("execute this expression when (and only when) test fails"))
|
||||
;
|
||||
|
||||
m.def("make_ifexpr", &make_ifexpr);
|
||||
} /*pyexpresion*/
|
||||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end pyexpression.cpp */
|
||||
25
xo-pyexpression/src/pyexpression/pyexpression.hpp.in
Normal file
25
xo-pyexpression/src/pyexpression/pyexpression.hpp.in
Normal file
|
|
@ -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 */
|
||||
Loading…
Add table
Add a link
Reference in a new issue