def transpile_body(self, body_ast, nesting_count): body_exec_string = "" for ast in body_ast: if self.check_ast('VariableDecleration', ast): var_obj = VariableObject(ast) transpile = var_obj.transpile() if self.should_dedent_trailing(ast, self.ast): body_exec_string += ( " " * (nesting_count - 1)) + transpile + "\n" else: body_exec_string += (" " * nesting_count) + transpile + "\n" if self.check_ast('PrebuiltFunction', ast): gen_builtin = BuiltInFunctionObject(ast) transpile = gen_builtin.transpile() if self.should_dedent_trailing(ast, self.ast): body_exec_string += ( " " * (nesting_count - 1)) + transpile + "\n" else: body_exec_string += (" " * nesting_count) + transpile + "\n" if self.check_ast('Comment', ast): gen_comment = CommentObject(ast) transpile = gen_comment.transpile() if self.should_dedent_trailing(ast, self.ast): body_exec_string += ( " " * (nesting_count - 1)) + transpile + "\n" else: body_exec_string += (" " * nesting_count) + transpile + "\n" if self.check_ast('ConditionalStatement', ast): if self.should_increment_nest_count(ast, self.ast): nesting_count += 1 condition_obj = ConditionObject(ast, nesting_count) if nesting_count == 2: body_exec_string += " " + condition_obj.transpile() else: body_exec_string += ( " " * (nesting_count - 1)) + condition_obj.transpile() if self.check_ast('ForLoop', ast): if self.should_increment_nest_count(ast, self.ast): nesting_count += 1 loop_obj = LoopObject(ast, nesting_count) body_exec_string += ( " " * (nesting_count - 1)) + loop_obj.transpile() return body_exec_string
def object_definer(self, isGettingBody): """ Object Definer This method will find all the different ast objects within the ast dictionary and call all the objects and pass in the ast dictionary to get a python string of code back which is the Y# code transpiled into python returns: python_code (str) : This will written a string of python code """ # Iterate through all ast dictionaries for ast in self.source_ast: # Create dictionary var object and append exec string global exec string if self.check_ast('VariableDecleration', ast): gen_var = VariableObject(ast) self.exec_string += gen_var.transpile() + '\n' # Create dictionary condition object and append exec string global exec string if self.check_ast('ConditionalStatement', ast): gen_condition = ConditionObject(ast, 1) self.exec_string += gen_condition.transpile() + '\n' # Create dictionary builtin object and append exec string global exec string if self.check_ast('PrebuiltFunction', ast): gen_builtin = BuiltInFunctionObject(ast) self.exec_string += gen_builtin.transpile() + "\n" # Create dictionary comment object and append exec string to global exec string when return from transpile methof if self.check_ast('Comment', ast): gen_comment = CommentObject(ast) self.exec_string += gen_comment.transpile() + "\n" # Create dictionary repetition object and append exec string global exec string if self.check_ast('ForLoop', ast): gen_loop = LoopObject(ast, 1) self.exec_string += gen_loop.transpile() + "\n" return self.exec_string
def translate_body(self, body_ast, nesting_count): """ Transpile Body This method will use the body AST in order to create a python version of the gleem code for the body statement while managing indentations return: body_exec_string (str) : The python transpiled code """ # Holds the body executable string of the first statement body_exec_string = "" # Loop through each ast item in the body dictionary for ast in body_ast: # This will parse variable declerations within the body if self.check_ast('VariableDecleration', ast): var_obj = VariableObject(ast) transpile = var_obj.translate() if self.should_dedent_trailing(ast, self.ast): body_exec_string += ( " " * (nesting_count - 1)) + transpile + "\n" else: body_exec_string += (" " * nesting_count) + transpile + "\n" # This will parse built-in within the body if self.check_ast('PrebuiltFunction', ast): gen_builtin = BuiltInFunctionObject(ast) transpile = gen_builtin.translate() if self.should_dedent_trailing(ast, self.ast): body_exec_string += ( " " * (nesting_count - 1)) + transpile + "\n" else: body_exec_string += (" " * nesting_count) + transpile + "\n" # This will parse comments within the body if self.check_ast('Comment', ast): gen_comment = CommentObject(ast) transpile = gen_comment.translate() if self.should_dedent_trailing(ast, self.ast): body_exec_string += ( " " * (nesting_count - 1)) + transpile + "\n" else: body_exec_string += (" " * nesting_count) + transpile + "\n" # This will parse nested conditional statement within the body if self.check_ast('ConditionalStatement', ast): # Increase nesting count because this is a condition statement inside a conditional statement # Only increase nest count if needed if self.should_increment_nest_count(ast, self.ast): nesting_count += 1 # Create conditional statement exec string condition_obj = ConditionObject(ast, nesting_count) # The second nested statament only needs 1 indent not 2 if nesting_count == 2: # Add the content of conditional statement with correct indentation body_exec_string += " " + condition_obj.translate() else: # Add the content of conditional statement with correct indentation body_exec_string += ( " " * (nesting_count - 1)) + condition_obj.translate() # This will parse nested conditional statement within the body if self.check_ast('ForLoop', ast): # Increase nesting count because this is a condition statement inside a conditional statement # Only increase nest count if needed if self.should_increment_nest_count(ast, self.ast): nesting_count += 1 # Create conditional statement exec string loop_obj = LoopObject(ast, nesting_count) body_exec_string += ( " " * (nesting_count - 1)) + loop_obj.translate() return body_exec_string