Browse Source

yml_to_pybind11 ;) YESSS

master
heck 5 years ago
parent
commit
1547b69b8e
  1. 23
      gen/examples/synth_shed/Makefile
  2. 65
      gen/examples/synth_shed/cid_grammar.yml2
  3. 1
      gen/examples/synth_shed/gen_pybind.marker
  4. 65
      gen/examples/synth_shed/gen_pybind.ysl2
  5. 44
      gen/examples/synth_shed/py_module.cc
  6. 53
      gen/examples/synth_shed/synth_shed.h.cid.yml2
  7. 5
      gen/examples/synth_shed/synth_shed.py
  8. 14
      gen/gen_cid/cid_2_yml.py

23
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)

65
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;
};
}

1
gen/examples/synth_shed/gen_pybind.marker

@ -0,0 +1 @@
""

65
gen/examples/synth_shed/gen_pybind.ysl2

@ -0,0 +1,65 @@
include yslt.yml2
tstylesheet {
template "pEp" {
document("py_module.cc", "text") {
||
#include <string>
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/detail/common.h>
#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», "")
||
}
}

44
gen/examples/synth_shed/py_module.cc

@ -0,0 +1,44 @@
#include <string>
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/detail/common.h>
#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, "")
;
}

53
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;
};

5
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()

14
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):

Loading…
Cancel
Save