Code conversion method and related device

Through semantic analysis, the method of assisting AST conversion is solved, and the problem of converting C# form applications to JavaScript code is achieved, efficient and reliable code conversion is achieved, and the integrity of the original semantics is maintained.

CN119597304APending Publication Date: 2025-03-11AGRICULTURAL BANK OF CHINA
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411743793.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-30
Publication Date
2025-03-11

AI Technical Summary

Technical Problem

When migrating C# form applications to web front-end applications, it is difficult for the existing technology to effectively convert C# language code to JavaScript code, especially the code related to form control events cannot be run after conversion, and the conversion speed and reliability are insufficient.

Method used

The method of semantic analysis assisted abstract syntax tree (AST) conversion is adopted, and the C# language abstract syntax tree is traversed through the listener mode, triggering the conversion event to obtain candidate JavaScript AST nodes, and adjusting these nodes according to the semantic analysis results, generating the corresponding JavaScript AST, and finally compiling and generating the JavaScript code conversion results.

Benefits of technology

It realizes the generation of JavaScript code on the basis of semantic preservation, so that the conversion method is not limited to syntax-level conversion, but also can understand and convert more complex semantic structures, improving the reliability and speed of code conversion, and ensuring high quality and consistency of the conversion results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119597304A_ABST
    Figure CN119597304A_ABST
Patent Text Reader

Abstract

The invention discloses a code conversion method and a related device, and relates to the technical field of software, and the code conversion method comprises the following steps: traversing a C # language abstract syntax tree by using a monitor mode, and when entering a C # language abstract syntax tree node, triggering a conversion event of the node to obtain a candidate JavaScript language AST node of the C # language AST node. When the nodes of the C # language abstract syntax tree exit, the candidate JavaScript language AST nodes are adjusted according to the semantic analysis result, and the JavaScript language AST nodes of the nodes are obtained. And based on the JavaScript language AST node of each C # language AST node, generating the JavaScript language AST of the target C # language source code. And compiling to obtain a JavaScript code conversion result. Semantic analysis is adopted to assist AST conversion, so that the AST conversion is not limited to grammar-level conversion, a more complex semantic structure can be understood and converted, and equivalent realization of characteristics from a C # language to a JavaScript language is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of big data technology, and in particular to a code conversion method and related devices. Background Art

[0002] With the development of the Internet era, B / S architecture has gradually replaced C / S architecture and become the mainstream. Many software projects that use C / S architecture need to be transformed into B / S architecture. The clients of many existing C / S projects are window applications developed in C# language. In the process of architecture conversion, C# window applications need to be transformed into web front-end applications running on browsers. Therefore, C# language codes need to be converted into JavaScript codes. Summary of the invention

[0003] In view of the above problems, the present application provides a code conversion method and related devices to achieve the purpose of improving the reliability of code conversion results. The specific scheme is as follows:

[0004] The first aspect of the present application provides a code conversion method, comprising:

[0005] Obtain the C# language abstract syntax tree of the target C# language source code to be converted;

[0006] Traversing the C# language abstract syntax tree by using a listener mode, for each C# language abstract syntax tree node, triggering a pre-configured conversion event of the C# language abstract syntax tree node when entering the C# language abstract syntax tree node, and obtaining a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node;

[0007] When exiting the C# language abstract syntax tree node, adjusting the candidate JavaScript language abstract syntax tree node according to the semantic analysis result to obtain a JavaScript language abstract syntax tree node of the C# language abstract syntax tree node;

[0008] Based on the JavaScript language abstract syntax tree nodes of each of the C# language abstract syntax tree nodes, generate a JavaScript language abstract syntax tree corresponding to the C# language abstract syntax tree of the target C# language source code;

[0009] The JavaScript language abstract syntax tree corresponding to the C# language abstract syntax tree is compiled to obtain a JavaScript code conversion result of the target C# language source code.

[0010] In a possible implementation, obtaining a C# language abstract syntax tree of a target C# language source code to be converted includes:

[0011] Get the grammar rule file of C# language;

[0012] Based on the grammar rule file, a grammar analyzer code file is generated, wherein the grammar analyzer code file includes a lexical analyzer file, a grammar analyzer file, a listener file and a visitor file;

[0013] Configure a grammar parser using the grammar rule file;

[0014] The target C# language source code is parsed using the syntax parser to generate a C# language abstract syntax tree of the target C# language source code.

[0015] In a possible implementation, the C# language abstract syntax tree is traversed using the listener pattern, including:

[0016] The C# language abstract syntax tree is traversed depth-first based on the listener mode, and when entering the C# language abstract syntax tree node, it is determined whether the C# language abstract syntax tree node is a complex node or a simple node at least according to the semantic analysis result.

[0017] In a possible implementation, when entering the C# language abstract syntax tree node, triggering a pre-configured conversion event of the C# language abstract syntax tree node, and obtaining a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node includes:

[0018] If the C# language abstract syntax tree node is a simple node, a pre-configured conversion event of the simple node is triggered, and the node conversion logic of the conversion event of the simple node includes:

[0019] Determine whether the C# language abstract syntax tree node has a corresponding node in the JavaScript language abstract syntax tree;

[0020] If yes, and the C# language abstract syntax tree node has child nodes, create a JSONObject to save the C# language abstract syntax tree node and place the C# language abstract syntax tree node in the parent node stack;

[0021] Acquire the parent node of the C# language abstract syntax tree node from the parent node stack, and associate the C# language abstract syntax tree node with the parent node;

[0022] If not, and the C# language abstract syntax tree node has no child nodes, create a JSONObject to save the C# language abstract syntax tree node, find the parent node of the C# language abstract syntax tree node from the parent node stack, and associate the C# language abstract syntax tree node to the parent node.

[0023] In a possible implementation, when entering the C# language abstract syntax tree node, triggering a pre-configured conversion event of the C# language abstract syntax tree node, obtaining a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node, further comprising:

[0024] If the C# language abstract syntax tree node is a complex node, a pre-configured conversion event of the complex node is triggered, and the node conversion logic of the conversion event of the complex node includes:

[0025] If the C# language abstract syntax tree node has multiple child nodes, obtain a child node sequence of the C# language abstract syntax tree node, where the child node sequence includes N child nodes arranged in a target order, where N is the number of child nodes;

[0026] Enter each target sub-node in sequence according to the target order;

[0027] When entering the first child node, a left node in the JavaScript language abstract syntax tree is generated at the first child node, and the operator is placed in the operation node of the JavaScript language abstract syntax tree;

