
1 changed files with 276 additions and 0 deletions
@ -0,0 +1,276 @@ |
|||||
|
#!/usr/bin/env python3 |
||||
|
# -*- coding: utf-8 -*- |
||||
|
|
||||
|
from gen_cid import generate_cid |
||||
|
from gen_cid import utils |
||||
|
|
||||
|
|
||||
|
class CID2YML: |
||||
|
|
||||
|
def generate_yml(self, cid): |
||||
|
ret = "" |
||||
|
ret += self.generate_functions(cid) |
||||
|
ret += self.generate_structs(cid) |
||||
|
ret += self.generate_enums(cid) |
||||
|
return ret |
||||
|
|
||||
|
def generate_functions(self, cid): |
||||
|
data_root = cid["functions"] |
||||
|
|
||||
|
# Main |
||||
|
tmpl_main = "" |
||||
|
tmpl_main += 'method {name} {{\n' |
||||
|
tmpl_main += ' return {return_type}\n' |
||||
|
tmpl_main += '{subitems}' |
||||
|
tmpl_main += '}}\n\n' |
||||
|
|
||||
|
kind_main = "CursorKind.FUNCTION_DECL" |
||||
|
|
||||
|
def format_func_main(tmpl, item, subitems_str): |
||||
|
return tmpl.format( |
||||
|
name=item["name"], |
||||
|
return_type=item["result_type"], |
||||
|
subitems=subitems_str) |
||||
|
|
||||
|
# Sub |
||||
|
tmpl_sub = "" |
||||
|
tmpl_sub += ' use "{type}" "{name}"' |
||||
|
|
||||
|
kind_sub = "CursorKind.PARM_DECL" |
||||
|
|
||||
|
def format_func_sub(tmpl, item): |
||||
|
return tmpl.format( |
||||
|
type=item["type"], |
||||
|
name=item["name"]) |
||||
|
|
||||
|
ret = self._format(data_root, tmpl_main, kind_main, format_func_main, tmpl_sub, kind_sub, format_func_sub) |
||||
|
|
||||
|
return ret |
||||
|
|
||||
|
def generate_structs(self, cid): |
||||
|
data_root = cid["types"] |
||||
|
|
||||
|
# Main |
||||
|
tmpl_main = "" |
||||
|
tmpl_main += 'struct {name} {{\n' |
||||
|
tmpl_main += '{subitems}' |
||||
|
tmpl_main += '}}\n\n' |
||||
|
|
||||
|
kind_main = "CursorKind.STRUCT_DECL" |
||||
|
|
||||
|
def format_func_main(tmpl, item, subitems_str): |
||||
|
return tmpl.format( |
||||
|
name=item["name"], |
||||
|
subitems=subitems_str) |
||||
|
|
||||
|
# Sub |
||||
|
tmpl_sub = "" |
||||
|
tmpl_sub+=' field "{type}" "{name}"' |
||||
|
kind_sub = "CursorKind.FIELD_DECL" |
||||
|
|
||||
|
def format_func_sub(tmpl, item): |
||||
|
return tmpl.format( |
||||
|
type=item["type"], |
||||
|
name=item["name"]) |
||||
|
|
||||
|
ret = self._format(data_root, tmpl_main, kind_main, format_func_main, tmpl_sub, kind_sub, format_func_sub) |
||||
|
|
||||
|
return ret |
||||
|
|
||||
|
def generate_enums(self, cid): |
||||
|
data_root = cid["types"] |
||||
|
|
||||
|
# Main |
||||
|
tmpl_main = "" |
||||
|
tmpl_main += 'enum {name} {{\n' |
||||
|
tmpl_main += '{subitems}' |
||||
|
tmpl_main += '}}\n\n' |
||||
|
|
||||
|
kind_main = "CursorKind.ENUM_DECL" |
||||
|
|
||||
|
def format_func_main(tmpl, item, subitems_str): |
||||
|
return tmpl.format( |
||||
|
name=item["name"], |
||||
|
subitems=subitems_str) |
||||
|
|
||||
|
# Sub |
||||
|
tmpl_sub = "" |
||||
|
tmpl_sub+= ' item "{name}" "{value}"' |
||||
|
kind_sub = "CursorKind.ENUM_CONSTANT_DECL" |
||||
|
|
||||
|
def format_func_sub(tmpl, item): |
||||
|
return tmpl.format( |
||||
|
name=item["name"], |
||||
|
value=item["value"]) |
||||
|
|
||||
|
ret = self._format(data_root, tmpl_main, kind_main, format_func_main, tmpl_sub, kind_sub, format_func_sub) |
||||
|
|
||||
|
return ret |
||||
|
|
||||
|
def _format(self, data, tmpl_main, kind_main, func_main, tmpl_sub, kind_sub, func_sub): |
||||
|
ret = "" |
||||
|
|
||||
|
def filt(item): |
||||
|
if item["kind"] == kind_main: |
||||
|
return True |
||||
|
|
||||
|
main_items = utils.recursive_query(data, filt) |
||||
|
|
||||
|
for item in main_items: |
||||
|
subitems_str = self._format_subitems(item, tmpl_sub, kind_sub, func_sub) |
||||
|
ret += func_main(tmpl_main, item, subitems_str) |
||||
|
|
||||
|
return ret |
||||
|
|
||||
|
def _format_subitems(self, data, tmpl, kind, func): |
||||
|
ret = "" |
||||
|
|
||||
|
def filt(data): |
||||
|
if data["kind"] == kind: |
||||
|
return True |
||||
|
|
||||
|
subitems = utils.recursive_query(data, filt) |
||||
|
|
||||
|
for item in subitems: |
||||
|
ret += func(tmpl, item) + "\n" |
||||
|
|
||||
|
return ret |
||||
|
|
||||
|
|
||||
|
# ENUMS |
||||
|
# |
||||
|
|
||||
|
# def _format_enums(self, cid): |
||||
|
# ret = "" |
||||
|
# enums = self._extract_enums(cid["types"]) |
||||
|
# for enum in enums: |
||||
|
# ret += self._format_enum(enum) |
||||
|
# return ret |
||||
|
# |
||||
|
# def _extract_enums(self, cid): |
||||
|
# def functions_filt(item): |
||||
|
# if item["kind"] == "CursorKind.ENUM_DECL": |
||||
|
# return True |
||||
|
# |
||||
|
# enums = utils.recursive_query(cid, functions_filt) |
||||
|
# return enums |
||||
|
# |
||||
|
# def _format_enum(self, enum): |
||||
|
# ret = "" |
||||
|
# items = self._extract_enum_items(enum) |
||||
|
# items_str = self._format_enum_items(items) |
||||
|
# |
||||
|
# ret = self.enum_tmpl.format( |
||||
|
# name=enum["name"], |
||||
|
# enum_items=items_str) |
||||
|
# return ret |
||||
|
# |
||||
|
# def _extract_enum_items(self, func): |
||||
|
# ret = [] |
||||
|
# |
||||
|
# def extract_items(data): |
||||
|
# if data["kind"] == "CursorKind.ENUM_CONSTANT_DECL": |
||||
|
# return True |
||||
|
# |
||||
|
# ret = utils.recursive_query(func, extract_items) |
||||
|
# return ret |
||||
|
# |
||||
|
# def _format_enum_items(self, items): |
||||
|
# ret = "" |
||||
|
# for item in items: |
||||
|
# ret += self._format_function_param(item) |
||||
|
# return ret |
||||
|
# |
||||
|
# def _format_enum_item(self, param): |
||||
|
# ret = "" |
||||
|
# ret = self.enum_item_tmpl.format( |
||||
|
# name=param["name"], |
||||
|
# value=param["value"]) |
||||
|
# return ret |
||||
|
# |
||||
|
# # |
||||
|
# # FUNCTIONS |
||||
|
# # |
||||
|
# |
||||
|
# def _format_functions(self, cid): |
||||
|
# ret = "" |
||||
|
# funcs = self._extract_functions(cid["functions"]) |
||||
|
# for func in funcs: |
||||
|
# ret += self._format_function(func) |
||||
|
# return ret |
||||
|
# |
||||
|
# def _extract_functions(self, cid): |
||||
|
# def functions_filt(item): |
||||
|
# if item["kind"] == "CursorKind.FUNCTION_DECL": |
||||
|
# return True |
||||
|
# |
||||
|
# funcs = utils.recursive_query(cid, functions_filt) |
||||
|
# return funcs |
||||
|
# |
||||
|
# def _format_function(self, func): |
||||
|
# ret = "" |
||||
|
# params = self._extract_params(func) |
||||
|
# params_str = self._format_function_params(params) |
||||
|
# |
||||
|
# ret = self.func_tmpl.format( |
||||
|
# name=func["name"], |
||||
|
# return_type=func["result_type"], |
||||
|
# params=params_str) |
||||
|
# return ret |
||||
|
# |
||||
|
# def _extract_params(self, func): |
||||
|
# ret = [] |
||||
|
# |
||||
|
# def extract_params(data): |
||||
|
# if data["kind"] == "CursorKind.PARM_DECL": |
||||
|
# return True |
||||
|
# |
||||
|
# ret = utils.recursive_query(func, extract_params) |
||||
|
# return ret |
||||
|
# |
||||
|
# def _format_function_params(self, params): |
||||
|
# ret = "" |
||||
|
# for param in params: |
||||
|
# ret += self._format_function_param(param) |
||||
|
# return ret |
||||
|
# |
||||
|
# def _format_function_param(self, param): |
||||
|
# ret = "" |
||||
|
# ret = self.func_item_tmpl.format( |
||||
|
# name=param["name"], |
||||
|
# type=param["type"]) |
||||
|
# return ret |
||||
|
|
||||
|
|
||||
|
def main(): |
||||
|
vars = [] |
||||
|
funcs = [] |
||||
|
funcs.append("init_synth_shed") |
||||
|
funcs.append("synth_create") |
||||
|
funcs.append("synth_set_osc_count") |
||||
|
funcs.append("tech_to_string") |
||||
|
funcs.append("update_identity") |
||||
|
funcs.append("filtertype_to_string") |
||||
|
funcs.append("play_synth") |
||||
|
|
||||
|
header_filename = "../../src/synth_shed/synth_shed.h" |
||||
|
libclang_path = "/opt/local/libexec/llvm-9.0/lib/libclang.dylib" |
||||
|
header = generate_cid.parse(libclang_path, header_filename, funcs, vars) |
||||
|
|
||||
|
# Debug output |
||||
|
utils.write_json(header["ast"], "./" + "/" + header["filename"] + ".ast.json") |
||||
|
utils.write_json(header["cid"], "./" + "/" + header["filename"] + ".cid.json") |
||||
|
utils.write_json(header, "./" + "/" + header["filename"] + ".header.json") |
||||
|
|
||||
|
cid = header["cid"] |
||||
|
|
||||
|
cid2yml = CID2YML() |
||||
|
|
||||
|
funcs_str = cid2yml.generate_yml(cid) |
||||
|
|
||||
|
print(funcs_str) |
||||
|
utils.write_file("./" + "/" + header["filename"] + ".cid.yml2", funcs_str) |
||||
|
|
||||
|
|
||||
|
if __name__ == "__main__": |
||||
|
main() |
Loading…
Reference in new issue