Code conversion method and device, medium, electronic equipment and program product

By acquiring source code files and processing them into an abstract syntax tree, and then using hook functions to traverse and attach processing functions, the inefficiency and performance loss in cross-language code conversion are solved, achieving efficient code conversion.

CN121433666APending Publication Date: 2026-01-30BEIJING ZITIAO NETWORK TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511524999.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-10-23
Publication Date
2026-01-30

AI Technical Summary

Technical Problem

Cross-language code conversion involves issues such as data type conversion, calling conventions, and exception handling, leading to inefficiency and performance degradation.

Method used

By obtaining the source code file, processing it into an abstract syntax tree, traversing and attaching the target processing function using pre-registered hook functions, and performing printing operations to generate the target file.

Benefits of technology

It improves code conversion efficiency and reduces device performance loss.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121433666A_ABST
    Figure CN121433666A_ABST
Patent Text Reader

Abstract

The invention discloses a code conversion method and device, a medium, electronic equipment and a program product, and relates to the technical field of computers, and the method comprises the following steps: obtaining a source code file, and processing the source code file to obtain a plurality of abstract syntax trees; traversing each node in the abstract syntax tree according to a pre-registered hook function, and mounting a target processing function for a target node meeting a specified condition; printing operation is executed on the abstract syntax tree on which the target processing function is mounted, a target file is obtained, and the printing operation is used for executing the target processing function mounted on the abstract syntax tree. According to the method, the mounting operation and the printing operation are executed on the abstract syntax tree obtained through analysis, so that the code conversion efficiency can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present disclosure relates to the technical field of computer, in particular, to a code conversion method and device, medium, electronic equipment and program product. BACKGROUND

[0002] With the development of technology, the code developed by programming language usually needs to run on different clients, that is, a set of code needs to run on multiple different clients. However, there are many problems in cross-language calling, such as data type conversion, calling convention, exception handling and the like. Therefore, how to better realize the conversion of cross-language code is a technical problem to be solved. SUMMARY

[0003] This summary is provided to introduce a selection of concepts that are further described below in the detailed description. This summary is not intended to identify key or essential features of the claimed subject matter, nor is it intended to be used to limit the scope of the claimed subject matter.

[0004] In a first aspect, the present disclosure provides a code conversion method, the method comprising: obtaining a source code file and processing the source code file to obtain a plurality of abstract syntax trees; traversing each node in the abstract syntax tree according to a pre-registered hook function, and mounting a target processing function for a target node meeting a specified condition; performing a printing operation on the abstract syntax tree on which the target processing function is mounted to obtain a target file, the printing operation being used to execute the target processing function mounted on the abstract syntax tree.

[0005] In a second aspect, the present disclosure provides a code conversion device, the device comprising: an obtaining module configured to obtain a source code file and process the source code file to obtain a plurality of abstract syntax trees; a mounting module configured to traverse each node in the abstract syntax tree according to a pre-registered hook function, and mount a target processing function for a target node meeting a specified condition; an executing module configured to perform a printing operation on the abstract syntax tree on which the target processing function is mounted to obtain a target file, the printing operation being used to execute the target processing function mounted on the abstract syntax tree.

[0006] In a third aspect, the present disclosure provides a computer-readable medium having a computer program stored thereon, the computer program being executed by a processing device to implement the steps of the method of the first aspect.

[0007] In a fourth aspect, the present disclosure provides an electronic device, comprising: a storage device having stored thereon a computer program; a processing device configured to execute the computer program in the storage device to implement the steps of the method of the first aspect.

[0008] In a fifth aspect, the present disclosure provides a computer program product comprising a computer program which, when executed by a processor, implements the steps of the method of the first aspect.

[0009] By the above technical solution, after the source code file is acquired and processed to obtain the abstract syntax tree, each node in the abstract syntax tree is traversed according to the pre-registered hook function, the target node meeting the specified condition can be filtered in the process of traversing the abstract syntax tree, and the mounting operation is performed on the filtered target node, that is, the target processing function is mapped to the target node, and on this basis, the printing operation is performed on the abstract syntax tree on which the target processing function is mounted, to obtain the target file, wherein the printing operation is used to execute the target processing function mounted on the abstract syntax tree, and by performing the mounting operation and the printing operation, the present embodiment can not only improve the efficiency of code translation, but also reduce the performance loss of the device.

[0010] Other features and advantages of the present disclosure will be described in detail in the following detailed description. BRIEF DESCRIPTION OF DRAWINGS

[0011] The above and other features, advantages, and aspects of embodiments of the present disclosure will become more apparent by describing in detail exemplary embodiments thereof with reference to the attached drawings in which: Figure 1 is a flowchart of a code conversion method according to an embodiment of the present disclosure.

[0012] Figure 2 is an example diagram of an abstract syntax tree in a code conversion method according to an embodiment of the present disclosure.