[0028] When entering the i-th child node, generate a right node of the JavaScript language abstract syntax tree at the i-th child node, combine the left node, the right node and the operation node into an intermediate JavaScript language abstract syntax tree node, use the intermediate JavaScript language abstract syntax tree node to update the left node, and put the operator into the new operation node; wherein 1<i≤N;

[0029] Until the Nth child node is exited, a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node is generated.

[0030] In a possible implementation, the code conversion method further includes:

[0031] The target C# language source code and the C# language abstract syntax tree are semantically analyzed to obtain the semantic analysis result, wherein the semantic analysis includes type system and type inference, scope and closure identification, method overloading and parameter polymorphism checking, and special feature and mode conversion checking.

[0032] A second aspect of the present application provides a code conversion device, comprising:

[0033] A C# syntax tree building unit, used to obtain a C# language abstract syntax tree of a target C# language source code to be converted;

[0034] a node conversion unit, configured to traverse the C# language abstract syntax tree by using a listener mode, and for each C# language abstract syntax tree node, trigger a pre-configured conversion event of the C# language abstract syntax tree node when entering the C# language abstract syntax tree node, and obtain a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node;

[0035] A node adjustment conversion unit, configured to adjust the candidate JavaScript language abstract syntax tree node according to the semantic analysis result when exiting the C# language abstract syntax tree node, to obtain a JavaScript language abstract syntax tree node of the C# language abstract syntax tree node;

[0036] A JavaScript syntax tree construction unit, configured to generate a JavaScript language abstract syntax tree corresponding to the C# language abstract syntax tree of the target C# language source code based on the JavaScript language abstract syntax tree nodes of each of the C# language abstract syntax tree nodes;

[0037] The JavaScript compiling unit is used to compile the JavaScript language abstract syntax tree corresponding to the C# language abstract syntax tree to obtain a JavaScript code conversion result of the target C# language source code.

[0038] A third aspect of the present application provides a computer program product, comprising computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements the code conversion method of the first aspect or any implementation of the first aspect.

[0039] A fourth aspect of the present application provides an electronic device, comprising at least one processor and a memory connected to the processor, wherein:

[0040] The memory is used to store computer programs;

[0041] The processor is used to execute the computer program so that the electronic device can implement the code conversion method of the first aspect or any implementation manner of the first aspect.

[0042] A fifth aspect of the present application provides a computer storage medium, which carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement the code conversion method of the first aspect or any implementation of the first aspect.

[0043] By means of the above technical scheme, the present application provides a code conversion method and related device. After obtaining the C# language abstract syntax tree of the target C# language source code to be converted, the C# language abstract syntax tree is first traversed according to the listener mode. For each C# language abstract syntax tree node, when entering the C# language abstract syntax tree node, the conversion event of the pre-configured C# language abstract syntax tree node is triggered to obtain the candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node. When exiting the C# language abstract syntax tree node, the candidate JavaScript language abstract syntax tree node is adjusted according to the semantic analysis result to obtain the JavaScript language abstract syntax tree node of the C# language abstract syntax tree node. Based on the JavaScript language abstract syntax tree nodes of each C# language abstract syntax tree node, the JavaScript language abstract syntax tree of the target C# language source code is generated. Further, the JavaScript language abstract syntax tree of the target C# language source code is compiled to obtain the JavaScript code conversion result of the target C# language source code. It can be seen that the present application adopts semantic analysis to assist abstract syntax tree conversion, which can generate JavaScript code on the basis of semantic preservation, so that the conversion method is not limited to syntax-level conversion, but can also understand and convert more complex semantic structures, and realize the equivalent implementation of C# language to JavaScript language features, keeping the original semantics unchanged. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.

[0045] Figure 1 A schematic diagram of a code conversion system architecture provided for this application;

[0046] Figure 2 A schematic diagram of the hardware structure of a terminal provided in this application;

[0047] Figure 3 A schematic diagram of the hardware structure of a server provided for this application;

[0048] Figure 4 A schematic diagram of a code conversion method provided in an embodiment of the present application;

[0049] Figure 5 A schematic diagram of the structure of a translator provided in an embodiment of the present application;

[0050] Figure 6A specific implementation flow chart of a code conversion method provided in an embodiment of the present application;

[0051] Figure 7 A schematic diagram of an AST conversion method provided in an embodiment of the present application;

[0052] Figure 8 A schematic diagram of a code conversion structure provided in an embodiment of the present application;

[0053] Fig. 9 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0054] The following describes the embodiments of the present application in conjunction with the drawings in the embodiments of the present application. The terms used in the implementation method section of the present application are only used to explain the specific embodiments of the present application, and are not intended to limit the present application.

[0055] The embodiments of the present application are described below in conjunction with the accompanying drawings. Those skilled in the art will appreciate that, with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.

[0056] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and need not be used to describe a specific order or sequential order. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances, which is only to describe the distinction mode adopted by the objects of the same attributes when describing in the embodiments of the present application. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, so that the process, method, system, product or equipment comprising a series of units need not be limited to those units, but may include other units that are not clearly listed or inherent to these processes, methods, products or equipment.

[0057] This application can be applied in the field of software technology, and specifically can be applied to transforming a C# language front-end client application into a web front-end application scenario running on a browser, and converting the code by mapping the form and event separately, so as to convert the C# language code related to the form control event into JavaScript code. The purpose is to improve the reliability and speed of the conversion from C# language code to JavaScript code.

[0058] A code conversion method provided in an embodiment of the present application can be applied to a code conversion system. The code conversion method is implemented through a terminal or a server in the code conversion system, or through the interaction between a terminal and a server in the code conversion system.

[0059] See also Figure 1 , Figure 1 1 shows a schematic diagram of a code conversion system architecture. The system may include a terminal 100 and a server 200. The server 200 may include one or more servers ( Figure 1 In the example, a server is included, and the server 200 can provide the method provided in the embodiment of the present application for one or more terminals.

[0060] Among them, a code conversion application can be installed on the terminal 100, and the above application and web page can provide an interface. The terminal 100 can receive relevant parameters entered by the user on the code conversion interface and send the above parameters to the server 200. The server 200 can obtain processing results based on the received parameters and return the processing results to the terminal 100.

[0061] It should be understood that in some optional implementations, the terminal 100 can also complete the action of obtaining the processing result based on the received parameters by itself without the cooperation of the server, and the embodiments of the present application are not limited to this.

[0062] Next describe Figure 1 The product form of the mid-terminal 100;

[0063] The terminal 100 in the embodiment of the present application can be a mobile phone, a tablet computer, a wearable device, a vehicle-mounted device, an augmented reality (AR) / virtual reality (VR) device, a laptop computer, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), etc., and the embodiment of the present application does not impose any limitation on this.

