def handle_if(self, gast, lvl=0): test = router.gast_to_code(gast["test"], "java") body_indent = "\n\t" + "\t" * lvl closing_brace_indent = "\n" + "\t" * lvl body = general_helpers.list_helper(gast["body"], "java", body_indent, lvl + 1) out = 'if (' + test + ') {' + body_indent + body + closing_brace_indent + "}" if len(gast["orelse"]) == 0: pass elif gast["orelse"][0]["type"] == "if": out += " else " + router.gast_to_code(gast["orelse"], "java") else: out += " else {\n\t" + general_helpers.list_helper( gast["orelse"], "java", "\n\t") + "\n}" return out
def handle_if(self, gast, lvl=0): test = router.gast_to_code(gast["test"], "bash") body_indent = "\n\t" + "\t" * lvl closing_exp_indent = "\n" + "\t" * lvl body = general_helpers.list_helper(gast["body"], "bash", body_indent, lvl + 1) out = "if [[ " + test + " ]]; then" + body_indent + body + closing_exp_indent if len(gast["orelse"]) == 0: out += "fi" elif gast["orelse"][0]["type"] == "if": out += "el" + router.gast_to_code(gast["orelse"], "bash") else: out += "else\n\t" + general_helpers.list_helper( gast["orelse"], "bash", "\n\t") + "\nfi" return out
def handle_function_declaration(self, gast, lvl=0): name = router.gast_to_code(gast["id"], "java") if len(gast["params"]) != 0: args = "CustomType " args += general_helpers.list_helper(gast["params"], "java", ", CustomType ") else: args = "" body_indent = "\n\t" + "\t" * lvl closing_brace_indent = "\n" + "\t" * lvl body = general_helpers.list_helper(gast["body"], "java", body_indent, lvl + 1) out = "public unknown unknown " + name out += "(" + args + ") {" + body_indent + body + closing_brace_indent + "}" return out
def handle_while(self, gast, lvl=0): test = router.gast_to_code(gast["test"], "js") body_indent = "\n\t" + "\t" * lvl closing_brace_indent = "\n" + "\t" * lvl body = general_helpers.list_helper(gast["body"], "js", body_indent, lvl + 1) out = 'while (' + test + ') {' + body_indent + body + closing_brace_indent + "}" return out
def handle_for_of(self, gast, lvl=0): arr_str = router.gast_to_code(gast["iter"], "js") var_name = gast["init"]["value"] body_indent = "\n\t" + "\t" * lvl closing_brace_indent = "\n" + "\t" * lvl body = general_helpers.list_helper(gast["body"], "js", body_indent, lvl + 1) out = "for (" + var_name + " of " + arr_str + ") {" + body_indent + body + closing_brace_indent + "}" return out
def handle_for_range(self, gast, lvl=0): loop_init = router.gast_to_code(gast["init"], "js") loop_test = router.gast_to_code(gast["test"], "js") loop_update = router.gast_to_code(gast["update"], "js") body_indent = "\n\t" + "\t" * lvl closing_brace_indent = "\n" + "\t" * lvl body = general_helpers.list_helper(gast["body"], "js", body_indent, lvl + 1) return "for (" + loop_init + "; " + loop_test + "; " + loop_update + ") {" + body_indent + body + closing_brace_indent + "}"
def handle_arrow_func(self, gast, lvl=0): args = router.gast_to_code(gast["params"], "js") body_indent = "\n\t" + "\t" * lvl closing_brace_indent = "\n" + "\t" * lvl body = general_helpers.list_helper(gast["body"], "js", body_indent, lvl + 1) out = "(" + args + ") => {" out += body_indent + body + closing_brace_indent + "}" return out
def handle_function_declaration(self, gast, lvl=0): name = router.gast_to_code(gast["id"], "js") args = router.gast_to_code(gast["params"], "js") body_indent = "\n\t" + "\t" * lvl closing_brace_indent = "\n" + "\t" * lvl body = general_helpers.list_helper(gast["body"], "js", body_indent, lvl + 1) out = "function " + name out += "(" + args + ") {" + body_indent + body + closing_brace_indent + "}" return out
def handle_root(self, gast): return general_helpers.list_helper(gast["body"], "bash", "\n")
def gast_to_code(gast, out_lang, lvl=0): """ gast router that takes generic ast and the output language that the gast needs to be converted to and executes the conversion recursively out_lang correspond to the language codes defined in datastructure: javascript: js python: py """ converter = ConverterRegistry.get_converter(out_lang) if type(gast) == list: return general_helpers.list_helper(gast, out_lang) # Primitives elif gast["type"] == "num": return str(gast["value"]) elif gast["type"] == "arr": return converter.handle_arr(gast) elif gast["type"] == "str": return '"' + gast["value"] + '"' elif gast["type"] == "bool": return converter.handle_bool(gast) elif gast["type"] == "if": return converter.handle_if(gast, lvl) elif gast["type"] == "none": return converter.handle_none(gast) # Loops elif gast["type"] == "whileStatement": return converter.handle_while(gast, lvl) elif gast["type"] == "forRangeStatement": return converter.handle_for_range(gast, lvl) elif gast["type"] == "forOfStatement": return converter.handle_for_of(gast, lvl) # Other elif gast["type"] == "root": return converter.handle_root(gast) elif gast["type"] == "break": return "break" elif gast["type"] == "continue": return "continue" elif gast["type"] == "logStatement": return converter.handle_log_statement(gast) elif gast["type"] == "varAssign": return converter.handle_var_assign(gast) elif gast["type"] == "augAssign": return converter.handle_aug_assign(gast) elif gast["type"] == "funcCall": return converter.handle_func_call(gast) elif gast["type"] == "subscript": return converter.handle_subscript(gast) elif gast["type"] == "name": return converter.handle_name(gast) elif gast["type"] == "attribute": return converter.handle_attribute(gast) elif gast["type"] == "builtInAttribute": return converter.handle_built_in_attribute(gast) elif gast["type"] == "dict": return converter.handle_dict(gast) elif gast["type"] == "property": return converter.handle_property(gast) elif gast["type"] == "binOp": return general_helpers.gast_to_node_bin_op_helper(gast, out_lang) elif gast["type"] == "boolOp": return converter.handle_bool_op(gast) elif gast["type"] == "unaryOp": return converter.handle_unary_op(gast) elif gast["type"] == "functionDeclaration": return converter.handle_function_declaration(gast, lvl) elif gast["type"] == "returnStatement": return converter.handle_return_statement(gast) elif gast["type"] == "assignPattern": return converter.handle_assign_pattern(gast) elif gast["type"] == "arrowExpression": return converter.handle_arrow_func(gast) elif gast["type"] == "error" and gast["value"] == "unsupported": error_string = converter.get_error_handler().unsupported_feature() return error_string else: return converter.get_error_handler().unknown_error( "shared/gast_to_code/gast_to_code_router.py", gast)