[0013] Figure 3 is an example diagram of hook function definition in a code conversion method according to an embodiment of the present disclosure.

[0014] Figure 4 is an example diagram of a default processing function in a code conversion method according to an embodiment of the present disclosure.

[0015] Figure 5 is an example diagram of traversing each node in an abstract syntax tree in a code conversion method according to an embodiment of the present disclosure.

[0016] Figure 6 This is an example diagram of source code files and target files in a code conversion method according to an embodiment of this disclosure.

[0017] Figure 7 This is a specific example diagram illustrating the acquisition of a target file in a code conversion method according to an embodiment of this disclosure.

[0018] Figure 8 This is an example diagram illustrating the mounting operation performed on an abstract syntax tree in a code transformation method according to an embodiment of this disclosure.

[0019] Figure 9 This is an example diagram illustrating the printing operation performed on an abstract syntax tree in a code transformation method according to an embodiment of this disclosure.

[0020] Figure 10 This is a block diagram of a code conversion apparatus according to an embodiment of the present disclosure.

[0021] Figure 11 This is a schematic diagram of the structure of an electronic device according to an embodiment of the present disclosure. Detailed Implementation

[0022] Embodiments of this disclosure will now be described in more detail with reference to the accompanying drawings. While some embodiments of this disclosure are shown in the drawings, it should be understood that this disclosure can be implemented in various forms and should not be construed as limited to the embodiments set forth herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of this disclosure. It should be understood that the accompanying drawings and embodiments of this disclosure are for illustrative purposes only and are not intended to limit the scope of protection of this disclosure.

[0023] It should be understood that the steps described in the method embodiments of this disclosure may be performed in different orders and / or in parallel. Furthermore, the method embodiments may include additional steps and / or omit the steps shown. The scope of this disclosure is not limited in this respect.

[0024] The term "comprising" and its variations as used herein are open-ended inclusions, meaning "including but not limited to". The term "based on" means "at least partially based on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". Definitions of other terms will be given in the description below.

[0025] It should be noted that the concepts of "first" and "second" mentioned in this disclosure are used only to distinguish different devices, modules or units, and are not used to limit the order of functions performed by these devices, modules or units or their interdependencies.

[0026] It should be noted that the terms "a" and "a plurality of" used in this disclosure are illustrative rather than restrictive, and those skilled in the art should understand that, unless otherwise expressly indicated in the context, they should be understood as "one or more".

[0027] The names of messages or information exchanged between multiple devices in the embodiments of this disclosure are for illustrative purposes only and are not intended to limit the scope of such messages or information.

[0028] It is understood that before using the technical solutions disclosed in the various embodiments of this disclosure, users should be informed of the types, scope of use, and usage scenarios of the personal information involved in this disclosure in an appropriate manner in accordance with relevant laws and regulations, and user authorization should be obtained.

[0029] For example, upon receiving a user's active request, a prompt message is sent to the user to explicitly inform them that the requested operation will require the acquisition and use of the user's personal information. This allows the user to independently choose whether to provide personal information to the software or hardware, such as the electronic device, application, server, or storage medium performing the operations of this disclosed technical solution, based on the prompt message.

[0030] As an optional but non-limiting implementation, in response to a user's active request, sending a prompt message to the user can be done via a pop-up window, where the prompt message can be presented in text format. Furthermore, the pop-up window can also include a selection control allowing the user to choose "agree" or "disagree" to provide personal information to the electronic device.

[0031] It is understood that the above notification and user authorization process are merely illustrative and do not constitute a limitation on the implementation of this disclosure. Other methods that comply with relevant laws and regulations may also be applied to the implementation of this disclosure.

[0032] Meanwhile, it is understood that the data involved in this technical solution (including but not limited to the data itself, the acquisition or use of the data) shall comply with the requirements of relevant laws, regulations and related provisions.

[0033] The following description, in conjunction with the accompanying drawings, provides further explanation and clarification of this disclosure.

[0034] Figure 1This is a flowchart illustrating a code conversion method according to an embodiment of the present disclosure. This code conversion method can be applied to electronic devices with processing capabilities, such as terminals or servers. Furthermore, the code conversion method can be executed by a code conversion device, which can be implemented by software and / or hardware, and the software and / or hardware can be configured in the electronic device. (Refer to...) Figure 1 The code conversion method may include the following steps.

[0035] In step S110, the source code file is obtained and processed to obtain multiple abstract syntax trees.

[0036] In this embodiment of the disclosure, the source code file can be a file containing source code corresponding to the target project. For example, the source code can be RTS code, Java code, etc., and the source code can be generated by calling a specified consumable header file. Here, the source code file can be obtained based on the configuration file corresponding to the target project, and the configuration file can include specified fields.