[0064] Figure 2 An optional hardware structure diagram of the terminal 100 is shown.

[0065] refer to Figure 2 As shown, the terminal 100 may include a radio frequency unit 110, a memory 120, an input unit 130, a display unit 140, a camera 150 (optional), an audio circuit 160 (optional), a speaker 161 (optional), a microphone 162 (optional), an earphone jack 163 (optional), a processor 170, an external interface 180, a power supply 190 and other components. Those skilled in the art will appreciate that Figure 2 These are merely examples of terminals or multi-function devices and do not constitute limitations on the terminals or multi-function devices, which may include more or fewer components than those shown in the figures, or combinations of certain components, or different components.

[0066] The input unit 130 can be used to receive input digital or character information, and generate key signal input related to the user settings and function control of the portable multifunctional device. Specifically, the input unit 130 may include a touch screen 131 (optional) and / or other input devices 132. The touch screen 131 can collect the user's touch operations on or near it (such as the user's operation on or near the touch screen using any suitable object such as fingers, joints, stylus, etc.), and drive the corresponding connection device according to a pre-set program. The touch screen can detect the user's touch action on the touch screen, convert the touch action into a touch signal and send it to the processor 170, and can receive and execute the command sent by the processor 170; the touch signal at least includes the touch point coordinate information. The touch screen 131 can provide an input interface and an output interface between the terminal 100 and the user. In addition, the touch screen can be implemented using multiple types such as resistive, capacitive, infrared and surface acoustic wave. In addition to the touch screen 131, the input unit 130 can also include other input devices. Specifically, other input devices 132 may include, but are not limited to, one or more of a physical keyboard, function keys (such as a volume control key, a switch key, etc.), a trackball, a mouse, a joystick, and the like.

[0067] Among them, the input device 132 can receive input data and the like.

[0068] The display unit 140 may be used to display information input by the user or provided to the user, various menus of the terminal 100, interactive interfaces, file display and / or playback of any multimedia file. In the embodiment of the present application, the display unit 140 may be used to display the interface of the code conversion, processing results, etc.

[0069] The memory 120 can be used to store instructions and data. The memory 120 can mainly include an instruction storage area and a data storage area. The data storage area can store various data, such as multimedia files, texts, etc.; the instruction storage area can store software units such as operating systems, applications, instructions required for at least one function, or their subsets and extensions. It can also include a non-volatile random access memory; provide the processor 170 with hardware, software and data resources including management of computing and processing equipment, and support control software and applications. It is also used for the storage of multimedia files, and the storage of running programs and applications.

[0070] The processor 170 is the control center of the terminal 100. It uses various interfaces and lines to connect various parts of the entire terminal 100. By running or executing instructions stored in the memory 120 and calling data stored in the memory 120, it executes various functions of the terminal 100 and processes data, thereby controlling the terminal device as a whole. Optionally, the processor 170 may include one or more processing units; preferably, the processor 170 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface and application program, and the modem processor mainly processes wireless communication. It is understandable that the above-mentioned modem processor may not be integrated into the processor 170. In some embodiments, the processor and the memory may be implemented on a single chip, and in some embodiments, they may also be implemented separately on separate chips. The processor 170 may also be used to generate corresponding operation control signals, send them to corresponding components of the computing and processing device, read and process data in the software, especially read and process data and programs in the memory 120, so that each functional module therein performs corresponding functions, thereby controlling the corresponding components to act according to the requirements of the instructions.

[0071] The memory 120 may be used to store software codes related to the code conversion method, the processor 170 may execute the steps of the code conversion method, and may also schedule other units (such as the above-mentioned input unit 130 and display unit 140) to implement corresponding functions.

[0072] The radio frequency unit 110 (optional) can be used for receiving and sending information or receiving and sending signals during a call, for example, after receiving the downlink information of the base station, it is sent to the processor 170 for processing; in addition, the designed uplink data is sent to the base station. Generally, the RF circuit includes but is not limited to an antenna, at least one amplifier, a transceiver, a coupler, a low noise amplifier (Low Noise Amplifier, LNA), a duplexer, etc. In addition, the radio frequency unit 110 can also communicate with network devices and other devices through wireless communication. The wireless communication can use any communication standard or protocol, including but not limited to Global System of Mobile communication (Global System of Mobile communication, GSM), General Packet Radio Service (General Packet Radio Service, GPRS), Code Division Multiple Access (Code Division Multiple Access, CDMA), Wideband Code Division Multiple Access (Wideband Code Division Multiple Access, WCDMA), Long Term Evolution (Long Term Evolution, LTE), email, Short Messaging Service (SMS), etc.

[0073] In this embodiment of the present application, the RF unit 110 can send data to the server 200 and receive processing results sent by the server 200.

[0074] It should be understood that the radio frequency unit 110 is optional and can be replaced by other communication interfaces, such as a network port.

[0075] The terminal 100 also includes a power supply 190 (such as a battery) for supplying power to various components. Preferably, the power supply can be logically connected to the processor 170 through a power management system, so that the power management system can manage functions such as charging, discharging, and power consumption.

[0076] The terminal 100 further includes an external interface 180 , which may be a standard Micro USB interface or a multi-pin connector, and may be used to connect the terminal 100 to communicate with other devices, or to connect a charger to charge the terminal 100 .

[0077] Although not shown, the terminal 100 may also include a flashlight, a wireless fidelity (WiFi) module, a Bluetooth module, sensors with different functions, etc., which are not described in detail here. Some or all of the methods described below may be applied in the following embodiments. Figure 2 In the terminal 100 shown.

[0078] Next describe Figure 1 The product form of the server 200;

[0079] Figure 3 A structural diagram of a server 200 is provided, such as Figure 3 As shown, the server 200 includes a bus 201, a processor 202, a communication interface 203, and a memory 204. The processor 202, the memory 204, and the communication interface 203 communicate with each other via the bus 201.

[0080] The bus 201 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 3 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0081] The processor 202 may be any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).

