Added new converters

This commit is contained in:
kevstone 2024-01-20 16:18:28 +00:00
parent 67be77071b
commit 4d208f0da6
18 changed files with 260 additions and 9 deletions

View File

@ -1,11 +1,10 @@
import utils.debug as debug
from code_object.declaration_specifier import DeclSpec
import code_object.arg as arg
import code_object.code_interface as code_interface
# Represent a struct argument
# Note - originally this was called struct.Struct but struct.py was causing a clash!
class StructArg(code_interface.CodeInterface):
# Note: decl_spec can be a string or an instance of class DeclSpec
def __init__(self, name, members=[]) -> None:
self._name = name
self._members = members
@ -20,6 +19,7 @@ class StructArg(code_interface.CodeInterface):
# Each entry should be an Arg
def add_member(self, member):
assert isinstance(member, arg.Arg), f"Expected Arg, got type=[{type(member)}]"
self._members.append(member)
def as_lines(self):

View File

@ -3,7 +3,6 @@ import utils.debug as debug
import code_object.arg as arg
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
from utils.standard_transforms import transform_variable_name
class ArgConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
@ -12,7 +11,7 @@ class ArgConverter(code_interface_converter.CodeInterfaceConverter):
def create_cpp_code_object(self, conversion_data):
cpp_decl_spec = conversion_funcs.convert_ccode_object(self._ccode_object.decl_spec, conversion_data)
cpp_name = transform_variable_name(self._ccode_object.name)
cpp_name = conversion_funcs.convert_ccode_object(self._ccode_object.name, conversion_data)
cpp_is_func_arg = self._ccode_object.is_func_arg
return arg.Arg(cpp_decl_spec, cpp_name, cpp_is_func_arg)

View File

@ -14,6 +14,3 @@ class ArrayAccessConverter(code_interface_converter.CodeInterfaceConverter):
cpp_index = conversion_funcs.convert_ccode_object(self._ccode_object.index, conversion_data)
return array_access.ArrayAccess(cpp_name, cpp_index)

View File

@ -0,0 +1,17 @@
import utils.debug as debug
import code_object.conditional_operation as conditional_operation
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class ConditionalOperationConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, conditional_operation.ConditionalOperation), f"Expected ConditionalOperation, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_bool_expression = conversion_funcs.convert_ccode_object(self._ccode_object.bool_expression, conversion_data)
cpp_true_expression = conversion_funcs.convert_ccode_object(self._ccode_object.true_expression, conversion_data)
cpp_false_expression = conversion_funcs.convert_ccode_object(self._ccode_object.false_expression, conversion_data)
return conditional_operation.ConditionalOperation(cpp_bool_expression, cpp_true_expression, cpp_false_expression)

View File