[0037] For example, in the process of obtaining source code files, this embodiment of the disclosure can read a specified field defined in the configuration file to obtain the compilation entry file corresponding to the source code through the specified field, such as the entry field. The compilation entry file can be obtained by reading the specified field defined in the configuration file. Based on this, all the dependent files associated with it are loaded according to the compilation entry file, and these dependent files and the compilation entry file can then be used as source code files.

[0038] Optionally, after obtaining the source code file, this embodiment of the disclosure can process the source code file to obtain multiple abstract syntax trees (Abstract Syntax Trees). That is, this embodiment of the disclosure can process the compilation entry file and multiple dependent files separately to obtain multiple abstract syntax trees. Each abstract syntax tree can include multiple nodes, and each node of the abstract syntax tree can be used to represent a specific element in the code, such as an identifier, function declaration, numeric literal, etc. In addition, each node has a corresponding type.

[0039] To better understand the abstract syntax tree corresponding to the source code file, this disclosure provides the following embodiments: Figure 2 The example diagram of the abstract syntax tree shown is as follows. Figure 2 The abstract syntax tree shown may include a first node 201, a second node 202, and a third node 203. As described above, each node in the abstract syntax tree can correspond to attribute information, which may include type information, numerical information, positional information, and structural relationship information, etc.

[0040] As can be seen, different nodes correspond to their own type information. For example, the first node 201 is of type VariableDeclaration, the second node 202 is of type Identifier, and the third node 203 is of type NumericLiteral. In addition, nodes in the abstract syntax tree can correspond to other attribute values, such as the node's start position, end position, node identifier, and transition identifier.

[0041] For example, in the process of processing source code files, this embodiment of the disclosure can first parse the source code file to obtain multiple initial syntax trees (AST trees). These multiple initial syntax trees can be contained within a sourcefile, which can be an object stored in memory as an abstract syntax tree after parsing plain text. In other words, parsing the source code file can yield multiple source file objects (sourcefiles). Each source file object can include file content, abstract syntax tree node information, etc., meaning a source file object can be an object representing a single source file and its complete AST.

[0042] Based on this, embodiments of this disclosure can perform a type checking operation (TypeCheck) on each initial syntax tree obtained through parsing to obtain multiple abstract syntax trees. Here, the type checking operation can also be called type inference, which is mainly used to determine the type of each node / variable in the initial syntax tree and associate the abstract syntax trees of each file. Based on this association, type content can be supplemented for nodes that lack type information.

[0043] In addition, type checking operations can also be used to verify the correctness of type usage in source code and to catch type errors. For example, in this embodiment of the disclosure, type checking operations can be used to check whether variable assignments, function calls, etc., in the abstract syntax tree conform to type declarations. In this embodiment of the disclosure, the multiple abstract syntax trees obtained through type checking operations can also be referred to as a compilation context set (program).

[0044] In step S120, each node in the abstract syntax tree is traversed according to the pre-registered hook functions, and the target processing function is attached to the target node that meets the specified conditions.

[0045] As an optional approach, after obtaining multiple abstract syntax trees (APIs), embodiments of this disclosure can traverse each node in each API and perform a mounting operation on target nodes that meet specified conditions. The mounting operation can be used to mount target processing functions to target nodes that meet the specified conditions. During this process, embodiments of this disclosure can match node attributes with pre-registered hook functions to achieve traversal of each node in the API. For example, node attributes may include the node's type.

[0046] Here, a hook can be registered with a specific type of AST Node. When the compiler encounters a Node of this type, the hook can be executed. Furthermore, when a hook is executed, the compiler can further examine and filter the affected Node. If it determines that further processing is needed, it can attach side effects to that Node, i.e., construct a mapping of the target processing function.

[0047] In other words, embodiments of this disclosure can traverse each node in the abstract syntax tree according to pre-registered hook functions, and attach target processing functions to target nodes that meet specified conditions. Here, attaching target processing functions to target nodes that meet specified conditions can be done by adding mapping information between target nodes and target processing functions, that is, establishing an array mapping of nodes to corresponding processing functions. For example, embodiments of this disclosure can establish a map mapping between target nodes and target processing functions, map = new Map<Node,effect> ().

[0048] In this embodiment of the disclosure, the pre-registered hook function can be a hook function that the user registers specifically based on the node type. That is, the user can register custom processing logic for one or more specific types of AST nodes. When the compiler encounters a node that matches the registered type during the AST traversal, it can execute the corresponding hook function.

[0049] To better understand the definition of hook functions, embodiments of this disclosure provide, as follows: Figure 3 The example diagram shown illustrates that embodiments of this disclosure can be achieved through, as described above. Figure 3 The method shown defines a hook in the compiler and uses the hook function to register a hook for the type ts.SyntaxKind.NumericLiteral, which can be applied to nodes of the numeric literal type.