[0082] The memory 204 may include a volatile memory, such as a random access memory (RAM). The memory 204 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD), or a solid state drive (SSD).

[0083] The memory 204 may be used to store software codes related to the code conversion method, and the processor 202 may execute the steps of the code conversion method of the chip, and may also schedule other units to implement corresponding functions.

[0084] It should be understood that the above-mentioned terminal 100 and server 200 can be centralized or distributed devices, and the processors in the above-mentioned terminal 100 and server 200 (such as processor 170 and processor 202) can be hardware circuits (such as application specific integrated circuit (ASIC), field-programmable gate array (FPGA), general-purpose processor, digital signal processor (DSP), microprocessor or microcontroller, etc.), or a combination of these hardware circuits. For example, the processor can be a hardware system with an instruction execution function, such as a CPU, DSP, etc., or a hardware system without an instruction execution function, such as an ASIC, FPGA, etc., or a combination of the above-mentioned hardware systems without an instruction execution function and hardware systems with an instruction execution function.

[0085] C# (C sharp) and JavaScript are both high-level languages. C# is an object-oriented, strongly typed programming language, while JavaScript is a lightweight, interpreted, weakly typed language, mainly used for web and server-side development. A high-level language to high-level language conversion tool can be seen as a special type of compiler, or this conversion tool can be called a transpiler. Compared with traditional conversion tools, compilers or interpreters, the execution target is different. Compilers usually compile high-level languages ​​into low-level languages ​​(such as assembly language or machine code), interpreters directly execute programs written in high-level languages, and transpilers convert between the same level of abstraction, that is, high-level languages.

[0086] The processing flow of these three conversion tools is similar, and can be divided into steps such as lexical analysis, syntactic analysis, semantic analysis, and code generation. The lexical analysis, syntactic analysis, and semantic analysis steps all process the source language code and generate the abstract syntax tree AST of the source language. There are significant differences in the code generation step: the compiler will generate low-level code or directly executable binary code, the interpreter may directly execute the AST or convert it into some intermediate representation (IR) for execution, and the translator may traverse the AST and generate code in another high-level language.

[0087] Among them, the Abstract Syntax Tree (AST) is a tree-like representation of the abstract syntax structure of the source code. It does not rely on the specific grammar of the source language, but converts the source code of the source program into a tree-like data structure, which can clearly show the syntax structure of the source code.

[0088] In AST, each node represents a structure in the source code, such as an operator, function name, variable name, constant value, etc. These nodes are connected by edges to form a tree that reflects the grammatical structure of the source code.

[0089] In the prior art, Bridge.NET is an open source compiler that converts C# language code into JavaScript code while maintaining the readability and performance of the code. For certain features and standard library support in the C# language, Bridge.NET provides corresponding JavaScript implementation or encapsulation to ensure that the converted code can work properly in the browser environment. The code operation depends on a private runtime support library. Bridge.NET was developed earlier and uses string concatenation to generate code. It does not support popular front-end frameworks such as React and Vue.

[0090] However, Bridge.NET only supports certain features and standard libraries in the C# language, and does not support C# language form applications. Forced conversion can only get syntax-level conversion code, and the form control event code in the C# language class cannot run after being converted to JavaScript. In addition, the JavaScript code generated by Bridge.NET directly using AST does not support the newer ECMAScript standard, does not use the modern compiler backend, and has a slow code generation speed.

[0091] Although C# and JavaScript differ in syntax and usage, they can both be represented as AST. Abstract syntax tree is a tree representation of the abstract symbols and structure of source code, which is used to represent the construction of programming language. Source code is also constructed in reverse through AST. Based on this, in order to solve the technical problems existing in the above-mentioned existing code conversion technology, the embodiment of the present application provides a code conversion method, and the code conversion method of the embodiment of the present application is described in detail below in conjunction with the accompanying drawings.

[0092] Reference Figure 4 , Figure 4 A schematic diagram of a code conversion method provided in an embodiment of the present application is shown in FIG. Figure 4 As shown, a code conversion method provided in an embodiment of the present application may include the following steps:

[0093] S401 , obtaining a C# language abstract syntax tree of the target C# language source code to be converted, that is, a C# language AST node.

[0094] S402. Use the listener mode to traverse the C# language abstract syntax tree. For each C# language AST node, when entering the C# language AST node, trigger the conversion event of the pre-configured C# language AST node to obtain the candidate JavaScript language abstract syntax tree node of the C# language AST node, that is, the candidate JavaScript language AST node.

[0095] S403. When exiting the C# language AST node, adjust the candidate JavaScript language AST node according to the semantic analysis result to obtain the JavaScript language AST node of the C# language AST node.

[0096] S404: Generate a JavaScript language AST of the target C# language source code based on the JavaScript language AST nodes of each C# language AST node.

[0097] S405 , compile the JavaScript language AST of the target C# language source code to obtain a JavaScript code conversion result of the target C# language source code.

[0098] It can be seen from the above technical scheme that a code conversion method provided by an embodiment of the present application obtains the C# language abstract syntax tree of the target C# language source code to be converted, and first traverses the C# language abstract syntax tree according to the listener mode. For each C# language abstract syntax tree node, when entering the C# language abstract syntax tree node, the conversion event of the pre-configured C# language abstract syntax tree node is triggered to obtain the candidate JavaScript language AST node of the C# language AST node. When exiting the C# language abstract syntax tree node, the candidate JavaScript language AST node is adjusted according to the semantic analysis result to obtain the JavaScript language AST node of the C# language AST node. Based on the JavaScript language AST nodes of each C# language AST node, the JavaScript language AST of the target C# language source code is generated. Further, the JavaScript language AST of the target C# language source code is compiled to obtain the JavaScript code conversion result of the target C# language source code. It can be seen that this application adopts semantic analysis to assist AST conversion, which can generate JavaScript code on the basis of semantic preservation, so that the conversion method is not limited to syntax-level conversion, but can also understand and convert more complex semantic structures, and realize the equivalent implementation of C# language to JavaScript language features, keeping the original semantics unchanged.

[0099] Furthermore, the embodiments of the present application can be applied to Figure 5 The translator shown, such as Figure 5As shown, a translator provided by an embodiment of the present application includes a syntax analyzer, a converter, a listener, a semantic analyzer, and a compiler. The C# language AST is generated by the syntax analyzer, and then the C# language AST is traversed using the listener mode to generate the JavaScript language AST. While traversing, the AST structure of the JavaScript language is adjusted in combination with the semantic analysis of the C# language code, and finally the JavaScript language AST is converted into JavaScript code using the JavaScript compiler. The present application reduces the complexity and workload of migrating the C# language form application to the web front-end application by automating the conversion process, while ensuring the high quality and consistency of the conversion results.