@ -4,6 +4,7 @@ import code_object.arg as arg
import code_object.binary_operation as binary_operation
import code_object.code_objects as code_objects
import code_object.compound_statement as compound_statement
import code_object.conditional_operation as conditional_operation
import code_object.constructor_function as constructor_function
import code_object.declaration_specifier as declaration_specifier
import code_object.destructor_function as destructor_function
@ -13,7 +14,19 @@ import code_object.funcsig as funcsig
import code_object.function as function
import code_object.function_call as function_call
import code_object.global_function as global_function
import code_object.if_statement as if_statement
import code_object.init_list as init_list
import code_object.literal as literal
import code_object.macro_definition as macro_definition
import code_object.macro_instantation as macro_instantation
import code_object.member_function as member_function
import code_object.operation as operation
import code_object.paren_expression as paren_expression
import code_object.return_statement as return_statement
import code_object.struct_arg as struct_arg
import code_object.struct_member_access as struct_member_access
import code_object.unary_expression as unary_expression
import code_object.unary_operation as unary_operation
import code_object.variable_declaration as variable_declaration
import code_object.virtual_member_function as virtual_member_function
@ -22,6 +35,7 @@ import code_object_converter.arg_converter as arg_converter
import code_object_converter.binary_operation_converter as binary_operation_converter
import code_object_converter.code_objects_converter as code_objects_converter
import code_object_converter.compound_statement_converter as compound_statement_converter
import code_object_converter.conditional_operation_converter as conditional_operation_converter
import code_object_converter.constructor_function_converter as constructor_function_converter
import code_object_converter.declaration_specifier_converter as declaration_specifier_converter
import code_object_converter.destructor_function_converter as destructor_function_converter
@ -31,12 +45,25 @@ import code_object_converter.funcsig_pointer_converter as funcsig_pointer_conver
import code_object_converter.function_call_converter as function_call_converter
import code_object_converter.function_converter as function_converter
import code_object_converter.global_function_converter as global_function_converter
import code_object_converter.if_statement_converter as if_statement_converter
import code_object_converter.init_list_converter as init_list_converter
import code_object_converter.literal_converter as literal_converter
import code_object_converter.macro_definition_converter as macro_definition_converter
import code_object_converter.macro_instantiation_converter as macro_instantiation_converter
import code_object_converter.member_function_converter as member_function_converter
import code_object_converter.operation_converter as operation_converter
import code_object_converter.paren_expression_converter as paren_expression_converter
import code_object_converter.return_statement_converter as return_statement_converter
import code_object_converter.struct_arg_converter as struct_arg_converter
import code_object_converter.struct_member_access_converter as struct_member_access_converter
import code_object_converter.unary_expression_converter as unary_expression_converter
import code_object_converter.unary_operation_converter as unary_operation_converter
import code_object_converter.variable_declaration_converter as variable_declaration_converter
import code_object_converter.virtual_member_function_converter as virtual_member_function_converter
import code_object_converter.code_interface_converter as code_interface_converter
import utils.debug as debug
from utils.standard_transforms import transform_variable_name
# Mapping from C code objects to their converters
CodeInterfaceConverterClasses = {
@ -45,6 +72,7 @@ CodeInterfaceConverterClasses = {
binary_operation.BinaryOperation : binary_operation_converter.BinaryOperationConverter,
code_objects.CodeObjects : code_objects_converter.CodeObjectsConverter,
compound_statement.CompoundStatement : compound_statement_converter.CompoundStatementConverter,
conditional_operation.ConditionalOperation : conditional_operation_converter.ConditionalOperationConverter,
constructor_function.ConstructorFunction : constructor_function_converter.ConstructorFunctionConverter,
declaration_specifier.DeclSpec : declaration_specifier_converter.DeclSpecConverter,
destructor_function.DestructorFunction : destructor_function_converter.DestructorFunctionConverter,
@ -54,7 +82,19 @@ CodeInterfaceConverterClasses = {
function_call.FunctionCall : function_call_converter.FunctionCallConverter,
function.Function : function_converter.FunctionConverter,
global_function.GlobalFunction : global_function_converter.GlobalFunctionConverter,
if_statement.IfStatement : if_statement_converter.IfStatementConverter,
init_list.InitList : init_list_converter.InitListConverter,
literal.Literal : literal_converter.LiteralConverter,
member_function.MemberFunction : member_function_converter.MemberFunctionConverter,
macro_definition.MacroDefinition : macro_definition_converter.MacroDefinitionConverter,
macro_instantation.MacroInstantation : macro_instantiation_converter.MacroInstantiationConverter,
operation.Operation : operation_converter.OperationConverter,
paren_expression.ParenExpression : paren_expression_converter.ParenExpressionConverter,
return_statement.ReturnStatement : return_statement_converter.ReturnStatementConverter,
struct_arg.StructArg : struct_arg_converter.StructArgConverter,
struct_member_access.StructMemberAccess : struct_member_access_converter.StructMemberAccessConverter,
unary_expression.UnaryExpression : unary_expression_converter.UnaryExpressionConverter,
unary_operation.UnaryOperation : unary_operation_converter.UnaryOperationConverter,
variable_declaration.VariableDeclaration : variable_declaration_converter.VariableDeclarationConverter,
virtual_member_function.VirtualMemberFunction : virtual_member_function_converter.VirtualMemberFunctionConverter,
}
@ -63,6 +103,13 @@ CodeInterfaceConverterClasses = {
def convert_ccode_object(ccode_object, conversion_data):
debug.line("convert_ccode_object", f"Converting ccode_object instance=[{ccode_object}]")
if ccode_object is None:
debug.line("convert_ccode_object", f" -> Type=[None], returning None!")
return None
elif isinstance(ccode_object, str):
debug.line("convert_ccode_object", f" -> Type=[str], performing a basic name transform")
return transform_variable_name(ccode_object)
converter_class = CodeInterfaceConverterClasses.get(type(ccode_object), code_interface_converter.CodeInterfaceConverter)
debug.line("convert_ccode_object", f"converter_class=[{converter_class}]")
converter = converter_class(ccode_object)

View File

@ -3,7 +3,6 @@ import utils.debug as debug
import code_object.function_call as function_call
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
from utils.standard_transforms import transform_function_name
class FunctionCallConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
@ -11,7 +10,7 @@ class FunctionCallConverter(code_interface_converter.CodeInterfaceConverter):
assert isinstance(ccode_object, function_call.FunctionCall), f"Expected FunctionCall, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_name = transform_function_name(self._ccode_object.name)
cpp_name = conversion_funcs.convert_ccode_object(self._ccode_object.name, conversion_data)
cpp_args = []
for arg_entry in self._ccode_object.args:
cpp_arg_entry = conversion_funcs.convert_ccode_object(arg_entry, conversion_data)

View File

@ -0,0 +1,16 @@
import utils.debug as debug
import code_object.if_statement as if_statement
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class IfStatementConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, if_statement.IfStatement), f"Expected IfStatement, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_expression = conversion_funcs.convert_ccode_object(self._ccode_object.expression, conversion_data)
cpp_action = conversion_funcs.convert_ccode_object(self._ccode_object.action, conversion_data)
return if_statement.IfStatement(cpp_expression, cpp_action)

View File

@ -0,0 +1,20 @@
import utils.debug as debug
import code_object.init_list as init_list
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class InitListConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, init_list.InitList), f"Expected InitList, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_decl_spec = conversion_funcs.convert_ccode_object(self._ccode_object.decl_spec, conversion_data)
cpp_init_list = init_list.InitList(cpp_decl_spec)
for entry in self._ccode_object.entries:
cpp_entry = conversion_funcs.convert_ccode_object(entry, conversion_data)
cpp_init_list.add_entry(cpp_entry)
return cpp_init_list

View File

@ -0,0 +1,14 @@
import utils.debug as debug
import code_object.literal as literal
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class LiteralConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, literal.Literal), f"Expected Literal, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
# By default we just return a copy
return literal.Literal(self._ccode_object.value)