[0050] As an optional approach, during the traversal of each node in the abstract syntax tree, this embodiment of the disclosure can obtain the type of each node. Based on this, it can determine whether there is a hook function among the pre-registered hook functions that matches the node type. If there is a hook function among the pre-registered hook functions that matches the node type, then this hook function can be used as the target hook function, and the target hook function can be attached to the corresponding target node. As described above, the pre-registered hook functions can be registered for specific types of AST nodes. Therefore, each hook function has type information, meaning that the target hook function that matches the node can be found among the pre-registered hook functions based on the node type.

[0051] In other words, for any node in the abstract syntax tree, if the node's type matches a target hook function in the pre-registered hook functions, then this embodiment of the disclosure can use that node as a target node and attach a target processing function to the target node. Here, the target processing function can be an instruction used to define how to modify the node's output logic or behavior; it can also be called a target side effect. Attaching a target processing function to a target node can be equivalent to attaching a target side effect to the target node.

[0052] For example, target processing functions (effects / side effects) can include `useReplace` (code replacement), `useRemove` (code removal), and `useBefore` (insertion before). `useReplace` is used to find specific code and replace it with new code, such as replacing an old API (Application Programming Interface) call with a new one; `useRemove` is used to remove specific code snippets, such as removing all debugging code; and `useBefore` is used to insert new code before specific code, such as inserting logs before the start of a function. Additionally, target processing functions can also include `useAfter` (insertion after), `useWrap` (code wrapping), `useTransform` (complex transformation), and `useVisit` (traversal control), etc.

[0053] Optionally, during the traversal of each node in the abstract syntax tree based on the pre-registered hook functions, this embodiment of the disclosure can first determine whether there is a hook function among the pre-registered multiple hook functions that matches the current node type. If there is a hook function among the pre-registered multiple hook functions that matches the current node type, then it can continue to determine whether the node's value satisfies the mounting condition. If it is determined that the node's value satisfies the mounting condition, then this embodiment of the disclosure can mount the target processing function onto the target node. For example, if the current node's type is a first type, and it is determined by searching that there is a target hook function among the pre-registered hook functions that matches the first type, then a mapping relationship between the target hook function and the current node can be constructed.

[0054] In addition, the target hook function may include mounting conditions (filtering conditions). When the value of the current node meets the mounting conditions, the embodiments of this disclosure can create a mapping relationship between the target node and the target processing function.

[0055] For example, if the first type of the current node is "NumericLiteral", then by searching and identifying multiple pre-registered hook functions, the following can be determined: Figure 3 The target hook function shown matches the first type, based on Figure 3 It can be seen that the mounting condition for the target hook function is: Node.getText() !== '0'. When the value of the target node is 0, it is determined that the mounting condition is met, and the mounting operation can be performed, that is, the mapping relationship between the target node and the target processing function can be created. Here, the mounting operation is used to mount side effects (processing functions).

[0056] Optionally, if the value of a node does not meet the mounting condition, the mounting operation is not performed; that is, the return operation can be performed directly in this embodiment. For example, when a NumericLiteral node with a value of 10 is encountered, the hook will also be executed. Based on the filter condition "Node.getText() !== '0'" in the hook, it is determined that the value 10 of the node meets the condition. At this time, the return operation can be performed directly, that is, no side effects are mounted.

[0057] It should be noted that a target node can correspond to one target hook function or multiple target hook functions. When a target node corresponds to one target hook function and the value of the target node does not meet the mounting conditions, there is no corresponding processing function for the node, that is, no mounting operation is performed for the node. In subsequent traversals of the node, the default processing function can be executed. When a target node corresponds to multiple target mounting functions, this embodiment of the disclosure can traverse the mounting conditions of each target mounting function.

[0058] As an example, a target node corresponds to two target hook functions, namely the first target hook function and the second target hook function. The value of the target node does not meet the first mounting condition of the first target hook function. After the return operation is executed, it enters the second target hook function. Through matching, it is determined that the value of the target node meets the second mounting condition of the second target hook function. At this time, the second target processing function of the second hook function can be mounted on the target node, that is, a mapping relationship between the target node and the second target processing function can be created.

[0059] In addition, when there are multiple target hook functions that meet the mounting conditions, and the node values ​​meet the mounting conditions of each hook function, the embodiments of this disclosure can mount the target processing functions corresponding to these target hook functions onto the target node.

[0060] As another example, a target node corresponds to two target hook functions, namely the first target hook function and the second target hook function. The value of the target node satisfies not only the first mounting condition of the first target hook function but also the second mounting condition of the second target hook function. In this case, the first target processing function of the first hook function and the second target processing function of the second hook function can both be mounted onto the target node. That is, two mapping relationships can be created for the target node: a first mapping relationship between the target node and the first processing function, and a second mapping relationship between the target node and the second processing function.