[0100] Figure 6 A specific implementation flow chart of a code conversion method provided in an embodiment of the present application is as follows: Figure 6 As shown, this method specifically includes:

[0101] S601: The syntax analyzer obtains the target C# language source code to be converted.

[0102] S602, the syntax analyzer parses the target C# language source code to obtain an abstract syntax tree of the target C# language source code, that is, a C# language AST.

[0103] In this embodiment, the syntax analyzer is constructed based on the syntax rule file of the C# language, and is used to parse the C# language source code based on the syntax rule file and construct an abstract syntax tree AST of the C# language source code, that is, the C# language AST.

[0104] Specifically, the specific method of parsing the target C# language source code through the syntax analyzer to obtain the C# language AST includes:

[0105] 1. Get the grammar rule file of C# language.

[0106] In this embodiment, a grammar rule file with a ".g4" extension is used to define the grammar of the C# language. The grammar rule file includes lexical rules (used to match vocabulary such as keywords, identifiers, etc.) and grammar rules (defining the structure of the language).

[0107] 2. Generate a grammar parser

[0108] In this embodiment, a syntax analyzer code file is generated based on the syntax rule file, and the syntax analyzer code includes a lexical analyzer file (Lexer), a syntax analyzer file (Parser), a listener file (Listener) and a visitor file (Visitor).

[0109] 3. Use the syntax parser to parse the target C# language source code and generate the AST of the target C# language source code.

[0110] In this embodiment, a parser instance is created, and the parser code is used to generate the target application code, which is used to parse the target C# language code file and generate the AST of the target C# language code.

[0111] Specifically, the character stream sp of the target C# language source code is used as the input of the syntax parser (also known as the language identifier). The syntax parser performs lexical analysis and syntax analysis on the target C# language code based on the lexical analyzer file and syntax analyzer file in the syntax parser to obtain the AST of the target C# language code.

[0112] S603, the semantic analyzer performs semantic analysis on the target C# language source code, obtains the semantic analysis result, and obtains the construction strategy of the JavaScript language AST according to the semantic analysis result.

[0113] In this embodiment, the C# language form application is based on class inheritance in object-oriented programming. The properties of the class may be defined in the inheritance process. The class to which the control method belongs may have multiple layers of inheritance, so the control method may come from the base class or be overwritten in the process of multiple layers of inheritance. In the process of code conversion, it is necessary to know which class the currently called method comes from, and extract the method for conversion and import it into the current JavaScript file.

[0114] In order to achieve the above goals, the C# language files of the entire project are parsed in advance, and classes, namespaces, parent classes, methods, and properties are extracted to form indexes. The property definitions and method overrides of each class can be queried through the class name. The class inheritance relationship chain can also be traced through the method name to query in which class the method is defined and in which class it is overridden, and provide an interface for query and call during subsequent AST node conversion.

[0115] Specifically, a custom analyzer is written to collect properties, events, delegates, type information, scope, method signatures, and method overloading details in C# language code, analyze data types and type systems, resolve method overloading and parameter polymorphism, and identify all method overloading instances.

[0116] S604. The listener uses depth-first traversal of the C# language AST based on the listener mode. When entering a C# language AST node in the C# language AST, it determines whether the C# language AST node is a complex node or a simple node based at least on the result of semantic analysis.

[0117] In this embodiment, nodes with the same structure in the C# language AST and the JavaScript language AST are simple nodes.

[0118] In this embodiment, nodes with different structures in the C# language AST and the JavaScript language AST are complex nodes.

[0119] In this embodiment, whether a C# language AST node is a complex node or a simple node is determined through the semantic analysis result of the C# language AST node.

[0120] In this embodiment, the listener mode is used, which can automatically trigger events during the parsing process. By inheriting the listener class file and customizing the AST node conversion method, the customized AST node conversion logic can be executed when entering or leaving the AST node. This method does not require manual traversal of the tree structure, simplifying the conversion process.

[0121] Specifically, a custom class that inherits the base listener class is created, the methods of entering and exiting nodes are rewritten, and a pre-configured data structure for storing AST of the JavaScript language is used to store AST nodes of the JavaScript language generated during the traversal process.

[0122] S605: If the C# language AST node is a simple node, the listener triggers a conversion event of the simple node, and the converter obtains a candidate JavaScript language AST node of the C# language AST node.

[0123] In this embodiment, when it is determined that the C# language AST node belongs to a simple node, when entering the C# language AST node, the AST node conversion event of the simple node is triggered, so that the AST node conversion logic of the simple node is executed as follows:

[0124] Determine whether the C# language AST node has a corresponding node in the JavaScript language AST.

[0125] If there is a corresponding node and there are child nodes in the C# language AST node, create a JSONObject to save the C# language AST node and put the C# language AST node in the parent node stack.

[0126] Get the parent node of the C# language AST node from the parent node stack and associate the C# language AST node with the parent node.

[0127] If there is no corresponding node and the C# language AST node has no child nodes, create a JSONObject to save the C# language AST node, find the parent node of the C# language AST node from the parent node stack, and associate the C# language AST node to the parent node.

[0128] In summary, the conversion of simple nodes is achieved by executing the conversion logic of simple nodes. For simple nodes, only simple branch pruning and conversion are required.

[0129] S606: If the C# language AST node is a complex node, the listener triggers a conversion event of the complex node, and the converter obtains a candidate JavaScript language AST node of the C# language AST node.

[0130] In this embodiment, when it is determined that the C# language AST node belongs to a complex node, when entering the C# language AST node, the AST node conversion event of the complex node is triggered, and the AST node conversion logic of the complex node is executed as follows:

[0131] If there are multiple child nodes in the C# language AST node, obtain the child node sequence of the C# language AST node. The child node sequence includes N target child nodes arranged in the target order. The target child node is the child node of the C# language AST node, where N is greater than 1. Enter each target child node in sequence according to the target order:

[0132] When entering the first target child node, a left node (left node) in the JavaScript language AST is generated at the first target child node, and the operator is placed in the operator node (operation node) of the JavaScript language AST.