View File

@ -0,0 +1,14 @@
import utils.debug as debug
import code_object.macro_definition as macro_definition
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class MacroDefinitionConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, macro_definition.MacroDefinition), f"Expected MacroDefinition, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
# For now, just return a copy...
return macro_definition.MacroDefinition(self._ccode_object.as_lines())

View File

@ -0,0 +1,14 @@
import utils.debug as debug
import code_object.macro_instantation as macro_instantation
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class MacroInstantiationConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, macro_instantation.MacroInstantation), f"Expected MacroInstantation, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
# For now, just return a copy...
return macro_instantation.MacroInstantation(self._ccode_object.as_lines())

View File

@ -0,0 +1,14 @@
import utils.debug as debug
import code_object.operation as operation
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class OperationConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, operation.Operation), f"Expected Operation, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
# By default we just return a copy of the value
return operation.Operation(self._ccode_object.value)

View File

@ -0,0 +1,15 @@
import utils.debug as debug
import code_object.paren_expression as paren_expression
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class ParenExpressionConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, paren_expression.ParenExpression), f"Expected ParenExpression, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_expression = conversion_funcs.convert_ccode_object(self._ccode_object.expression, conversion_data)
return paren_expression.ParenExpression(cpp_expression)

View File

@ -0,0 +1,15 @@
import utils.debug as debug
import code_object.return_statement as return_statement
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class ReturnStatementConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, return_statement.ReturnStatement), f"Expected ReturnStatement, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_expression = conversion_funcs.convert_ccode_object(self._ccode_object.expression, conversion_data)
return return_statement.ReturnStatement(cpp_expression)

View File

@ -0,0 +1,19 @@
import utils.debug as debug
import code_object.struct_arg as struct_arg
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class StructArgConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, struct_arg.StructArg), f"Expected StructArg, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_name = conversion_funcs.convert_ccode_object(self._ccode_object.name, conversion_data)
cpp_members=[]
for member in self._ccode_object.members:
cpp_member = conversion_funcs.convert_ccode_object(member, conversion_data)
cpp_members.append(cpp_member)
return struct_arg.StructArg(cpp_name, cpp_members)

View File

@ -0,0 +1,19 @@
import utils.debug as debug
import code_object.struct_member_access as struct_member_access
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class StructMemberAccessConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, struct_member_access.StructMemberAccess), f"Expected StructMemberAccess, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_access = conversion_funcs.convert_ccode_object(self._ccode_object.access, conversion_data)
cpp_name = conversion_funcs.convert_ccode_object(self._ccode_object.name, conversion_data)
cpp_index = conversion_funcs.convert_ccode_object(self._ccode_object.index, conversion_data)
cpp_member = conversion_funcs.convert_ccode_object(self._ccode_object.member, conversion_data)
return struct_member_access.StructMemberAccess(cpp_access, cpp_name, cpp_index, cpp_member)

View File

@ -0,0 +1,16 @@
import utils.debug as debug
import code_object.unary_expression as unary_expression
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class UnaryExpressionConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, unary_expression.UnaryExpression), f"Expected UnaryExpression, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_keyword = conversion_funcs.convert_ccode_object(self._ccode_object.keyword, conversion_data)
cpp_expression = conversion_funcs.convert_ccode_object(self._ccode_object.expression, conversion_data)
return unary_expression.UnaryExpression(cpp_keyword, cpp_expression)

View File

@ -0,0 +1,16 @@
import utils.debug as debug
import code_object.unary_operation as unary_operation
import code_object_converter.code_interface_converter as code_interface_converter
import code_object_converter.conversion_funcs as conversion_funcs
class UnaryOperationConverter(code_interface_converter.CodeInterfaceConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, unary_operation.UnaryOperation), f"Expected UnaryOperation, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_unary_op = conversion_funcs.convert_ccode_object(self._ccode_object.unary_op, conversion_data)
cpp_operand = conversion_funcs.convert_ccode_object(self._ccode_object.operand, conversion_data)
return unary_operation.UnaryOperation(cpp_unary_op, cpp_operand)