[0061] Furthermore, when there are multiple target hook functions that meet the mounting conditions, and the node's value does not meet the mounting conditions of each hook function, this embodiment of the disclosure can directly perform a return operation, meaning that the node does not correspond to any target processing function. In this case, this embodiment of the disclosure does not perform a mounting operation on the node. Subsequently, when traversing the node a second time, since no corresponding mapping relationship has been created, the default processing function (default printing logic) is directly executed.

[0062] As another example, a target node corresponds to two target hook functions, namely the first target hook function and the second target hook function. The value of the target node does not satisfy the first mounting condition of the first target hook function, meaning the node's value is not the Node of interest to the current hook; therefore, a return operation is performed directly without further processing. Based on this, this embodiment can iterate through other hook functions under the target node. During this process, if it is determined that the target node's value also does not satisfy the second mounting condition of the second target hook function, a return operation is similarly performed without any mounting operation. In other words, no mapping relationship is created for this node.

[0063] As an alternative approach, if the node type does not match any of the pre-registered hook functions, this embodiment of the disclosure can obtain the default processing function corresponding to that type. That is, if no target hook function matching the node type exists among the pre-registered hook functions, this embodiment of the disclosure can obtain the default processing function corresponding to that node type. Here, the default processing function can also be called default output logic or default printing logic. For example, the default output logic can be used to replace the output content of a node, delete a node, or insert content before or after a node, etc.

[0064] Each node type can have its own default processing function, and different node types may have different default processing functions. To better understand the default processing function, embodiments of this disclosure provide, for example... Figure 4 The example diagram shown is as follows. Figure 4 It defines how to handle different types of nodes by default. For example, for a node of type Identifier, its text (i.e., the name of the identifier) ​​can be directly retrieved and appended to the output.

[0065] As an example, there is a variable declaration in the source code: let myVar = 1. When the compiler traverses the Identifier node myVar in the abstract syntax tree, it does not find its corresponding hook function (side effect). At this time, the embodiment of this disclosure can obtain the default processing function of the type pair, that is, the default call is "append("myVar")", so that "myVar" is retained in the output.

[0066] By introducing default processing functions, embodiments of this disclosure can ensure that a node can be correctly converted to its default text representation even if no hooks are registered or any processing functions (side effects) are attached for a node of a particular abstract syntax tree. This allows users to focus on defining the specific logic that needs to be modified when implementing code conversion, without having to write output logic for all node types from scratch. This greatly simplifies the development complexity of the transpatch, thereby improving the robustness and maintainability of the system.

[0067] In step S130, a printing operation is performed on the abstract syntax tree that mounts the target processing function to obtain the target file.

[0068] As described above, during the first traversal of the abstract syntax tree, this embodiment can attach target processing functions to target nodes that meet specified conditions, that is, create a mapping relationship between target nodes and corresponding target processing functions. After the first traversal of each abstract syntax tree is completed, this embodiment can perform a second traversal of the abstract syntax tree, through which the processing functions can be printed.

[0069] In other words, after performing the first traversal of each abstract syntax tree (AST), that is, after performing the mounting operation on the target nodes that meet the specified conditions in each AST, this embodiment of the disclosure can perform a printing operation on the AST that mounts the target processing function to obtain the target file. The printing operation can be used to execute the target processing function mounted on the AST.

[0070] For example, in this embodiment of the disclosure, a second traversal can be performed on each node in the abstract syntax tree that carries the target processing function to determine whether the node carries the target processing function. If it is determined that the node carries the target processing function, this embodiment of the disclosure can execute the corresponding target processing function. Conversely, if the node in the abstract syntax tree does not carry the target processing function, this embodiment of the disclosure can execute the default processing function.

[0071] For a detailed explanation of the traversal process for each node in the abstract syntax tree, please refer to [link to documentation / reference]. Figure 5 ,based on Figure 5 As can be seen, during the secondary traversal of the abstract syntax tree, the embodiments of this disclosure can determine whether a node has a corresponding target processing function. If a corresponding target processing function has been created, it can be executed, i.e., "call effect". Conversely, if no corresponding mapping relationship has been created on the node, i.e., no target processing function is attached, the default processing function can be executed, i.e., the "asthandlers" function is called. In this way, a secondary traversal of each node can be achieved.

[0072] As described above, a source file is a file containing source language code, while a target file can be a user-specified file containing target language code for conversion. Based on the embodiments of this disclosure, the source file can be parsed and type-checked to obtain an abstract syntax tree (AST). Then, the AST is traversed twice to obtain the target file. The comparison between the source file and the target file can be as follows: Figure 6 As shown, Figure 6 The first file 601 is the source code file, and the second file 602 is the target file obtained by conversion in the above manner. By comparing the first file 601 and the second file 602, it can be seen that the code conversion can be achieved accurately and efficiently based on the embodiments of this disclosure.