[0133] When entering the i-th (1<i≤N) target child node, generate the right node (right node) of the JavaScript language AST at the i-th target child node, combine the left node, the right node and the operator node into an intermediate JavaScript language AST node, use the intermediate JavaScript language AST node to update the left node, and put the operator into the new operator node.

[0134] Until the Nth target child node is exited, a candidate JavaScript language AST node of the C# language AST node is generated.

[0135] Taking a complex node P as an example, the P node has four child nodes A, B, C, and D, and the child node sequence of the P node is [A, B, C, D]. When entering the P node, the mapping result of the P node is generated according to the following steps, that is, the candidate JavaScript language AST node of the P node:

[0136] 1. Get the child nodes A, B, C, and D in the P node. The four child nodes are in parallel and the order is not important.

[0137] 2. Enter node A, generate a left node in JavaScript AST at node A, and put the operator into the operator node of JavaScript AST;

[0138] 3. Enter node B and generate the right node in JavaScript AST at node B;

[0139] 4. Combine the left node, right node, and operator node generated above into a JavaScript AST node, use the whole as the left node, and put the operator into the new operator node;

[0140] 5. Enter node C and generate the right node at node C;

[0141] 6. Combine the left node, right node and operator node generated above into a JavaScriptAST node as the left node, and put the operator into the new operator node;

[0142] 7. Enter node D and generate the right node at node D;

[0143] 8. Combine the left node, right node and operator node generated above into a JavaScript AST node as the left node, and put the operator into the new operator node to obtain the JavaScript language AST node of the P node.

[0144] In summary, AST conversion of complex nodes requires not only branch pruning and conversion, but also pruning multi-branch branches into binary branches.

[0145] S607 . When traversing and exiting the C# language AST node, the converter dynamically adjusts the candidate JavaScript language AST node of the C# language AST node by using the semantic analysis result to obtain the JavaScript language AST node of the C# language AST node.

[0146] In this embodiment, candidate JavaScript language AST nodes are adjusted according to the scope and closure characteristics of C# language code, IIFE (Immediately Invoked Function Expression) is introduced to simulate the scope of C# language, and methods are imported according to the semantic analysis results of method declaration nodes.

[0147] S608: until the traversal exits the last C# language AST node, the converter generates a JavaScript language AST of the C# language AST.

[0148] In this embodiment, by repeatedly nesting AST nodes, each AST node in the AST of the C# language is converted into an AST node of the JavaScript language, and finally an AST of the JavaScript language is obtained based on the AST nodes of the JavaScript language.

[0149] S609: The compiler converts the AST of the JavaScript language into JavaScript code.

[0150] In this embodiment, a JavaScript language compiler is used to convert the JavaScript language AST into JavaScript code.

[0151] It should be noted that different compilers can be selected to implement the process from AST to code. The types of compilers include but are not limited to the Babel compiler, which is used to convert the AST of the JavaScript language into JavaScript code.

[0152] It can be seen from the above technical solutions that a code conversion method provided in the embodiment of the present application can achieve the following technical related aspects:

[0153] (1) Improved the interpretability, accuracy and efficiency of code conversion:

[0154] By using a syntax analyzer to parse C# language code to generate AST, the structure and semantics of the source code can be accurately represented. The listener mode can accurately traverse each node of the C# language AST. The configurable AST node conversion method ensures that the generation of each code snippet is controllable and explainable. The AST-level operation conversion of different languages ​​is realized. The process from AST to generated code can be assisted by modern compiler backend tools. The generation speed is extremely fast, and the conversion from C# language to JavaScript can be automatically completed, greatly reducing the time required for manual code writing and debugging.

[0155] (2) Enhanced semantic preservation and scalability of code conversion:

[0156] C# and JavaScript are very different. There are a lot of grammatical structures in C# that cannot be mapped in JavaScript. Therefore, C# AST nodes cannot form a one-to-one correspondence with JavaScript AST nodes. In particular, there is no mature solution to directly convert the two ASTs from C# form application projects to JavaScript codes in web front-end projects. For example, the form control event method in the C# form application cannot be run after being converted into JavaScript at the grammatical level.

[0157] The prior art converts C# language code at the grammatical level. The present invention adopts semantic analysis-assisted AST conversion for C# language form application projects, which can generate JavaScript code on the basis of maintaining semantics. By using semantic analysis-assisted conversion, the conversion method is not limited to grammatical level conversion, but can also understand and convert more complex semantic structures, realize the equivalent implementation of C# language to JavaScript language features, and keep the original semantics unchanged.

[0158] Specifically, Figure 7 A schematic diagram of an AST conversion method provided in an embodiment of the present application, such as Figure 7 As shown, the AST transformation methods include:

[0159] Use the listener to traverse the C#AST nodes. The node types include compilation units, namespace declaration nodes, class declaration nodes, method declaration nodes, variable declaration nodes, property declaration nodes, etc.

[0160] When traversing into each C# language AST node (such as method declaration), prepare the JavaScript language AST construction strategy according to the results of semantic analysis, use the inheritance listener method and rewrite the conversion methods of various types of C# AST nodes, convert the AST nodes according to the corresponding conversion methods, and perform precise AST mapping and conversion based on the grammatical differences between C# and JavaScript languages.

[0161] When traversing to the AST method node, call the interface query of the semantic analysis result to find out which class the current method comes from, convert the method and class attributes into JavaScript code and store it, and assist in generating the AST of the JavaScript language.

[0162] When traversing and exiting the C# language AST node, the JavaScript language AST node is dynamically adjusted using semantic analysis. The semantic analysis includes type system and type inference, scope and closure identification, method overloading and parameter polymorphism checking, special features and mode conversion checking. Optionally, the JavaScript language AST is adjusted according to the scope and closure characteristics of the C# language code, and IIFE (Immediately Invoked Function Expression) is introduced to simulate the scope of the C# language; methods are imported according to the semantic analysis results of the method declaration node.

[0163] A code conversion method provided by an embodiment of the present application is introduced above, and a device for executing the above code conversion method will be introduced below.

[0164] See also Figure 8 , Figure 8 Schematic diagram of the structure of a code conversion device provided in an embodiment of the present application. Figure 8 As shown, the code conversion device 800 includes:

[0165] A C# syntax tree construction unit 801 is used to obtain a C# language abstract syntax tree of the target C# language source code to be converted;

[0166] a node conversion unit, configured to traverse the C# language abstract syntax tree by using a listener mode, and for each C# language abstract syntax tree node, trigger a pre-configured conversion event of the C# language abstract syntax tree node when entering the C# language abstract syntax tree node, and obtain a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node;

