Code conversion method and device, computer equipment and storage medium
By generating an abstract syntax tree and adding language calls function nodes, the problem of inefficient processing of existing multilingual conversion code is solved, and an efficient code conversion process is realized.
Patent Information
- Application Number
- CN202410105151.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-24
- Publication Date
- 2025-07-25
AI Technical Summary
The existing multilingual conversion code processing is inefficient, and it requires manual search of copy and translation and generation of key-value json data, resulting in a long processing process.
Generate an abstract syntax tree by getting the initial code, add language call functions as new nodes, generate a new abstract syntax tree, and eventually convert it into object code, avoiding additional copywriting and code conversion work.
It improves the code processing efficiency of multilingual conversion, reduces processing time, and improves the efficiency of multilingual development requirements.
Smart Images

Figure CN120371305A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of code generation, and particularly to a code conversion method, apparatus, computer device, storage medium, and computer program product. Background Art
[0002] Multilingual conversion means that the user switches to Chinese, English, or French through a certain operation, and all corresponding copy on the page is changed to the corresponding language type. If a current web page expects to implement multilingual conversion, the following implementation steps are required: First, manually find out the copy that needs to be translated in the code one by one, then translate the copy. After the translation is completed, generate key-value json data, where the key is the unique identifier of the same Chinese character, and the value is the different translated copy. In the code, a multilingual call function is encapsulated by referring to the json data, and the call function is added at each place of the Chinese copy. Each of the above steps takes a long time, resulting in a very long entire processing process.
[0003] Currently, the code editing volume for multilingual conversion is large, and the code processing efficiency is low. Summary of the Invention
[0004] Based on this, in view of the above technical problems, it is necessary to provide a code conversion method, apparatus, computer device, computer-readable storage medium, and computer program product that can improve the code processing efficiency of multilingual conversion.
[0005] In a first aspect, this application provides a code conversion method, including:
[0006] Obtain an initial code; the initial code includes a first-language code;
[0007] Generate an abstract syntax tree according to the initial code; the abstract syntax tree includes a first-language node corresponding to the first-language code;
[0008] According to a language call function for implementing language conversion, add a second-language node connected to the first-language node in the abstract syntax tree to obtain a new abstract syntax tree;
[0009] Generate a target code according to the new abstract syntax tree.
[0010] In one of the embodiments, obtaining the initial code includes:
[0011] Determine the target code type to be converted;
[0012] Traverse the code files in the target code repository to obtain target code files belonging to the target code type;
[0013] Read the target code files to obtain the initial code.
[0014] In one embodiment, the first language node includes at least one language identification node and language content nodes corresponding to the respective language identification nodes; according to the language call function for implementing language conversion, a second language node connected to the first language node is added in the abstract syntax tree to obtain a new abstract syntax tree, including:
[0015] Obtain the function call syntax element, function identification syntax element, and function name syntax element in the language call function, and generate a second language node according to the function call syntax element, function identification syntax element, and function name syntax element; the second language node includes a function call node, a function identification node, and a function name node;
[0016] Insert a function call node at the upper layer of each language identification node in the abstract syntax tree;
[0017] Add a function identification node at the lower layer of each function call node, and add a function name node at the lower layer of each function identification node to obtain a new abstract syntax tree.
[0018] In one embodiment, the method further includes:
[0019] Obtain the first language data in the new abstract syntax tree according to each first language node in the new abstract syntax tree.
[0020] In one embodiment, the method further includes:
[0021] Call the machine translation interface corresponding to the first language code to perform translation processing on the first language data to obtain a translation file;
[0022] Generate a code conversion result according to the target code and the translation file.
[0023] In one embodiment, the initial code is JavaScript code; generating a code conversion result according to the target code and the translation file includes:
[0024] Write the translation file into a json data file;
[0025] Generate a new code file according to the target code;
[0026] Integrate the new code file and the json data file to obtain a code conversion result.
[0027] In a second aspect, the present application further provides a code conversion device, including:
[0028] An acquisition module, configured to acquire an initial code; the initial code includes a first language code;
[0029] A generation module, configured to generate an abstract syntax tree according to an initial code; the abstract syntax tree includes first language nodes corresponding to the first language code;
[0030] An adding module, configured to add second language nodes connected to the first language nodes in the abstract syntax tree according to a language call function for implementing language conversion, so as to obtain a new abstract syntax tree;
[0031] A conversion module, configured to generate target code according to the new abstract syntax tree.
[0032] In a third aspect, the present application further provides a computer device, including a memory and a processor, where the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0033] Obtain an initial code; the initial code includes a first language code;
[0034] Generate an abstract syntax tree according to the initial code; the abstract syntax tree includes first language nodes corresponding to the first language code;
[0035] Add second language nodes connected to the first language nodes in the abstract syntax tree according to a language call function for implementing language conversion, so as to obtain a new abstract syntax tree;
[0036] Generate target code according to the new abstract syntax tree.
[0037] In a fourth aspect, the present application further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the following steps are implemented:
[0038] Obtain an initial code; the initial code includes a first language code;
[0039] Generate an abstract syntax tree according to the initial code; the abstract syntax tree includes first language nodes corresponding to the first language code;
[0040] Add second language nodes connected to the first language nodes in the abstract syntax tree according to a language call function for implementing language conversion, so as to obtain a new abstract syntax tree;
[0041] Generate target code according to the new abstract syntax tree.
[0042] In a fifth aspect, the present application further provides a computer program product, including a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0043] Obtain an initial code; the initial code includes a first language code;
[0044] Generate an abstract syntax tree according to the initial code; the abstract syntax tree includes first-language nodes corresponding to the first-language code;
[0045] According to the language call function for implementing language conversion, add second-language nodes connected to the first-language nodes in the abstract syntax tree to obtain a new abstract syntax tree;
[0046] Generate target code according to the new abstract syntax tree.
[0047] The above code conversion method, device, computer device, storage medium and computer program product obtain the initial code containing the first-language code; generate an abstract syntax tree containing first-language nodes according to the initial code; according to the language call function for implementing language conversion, add second-language nodes connected to the first-language nodes in the abstract syntax tree to obtain a new abstract syntax tree; generate target code according to the new abstract syntax tree. By converting the initial code into an abstract syntax tree, inserting the language call function as a new node into the abstract syntax tree, and then converting the new abstract syntax tree into new code, the new code can convert the first-language code in the initial code into the second-language code, without the need for additional copywriting translation and code conversion work, which can improve the code processing efficiency of multilingual conversion, thereby improving the development efficiency of multilingual requirements. Brief Description of the Drawings
[0048] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the following will briefly introduce the drawings required for use in the description of the embodiments or related technologies. Obviously, the following drawings are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.
[0049] Figure 1 It is an application environment diagram of the code conversion method in an embodiment;
[0050] Figure 2 It is a flowchart of the code conversion method in an embodiment;
[0051] Figure 3 It is a schematic diagram of generating an abstract syntax tree from code in an embodiment;
[0052] Figure 4 It is a schematic diagram of the abstract syntax tree corresponding to the code in an embodiment;
[0053] Figure 5 It is a schematic diagram of the first-language nodes in the abstract syntax tree in an embodiment;
[0054] Figure 6 It is a schematic diagram of adding second-language nodes to the abstract syntax tree in an embodiment;
[0055] Figure 7 Schematic diagram of a JSON data file in an embodiment
[0056] Figure 8 Structural block diagram of a code conversion device in an embodiment
[0057] Figure 9 Internal structure diagram of a computer device in an embodiment Detailed implementation manners
[0058] In order to make the objectives, technical solutions and advantages of the present application clearer, the present application will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application, and are not used to limit the present application.
[0059] The code conversion method provided by the embodiments of the present application can be applied to an application environment as shown in Figure 1 In the figure. Among them, the terminal 102 communicates with the server 104 through a network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or can be placed in the cloud or other network servers. Among them, the terminal 102 can be, but is not limited to, various personal computers, laptop computers, smart phones, tablet computers, Internet of Things devices, and portable wearable devices. The Internet of Things devices can be smart speakers, smart TVs, smart air conditioners, smart vehicle-mounted devices, etc. The portable wearable devices can be smart watches, smart bracelets, head-mounted devices, etc. The server 104 can be implemented by an independent server or a server cluster composed of multiple servers.
[0060] In an exemplary embodiment, as shown in Figure 2 In the figure, a code conversion method is provided. Taking the method applied to the terminal 102 in Figure 1 as an example, the following steps 202 to 208 are included. Among them:
[0061] Step 202, obtain an initial code; the initial code includes a first language code.
[0062] Among them, the first language code refers to the code part that needs to be translated in the initial code, and the first language can be any language type, including but not limited to Chinese, English, Japanese, etc.
[0063] Optionally, before performing code conversion, it is necessary to first determine the code type and the first language for which code conversion is to be performed. The code type includes, but is not limited to, Javascript code. The terminal scans the selected code repository based on the determined code type and the first language, searches for code files that match the code type and contain the first language code, and then reads the file contents of these code files to obtain the initial code.
[0064] Step 204, generate an abstract syntax tree based on the initial code; the abstract syntax tree includes first language nodes corresponding to the first language code.
[0065] Among them, the abstract syntax tree (AST), or simply referred to as the syntax tree, is an abstract representation of the syntax structure of the source code. It represents the syntax structure of a programming language in a tree-like form, and each node on the tree represents a structure in the source code.
[0066] Optionally, the terminal performs lexical analysis and syntax analysis on the initial code to generate an abstract syntax tree corresponding to the initial code.
[0067] For example, for the initial code let sum=10+66, in the lexical analysis stage, the string-form code is converted into a stream of tokens. Reading the source program character by character from left to right, identifying individual "words", "symbols", etc.:
[0068] Word Word Symbol Number Symbol Number Symbol
[0069] |let|sum|=|10|+|66|;|
[0071] {"type":"word",value:"let"}
[0072] {"type":"word",value:"sum"}
[0073] {"type":"Punctuator",value:"="}
[0074] {"type":"Numeric",value:"10"}
[0075] {"type":"Punctuator",value:"+"}
[0076] {"type":"Numeric",value:"66""}
[0077] { "type": "Punctuator", "value": ";"}
[0079] During the syntax analysis phase, a token stream is converted into the form of an AST. In this phase, the information in the tokens is used to convert them into a representational structure of an AST, which is more conducive to subsequent operations. That is: based on the syntax of the current programming language on the basis of lexical analysis, the sequence of words is combined into various syntax phrases
[0080] Keyword Identifier Assignment Operator Literal Binary Operator Literal End Symbol
[0081]
[0082]
[0083]
[0084] Step 206, according to the language call function used to implement language conversion, in the abstract syntax tree, add a second language node connected to the first language node to obtain a new abstract syntax tree.
[0085] Among them, the language call function corresponds to the selected first language and second language, and is used to convert the first language code into the second language code. Multiple language call functions are pre-configured in the terminal, and different language call functions can convert the first language into different second languages. The second language code refers to the translated code part in the initial code, and the second language can be any language type, including but not limited to Chinese, English, Japanese, etc. Usually, the second language and the first language belong to different language types.
[0086] Optionally, the terminal obtains the language call function that can convert the first language into the second language from the preset multiple language call functions according to the selected first language and second language, then performs syntax element splitting on the obtained language call function to obtain multiple syntax elements corresponding to the language call function, and uses these syntax elements as new nodes, that is, the second language nodes, to add to the abstract syntax tree and establish a hierarchical relationship with the first language nodes to obtain a new abstract syntax tree.
[0087] Step 208, generate target code according to the new abstract syntax tree.
[0088] Optionally, the terminal traverses the nodes of the new abstract syntax tree and performs code conversion to generate new code as the target code, and the target code corresponds to the new abstract syntax tree.
[0089] For example, when the initial code is JavaScript code, the terminal calls Babel to transform the new abstract syntax tree. Babel is a JavaScript compiler. Babel uses @babel / parser to parse the code, and the input js code string generates an AST according to the ESTree specification. The parser used by Babel is babylon.
[0090] The transformation step receives the AST and traverses it, during which operations such as adding, updating, and removing nodes are performed. This is the most complex process in Babel or other compilers. Babel provides the @babel / traverse method to maintain the overall state of the AST tree and can complete operations such as replacing, deleting, or adding nodes. The parameters of this method are the original AST and custom transformation rules, and the return result is the transformed AST. Mainly, strings are inserted through the code transformation API of babel-core.
[0091] Import the API, call the API, and process the input and output streams; process the input stream, determine the node type, and perform replacements; test the declaration variables let, const, and arrow functions; finally, obtain the new code after transformation.
[0092] In the above code transformation method, obtain the initial code containing the first language code; generate an abstract syntax tree containing the first language nodes according to the initial code; call a function according to the language used to implement language conversion, and add second language nodes connected to the first language nodes in the abstract syntax tree to obtain a new abstract syntax tree; generate the target code according to the new abstract syntax tree. By converting the initial code into an abstract syntax tree, inserting the language call function as a new node into the abstract syntax tree, and then converting the new abstract syntax tree into new code, the new code can convert the first language code in the initial code into the second language code without additional copywriting translation and code conversion work, which can improve the code processing efficiency of multi-language conversion and thus improve the development efficiency of multi-language requirements.
[0093] In one embodiment, obtaining the initial code includes: determining the target code type to be transformed; traversing the code files in the target code repository to obtain the target code files belonging to the target code type; reading the target code files to obtain the initial code.
[0094] Optionally, when the initial code is JavaScript code, the terminal can use the node.js tool to traverse and scan all JavaScript code files in the current code repository, and read the file content into memory for subsequent parsing operations.
[0095] In this embodiment, automatically obtaining the initial code to be converted based on the code type can improve the processing efficiency of code conversion.
[0096] In one embodiment, an abstract syntax tree is generated according to the initial code; the abstract syntax tree includes first-language nodes corresponding to the first-language code, the first-language nodes include at least one language identification node, and language content nodes corresponding to each language identification node.
[0097] Furthermore, obtain the function call syntax element, function identification syntax element, and function name syntax element in the language call function, and generate second-language nodes according to the function call syntax element, function identification syntax element, and function name syntax element; the second-language nodes include function call nodes, function identification nodes, and function name nodes; insert a function call node at the upper layer of each language identification node in the abstract syntax tree; add a function identification node at the lower layer of each function call node, and add a function name node at the lower layer of each function identification node to obtain a new abstract syntax tree.
[0098] Finally, generate the target code according to the new abstract syntax tree.
[0099] Optionally, the terminal generates an AST syntax tree according to the initial Javascript code, as Figure 3 shown in generating the AST syntax tree json through the code, the first language is Chinese, as Figure 4 shown in the graphical display description corresponding to the syntax tree json, Figure 4 the Literal node in is the language identification node, and the "Hello" node and the "classmate" node are the language content nodes.
[0100] Such as Figure 5 shown, traverse the Chinese nodes according to the generated AST syntax tree. As Figure 6 shown, add the language call function t at the Chinese node to generate a new abstract syntax tree, and then convert the newly generated abstract syntax tree into a new JavaScript code. Figure 6Among them, the CallExpression node is a function call node, the Identifier node is a function identifier node, and the t node is a function name node. It can be seen that it is necessary to insert a CallExpression node at the upper layer of each Literal node in the abstract syntax tree, add an Identifier node at the lower layer of each CallExpression node, and add a t node at the lower layer of each Identifier node. Language content nodes such as the "Hello" node and the "classmate" node are kept at the lower layer of the Literal node. The above operations are performed for each first language node to obtain a new abstract syntax tree.
[0101] Finally, as Figure 6 shown, according to the new syntax tree, corresponding code is generated to obtain the target code after the language conversion of the initial code.
[0102] In this embodiment, by converting the initial code into an abstract syntax tree, inserting the language call function as a new node into the abstract syntax tree, and then converting the new abstract syntax tree into new code, the new code can convert the first language code in the initial code into the second language code without the need for additional copywriting translation and code conversion work, which can improve the code processing efficiency of multilingual conversion and thus improve the development efficiency of multilingual requirements.
[0103] In one embodiment, the method further includes: obtaining the first language data in the new abstract syntax tree according to each first language node in the new abstract syntax tree. Invoking the machine translation interface corresponding to the first language code to perform translation processing on the first language data to obtain a translation file; generating a code conversion result according to the target code and the translation file.
[0104] Among them, the machine translation interface, also known as the machine translation API, includes but is not limited to machine translation interfaces such as Google, Tencent, Youdao, and Baidu.
[0105] Optionally, when the initial code is JavaScript code, obtain the first language data in the new abstract syntax tree according to each first language node in the new abstract syntax tree, and output the first language data to the memory. Then invoke the machine translation interface corresponding to the first language code to perform translation processing on the first language data to obtain a translation file; write the translation file into a json data file, as Figure 7 shown; generate a new code file according to the target code; integrate the new code file and the json data file to obtain a code conversion result.
[0106] In this embodiment, by calling the machine translation interface to translate the first language data file, writing the translation file into a JSON data file, and integrating the new code file, the development efficiency for multi-language requirements can be improved.
[0107] In one embodiment, a JavaScript code conversion method includes:
[0108] Determine the target code type to be converted; traverse the JavaScript code files in the target code repository to obtain the target JavaScript code files belonging to the target code type; read the target JavaScript code files to obtain the JavaScript code. The JavaScript code includes the first language code.
[0109] Generate an abstract syntax tree according to the JavaScript code; the abstract syntax tree includes the first language nodes corresponding to the first language code, the first language nodes include at least one language identification node, and the language content nodes corresponding to each language identification node.
[0110] Obtain the function call syntax element, function identification syntax element, and function name syntax element in the language call function, and generate the second language nodes according to the function call syntax element, function identification syntax element, and function name syntax element; the second language nodes include function call nodes, function identification nodes, and function name nodes; insert a function call node at the upper layer of each language identification node in the abstract syntax tree; add a function identification node at the lower layer of each function call node, and add a function name node at the lower layer of each function identification node to obtain a new abstract syntax tree.
[0111] Generate the JavaScript code after language conversion according to the new abstract syntax tree.
[0112] Obtain the first language data in the new abstract syntax tree according to each first language node in the new abstract syntax tree.
[0113] Call the machine translation interface corresponding to the first language code to perform translation processing on the first language data to obtain a translation file; write the translation file into a JSON data file; generate a new JavaScript code file according to the JavaScript code after language conversion; integrate the new JavaScript code file and the JSON data file to obtain the code conversion result.
[0114] It should be understood that although the steps in the flowcharts involved in the above-described embodiments are shown in sequence according to the indications of the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless there is a clear indication in this article, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-described embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily executed at the same moment, but can be executed at different moments. The execution order of these steps or stages is not necessarily sequential, but can be executed alternately or in rotation with at least a part of other steps or steps or stages in other steps.
[0115] Based on the same inventive concept, an embodiment of the present application further provides a code conversion device for implementing the above-mentioned code conversion method. The solution provided by this device for solving problems is similar to the solution described in the above method. Therefore, the specific limitations in one or more embodiments of the following code conversion device can refer to the limitations on the code conversion method in the above text, and will not be elaborated here.
[0116] In an exemplary embodiment, as Figure 8 shown, a code conversion device 800 is provided, including: an acquisition module 801, a generation module 802, an addition module 803, and a conversion module 804, where:
[0117] The acquisition module 801 is used to acquire an initial code; the initial code includes a first language code.
[0118] The generation module 802 is used to generate an abstract syntax tree according to the initial code; the abstract syntax tree includes first language nodes corresponding to the first language code.
[0119] The addition module 803 is used to add second language nodes connected to the first language nodes in the abstract syntax tree according to the language call function for implementing language conversion, to obtain a new abstract syntax tree.
[0120] The conversion module 804 is used to generate target code according to the new abstract syntax tree.
[0121] In one embodiment, the acquisition module 801 is further used to determine the type of target code to be converted; traverse the code files in the target code repository to obtain target code files belonging to the target code type; read the target code files to obtain the initial code.
[0122] In one embodiment, the first language node includes at least one language identification node and a language content node corresponding to each language identification node; the adding module 803 is further configured to obtain function call syntax elements, function identification syntax elements, and function name syntax elements in the language call function, and generate a second language node according to the function call syntax elements, function identification syntax elements, and function name syntax elements; the second language node includes a function call node, a function identification node, and a function name node; insert a function call node at the upper layer of each language identification node in the abstract syntax tree; add a function identification node at the lower layer of each function call node, and add a function name node at the lower layer of each function identification node to obtain a new abstract syntax tree.
[0123] In one embodiment, the conversion module 804 is further configured to obtain the first language data in the new abstract syntax tree according to each first language node in the new abstract syntax tree.
[0124] In one embodiment, the conversion module 804 is further configured to call a machine translation interface corresponding to the first language code, perform translation processing on the first language data to obtain a translation file; generate a code conversion result according to the target code and the translation file.
[0125] In one embodiment, the initial code is JavaScript code; the conversion module 804 is further configured to write the translation file into a json data file; generate a new code file according to the target code; integrate the new code file and the json data file to obtain a code conversion result.
[0126] Each module in the above code conversion device can be implemented in whole or in part by software, hardware, and their combination. The above modules can be embedded in or independent of the processor in the computer device in the form of hardware, or stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.
[0127] In an exemplary embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as Figure 9As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, abbreviated as I / O), and a communication interface. Among them, the processor, the memory, and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program, and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store language call function data. The input / output interface of the computer device is used to exchange information between the processor and external devices. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, it implements a code conversion method.
[0128] Those skilled in the art can understand that Figure 9 the structure shown in [figure reference] is only a block diagram of some structures related to the solution of this application, and does not constitute a limitation on the computer device to which the solution of this application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine some components, or have a different component layout.
[0129] In an exemplary embodiment, a computer device is provided, including a memory and a processor. A computer program is stored in the memory. When the processor executes the computer program, the following steps are implemented: obtaining an initial code; the initial code includes a first language code; generating an abstract syntax tree according to the initial code; the abstract syntax tree includes a first language node corresponding to the first language code; adding a second language node connected to the first language node in the abstract syntax tree according to a language call function for implementing language conversion to obtain a new abstract syntax tree; generating a target code according to the new abstract syntax tree.
[0130] In one embodiment, when the processor executes the computer program, the following steps are further implemented: determining the type of the target code to be converted; traversing the code files in the target code repository to obtain the target code files belonging to the target code type; reading the target code files to obtain the initial code.
[0131] In one embodiment, the first language node includes at least one language identification node and a language content node corresponding to each language identification node; when the processor executes the computer program, the following steps are further implemented: obtaining function call syntax elements, function identification syntax elements, and function name syntax elements in the language call function, and generating a second language node according to the function call syntax elements, function identification syntax elements, and function name syntax elements; the second language node includes a function call node, a function identification node, and a function name node; inserting a function call node at the upper layer of each language identification node in the abstract syntax tree; adding a function identification node at the lower layer of each function call node, and adding a function name node at the lower layer of each function identification node to obtain a new abstract syntax tree.
[0132] In one embodiment, when the processor executes the computer program, the following steps are further implemented: obtaining first language data in the new abstract syntax tree according to each first language node in the new abstract syntax tree.
[0133] In one embodiment, when the processor executes the computer program, the following steps are further implemented: calling a machine translation interface corresponding to the first language code to perform translation processing on the first language data to obtain a translation file; generating a code conversion result according to the target code and the translation file.
[0134] In one embodiment, the initial code is JavaScript code; when the processor executes the computer program, the following steps are further implemented: writing the translation file into a json data file; generating a new code file according to the target code; integrating the new code file and the json data file to obtain a code conversion result.
[0135] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the following steps are implemented: obtaining an initial code; the initial code includes a first language code; generating an abstract syntax tree according to the initial code; the abstract syntax tree includes first language nodes corresponding to the first language code; adding second language nodes connected to the first language nodes in the abstract syntax tree according to a language call function for implementing language conversion to obtain a new abstract syntax tree; generating a target code according to the new abstract syntax tree.
[0136] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: determining the type of the target code to be converted; traversing the code files in the target code repository to obtain target code files belonging to the target code type; reading the target code files to obtain the initial code.
[0137] In one embodiment, the first language node includes at least one language identification node and language content nodes corresponding to each language identification node; when the computer program is executed by a processor, the following steps are further implemented: obtaining function call syntax elements, function identification syntax elements, and function name syntax elements in the language call function, and generating a second language node according to the function call syntax elements, function identification syntax elements, and function name syntax elements; the second language node includes a function call node, a function identification node, and a function name node; inserting a function call node at the upper layer of each language identification node in the abstract syntax tree; adding a function identification node at the lower layer of each function call node, and adding a function name node at the lower layer of each function identification node to obtain a new abstract syntax tree.
[0138] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: obtaining first language data in the new abstract syntax tree according to each first language node in the new abstract syntax tree.
[0139] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: calling a machine translation interface corresponding to the first language code to perform translation processing on the first language data to obtain a translation file; generating a code conversion result according to the target code and the translation file.
[0140] In one embodiment, the initial code is JavaScript code; when the computer program is executed by a processor, the following steps are further implemented: writing the translation file into a json data file; generating a new code file according to the target code; integrating the new code file and the json data file to obtain a code conversion result.
[0141] In one embodiment, a computer program product is provided, including a computer program, which when executed by a processor implements the following steps: obtaining an initial code; the initial code includes a first language code; generating an abstract syntax tree according to the initial code; the abstract syntax tree includes first language nodes corresponding to the first language code; adding second language nodes connected to the first language nodes in the abstract syntax tree according to a language call function for implementing language conversion to obtain a new abstract syntax tree; generating a target code according to the new abstract syntax tree.
[0142] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: determining the type of the target code to be converted; traversing the code files in the target code repository to obtain target code files belonging to the target code type; reading the target code files to obtain the initial code.
[0143] In one embodiment, the first language node includes at least one language identification node and a language content node corresponding to each language identification node; when the computer program is executed by a processor, the following steps are further implemented: obtaining function call syntax elements, function identification syntax elements, and function name syntax elements in a language call function, and generating a second language node according to the function call syntax elements, function identification syntax elements, and function name syntax elements; the second language node includes a function call node, a function identification node, and a function name node; inserting a function call node at the upper layer of each language identification node in the abstract syntax tree; adding a function identification node at the lower layer of each function call node, and adding a function name node at the lower layer of each function identification node to obtain a new abstract syntax tree.
[0144] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: obtaining first language data in the new abstract syntax tree according to each first language node in the new abstract syntax tree.
[0145] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: calling a machine translation interface corresponding to the first language code to perform translation processing on the first language data to obtain a translation file; generating a code conversion result according to the target code and the translation file.
[0146] In one embodiment, the initial code is JavaScript code; when the computer program is executed by a processor, the following steps are further implemented: writing the translation file into a json data file; generating a new code file according to the target code; integrating the new code file and the json data file to obtain a code conversion result.
[0147] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use, and processing of relevant data need to comply with relevant regulations.
[0148] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above methods. Among them, any reference to a memory, database, or other medium used in the embodiments provided in the present application can include at least one of non-volatile and volatile memories. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The databases involved in the embodiments provided in the present application can include at least one of relational databases and non-relational databases. Non-relational databases can include distributed databases based on blockchain, etc., without limitation. The processors involved in the embodiments provided in the present application can be general-purpose processors, central processors, graphics processors, digital signal processors, programmable logic devices, data processing logics based on quantum computing, etc., without limitation.
[0149] The technical features of the above embodiments can be combined arbitrarily. For the sake of concise description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope recorded in this specification.
[0150] The above-described embodiments only represent several implementation manners of the present application. The description is relatively specific and detailed, but it should not be construed as a limitation on the patent scope of the present application. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present application, several modifications and improvements can still be made, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the appended claims.
Claims
1. A code conversion method, characterized in that, The method includes: Obtaining an initial code; the initial code includes a first language code; Generating an abstract syntax tree according to the initial code; the abstract syntax tree includes first language nodes corresponding to the first language code; Adding second language nodes connected to the first language nodes in the abstract syntax tree according to a language call function for implementing language conversion to obtain a new abstract syntax tree; Generating a target code according to the new abstract syntax tree.
2. The method according to claim 1, wherein The obtaining of the initial code includes: Determining the type of the target code to be converted; Traversing the code files in the target code repository to obtain target code files belonging to the target code type; Reading the target code files to obtain the initial code.
3. The method according to claim 1, wherein The first language nodes include at least one language identification node and language content nodes corresponding to each language identification node; the adding of second language nodes connected to the first language nodes in the abstract syntax tree according to a language call function for implementing language conversion to obtain a new abstract syntax tree includes: Obtaining function call syntax elements, function identification syntax elements, and function name syntax elements in the language call function, and generating the second language nodes according to the function call syntax elements, the function identification syntax elements, and the function name syntax elements; the second language nodes include function call nodes, function identification nodes, and function name nodes; Inserting a function call node at the upper layer of each language identification node in the abstract syntax tree; Adding a function identification node at the lower layer of each function call node, and adding a function name node at the lower layer of each function identification node to obtain the new abstract syntax tree.
4. The method according to claim 1, characterized in that, The method further includes: Obtaining first language data in the new abstract syntax tree according to each first language node in the new abstract syntax tree.
5. The method according to claim 4, wherein The method further includes: Invoking a machine translation interface corresponding to the first language code to perform translation processing on the first language data to obtain a translation file; Generating a code conversion result according to the target code and the translation file.
6. The method according to claim 5, wherein The initial code is JavaScript code; the generating of a code conversion result according to the target code and the translation file includes: Writing the translation file into a json data file; Generating a new code file according to the target code; Integrating the new code file and the json data file to obtain the code conversion result.
7. A code conversion device, characterized in that, The apparatus includes: An obtaining module, configured to obtain an initial code; the initial code includes a first language code; A generating module, configured to generate an abstract syntax tree according to the initial code; the abstract syntax tree includes first language nodes corresponding to the first language code; An adding module, configured to add second language nodes connected to the first language nodes in the abstract syntax tree according to a language call function for implementing language conversion to obtain a new abstract syntax tree; A conversion module, configured to generate a target code according to the new abstract syntax tree.
8. A computer device, comprising a memory and a processor, the memory storing a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the steps of the method according to any one of claims 1 to 6.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by the processor, it implements the steps of the method according to any one of claims 1 to 6.