[0073] It should be noted that the execution of the default processing function in this embodiment includes two cases: one is that there is no hook function matching the node type among the pre-registered hook functions, and the other is that there is a hook function matching the node type among the pre-registered hook functions, but the node's value does not meet the hook function's mounting conditions. The default processing function can be executed if either of these two cases is met.

[0074] As a specific implementation method, such as Figure 7 As shown, for a source code project, such as an RTS code project, this embodiment of the disclosure can obtain a specified consumable header file based on a specified type of tool, such as obtaining an RTS consumable header file (.d.ts) based on the SalamanderType tool. Source code (RTS code) can be generated from this header file. The source code project can correspond to a configuration file, which can be rtsconfig.json. Based on this, this embodiment of the disclosure can obtain the source code file.

[0075] Specifically, the specified fields defined in the configuration file are read to obtain the compilation entry file. These specified fields can be `entry` fields. Then, based on the entry file, all dependent files can be loaded. These dependent files and the entry file together form the source code file. After obtaining the entry file and dependent files, this embodiment can perform a parsing operation to obtain multiple source file objects (sourcefiles) including AST trees, i.e., multiple initial syntax trees. For each source file object, this embodiment can perform a type checking operation (TypeCheck) to obtain multiple compilation context sets (Programs), which can be the aforementioned abstract syntax trees. Based on this, this embodiment can perform an initial traversal on each source file object (abstract syntax tree).

[0076] Based on the above description, the first traversal of each abstract syntax tree (source file) can be performed by hooking each abstract syntax tree. The hooking process can be as follows: Figure 8 As shown, based on Figure 8 As can be seen, during the first traversal of the abstract syntax tree, this embodiment of the present disclosure can determine whether there is a hook function among the pre-registered hook functions that corresponds to the current node type. That is, it searches for a hook function that matches the current node type among all registered hook functions. After the determination of the current node is completed, this embodiment of the present disclosure can traverse the next node. For each hook, this embodiment of the present disclosure can determine whether to execute side effects (processing functions) such as useBefore and useAfter, according to the actual situation. It can be seen that by performing the first traversal of the abstract syntax tree, this embodiment of the present disclosure can collect all its corresponding side effects. In other words, after traversing the entire abstract syntax tree, the compiler can collect the side effects corresponding to all nodes.

[0077] Based on this, embodiments of this disclosure can perform effect printing on each abstract syntax tree (source file), where effect printing can be the execution of a target processing function. Here, the effect printing process can be as follows: Figure 9 As shown, based on Figure 9 As can be seen, during the second traversal of the abstract syntax tree, this embodiment can determine whether the currently traversed node has side effects, that is, whether the current node has a corresponding processing function (effect). If it is determined that the current node has side effects (target processing function), then all side effects corresponding to that node are executed. Here, the target processing function can be mounted during the first traversal of the abstract syntax tree. Conversely, if the current node does not have corresponding side effects, this embodiment executes the default printing logic (default processing function). After the printing operation is performed on the current node, this embodiment can traverse the next node until each node has been traversed, at which point the traversal of the abstract syntax tree ends.

[0078] After the printing operation is completed, the target file can be obtained in this embodiment of the disclosure. The target file can be a Swift file. Based on this, the embodiment of the disclosure can execute a script to automatically synchronize, such as synchronizing the target file to the Xcode project in the iOS development environment, so as to compile and package Xcode. In this way, the target file can be compiled into an executable file or application package and applied to the application of the specified operating system, such as applying it to the iOS APP.

[0079] It should be noted that the embodiments of this disclosure can convert source code into one language or convert source code into multiple languages ​​simultaneously. For example, the method proposed in the embodiments of this disclosure can convert source code files in a first language into target files in a second language, and can simultaneously convert source code files in a first language into target files in a third language. The specific languages ​​to which the source code files are converted are not explicitly limited here, and can be selected according to the actual situation.

[0080] In this embodiment, after obtaining the source code file and processing it to obtain an abstract syntax tree (AST), each node in the AST is traversed according to pre-registered hook functions. During the traversal of the AST, target nodes that meet specified conditions can be filtered, and a mounting operation is performed on the filtered target nodes, that is, the target processing function is mapped to the target node. Based on this, a printing operation is performed on the AST with the mounted target processing function to obtain the target file. The printing operation is used to execute the target processing function mounted on the AST. By performing the mounting operation and the printing operation, this embodiment can not only improve the efficiency of code translation, but also reduce the performance loss of the device.

[0081] Based on the same inventive concept, this disclosure also provides a code conversion device. Figure 10 This is a block diagram illustrating a code conversion apparatus 1000 according to an exemplary embodiment, such as... Figure 10 As shown, the code conversion device 1000 may include an acquisition module 1010, a mounting module 1020, and an execution module 1030.

