def java_node_list_helper(gast_list,
                          is_returning_functions,
                          csv_delimiter=", ",
                          lvl=0):
    """
    Almost identical to regular list_helper however is_returning_functions is passed in
    as a boolean to determine whether the list_helper ignores functions or ignores non-functions
    """

    out = ""

    for i in range(0, len(gast_list)):
        if is_returning_functions and gast_list[i][
                "type"] == "functionDeclaration":
            out += router.gast_to_code(gast_list[i], "java", lvl)
            if i < len(gast_list) - 1:  # don't add delimiter for last item
                out += csv_delimiter

        elif (not is_returning_functions
              ) and gast_list[i]["type"] != "functionDeclaration":
            out += router.gast_to_code(gast_list[i], "java", lvl)
            if i < len(gast_list) - 1:  # don't add delimiter for last item
                out += csv_delimiter

    return out
Beispiel #2
0
    def handle_var_assign(self, gast):
        var_id = router.gast_to_code(gast["varId"], "java")
        var_value = router.gast_to_code(gast["varValue"], "java")

        kind = java_helpers.gast_to_java_type(gast["varValue"],
                                              error_handler=self.error_handler)

        return kind + " " + var_id + " = " + var_value
    def handle_var_assign(self, gast):
        kind = gast["kind"]
        varId = router.gast_to_code(gast["varId"], "js")
        varValue = router.gast_to_code(gast["varValue"], "js")

        if gast["varId"]["type"] == "subscript":
            return varId + " = " + varValue
        return kind + " " + varId + " = " + varValue
Beispiel #4
0
 def handle_aug_assign(self, gast):
     if "right" in gast:
         return router.gast_to_code(
             gast["left"],
             "bash") + " " + gast["op"] + " " + router.gast_to_code(
                 gast["right"], "bash")
     else:
         return router.gast_to_code(gast["left"], "bash") + gast["op"]
    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_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
Beispiel #7
0
    def handle_func_call(self, gast):
        # handles logstatement for single array
        if gast["value"]["type"] == "logStatement" and len(
                gast["args"]) == 1 and gast["args"][0]["type"] == "arr":
            log_statement = router.gast_to_code(gast["value"], "java")
            type_declaration = java_helpers.gast_to_java_type(
                gast["args"][0], error_handler=self.error_handler)
            arr = router.gast_to_code(gast["args"], "java")
            return log_statement + "(Arrays.toString(new " + type_declaration + " " + arr + "))"

        return router.gast_to_code(gast["value"],
                                   "java") + "(" + router.gast_to_code(
                                       gast["args"], "java") + ")"
def gast_to_code_caller(gast, output_lang, error_handler):
    output_code = gtc.gast_to_code(gast, output_lang)

    #post processing
    post_processed_output_code = ConverterRegistry.get_converter(
        output_lang).gast_to_code_post_processing(output_code)

    return post_processed_output_code
Beispiel #9
0
    def handle_arr(self, gast):
        # This logic returns an error for nested arrays which are not supported in bash
        if general_helpers.arr_in_list(gast["elements"]):
            return self.error_handler.impossible_translation([
                "https://stackoverflow.com/a/11234169",
                "https://github.com/pppoe/Nested-Array-Bash"
            ])

        return "(" + router.gast_to_code(gast["elements"], "bash") + ")"
Beispiel #10
0
    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
Beispiel #11
0
    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_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_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 bash_arg_helper(gast_list):
    '''
    Called on func args only and return the correct translation since
    variables are written different when function params
    '''
    out = ""
    for i in range(0, len(gast_list)):
        if gast_list[i]["type"] == "name":
            out += handle_var_arg(gast_list[i])
        else:
            out += router.gast_to_code(gast_list[i], "bash")

        if i < len(gast_list) - 1:  # don't add delimiter for last item
            out += " "
    return out
Beispiel #16
0
def list_helper(gast_list, out_lang, csv_delimiter=", ", lvl=0):
    """
    Helper for lists of gast
    Default is to put comma and space btwn each stringified gast
        i.e. list_helper({str_gast}, {str_gast}, out_lang) --> str, str
    Can specify different btwn string with third parameter
        i.e. list_helper({str_gast}, {str_gast}, out_lang, "**") --> str**str
    """
    out = ""

    for i in range(0, len(gast_list)):
        out += router.gast_to_code(gast_list[i], out_lang, lvl)

        if i < len(gast_list) - 1:  # don't add delimiter for last item
            out += csv_delimiter

    return out
Beispiel #17
0
    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 test_multi_body(self):
     code = 'x = 5\nx = 5'
     self.assertEqual(code, gtc.gast_to_code(gasts.gast_multi_body, "bash"))
Beispiel #19
0
 def handle_func_call(self, gast):
     return router.gast_to_code(gast["value"],
                                "bash") + " " + bash_helpers.bash_arg_helper(
                                    gast["args"])
 def test_indent_if(self):
     code = 'if [[ x == $$E6$$ ]]; then\n\tif [[ y == $$E7$$ ]]; then\n\t\techo "y and x are true"\n\tfi\nfi'
     self.assertEqual(code, gtc.gast_to_code(gasts.gast_indent_if, "bash"))
 def test_forOf(self):
     code = '$$E2$$'
     self.assertEqual(code, gtc.gast_to_code(gasts.gast_for_of, "bash"))
 def test_forRange(self):
     code = '$$E3$$'
     self.assertEqual(code, gtc.gast_to_code(gasts.gast_for_range, "bash"))
 def test_elif(self):
     code = 'if [[ 1 ]]; then\n\techo "1 is true"\nelif [[ 2 ]]; then\n\techo "2 is true"\n\techo "second line"\nfi'
     self.assertEqual(code, gtc.gast_to_code(gasts.gast_elif_log, "bash"))
 def test_else(self):
     code = 'if [[ 1 ]]; then\n\techo "1 is true"\nelse\n\techo "1 is NOT true"\nfi'
     self.assertEqual(code, gtc.gast_to_code(gasts.gast_else_log, "bash"))
Beispiel #25
0
 def handle_var_assign(self, gast):
     value = router.gast_to_code(gast["varValue"], "bash")
     return router.gast_to_code(gast["varId"], "bash") + " = " + value
 def test_primitive_str(self):
     code = '"hello world"'
     self.assertEqual(code, gtc.gast_to_code(gasts.gast_str, "bash"))
 def test_varAssign_const(self):
     code = 'x = 5'
     self.assertEqual(code,
                      gtc.gast_to_code(gasts.gast_varAssign_const, "bash"))
 def test_logStatement_bool(self):
     code = 'echo $$E8$$'
     self.assertEqual(code,
                      gtc.gast_to_code(gasts.gast_logStatement_bool, "bash"))
 def test_logStatement_two_arguments(self):
     code = 'echo "hello world" 5'
     self.assertEqual(
         code, gtc.gast_to_code(gasts.gast_logStatement_two_args, "bash"))
 def test_if(self):
     code = 'if [[ $$E4$$ ]]; then\n\techo "This is true"\nfi'
     self.assertEqual(code, gtc.gast_to_code(gasts.gast_if_log, "bash"))