[0167] A node adjustment conversion unit 802 is used to adjust the candidate JavaScript language abstract syntax tree node according to the semantic analysis result when exiting the C# language abstract syntax tree node, and obtain a JavaScript language abstract syntax tree node of the C# language abstract syntax tree node;

[0168] A JavaScript syntax tree construction unit 803 is used to generate a JavaScript language abstract syntax tree corresponding to the C# language abstract syntax tree of the target C# language source code based on the JavaScript language abstract syntax tree nodes of each of the C# language abstract syntax tree nodes;

[0169] The JavaScript compiling unit 804 is used to compile the JavaScript language abstract syntax tree corresponding to the C# language abstract syntax tree to obtain a JavaScript code conversion result of the target C# language source code.

[0170] In a possible implementation, when the C# syntax tree construction unit is used to obtain the C# language abstract syntax tree of the target C# language source code to be converted, it is specifically used to:

[0171] Get the grammar rule file of C# language;

[0172] Based on the grammar rule file, a grammar analyzer code file is generated, wherein the grammar analyzer code file includes a lexical analyzer file, a grammar analyzer file, a listener file and a visitor file;

[0173] Configure a grammar parser using the grammar rule file;

[0174] The target C# language source code is parsed using the syntax parser to generate a C# language abstract syntax tree of the target C# language source code.

[0175] In a possible implementation, when the node conversion unit is used to traverse the C# language abstract syntax tree using the listener mode, it is specifically used to:

[0176] The C# language abstract syntax tree is traversed depth-first based on the listener mode, and when entering the C# language abstract syntax tree node, it is determined whether the C# language abstract syntax tree node is a complex node or a simple node at least according to the semantic analysis result.

[0177] In a possible implementation, the node conversion unit is used to trigger a pre-configured conversion event of the C# language abstract syntax tree node when entering the C# language abstract syntax tree node, and obtain a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node, specifically for:

[0178] If the C# language abstract syntax tree node is a simple node, a pre-configured conversion event of the simple node is triggered, and the node conversion logic of the conversion event of the simple node includes:

[0179] Determine whether the C# language abstract syntax tree node has a corresponding node in the JavaScript language abstract syntax tree;

[0180] If yes, and the C# language abstract syntax tree node has child nodes, create a JSONObject to save the C# language abstract syntax tree node and place the C# language abstract syntax tree node in the parent node stack;

[0181] Acquire the parent node of the C# language abstract syntax tree node from the parent node stack, and associate the C# language abstract syntax tree node with the parent node;

[0182] If not, and the C# language abstract syntax tree node has no child nodes, create a JSONObject to save the C# language abstract syntax tree node, find the parent node of the C# language abstract syntax tree node from the parent node stack, and associate the C# language abstract syntax tree node to the parent node.

[0183] In a possible implementation, the node conversion unit is used to trigger a pre-configured conversion event of the C# language abstract syntax tree node when entering the C# language abstract syntax tree node, and when obtaining a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node, is also used to:

[0184] If the C# language abstract syntax tree node is a complex node, a pre-configured conversion event of the complex node is triggered, and the node conversion logic of the conversion event of the complex node includes:

[0185] If the C# language abstract syntax tree node has multiple child nodes, obtain a child node sequence of the C# language abstract syntax tree node, where the child node sequence includes N child nodes arranged in a target order, where N is the number of child nodes;

[0186] Enter each target sub-node in sequence according to the target order;

[0187] When entering the first child node, a left node in the JavaScript language abstract syntax tree is generated at the first child node, and the operator is placed in the operation node of the JavaScript language abstract syntax tree;

[0188] When entering the i-th child node, generate a right node of the JavaScript language abstract syntax tree at the i-th child node, combine the left node, the right node and the operation node into an intermediate JavaScript language abstract syntax tree node, use the intermediate JavaScript language abstract syntax tree node to update the left node, and put the operator into the new operation node; wherein 1<i≤N;

[0189] Until the Nth child node is exited, a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node is generated.

[0190] In a possible implementation, the code conversion device further includes: a semantic analysis unit, configured to:

[0191] The target C# language source code and the C# language abstract syntax tree are semantically analyzed to obtain the semantic analysis result, wherein the semantic analysis includes type system and type inference, scope and closure identification, method overloading and parameter polymorphism checking, and special feature and mode conversion checking.

[0192] The present application also provides an electronic device in an embodiment. Fig. 9 As shown, it shows a schematic diagram of the structure of an electronic device suitable for implementing the embodiment of the present application. The electronic device in the embodiment of the present application may include but is not limited to fixed terminals such as mobile phones, laptops, PDAs (personal digital assistants), PADs (tablet computers), desktop computers, etc. Fig. 9 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.

[0193] like Fig. 9 As shown, the electronic device may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 901, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 902 or a program loaded from a storage device 908 to a random access memory (RAM) 903. When the electronic device is powered on, various programs and data required for the operation of the electronic device are also stored in the RAM 903. The processing device 901, the ROM 902, and the RAM 903 are connected to each other via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.

[0194] Typically, the following devices may be connected to the I / O interface 905: an input device 906 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 907 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 908 including, for example, a memory card, a hard disk, etc.; and a communication device 909. The communication device 909 may allow the electronic device to communicate with other devices wirelessly or by wire to exchange data. Although Figure 6 An electronic device having various devices is shown, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.

[0195] An embodiment of the present application also provides a computer program product including computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements any code conversion method provided in the embodiment of the present application.

[0196] A computer-readable storage medium is also provided in an embodiment of the present application. The storage medium carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement any code conversion method provided in the embodiment of the present application.

[0197] It should also be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed over multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the drawings of the device embodiments provided by the present application, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines.

[0198] Through the description of the above implementation mode, the technicians in the field can clearly understand that the present application can be implemented by means of software plus necessary general hardware, and of course, it can also be implemented by special hardware including special integrated circuits, special CPUs, special memories, special components, etc. In general, all functions completed by computer programs can be easily implemented by corresponding hardware, and the specific hardware structure used to implement the same function can also be various, such as analog circuits, digital circuits or special circuits. However, for the present application, software program implementation is a better implementation mode in more cases. Based on such an understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a readable storage medium, such as a computer floppy disk, a U disk, a mobile hard disk, a ROM, a RAM, a disk or an optical disk, etc., including a number of instructions to enable a computer device (which can be a personal computer, a training device, or a network device, etc.) to execute the methods described in each embodiment of the present application.