[0082] The acquisition module 1010 is configured to acquire source code files and process the source code files to obtain multiple abstract syntax trees; The mounting module 1020 is configured to traverse each node in the abstract syntax tree according to the pre-registered hook functions, and mount the target processing function for the target node that meets the specified conditions; The execution module 1030 is configured to perform a print operation on the abstract syntax tree on which the target processing function is mounted, to obtain a target file. The print operation is used to execute the target processing function mounted on the abstract syntax tree.

[0083] In some implementations, the mounting module 1020 may also be configured to, for any node in the abstract syntax tree, if the type of the node matches the target hook function in the pre-registered hook functions, then treat the node as a target node and mount the target processing function on the target node.

[0084] In some implementations, the mounting module 1020 may also be configured to obtain the value of the target node if the type of the node matches the target hook function in the pre-registered hook functions; and to mount the target processing function onto the target node if the value satisfies the mounting conditions.

[0085] In some embodiments, the code conversion device 1000 may further include: The return module is configured to not perform the mounting operation if the value does not meet the mounting conditions.

[0086] In some implementations, the acquisition module 1010 may also be configured to acquire the default processing function corresponding to the type if the type of the node does not match any of the pre-registered hook functions.

[0087] In some implementations, the execution module 1030 may also be configured to traverse each node in the abstract syntax tree on which the target processing function is attached, determine whether the target processing function is attached to the node, and execute the target processing function if the target processing function is attached to the node.

[0088] In some implementations, the execution module 1030 may also be configured to execute a default processing function if the target processing function is not mounted on the node.

[0089] In some implementations, the acquisition module 1010 may also be configured to parse the source code file to obtain an initial syntax tree; and perform a type checking operation on the initial syntax tree to obtain a plurality of abstract syntax trees.

[0090] In this embodiment, after obtaining the source code file and processing it to obtain an abstract syntax tree (AST), each node in the AST is traversed according to pre-registered hook functions. During the traversal of the AST, target nodes that meet specified conditions can be filtered, and a mounting operation is performed on the filtered target nodes, that is, the target processing function is mapped to the target node. Based on this, a printing operation is performed on the AST with the mounted target processing function to obtain the target file. The printing operation is used to execute the target processing function mounted on the AST. By performing the mounting operation and the printing operation, this embodiment can not only improve the efficiency of code translation, but also reduce the performance loss of the device.

[0091] The following is for reference. Figure 11 The diagram illustrates a structural schematic of an electronic device 1100 suitable for implementing embodiments of the present disclosure. Terminal devices in embodiments of the present disclosure may include, but are not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. Figure 11 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of the embodiments disclosed herein.

[0092] like Figure 11 As shown, electronic device 1100 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 1101, which can perform various appropriate actions and processes according to a program stored in read-only memory (ROM) 1102 or a program loaded from storage device 1108 into random access memory (RAM) 1103. The RAM 1103 also stores various programs and data required for the operation of electronic device 1100. The processing device 1101, ROM 1102, and RAM 1103 are interconnected via bus 1104. Input / output (I / O) interface 1105 is also connected to bus 1104.

[0093] Typically, the following devices can be connected to I / O interface 1105: input devices 1106 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 1107 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 1108 including, for example, magnetic tapes, hard disks, etc.; and communication devices 1109. Communication device 1109 allows electronic device 1100 to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 11 An electronic device 1100 with various devices is shown; however, it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed alternatively.

[0094] In particular, according to embodiments of this disclosure, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication device 1109, or installed from storage device 1108, or installed from ROM 1102. When the computer program is executed by processing device 1101, it performs the functions defined in the methods of embodiments of this disclosure.

[0095] It should be noted that the computer-readable medium described in this disclosure can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this disclosure, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this disclosure, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium can be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wires, optical fibers, RF (radio frequency), etc., or any suitable combination thereof.

[0096] In some implementations, the client can communicate using any currently known or future-developed network protocol such as HTTP (Hypertext Transfer Protocol) and can interconnect with digital data communication (e.g., communication networks) of any form or medium. Examples of communication networks include local area networks (“LANs”), wide area networks (“WANs”), the Internet (e.g., the Internet), and peer-to-peer networks (e.g., ad hoc peer-to-peer networks), as well as any currently known or future-developed networks.

[0097] The aforementioned computer-readable medium may be included in the aforementioned electronic device; or it may exist independently and not assembled into the electronic device.

[0098] The aforementioned computer-readable medium carries one or more programs. When the aforementioned one or more programs are executed by the electronic device, the electronic device causes the following: to acquire a source code file and process the source code file to obtain multiple abstract syntax trees; to traverse each node in the abstract syntax trees according to pre-registered hook functions, and to attach target processing functions to target nodes that meet specified conditions; and to perform a printing operation on the abstract syntax trees on which the target processing functions are attached, thereby obtaining a target file, wherein the printing operation is used to execute the target processing functions attached to the abstract syntax trees.

