Added code_interface_converter

This commit is contained in:
kevstone 2024-01-13 21:34:11 +00:00
parent 35ab5ff4f0
commit 34bdc97ba7
3 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,36 @@
import code_object.code_interface as code_interface
import code_object_converter.conversion_data as conversion_data
import code_object.code_objects as code_objects
import copy
import debug
# Base class for converting a C code object to C++
#
# Call convert_ccode_object() for generic convert behaviour
class CodeInterfaceConverter:
def __init__(self, ccode_object) -> None:
assert isinstance(ccode_object, code_interface.CodeInterface), f"Cannot convert object of type [{type(ccode_object)}]"
self._ccode_object = ccode_object
# Convert the ccode_object to a cpp_code_object
# It must be overridden - this version just returns a copy of the ccode_object
def to_cpp_code_object(self, conversion_data):
cpp_code_object = copy.deepcopy(self._ccode_object)
debug.line("to_cpp_code", f"Base version - just returning a copy of the C code object")
return cpp_code_object
# Mapping from C code objects to their converters
CodeInterfaceConverterClasses = {
# code_interface.CodeInterface: CodeInterfaceConverter,
}
# Convert a collection of C code_objects into C++ code_objects
def convert_ccode_objects(ccode_objects, conversion_data):
cpp_code_objects = code_objects.CodeObjects()
for obj in ccode_objects.code_objects:
converter_class = CodeInterfaceConverterClasses.get(type(obj), CodeInterfaceConverter)
debug.line("convert_ccode_object", f"ccode_object type=[{type(obj)}] converter_class=[{type(converter_class)}]")
converter = converter_class(obj)
cpp_code_objects.add_code_object(obj)
return cpp_code_objects

View File

@ -0,0 +1,8 @@
# Contains data to be used by the converters.
#
# The converters will add/update the entries here as required
class ConversionData:
def __init__(self) -> None:
pass

View File

@ -5,6 +5,8 @@ import default.default_cast_parser as default_cast_parser
import code_object.cppfunction as cppfunction
import code_object_converter.cfuncsig_converter as cfuncsig_converter
import default.default_cppcode as default_cppcode
import code_object_converter.conversion_data as conversion_data
import code_object_converter.code_interface_converter as code_interface_converter
# Convert a CCode object into a CppCode object, using the cconverter and derived classes as helpers
@ -25,6 +27,7 @@ class DefaultCCodeConverter:
def convert(self):
self.create_cpp_code()
self.create_transforms()
self.create_conversion_data()
self.convert_global_declarations()
self.convert_functions()
@ -36,10 +39,16 @@ class DefaultCCodeConverter:
def create_transforms(self):
self._transforms = transforms.Transforms()
def create_conversion_data(self):
self._conversion_data = conversion_data.ConversionData()
def convert_global_declarations(self):
global_decl_ast_parser = self.cast_parser_class()
global_decl_ccode_objects = global_decl_ast_parser.to_ccode_objects(self._ccode.global_declarations, self._transforms, self._ccode.macro_details)
debug.line("convert_global_declarations", global_decl_ccode_objects.as_lines())
global_decl_cpp_code_objects = code_interface_converter.convert_ccode_objects(global_decl_ccode_objects, self._conversion_data)
debug.line("convert_global_declarations", f"Converted C++ code...")
debug.line("convert_global_declarations", global_decl_cpp_code_objects.as_lines())
# Helper to create the funcsig mapping and update the transforms
def convert_cfunction_funcsig(self, cfunc):