[0199] In the above embodiments, all or part of the embodiments may be implemented by software, hardware, firmware or any combination thereof. When implemented by software, all or part of the embodiments may be implemented in the form of a computer program product.

[0200] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from a website site, a computer, a training device, or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, training device, or data center. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a training device, a data center, etc. that includes one or more available media integrations. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)), etc.

Claims

1. A code conversion method, characterized in that: include: Obtain the C# language abstract syntax tree of the target C# language source code to be converted; Traversing the C# language abstract syntax tree by using a listener mode, for each C# language abstract syntax tree node, triggering a pre-configured conversion event of the C# language abstract syntax tree node when entering the C# language abstract syntax tree node, and obtaining a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node; When exiting the C# language abstract syntax tree node, adjusting the candidate JavaScript language abstract syntax tree node according to the semantic analysis result to obtain a JavaScript language abstract syntax tree node of the C# language abstract syntax tree node; Generate a JavaScript language abstract syntax tree corresponding to the C# language abstract syntax tree of the target C# language source code based on the JavaScript language abstract syntax tree nodes of each of the C# language abstract syntax tree nodes; Compile the JavaScript language abstract syntax tree corresponding to the C# language abstract syntax tree to obtain a JavaScript code conversion result of the target C# language source code.

2. The code conversion method according to claim 1, characterized in that: The step of obtaining a C# language abstract syntax tree of the target C# language source code to be converted includes: Get the grammar rule file of C# language; Based on the grammar rule file, a grammar analyzer code file is generated, wherein the grammar analyzer code file includes a lexical analyzer file, a grammar analyzer file, a listener file and a visitor file; Configure a grammar parser using the grammar rule file; The target C# language source code is parsed using the syntax parser to generate a C# language abstract syntax tree of the target C# language source code.

3. The code conversion method according to claim 1, characterized in that: The method of traversing the C# language abstract syntax tree using the listener mode includes: The C# language abstract syntax tree is traversed depth-first based on the listener mode, and when entering the C# language abstract syntax tree node, it is determined whether the C# language abstract syntax tree node is a complex node or a simple node at least according to the semantic analysis result.

4. The code conversion method according to claim 3, characterized in that: When entering the C# language abstract syntax tree node, triggering a pre-configured conversion event of the C# language abstract syntax tree node, and obtaining a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node, including: If the C# language abstract syntax tree node is a simple node, a pre-configured conversion event of the simple node is triggered, and the node conversion logic of the conversion event of the simple node includes: Determine whether the C# language abstract syntax tree node has a corresponding node in the JavaScript language abstract syntax tree; If yes, and the C# language abstract syntax tree node has child nodes, create a JSONObject to save the C# language abstract syntax tree node and place the C# language abstract syntax tree node in the parent node stack; Acquire the parent node of the C# language abstract syntax tree node from the parent node stack, and associate the C# language abstract syntax tree node with the parent node; If not, and the C# language abstract syntax tree node has no child nodes, create a JSONObject to save the C# language abstract syntax tree node, find the parent node of the C# language abstract syntax tree node from the parent node stack, and associate the C# language abstract syntax tree node to the parent node.

5. The code conversion method according to claim 4, characterized in that: When entering the C# language abstract syntax tree node, triggering a pre-configured conversion event of the C# language abstract syntax tree node, obtaining a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node, further comprising: If the C# language abstract syntax tree node is a complex node, a pre-configured conversion event of the complex node is triggered, and the node conversion logic of the conversion event of the complex node includes: If the C# language abstract syntax tree node has multiple child nodes, obtain a child node sequence of the C# language abstract syntax tree node, where the child node sequence includes N child nodes arranged in a target order, where N is the number of child nodes; Enter each target sub-node in sequence according to the target order; When entering the first child node, a left node in the JavaScript language abstract syntax tree is generated at the first child node, and the operator is placed in the operation node of the JavaScript language abstract syntax tree; When entering the i-th child node, generate a right node of the JavaScript language abstract syntax tree at the i-th child node, combine the left node, the right node and the operation node into an intermediate JavaScript language abstract syntax tree node, use the intermediate JavaScript language abstract syntax tree node to update the left node, and put the operator into the new operation node; wherein 1<i≤N; Until the Nth child node is exited, a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node is generated.

6. The code conversion method according to claim 1, characterized in that: The code conversion method also includes: The target C# language source code and the C# language abstract syntax tree are semantically analyzed to obtain the semantic analysis result, wherein the semantic analysis includes type system and type inference, scope and closure identification, method overloading and parameter polymorphism checking, and special feature and mode conversion checking.

7. A code conversion device, characterized in that: include: A C# syntax tree building unit, used to obtain a C# language abstract syntax tree of a target C# language source code to be converted; a node conversion unit, configured to traverse the C# language abstract syntax tree by using a listener mode, and for each C# language abstract syntax tree node, trigger a pre-configured conversion event of the C# language abstract syntax tree node when entering the C# language abstract syntax tree node, and obtain a candidate JavaScript language abstract syntax tree node of the C# language abstract syntax tree node; A node adjustment conversion unit, configured to adjust the candidate JavaScript language abstract syntax tree node according to the semantic analysis result when exiting the C# language abstract syntax tree node, to obtain a JavaScript language abstract syntax tree node of the C# language abstract syntax tree node; A JavaScript syntax tree construction unit is used to generate a JavaScript language abstract syntax tree corresponding to the C# language abstract syntax tree of the target C# language source code based on the JavaScript language abstract syntax tree nodes of each of the C# language abstract syntax tree nodes; The JavaScript compilation unit is used to compile the JavaScript language abstract syntax tree corresponding to the C# language abstract syntax tree to obtain the JavaScript code conversion result of the target C# language source code.

8. A computer program product, characterized in that The method comprises computer-readable instructions, and when the computer-readable instructions are executed on an electronic device, the electronic device implements the code conversion method according to any one of claims 1 to 6.

9. An electronic device, characterized in that: The method comprises at least one processor and a memory connected to the processor, wherein: The memory is used to store computer programs; The processor is used to execute the computer program so that the electronic device can implement the code conversion method according to any one of claims 1 to 6.

10. A computer storage medium, characterized in that: The storage medium carries one or more computer programs, and when the one or more computer programs are executed by an electronic device, the electronic device can implement the code conversion method as described in any one of claims 1 to 6.