[0099] Computer program code for performing the operations of this disclosure can be written in one or more programming languages ​​or a combination thereof, including but not limited to object-oriented programming languages ​​such as Java, Smalltalk, and C++, as well as conventional procedural programming languages ​​such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a local area network (LAN) or a wide area network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0100] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0101] The modules described in the embodiments of this disclosure can be implemented in software or in hardware. The name of a module does not necessarily limit the module itself; for example, an acquisition module can also be described as "a module that acquires source code files, processes the source code files, and obtains multiple abstract syntax trees".

[0102] The functions described above in this document can be performed at least in part by one or more hardware logic components. For example, exemplary types of hardware logic components that can be used, without limitation, include: field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), system-on-a-chip (SoCs), complex programmable logic devices (CPLDs), and so on.

[0103] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0104] The above description is merely a preferred embodiment of this disclosure and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of this disclosure is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described concept. For example, technical solutions formed by substituting the above features with (but not limited to) technical features disclosed in this disclosure that have similar functions.

[0105] Furthermore, while the operations are described in a specific order, this should not be construed as requiring these operations to be performed in the specific order shown or in a sequential order. In certain environments, multitasking and parallel processing may be advantageous. Similarly, while several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of this disclosure. Certain features described in the context of individual embodiments may also be implemented in combination in a single embodiment. Conversely, various features described in the context of a single embodiment may also be implemented individually or in any suitable sub-combination in multiple embodiments.

[0106] Although the subject matter has been described using language specific to structural features and / or methodological logic, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. Rather, the specific features and actions described above are merely illustrative forms of implementing the claims. Regarding the apparatus in the above embodiments, the specific manner in which the various modules perform their operations has been described in detail in the embodiments relating to the method, and will not be elaborated upon here.

Claims

1. A transcoding method, characterized by, The method comprises: obtaining a source code file and processing the source code file to obtain a plurality of abstract syntax trees; traversing each node in the abstract syntax tree according to a pre-registered hook function, and mounting a target processing function on a target node meeting a specified condition; performing a printing operation on the abstract syntax tree mounting the target processing function to obtain a target file, wherein the printing operation is used to execute the target processing function mounted on the abstract syntax tree.

2. The transcoding method of claim 1, wherein, The traversing each node in the abstract syntax tree according to a pre-registered hook function, and mounting a target processing function on a target node meeting a specified condition, comprises: for any node in the abstract syntax tree, if the type of the node matches a target hook function in the pre-registered hook function, the node is taken as a target node, and the target processing function is mounted on the target node.

3. The transcoding method of claim 2, wherein, The if the type of the node matches a target hook function in the pre-registered hook function, the node is taken as a target node, and the target processing function is mounted on the target node, comprises: if the type of the node matches a target hook function in the pre-registered hook function, the value of the target node is obtained; if the value meets a mounting condition, the target processing function is mounted on the target node.

4. The transcoding method of claim 3, wherein, The method further comprises: if the value does not meet the mounting condition, no mounting operation is performed.

5. The transcoding method of claim 2, wherein, The method further comprises: if the type of the node does not match any hook function in the pre-registered hook function, a default processing function corresponding to the type is obtained.

6. The transcoding method of any of claims 1 to 5, wherein, The performing a printing operation on the abstract syntax tree mounting the target processing function to obtain a target file, comprises: traversing each node in the abstract syntax tree mounting the target processing function, and determining whether the target processing function is mounted on the node; if the target processing function is mounted on the node, the target processing function is executed.

7. The transcoding method of claim 6, wherein, The method further comprises: if the target processing function is not mounted on the node, a default processing function is executed.

8. The transcoding method of any of claims 1 to 5, wherein, The processing the source code file to obtain a plurality of abstract syntax trees, comprises: parsing the source code file to obtain an initial syntax tree; performing a type checking operation on the initial syntax tree to obtain a plurality of abstract syntax trees.

9. A transcoding device, characterized by The apparatus comprises: an obtaining module configured to obtain a source code file and process the source code file to obtain a plurality of abstract syntax trees; a mounting module configured to traverse each node in the abstract syntax tree according to a pre-registered hook function, and mount a target processing function on a target node meeting a specified condition; an executing module configured to perform a printing operation on the abstract syntax tree mounting the target processing function to obtain a target file, wherein the printing operation is used to execute the target processing function mounted on the abstract syntax tree.

10. A computer readable medium having stored thereon a computer program, characterized in that, The computer program is executed by a processing apparatus to implement the steps of the method of any one of claims 1-8.

11. An electronic device, comprising: comprises: a storage device having a computer program stored thereon; processing means for executing the computer program in the storage means to implement the steps of the method of any of claims 1-8.

12. A computer program product comprising a computer program, characterized in that, The computer program which, when executed by the processor, carries out the steps of the method of any of claims 1-8.