From 1547b69b8e3bc381c3867a91061f5e64fa5ca392 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 24 Dec 2020 02:28:34 +0100 Subject: [PATCH] yml_to_pybind11 ;) YESSS --- gen/examples/synth_shed/Makefile | 23 +++++++ gen/examples/synth_shed/cid_grammar.yml2 | 65 +++++++++++++++++++ gen/examples/synth_shed/gen_pybind.marker | 1 + gen/examples/synth_shed/gen_pybind.ysl2 | 65 +++++++++++++++++++ gen/examples/synth_shed/py_module.cc | 44 +++++++++++++ gen/examples/synth_shed/synth_shed.h.cid.yml2 | 53 +++++++++++++++ gen/examples/synth_shed/synth_shed.py | 5 +- gen/gen_cid/cid_2_yml.py | 14 ++-- 8 files changed, 259 insertions(+), 11 deletions(-) create mode 100644 gen/examples/synth_shed/Makefile create mode 100644 gen/examples/synth_shed/cid_grammar.yml2 create mode 100644 gen/examples/synth_shed/gen_pybind.marker create mode 100644 gen/examples/synth_shed/gen_pybind.ysl2 create mode 100644 gen/examples/synth_shed/py_module.cc create mode 100644 gen/examples/synth_shed/synth_shed.h.cid.yml2 diff --git a/gen/examples/synth_shed/Makefile b/gen/examples/synth_shed/Makefile new file mode 100644 index 0000000..e10ccd1 --- /dev/null +++ b/gen/examples/synth_shed/Makefile @@ -0,0 +1,23 @@ +YML2_PATH=$(HOME)/src/pepbase/default/yml2 +YML2_PROC=$(YML2_PATH)/yml2proc $(YML2_OPTS) +YML2_OPTS=--encoding=utf8 + + +MARKER_DIR=. + +YML2_MARKERS= \ + $(MARKER_DIR)/gen_pybind.marker + +.PHONY: all codegen clean + +all: codegen + +# ------------- YML2 CodeGen -------------- +codegen: $(YML2_MARKERS) + +$(YML2_MARKERS): $(MARKER_DIR)/%.marker : %.ysl2 cid_grammar.yml2 + $(YML2_PROC) -y $< cid_grammar.yml2 + +clean: -dirs + echo -f $(YML2_MARKERS) + diff --git a/gen/examples/synth_shed/cid_grammar.yml2 b/gen/examples/synth_shed/cid_grammar.yml2 new file mode 100644 index 0000000..de6cd82 --- /dev/null +++ b/gen/examples/synth_shed/cid_grammar.yml2 @@ -0,0 +1,65 @@ +decl method @name; + +decl struct @name; +decl field @name; + +decl enum @name; +decl item @name; + +namespace pEp { + +method init_synth_shed { + return type="void"; +}; + +method synth_create { + return type="synth_t *"; + use name type="const char *"; +}; + +method synth_set_osc_count { + return type="SYNTH_STATUS"; + use synth type="synth_t *"; + use osc_count type="int"; +}; + +method tech_to_string { + return type="const char *"; + use tech type="const tech_t *"; +}; + +method filtertype_to_string { + return type="const char *"; + use filt type="const filtertype_t *"; +}; + +method play_synth { + return type="const char *"; + use synth type="synth_t *"; +}; + +struct _synth { + field model_name type="const char *"; + field osc_count type="int"; + field technolgy type="tech_t"; + field filter type="filter_t"; +}; + +struct _filter { + field technology type="tech_t"; + field type type="filtertype_t"; +}; + +enum _tech { + item ANALOG value=0; + item DIGITAL value=1; +}; + +enum _filtertype { + item HPF value=0; + item LPF value=1; + item BPF value=2; +}; + + +} diff --git a/gen/examples/synth_shed/gen_pybind.marker b/gen/examples/synth_shed/gen_pybind.marker new file mode 100644 index 0000000..3cc762b --- /dev/null +++ b/gen/examples/synth_shed/gen_pybind.marker @@ -0,0 +1 @@ +"" \ No newline at end of file diff --git a/gen/examples/synth_shed/gen_pybind.ysl2 b/gen/examples/synth_shed/gen_pybind.ysl2 new file mode 100644 index 0000000..1333d78 --- /dev/null +++ b/gen/examples/synth_shed/gen_pybind.ysl2 @@ -0,0 +1,65 @@ +include yslt.yml2 + +tstylesheet { + template "pEp" { + document("py_module.cc", "text") { + || + #include + #include + #include + #include + #include "synth_shed.h" + + using namespace std; + namespace py = pybind11; + + PYBIND11_MODULE(synth_shed, m) { + + `` apply "method" + + `` apply "struct" + + `` apply "enum" + + } + || + } + document("gen_pybind.marker", "text") > "" + } + + template "method" { + || + m.def("«@name»", &«@name», ""); + || + } + + template "struct" { + || + py::class_<«@name»>(m, "«@name»") + ``apply "field" + ; + + || + } + + template "field" { + || + .def_readwrite("«@name»", &::«../@name»::«@name») + || + } + + template "enum" { + || + py::enum_<::«@name»>(m, "«@name»") + ``apply item + ; + + || + } + + template "item" { + || + .value("«@name»", ::«@name», "") + || + } +} diff --git a/gen/examples/synth_shed/py_module.cc b/gen/examples/synth_shed/py_module.cc new file mode 100644 index 0000000..51bc844 --- /dev/null +++ b/gen/examples/synth_shed/py_module.cc @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include "synth_shed.h" + +using namespace std; +namespace py = pybind11; + +PYBIND11_MODULE(synth_shed, m) { + + m.def("init_synth_shed", &init_synth_shed, ""); + m.def("synth_create", &synth_create, ""); + m.def("synth_set_osc_count", &synth_set_osc_count, ""); + m.def("tech_to_string", &tech_to_string, ""); + m.def("filtertype_to_string", &filtertype_to_string, ""); + m.def("play_synth", &play_synth, ""); + + py::class_<_synth>(m, "_synth") + .def_readwrite("model_name", &::_synth::model_name) + .def_readwrite("osc_count", &::_synth::osc_count) + .def_readwrite("technolgy", &::_synth::technolgy) + .def_readwrite("filter", &::_synth::filter) + ; + + py::class_<_filter>(m, "_filter") + .def_readwrite("technology", &::_filter::technology) + .def_readwrite("type", &::_filter::type) + ; + + + py::enum_<::_tech>(m, "_tech") + .value("ANALOG", ::ANALOG, "") + .value("DIGITAL", ::DIGITAL, "") + ; + + py::enum_<::_filtertype>(m, "_filtertype") + .value("HPF", ::HPF, "") + .value("LPF", ::LPF, "") + .value("BPF", ::BPF, "") + ; + + +} diff --git a/gen/examples/synth_shed/synth_shed.h.cid.yml2 b/gen/examples/synth_shed/synth_shed.h.cid.yml2 new file mode 100644 index 0000000..3804762 --- /dev/null +++ b/gen/examples/synth_shed/synth_shed.h.cid.yml2 @@ -0,0 +1,53 @@ +method init_synth_shed { + return type="void"; +}; + +method synth_create { + return type="synth_t *"; + use name type="const char *"; +}; + +method synth_set_osc_count { + return type="SYNTH_STATUS"; + use synth type="synth_t *"; + use osc_count type="int"; +}; + +method tech_to_string { + return type="const char *"; + use tech type="const tech_t *"; +}; + +method filtertype_to_string { + return type="const char *"; + use filt type="const filtertype_t *"; +}; + +method play_synth { + return type="const char *"; + use synth type="synth_t *"; +}; + +struct _synth { + field model_name type="const char *"; + field osc_count type="int"; + field technolgy type="tech_t"; + field filter type="filter_t"; +}; + +struct _filter { + field technology type="tech_t"; + field type type="filtertype_t"; +}; + +enum _tech { + item ANALOG value=0; + item DIGITAL value=1; +}; + +enum _filtertype { + item HPF value=0; + item LPF value=1; + item BPF value=2; +}; + diff --git a/gen/examples/synth_shed/synth_shed.py b/gen/examples/synth_shed/synth_shed.py index 997af0f..3d733cd 100755 --- a/gen/examples/synth_shed/synth_shed.py +++ b/gen/examples/synth_shed/synth_shed.py @@ -21,13 +21,10 @@ def main(): cidtools = gen_cid.CIDTools(libclang_path, header_filename) header = cidtools.extract(vars,funcs,debug_ast=True,debug_cid=True,debug_yml=True) - # Debug output - - cid = header["cid"] - print(header["yml"]) utils.write_string( header["yml"], "./" + "/" + header["filename"] + ".cid.yml2",) + if __name__ == "__main__": main() diff --git a/gen/gen_cid/cid_2_yml.py b/gen/gen_cid/cid_2_yml.py index 151b574..b8d8ca1 100644 --- a/gen/gen_cid/cid_2_yml.py +++ b/gen/gen_cid/cid_2_yml.py @@ -14,9 +14,9 @@ def generate_functions(cid): # Main tmpl_main = "" tmpl_main += 'method {name} {{\n' - tmpl_main += ' return {return_type}\n' + tmpl_main += ' return type="{return_type}";\n' tmpl_main += '{subitems}' - tmpl_main += '}}\n\n' + tmpl_main += '}};\n\n' kind_main = "CursorKind.FUNCTION_DECL" @@ -28,7 +28,7 @@ def generate_functions(cid): # Sub tmpl_sub = "" - tmpl_sub += ' use "{type}" "{name}"' + tmpl_sub += ' use {name} type="{type}";' kind_sub = "CursorKind.PARM_DECL" @@ -48,7 +48,7 @@ def generate_structs(cid): tmpl_main = "" tmpl_main += 'struct {name} {{\n' tmpl_main += '{subitems}' - tmpl_main += '}}\n\n' + tmpl_main += '}};\n\n' kind_main = "CursorKind.STRUCT_DECL" @@ -59,7 +59,7 @@ def generate_structs(cid): # Sub tmpl_sub = "" - tmpl_sub+=' field "{type}" "{name}"' + tmpl_sub+=' field {name} type="{type}";' kind_sub = "CursorKind.FIELD_DECL" def format_func_sub(tmpl, item): @@ -78,7 +78,7 @@ def generate_enums(cid): tmpl_main = "" tmpl_main += 'enum {name} {{\n' tmpl_main += '{subitems}' - tmpl_main += '}}\n\n' + tmpl_main += '}};\n\n' kind_main = "CursorKind.ENUM_DECL" @@ -89,7 +89,7 @@ def generate_enums(cid): # Sub tmpl_sub = "" - tmpl_sub+= ' item "{name}" "{value}"' + tmpl_sub+= ' item {name} value={value};' kind_sub = "CursorKind.ENUM_CONSTANT_DECL" def format_func_sub(tmpl, item):