Source code conversion method and electronic equipment

By converting the source code into intermediate code under the intermediate representation framework and using conversion rules to realize arbitrary language conversion, the problem of poor scalability of existing tools is solved and low-cost multilingual applicability conversion is achieved.

CN120447912APending Publication Date: 2025-08-08HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410174843.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-02-06
Publication Date
2025-08-08

AI Technical Summary

Technical Problem

The existing code conversion tools are strongly bound to the original language compiler, resulting in poor scalability and high development costs, and cannot effectively support flexible conversion between different programming languages.

Method used

By setting syntax rules, convert the source code of different languages into intermediate code under the intermediate representation framework, and use the conversion rules under the intermediate representation framework to achieve conversion between arbitrary languages, avoiding re-customizing the code conversion program.

Benefits of technology

It realizes high applicability conversion between any programming language, reduces development costs and improves scalability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120447912A_ABST
    Figure CN120447912A_ABST
Patent Text Reader

Abstract

The invention discloses a source code conversion method and electronic equipment, which are used for improving the applicability of source code conversion. The electronic device may display a second code of a second language in response to a first operation while displaying a first code of a first language. The first operation is used for converting a first code of a first language into a second code of a second language, the second code is obtained by performing language conversion on a first intermediate code based on a conversion rule, and the first intermediate code is obtained by analyzing the first code based on a grammar rule; the conversion rule is used for indicating a conversion relationship between the first language and the second language under the intermediate representation framework, and the grammar rule is used for analyzing the first code into a first intermediate code under the intermediate representation framework. Therefore, the electronic equipment can uniformly limit the architecture of the intermediate code generated in the conversion process as the intermediate representation framework by setting the grammar rule, and realizes the conversion between any languages by using the conversion rule under the intermediate representation framework, so that the applicability of the source code conversion method can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a source code conversion method and electronic device. Background Art

[0002] With the development of computer technology, programming languages are constantly evolving, with new languages emerging one after another. The need to convert existing engineering code from one language to another is becoming increasingly urgent.

[0003] Current code conversion tools (such as j2cl for Java to JavaScript and ts-migrate for JavaScript to TypeScript) are mostly tightly bound to the original language's compiler, used to convert source code into the target language. When converting between two languages, the compiler for each language parses the source code to generate intermediate code. During the conversion, a conversion program is written for this intermediate code to generate the corresponding code conversion tool, which is then used to perform the language conversion. This approach has low versatility and high development costs. Summary of the Invention

[0004] The embodiments of the present application provide a source code conversion method and electronic device, which converts source codes in different languages into intermediate codes under an intermediate representation framework by setting grammatical rules, and realizes conversion between arbitrary programming languages by setting conversion rules between arbitrary programming languages under the intermediate representation framework. It has high applicability and low development cost.

[0005] In a first aspect, the present application provides a source code conversion method, which is applied to an electronic device. The method comprises:

[0006] When displaying a first code in a first language, the electronic device displays a second code in a second language in response to a first operation triggered by a user. The first operation is used to convert the first code in the first language into a second code in the second language, the first language and the second language are different programming languages, the second code is obtained by performing language conversion on the first intermediate code based on a conversion rule, and the first intermediate code is obtained by parsing the first code based on a grammar rule, the conversion rule is used to indicate the conversion relationship between the first language and the second language in an intermediate representation framework, and the grammar rule is used to parse the first code into the first intermediate code in the intermediate representation framework.

[0007] In this method, the electronic device can parse the first code into a first intermediate code under an intermediate representation framework according to the grammar rules, and convert the first intermediate code under the intermediate representation framework into a second code in a second language according to the conversion rules. In this way, the electronic device can uniformly define the architecture of the intermediate code obtained by parsing the source code for language conversion as an intermediate representation framework by setting grammar rules, and realize conversion between any languages by setting conversion rules between any languages under the intermediate representation framework, so that the method provided by the present application has a strong applicability. In addition, the electronic device defines the language of the source code for language conversion through grammar rules and conversion rules, so that when converting a new language, it only needs to add the grammar rules and conversion rules corresponding to the new language to the grammar rules and conversion rules to realize the conversion of the new language, without having to customize the code conversion program for the new language. Therefore, the method provided by the present application also has the effects of strong scalability and low development cost.

[0008] In one possible design, the electronic device may parse the first code according to the grammatical rules of the first language to obtain a first intermediate code. The electronic device may also convert the language of the first intermediate code into a second language according to the conversion rules between the first language and the second language to obtain a second intermediate code. The second intermediate code is a code in the second language within the same intermediate representation framework as the first intermediate code. The electronic device may convert the second intermediate code into the second code according to the grammatical rules of the second language.

[0009] Through this design, electronic devices can use grammatical rules to uniformly limit the architecture of the intermediate code generated during the code conversion process to an intermediate representation architecture, so that different languages use the same intermediate representation architecture to represent the intermediate code, and then use the conversion rules between languages under the intermediate representation framework to perform language conversion on the first intermediate code, thereby realizing conversion between any languages, with strong applicability and low development cost.

[0010] In a possible design, the electronic device may read the grammar rules of the first language from a grammar rule library through a parsing engine, and parse the first code through the parsing engine to obtain the first intermediate code.

[0011] Through this design, the electronic device can use a parsing engine that reads grammar rules from a grammar rule library to parse the code, so that the parsing engine can parse codes in different languages without having to replace the parsing engine according to the language of the code to be converted from the source code, and has strong applicability.

[0012] In a possible design, the intermediate representation framework includes multiple framework elements, any one of the multiple framework elements is composed of multiple basic types, and any one of the multiple basic types is used to indicate the type of basic elements constituting the programming language.

[0013] In one possible design, the electronic device may parse the first code through a parsing engine to determine multiple first basic elements that constitute the first code; the electronic device may determine the first basic types corresponding to each of the multiple first basic elements from the grammatical rules of the first language through the parsing engine, and determine the framework elements composed of the multiple first basic types; the electronic device may generate a first intermediate code based on the multiple first basic elements and the framework elements through the parsing engine.

[0014] Through this design, the parsing engine in the electronic device can determine the framework elements corresponding to the multiple basic elements that constitute the first code based on the read grammatical rules, so that the architecture of the intermediate code parsed by the parsing engine can be uniformly limited to an intermediate representation framework, and then conversion between any languages can be realized, which is highly adaptable.

[0015] In one possible design, the electronic device may read the conversion rules between the first language and the second language from a conversion rule library through a conversion engine, and convert the first intermediate code into the second intermediate code through the conversion engine.

[0016] Through this design, electronic devices can realize conversion between any languages based on the conversion rules in the conversion rule library, with strong adaptability.

[0017] In one possible design, if the conversion rule library does not include conversion rules between the first language and the second language, the electronic device may also read the grammar rules of the first language and the grammar rules of the second language from the grammar rule library. The electronic device may establish a mapping relationship between the framework elements in the grammar rules of the first language and the framework elements in the grammar rules of the second language based on the intermediate representation framework, obtain the conversion rules between the first language and the second language, and store the obtained conversion rules in the conversion rule library.

[0018] Through this design, electronic devices can also create conversion rules between any languages based on the grammatical rules of different languages in the grammatical rule library, thereby realizing conversion between any languages with low development cost and strong scalability.

[0019] In one possible design, the electronic device may further determine a framework element in the first intermediate code through a conversion engine, and determine a conversion rule corresponding to the framework element from the conversion rules between the first language and the second language through the conversion engine. The conversion rule corresponding to the framework element is used to indicate a mapping relationship between multiple first basic types in the first language constituting the framework element and multiple second basic types in the second language constituting the framework element. The electronic device may further convert the first intermediate code into the second intermediate code through the conversion engine according to the conversion rule.

[0020] Through this design, the electronic device can use the conversion engine to read the conversion rules corresponding to the framework elements of the first intermediate code from the conversion rules between the first language and the second language, thereby realizing code conversion between any languages with strong adaptability.

[0021] In one possible design, the electronic device can determine the second basic elements corresponding to multiple second basic types based on multiple first basic elements and conversion rules in the first intermediate code; the electronic device can also generate a second intermediate code based on the framework element and multiple second basic elements.

[0022] Through this design, the electronic device can convert the first intermediate code into the second intermediate code under the intermediate representation framework according to the conversion rules, so as to uniformly define the architecture of the intermediate code generated during the code conversion process as the intermediate representation framework, thereby realizing conversion between any languages.

[0023] In a possible design, the electronic device may read the grammatical rules of the second language from a grammatical rule library through a parsing engine, and parse the second intermediate code through the parsing engine to obtain the second code.

[0024] Through this design, electronic devices can uniformly define the architecture of the intermediate code generated during the code conversion process as an intermediate representation framework based on grammatical rules to achieve conversion between arbitrary languages.

[0025] In a second aspect, an embodiment of the present application provides an electronic device, comprising one or more memories and one or more processors; wherein the one or more memories are used to store computer program code, and the computer program code comprises computer instructions; when the computer instructions are executed by the one or more processors, the electronic device executes the method described in the above-mentioned first aspect or any possible design of the first aspect.

[0026] In a third aspect, the present application provides a computer-readable storage medium storing a computer program. When the computer program runs on an electronic device, the electronic device executes the method described in the first aspect or any possible design of the first aspect.

[0027] In a fourth aspect, the present application provides a computer program product, which includes a computer program or instructions. When the computer program or instructions are run on an electronic device, the electronic device executes the method described in the first aspect or any possible design of the first aspect.

[0028] In a fifth aspect, the present application provides a chip system comprising a processor and a memory, wherein the memory stores instructions; when the instructions are executed by the processor, the method described in the first aspect or any possible design of the first aspect is implemented. The chip system may be composed of a chip alone, or may include a chip and other discrete components.

[0029] For the beneficial effects of the second to fifth aspects mentioned above, please refer to the description of the beneficial effects of the relevant content of the first aspect mentioned above, and no further details will be given here. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] Figure 1 This is a flow chart of source code conversion;

[0031] Figure 2 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application;

[0032] Figure 3 A software structure block diagram of an electronic device provided in an embodiment of the present application;

[0033] Figure 4 A schematic diagram of a source code conversion method provided in this application;

[0034] Figure 5 A schematic diagram of a display interface provided by this application;

[0035] Figure 6 Schematic diagram of the intermediate representation framework provided for this application;

[0036] Figure 7 A schematic diagram of the application architecture of an electronic device provided in this application;

[0037] Figure 8 This is a schematic diagram of the execution of a source code conversion example provided by this application. DETAILED DESCRIPTION

[0038] The technical solutions in the embodiments of the present application will be described in detail below in conjunction with the drawings in the following embodiments of the present application.

[0039] First, the concepts related to the embodiments of the present application are explained.

[0040] (1) An electronic device may be a device with a code conversion function.

[0041] In some embodiments of the present application, the electronic device may be a server.

[0042] In some embodiments of the present application, the electronic device may also be a terminal device. The terminal device may be a portable device, such as a mobile phone, a tablet computer, a wearable device with wireless communication capabilities (e.g., a watch, a bracelet, etc.), an in-vehicle terminal 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), a smart home device (e.g., a smart TV, a smart speaker, etc.), an intelligent robot, workshop equipment, a wireless terminal in self-driving, a wireless terminal in remote medical surgery, a wireless terminal in a smart grid, a wireless terminal in transportation safety, a wireless terminal in a smart city, a wireless terminal in a smart home, an aircraft device (e.g., an intelligent robot, a drone, an airplane), etc. A wearable device is a portable device that a user can wear directly on the body or integrate into the user's clothing or accessories.

[0043] In some embodiments of the present application, the electronic device may also be a portable terminal device that also includes other functions such as a personal digital assistant. Or a portable terminal device with other operating systems. The portable terminal device may also be other portable terminal devices, such as a laptop computer with a touch-sensitive surface (e.g., a touch panel). It should also be understood that in some other embodiments of the present application, the electronic device may not be a portable terminal device, but a desktop computer or other device.

[0044] (2) Syntax tree is an abstract representation of the grammatical structure of the source code. The syntax tree represents the grammatical structure of the programming language in a tree-like form, and each node on the tree represents a structure in the source code. The syntax tree is also called the abstract syntax tree. The reason why the syntax is "abstract" is that the syntax here does not represent every detail that appears in the real syntax. For example, nested parentheses are implicit in the tree structure and are not presented in the form of nodes; and conditional jump statements such as if-condition-then can be represented using a node with two branches. Syntax trees are generated by different syntax analyzers corresponding to different languages. These analyzers input the source code as a string and parse it to construct a syntax tree. For example, different languages may have different expression representations, such as infix expressions, postfix expressions, or prefix expressions, and their syntax trees will also show the differences in these expressions accordingly.

[0045] (3) Intermediate representation (IR) is the data structure or code used by the compiler to represent the source code. It is the program representation between the source language and the target language during the program compilation process.

[0046] (4) A statement is the smallest independent element in an imperative programming language in computer science. A computer program consists of a series of one or more statements. A single statement itself may have some internal structure (e.g., an expression). Many languages (e.g., C) clearly separate statements from definitions, because statements only have operators and some identifiers.

[0047] (5) Declaration refers to the "placeholder" instruction issued to the memory when a computer program needs to call memory space.

[0048] (6) Backus Normal Form (BNF), also known as Backus-Naur Form, is a context-free language. BNF is widely used in the grammatical representation of programming languages, instruction sets, and communication protocols. In various literatures, there are also some variants of BNF, such as Extended Backus Naur Form (ENBF) or Extended Backus Naur Form. Among them, the embodiment of the present application adopts the BNF-like form.

[0049] (7) Lexical analysis refers to the process of converting a sequence of characters into a sequence of words (tokens) in computer science.

[0050] In the embodiments of the present application, "at least one" refers to one or more, and "more" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: the existence of A alone, the existence of A and B at the same time, and the existence of B alone, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can represent: a, b, c, a and b, a and c, b and c, or a, b and c, where a, b, c can be single or multiple.

[0051] Furthermore, unless otherwise indicated, ordinal numbers such as "first" and "second" in the embodiments of this application are used to distinguish between multiple objects and are not used to limit the size, content, order, timing, priority, or importance of the multiple objects. For example, "second file" and "second file" are only used to distinguish different files and do not indicate a difference in size, content, priority, or importance between the two files.

[0052] Currently, most code conversion tools are strongly bound to the compiler of the original language and convert the source code into the target language code. Figure 1 As shown, source code 1 and source code 2 in two languages are converted to each other. The language of source code 1 is language 1, and the language of source code 2 is language 2. The conversion process of source code 1 is as follows: use the compiler of language 1 corresponding to source code 1 to parse source code 1 to generate syntax tree 1 of source code 1, use code conversion tool 1 of the language corresponding to source code 1 to intrusively modify syntax tree 1 to obtain syntax tree 1', and then use code conversion tool 1 to convert syntax tree 1' to source code 2. Among them, the intrusive modification of syntax tree 1 by code conversion tool 1 is a highly customized program, which is only applicable to the scenario of converting syntax tree 1 to language 2 and cannot be reused. In addition, the modified syntax tree 1' can be a complete syntax tree framework or an incomplete syntax tree framework. The conversion process of source code 2 is as follows: use the compiler of the language corresponding to source code 2 to parse source code 2 to generate syntax tree 2 of source code 2, use code conversion tool 2 of the language corresponding to source code 2 to convert syntax tree 2 to syntax tree 2', and then use code conversion tool 2 to convert syntax tree 2' to source code 1. The intrusive modification of syntax tree 2 by code conversion tool 2 is a highly customized program that is only applicable to the scenario of converting syntax tree 2 into language 2' and cannot be reused. In addition, the modified syntax tree 2' can be a complete syntax tree framework or an incomplete syntax tree framework.

[0053] Figure 1 In the example, syntax tree 1 is parsed by the compiler for language 1, and syntax tree 2 is parsed by the compiler for language 2. Because the compilers for language 1 and language 2 are different, and the different compilers output different syntax tree frameworks, the architecture of syntax tree 1 and syntax tree 2 differ. Furthermore, syntax tree 1' is a complete or incomplete framework obtained by intrusive modification of syntax tree 1. The rules for intrusive modification of syntax tree 1 in code conversion tool 1 are highly customized programs, meaning that the final framework of syntax tree 1' is based on the program written by the developer in code conversion tool 1. Therefore, the architecture of syntax tree 1' differs significantly from that of syntax tree 2. Similarly, the architecture of syntax tree 1 differs significantly from that of syntax tree 2'. In this case, code conversion tool 1 can only convert syntax tree 1 into syntax tree 1' and cannot intrusively modify syntax tree 2, output by the compiler of source code 2, into syntax tree 1. Similarly, code conversion tool 2 can only convert syntax tree 2 into syntax tree 2' and cannot intrusively modify syntax tree 1, output by the compiler of source code 1, into syntax tree 2. Therefore, when converting source code for a new language, users cannot use existing code conversion tools. Instead, they need to customize the code conversion tool based on the syntax tree structure generated by the compiler used by the source code. As a result, existing code conversion methods have poor scalability and high development costs.

[0054] In order to improve the scalability of source code conversion, an embodiment of the present application provides a source code conversion method, which is applied to an electronic device. In this method, the electronic device can convert the source code of the first language into a first intermediate code under an intermediate representation framework (IRframework) according to grammatical rules. The electronic device can convert the first intermediate code into a second intermediate code of the second language under the intermediate representation framework according to the conversion rules of the first language and the target code corresponding to the second language under the intermediate representation framework. The electronic device can also convert the second intermediate code under the intermediate representation framework into the target code according to the grammatical rules. Since the method provided by the embodiment of the present application can use grammatical rules to uniformly define the architecture of the intermediate code generated during the source code conversion process as an intermediate representation architecture, different languages use the same intermediate representation architecture to represent the intermediate code, and then use the conversion rules between languages under the intermediate representation framework to perform language conversion on the intermediate code, thereby achieving conversion between any languages; and when it is necessary to perform source code conversion on a new language, only the grammatical rules and conversion rules corresponding to the new language need to be added to perform source code conversion on the new language, thereby avoiding the user from rewriting a new code conversion program based on the architecture of the intermediate code parsed by the compiler of the new language, thereby having the effect of strong scalability and low development cost.

[0055] The embodiments of the present application will be described in detail below with reference to the accompanying drawings.

[0056] Figure 2 A possible hardware structure diagram of an electronic device is shown in FIG. Figure 2 As shown, the electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.

[0057] The processor 110 may include one or more processing units. For example, the processor 110 may include an application processor (AP), a microcontroller unit (MCU), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU). The different processing units may be independent devices or integrated into one or more processors. The controller may serve as the nerve center and command center of the electronic device 100. The controller may generate operation control signals based on instruction opcodes and timing signals to control instruction fetching and execution. The processor 110 may also include memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a high-speed cache memory. This memory may store instructions or data that have just been used or are being recycled by the processor 110. If the processor 110 needs to use the instruction or data again, it can directly call it from the memory. Repeated access is avoided, the waiting time of the processor 110 is reduced, and the efficiency of the system is improved.

[0058] In the embodiment of the present application, the processor 110 may convert the first code into a first intermediate code within the intermediate representation framework according to the grammatical rules of the first language, and convert the language of the first intermediate code from the first language to the second language according to the conversion rules between the first language and the second language, thereby obtaining a second intermediate code within the intermediate representation framework. Furthermore, the processor 110 may also convert the second intermediate code into a second code in the second language according to the grammatical rules of the second language, and display the second code on the display screen 194.

[0059] The USB interface 130 is an interface that complies with USB standards and specifications, and may be a Mini USB interface, a Micro USB interface, a USB Type-C interface, or the like. The USB interface 130 can be used to connect a charger to charge the electronic device 100, and can also be used to transfer data between the electronic device 100 and peripheral devices. The charging management module 140 is used to receive charging input from the charger. The power management module 141 is used to connect the battery 142, the charging management module 140, and the processor 110. The power management module 141 receives input from the battery 142 and / or the charging management module 140, and provides power to the processor 110, the internal memory 121, the external memory, the display 194, the camera 193, and the wireless communication module 160.

[0060] The wireless communication functionality of electronic device 100 can be implemented using antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, a modem processor, and a baseband processor. Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve antenna utilization. For example, antenna 1 can be reused as a diversity antenna for a wireless local area network. In other embodiments, the antennas can be used in conjunction with a tuning switch.

[0061] The mobile communication module 150 can provide solutions for wireless communications including 2G / 3G / 4G / 5G applied to the electronic device 100. The mobile communication module 150 may include at least one filter, a switch, a power amplifier, a low noise amplifier (LNA), etc. The mobile communication module 150 can receive electromagnetic waves from the antenna 1, and filter, amplify, and process the received electromagnetic waves, and transmit them to the modulation and demodulation processor for demodulation. The mobile communication module 150 can also amplify the signal modulated by the modulation and demodulation processor, and convert it into electromagnetic waves for radiation through the antenna 1. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the processor 110. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the same device as at least some of the modules of the processor 110.

[0062] The wireless communication module 160 can provide wireless communication solutions including wireless local area network (WLAN) (such as wireless fidelity (Wi-Fi) network), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared technology (IR), etc. applied to the electronic device 100. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, frequency modulates and filters the electromagnetic wave signal, and sends the processed signal to the processor 110. The wireless communication module 160 can also receive the signal to be sent from the processor 110, frequency modulate it, amplify it, and convert it into electromagnetic waves for radiation through the antenna 2.

[0063] In some embodiments, antenna 1 of electronic device 100 is coupled to mobile communication module 150, and antenna 2 is coupled to wireless communication module 160, so that electronic device 100 can communicate with a network and other devices via wireless communication technologies. The wireless communication technologies may include global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), wideband code division multiple access (WCDMA), time-division code division multiple access (TD-CDMA), long term evolution (LTE), the fifth generation (5G) mobile communication system, future communication systems such as the sixth generation (6G) system, BT, GNSS, WLAN, NFC, FM and / or IR technology, etc. The GNSS may include a global positioning system (GPS), a global navigation satellite system (GLONASS), a Beidou navigation satellite system (BDS), a quasi-zenith satellite system (QZSS) and / or a satellite based augmentation system (SBAS).

[0064] The display screen 194 is used to display target codes, images, videos, etc. The display screen 194 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode or an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-oLed, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the electronic device 100 may include 1 or N display screens 194, where N is a positive integer greater than 1. In an embodiment of the present application, the display screen 194 can be used to display a main interface, an application interface, granularity adjustment controls, etc.

[0065] The camera 193 is used to capture still images or videos. The camera 193 may include a front camera and a rear camera.

[0066] The internal memory 121 can be used to store computer executable program codes, which include instructions. The processor 110 executes various functional applications and data processing of the electronic device 100 by running the instructions stored in the internal memory 121. The internal memory 121 may include a program storage area and a data storage area. Among them, the program storage area can store an operating system, and software code of at least one application (such as Huawei Video, Changlian, etc.). The data storage area can store data (such as images, videos, etc.) generated during the use of the electronic device 100. In addition, the internal memory 121 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc.

[0067] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 via the external memory interface 120 to implement data storage functions. For example, files such as pictures and videos can be stored on the external memory card.

[0068] The electronic device 100 can implement audio functions such as music playback and recording through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone jack 170D, and the application processor.

[0069] It is understandable that Figure 2 The components shown do not constitute a specific limitation on the electronic device. The electronic device may also include more or fewer components than shown in the figure, or combine some components, or separate some components, or arrange the components differently. Figure 1 The electronic device 100 is taken as an example for introduction.

[0070] The software system of the electronic device 100 can adopt a layered architecture, an event-driven architecture, a micro-kernel architecture, a microservice architecture, or a cloud architecture. The layered architecture divides the software into several layers, each with a clear role and division of labor. The layers communicate with each other through software interfaces. For example, Figure 3 As shown in the figure, the software architecture can be divided into four layers, from top to bottom: application layer, application framework layer (framework, FWK), runtime and system library, and (Linux) kernel layer.

[0071] The application layer is the top layer of the operating system, including native applications of the operating system, such as camera, gallery, calendar, Bluetooth, music, video, etc., and may also include third-party applications. The application involved in the embodiment of the present application is referred to as application (APP), which is a software program that can realize one or more specific functions. Typically, multiple applications can be installed in an electronic device, such as a camera application. The applications mentioned below can be system applications that are installed on the electronic device when it leaves the factory, or they can be third-party applications that the user downloads from the Internet or obtains from other electronic devices while using the electronic device.

[0072] Of course, developers can write applications and install them into this layer. In one possible implementation, applications can be developed using the Java language by calling the application programming interface (API) provided by the application framework layer. Developers can use the application framework to interact with the underlying layer of the operating system (such as the kernel layer) and develop their own applications.

[0073] The application framework layer provides the application API and programming framework. It includes predefined functions and can include a window manager, content provider, view system, telephony manager, resource manager, and notification manager.

[0074] The window manager is used to manage window programs. The window manager can obtain the display size, determine whether there is a status bar, lock the display (or screen), capture the display, etc.

[0075] Content providers are used to store and retrieve data and make it accessible to applications. The data may include files (such as documents, videos, images, audio), text, and other information.

[0076] The view system includes visual controls, such as those that display text, images, and documents. The view system is used to build applications. The interface within a display window can be composed of one or more views. For example, the interface for a text notification icon might include a view that displays text and a view that displays an image.

[0077] The phone manager provides communication functionality for electronic devices. The notification manager enables applications to display notifications in the status bar, which can be used to convey informational messages and automatically disappear after a short period of time without user interaction.

[0078] The runtime includes the core library and the virtual machine. The runtime is responsible for the scheduling and management of the system.

[0079] The system's core library consists of two parts: one containing the Java language's callable functions and the other the system's core library. The application layer and application framework layer run within a virtual machine. For example, in Java, the virtual machine executes Java files from the application and framework layers as binary files. The virtual machine manages object lifecycles, stack management, thread management, security and exception management, and garbage collection.

[0080] The system library can include multiple functional modules. For example: code converter, media library, 3D graphics processing library (for example: OpenGL ES), 2D graphics engine (for example: SGL), image processing library, etc. The code conversion tool is used to implement code conversion between two languages. The media library supports playback and recording of a variety of common audio and video formats, as well as static image files, etc. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.564, MP3, AAC, AMR, JPG, PNG, etc. The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, synthesis and layer processing, etc. The 2D graphics engine is a drawing engine for 2D drawing.

[0081] The kernel layer provides the operating system's core system services, such as security, memory management, process management, the network protocol stack, and the driver model. These services are all implemented at the kernel layer. The kernel layer also serves as an abstraction layer between the hardware and software stacks. This layer contains many drivers related to electronic devices, including the display driver, the keyboard driver for input devices, the Flash driver for memory-based devices, the camera driver, the audio driver, the Bluetooth driver, and the Wi-Fi driver.

[0082] It should be understood that the functional services described above are only examples. In actual applications, electronic devices can also be divided into more or fewer functional services according to other factors, or the functions of each service can be divided in other ways, or the functional services can be not divided but work as a whole.

[0083] The present application is described below with reference to specific embodiments.

[0084] Figure 4 A schematic diagram of a source code conversion method provided in an embodiment of the present application. Figure 4 As shown, the method includes:

[0085] S401: The electronic device displays source code, wherein the source code is the code that the user wants to convert into a language, and the language of the source code can be any programming language.

[0086] The electronic device can display the source code in the display interface in response to the user's operation. The user's operation can be copy, cut, open, etc., but is not limited here. For example, Figure 5 As shown, the user can copy the first code that they want to convert to a different language as the source code to the source code area in the display interface. In response to the user's copy operation, the electronic device can display the first code in the first language in the source code area in the display interface. For another example, the user can also input the first code that they want to convert to a different language in the source code area. In response to the user's input operation, the electronic device displays the first code in the source code area in the display interface. For another example, the user can also open a document containing the first code in the source code area. In response to the user's open operation, the electronic device displays the first code in the document in the source code area in the display interface.

[0087] The electronic device can also determine the programming language corresponding to the source code and the programming language the user wants to convert to in response to the user's selection operation. Figure 5 As shown, the user can select or input the first language corresponding to the first code in the source language area, and select or input the second language to be converted into the language of the first code in the target language area.

[0088] S402: The electronic device displays a target code in response to a first operation by the user.

[0089] Among them, the target code can be the code of the target language that the user wants to convert the source code into. The first operation is used to convert the source code into the target code. The language of the source code and the target language of the target code are different programming languages. The target code is obtained by converting the language of the first intermediate code based on the conversion rules, and the first intermediate code is obtained by parsing the source code based on the grammar rules. The conversion rules are used to indicate the conversion relationship between the language of the source code and the target language under the intermediate representation framework, and the grammar rules are used to parse the source code into the intermediate code under the intermediate representation framework. In this way, the electronic device can use the grammar rules and conversion rules to uniformly define the architecture of the intermediate code generated during the source code conversion process as the intermediate representation framework. In this case, no matter what language the source code and the target code are, the electronic device can parse the source code into the intermediate code under the intermediate representation framework according to the grammar rules, and perform language conversion on the intermediate code under the intermediate representation framework based on the conversion rules to obtain the target code, thereby realizing code conversion between any languages, with strong versatility.

[0090] For example, Figure 5 As shown, the user can click the code conversion button in the display interface to trigger the code conversion operation. In response to the user-triggered operation, the electronic device can display the second code in the second language in the target code area of the display interface. The second code is the target code.

[0091] In some embodiments, the source code and target code in the source code conversion performed by the user are selected based on their own needs. For example, the user can use the first code in the first language as the source code and the second code in the second language as the target code; or, the user can also use the second code in the second language as the source code and the first code in the first language as the target code. The first language and the second language are different programming languages. The execution process of the electronic device converting the first code in the first language into the second code in the second language is the same as the execution process of converting the second code in the second language into the first code in the first language. The following takes the first code in the first language as the source code and the second code in the second language as the target code as an example to illustrate the process of determining the target code.

[0092] In some embodiments, the electronic device may convert the first code into the second code through the following steps A1 to A3.

[0093] A1: The electronic device may parse the first code according to grammatical rules corresponding to the first language to obtain a first intermediate code.

[0094] In some embodiments, the electronic device can determine the grammar rules corresponding to the first language from the grammar rule library. The grammar rule library can include grammar rules for multiple programming languages. For example, the grammar rules of JavaScript, the grammar rules of Python, and the grammar rules of C language can be stored together in the grammar rule library. For another example, the grammar rules of different programming languages can be stored in their respective corresponding grammar rule libraries. For example, the grammar rules of JavaScript can be stored in the JavaScript grammar rule library, the grammar rules of Python can be stored in the Python grammar rule library, and the grammar rules of C language can be stored in the C grammar library.

[0095] Syntax rules are generated based on a predefined intermediate representation framework to convert source code into a set of syntactic elements within the intermediate representation framework. Since source code can be in different languages, grammar rules are used to abstract code in different languages into a set of syntactic elements within the intermediate representation framework. While code in different languages has its own unique characteristics, the core of these codes is nothing more than a set of framework elements such as variable declarations, function declarations, conditional controls, and loop controls, composed of basic elements such as variables, data, and keywords. This means that different languages may have different basic elements, and the way these basic elements are combined into framework elements varies. For example, with regard to declaration statements, although different languages have their own methods for declaring variables, they all share a similar declaration structure. Therefore, declaration statements in different languages can be abstracted into a unified variable declaration statement structure. In this case, the present application can use grammar rules to abstract code in different languages into a set of syntactic elements within a unified framework structure. This unified framework structure can be the intermediate representation framework.

[0096] The intermediate representation framework includes but is not limited to: basic types and framework types. Among them, the basic type is the bottom layer of the intermediate code, and the framework type is the top layer of the intermediate code. Different framework types correspond to different framework elements, and different basic types correspond to different basic elements. The framework element is a collection of basic elements of multiple basic types, and the basic element can be called a syntax element. Exemplarily, the basic types include but are not limited to: variable name id, number (number), string (string), character data type (char), comment (comment), keyword key. Framework types include but are not limited to: program (program), statement (statement), declaration (declaration). For example, when the code is a script-type program, the framework type can include statement and declaration.

[0097] The middle part of the intermediate representation frame can be a frame element of the frame type, which is customized according to the code characteristics. For example, Figure 6As shown, the intermediate representation framework includes a program, which can be divided into statements and declarations. The statement framework includes multiple statement framework elements, including but not limited to: conditional judgment statements (ifstatement), loop statements (for statement), and while statements (while statement). The declaration framework includes multiple declaration framework elements, including but not limited to: variable declarations (variable declarations) and function declarations (function declarations). The ellipsis between the framework element and the basic type is used to represent the basic type set corresponding to the framework element.

[0098] In some examples, the grammar rule library can use BNF-like syntax to represent grammar rules. For example, the grammar rule corresponding to the variable declaration can be as follows:

[0099] <variabledeclaration>:

[0100] <let key> <id> <assignmentop> <literal>

[0101] | <var key> <id> <assignmentop> <literal>

[0102] |…

[0103] Among them, the symbol "|" in the above grammar rules represents a metacharacter, which means "or"; the angle brackets "<>" are non-terminal symbols; if a non-terminal symbol appears in the interpretation position of a grammar rule, it means that another grammar rule will interpret its position; the specific meaning of this rule is variable declaration <variabledeclaration>can be interpreted as<let key> <id> <assignmentop> <literal>or

[0104] <var key> <id> <assignmentop> <literal>or……

[0105] <let key> :

[0106] let

[0107] The specific meaning of this rule is<let key> The grammatical element is let;

[0108] <var key>:

[0109] var

[0110] The specific meaning of this rule is <var key>The syntax element is var;

[0111] <assignmentop>:

[0112] =

[0113] The specific meaning of this rule is <assignmentop>The grammatical elements are =;

[0114] <literal> :

[0115] <number>

[0116] | <string>

[0117] |…;

[0118] The specific meaning of this rule is <literal>The basic type is <number>or <string>or……

[0119] In other embodiments, when the electronic device fails to determine the grammatical rules of the first language from the grammatical rule library, the electronic device may determine that source code conversion cannot be performed and display a prompt message. The prompt message is used to prompt the user that the grammatical rules of the first language have not been read. When the user determines that the grammatical rules of the first language are not in the grammatical rule library, the user may develop the grammatical rules of the first language based on the intermediate representation framework. After the grammatical rules of the first language are developed, the user may trigger a save operation on the electronic device to store the developed grammatical rules of the first language in the grammatical rule library. The electronic device may store the grammatical rules of the first language in the grammatical rule library in response to the save operation triggered by the user. In addition, when the first language is a new language generated based on an old language, the user may develop based on the grammatical rules corresponding to the old language in the grammatical rule library, thereby reducing the development time of the grammatical rules of the new language.

[0120] After the grammatical rules of the first language are saved, the electronic device may further obtain the grammatical rules of the first language from the grammatical rule library in response to a first operation triggered by the user.

[0121] After the electronic device reads the grammar rules corresponding to the first language from the grammar rule library, it can use a parsing engine to parse the source code based on the grammar rules corresponding to the first language to generate a first intermediate code. The parsing engine is an engine that can read the grammar rules, parse the code, and generate an intermediate representation framework. In an embodiment of the present application, since code in any language can be used as source code for language conversion, the electronic device can use the parsing engine to parse source code in different languages. That is, during the language conversion process, the electronic device will use a unified parsing engine to parse the source code and will not change the parsing engine based on the language change of the source code.

[0122] In some examples, the electronic device may decompose the first code into multiple basic types based on the grammatical rules corresponding to the first language. For example, the electronic device may perform lexical analysis on the first code using a parsing engine to determine the multiple basic elements that constitute the first code. The electronic device may determine the basic types corresponding to each of the multiple basic elements based on the grammatical rules corresponding to the first language. The electronic device may also determine, based on the grammatical rules, a framework element composed of the multiple basic types and generate a first intermediate code. The first intermediate code includes the framework element and the first code.

[0123] For example, when the first language is JavaScript and the first code is let a=1, the electronic device uses a general parsing engine to read the grammar rules of the JavaScript language from the grammar rule library. The electronic device can use the parsing engine to perform lexical analysis on the first code and determine that the basic elements included in the first code are "let", "a", "=", and "1". The electronic device can determine, based on the JavaScript grammar rules, that the basic type of "let" is let key, the basic type of "a" is id, the basic type of "=" is AssignmentOp, and the basic type of "1" is "literal". The electronic device can determine from the grammar rules that the intermediate representation framework corresponding to the set of basic types of the first code is a variable declaration statement structure. Among them, the variable declaration statement structure in the JavaScript grammar rules is as follows:

[0124] <variabledeclaration>:

[0125] <let key> <id> <assignmentop> <literal>The electronic device can obtain the first intermediate code according to the determined variable declaration statement structure and the first code. The first intermediate code is as follows:

[0126] <variabledeclaration>

[0127] let a=1

[0128] < / variabledeclaration> .

[0129] For another example, when the first language is python and the first code is a=1, the electronic device uses a general parsing engine to read the grammar rules of python from the grammar rule library. The electronic device can use the parsing engine to perform lexical analysis on the first code and determine that the basic elements included in the first code are "a", "=", and "1". The electronic device can determine that the basic type of "a" is id, the basic type of "=" is AssignmentOp, and the basic type of "1" is "literal" based on the python grammar rules. The electronic device can determine that the intermediate representation framework corresponding to the set of basic types of the first code is a variable declaration statement structure based on the python grammar rules. Among them, the variable declaration statement structure in the python grammar rules is as follows:

[0130] <variabledeclaration> :

[0131] <id> <assignmentop> <literal>The electronic device can obtain the first intermediate code according to the determined variable declaration statement structure and the first code. The first intermediate code is as follows:

[0132] <variabledeclaration>

[0133] a=1

[0134] < / variabledeclaration> .

[0135] For another example, when the first language is C and the first code is int a=1, the electronic device uses a general parsing engine to read the grammar rules of C from the grammar rule library. The electronic device can use the parsing engine to perform lexical analysis on the first code and determine that the basic elements included in the first code are "int", "a", "=", and "1". The electronic device can determine, based on the C grammar rules, that the basic type of "int" is "type", the basic type of "a" is id, the basic type of "=" is AssignmentOp, and the basic type of "1" is "literal". Based on the C grammar rules, the electronic device can determine that the intermediate representation framework corresponding to the set of basic types of the first code is a variable declaration statement structure. Among them, the variable declaration statement structure in the C grammar rules is as follows:

[0136] <variabledeclaration> :

[0137] <type> <id> <assignmentop> <literal>The electronic device can obtain the first intermediate code according to the determined variable declaration statement structure and the first code. The first intermediate code is as follows:

[0138] <variabledeclaration>

[0139] int a=1

[0140] < / variabledeclaration> .

[0141] A2: The electronic device converts the language of the first intermediate code from the first language to the second language based on the conversion rule between the first language and the second language to obtain the second intermediate code.

[0142] Among them, the conversion rules are used to indicate the conversion relationship between the intermediate code of the first language and the intermediate code of the second language under the same intermediate representation framework. Exemplarily, the conversion rules are used to convert the grammatical declarations under the same framework type. That is, the conversion rules can indicate that the basic element sets corresponding to different languages under the same framework can be converted to each other. For example, when the first language is C and the second language is JavaScript, the conversion rules between C and JavaScript in the conversion rule library can be c2js conversion rules. Taking the variable declaration statement structure as an example, in the intermediate representation framework, the basic element set of the declaration statement structure of the first language is <type> <id> <assignmentop> <literal>, the basic element set of the second language declaration statement structure is<let key> <id> <assignmentop> <literal>, the c2js conversion rule can be <type> <id> <assignmentop> <literal>and<let key> <id> <assignmentop> <literal>They can be converted into each other.

[0143] In some examples, the electronic device can use a conversion engine to identify the first intermediate code and determine the framework element of the intermediate representation framework corresponding to the first intermediate code. The conversion engine refers to an engine that can read the conversion rules to convert the intermediate code. Since the electronic device uses a parsing engine to parse the source code of any language into the intermediate code of the intermediate representation architecture when performing source code conversion, when the electronic device performs language conversion on the intermediate code of any language, as long as there is a conversion rule corresponding to the language for language conversion, the conversion engine can be used to read the conversion rule for language conversion. The electronic device can use the conversion engine to read the conversion rule between the first language and the second language corresponding to the framework element from the conversion rule library. The conversion rule can also be stored in the conversion rule library in a BNF-like form.

[0144] In other examples, if the electronic device does not read the conversion rules between the first language and the second language from the conversion rule library, the electronic device can create the conversion rules between the first language and the second language based on the intermediate representation framework, the grammatical rules of the first language, and the grammatical rules of the second language, and store them in the conversion rule library. For example, the electronic device can establish a mapping relationship between the framework elements in the grammatical rules of the first language and the framework elements in the grammatical rules of the second language based on the intermediate representation framework to obtain the conversion rules between the first language and the second language.

[0145] For example, the grammatical rules of the first language are:

[0146] <variabledeclaration> :

[0147] <type> <id> <assignmentop> <literal>; The framework elements of the first language are the grammatical rules of the second language:

[0148] <variabledeclaration>:

[0149] <let key> <id> <assignmentop> <literal>The electronic device can determine the grammatical rules of the first language under the frame element variabledeclaration and the grammatical rules of the second language; the electronic device can establish a mapping relationship between the grammatical rules of the first language under the variabledeclaration and the grammatical rules of the second language under the variabledeclaration, that is, establish <type> <id> <assignmentop> <literal>and<let key> <id> <assignmentop> <literal>The mapping relationship is used to obtain the conversion rule. The conversion rule can be as follows:

[0150] <variabledeclaration> :

[0151] <type> <id> <assignmentop> <literal>←→<let key> <id> <assignmentop> <literal>;in,

[0152] <type> <id> <assignmentop> <literal>and<let key> <id> <assignmentop> <literal>They can be converted into each other.

[0153] The electronic device can use the conversion engine to convert the first intermediate code according to the conversion rules read to obtain the second intermediate code. Exemplarily, the conversion engine can determine the conversion rule corresponding to the framework element from the conversion rules based on the framework element in the first intermediate code. The conversion engine can determine the basic elements corresponding to the multiple basic types of the second language that constitute the framework element based on the multiple basic elements and conversion rules in the first intermediate code. The conversion engine can also generate the second intermediate code based on the framework element and the multiple basic elements of the determined second language. For example, when the first language is C and the second language is JavaScript, the conversion rule between C and JavaScript in the conversion rule library is the c2js conversion rule, and the first intermediate code is:

[0154] <variabledeclaration>

[0155] int a=1

[0156] < / variabledeclaration> ;

[0157] The electronic device uses a conversion engine to determine that a frame element of the intermediate representation frame of the first intermediate code is a variable declaration; the electronic device can read a C2JS conversion rule for the variable declaration from a conversion rule library. The C2JS conversion rule for the variable declaration can be as follows:

[0158] <variabledeclaration> :

[0159] <type> <id> <assignmentop> <literal>←→<let key> <id> <assignmentop> <literal>;in,

[0160] <type> <id> <assignmentop> <literal>and<let key> <id> <assignmentop> <literal>The electronic device can convert the first intermediate code into <type> <id> <assignmentop> <literal>The corresponding multiple basic elements int, a, =, 1 are converted to<let key> <id> <assignmentop> <literal>The corresponding basic element let,a,=,1,gets the second intermediate code. The second intermediate code is as follows:

[0161] <variabledeclaration>

[0162] let a=1

[0163] < / variabledeclaration> .

[0164] A3: The electronic device converts the second intermediate code into the second code according to the grammatical rules of the second language.

[0165] The electronic device can use a parsing engine to read the grammatical rules of the second language from a grammatical rule library and parse the second intermediate code based on the grammatical rules of the second language to obtain the second code. For example, the parsing engine and conversion engine in the electronic device can be two separate software programs or a single integrated software program with parsing and conversion functions.

[0166] Based on the above embodiment, the electronic device can, based on the grammar rules and conversion rules, uniformly define the framework of the intermediate code generated during the source code conversion process as an intermediate representation framework. In this way, the electronic device can use the parsing engine to read the grammar rules, parse the code of any language, obtain the intermediate code under the intermediate code architecture, and then combine the conversion rules under the unified intermediate code architecture to convert the language of the intermediate code to achieve conversion between any languages, with strong versatility. Moreover, when performing language conversion on the code of a new language, the user only needs to add the grammar rules and conversion rules of the new language on the basis of the original grammar rules and conversion rules, and the electronic device can complete the source code conversion of the new language, so that the source code conversion method provided by the above embodiment has the effect of strong development scalability and low development cost.

[0167] The content executed by the electronic device shown in the above embodiments may be executed by an application in the electronic device. Figure 7 This is an application architecture diagram of an electronic device provided in an embodiment of the present application. The electronic device may include a code converter, which is used to perform code language conversion. Figure 7 As shown, the transcoder may include a grammar rule base 10 , a conversion rule base 20 , a parsing engine 30 , and a conversion engine 40 .

[0168] The grammar rule library 10 is used to store the grammar rules of the language, and the grammar rules are used to realize the conversion between the source code and the intermediate code under the intermediate representation architecture.

[0169] The conversion rule library 20 is used to store conversion rules between intermediate codes in different languages. The conversion rules are used to implement conversion of intermediate codes in different languages under a unified intermediate representation framework.

[0170] Parsing engine 30 is used to parse source code and convert it into intermediate code. Parsing engine 30 is an engine that can read grammar rules, parse code, and generate an intermediate representation framework. For example, parsing engine 30 can read required grammar rules from a grammar rule library to convert source code into intermediate code. Parsing engine 30 can also convert intermediate code into target code based on the grammar rules.

[0171] The conversion engine 40 is used to convert the language of the intermediate code into the language of the target code according to the conversion rules. The conversion engine 40 is an engine that can read the conversion rules and convert the intermediate code.

[0172] like Figure 7 As shown, the parsing engine 30 and the conversion engine 40 in the code converter can be two separate software. In another possible situation, the parsing engine 30 and the conversion engine 40 in the code converter can also be an integrated software with parsing and conversion functions.

[0173] The transcoder can be a transcoder developed by the transcoder developer and installed in the electronic device, or it can be a fully developed transcoder installed in the electronic device. During the transcoder development process, the transcoder developer can define an intermediate representation framework to define the architecture of the intermediate code generated during the source code conversion process. The intermediate representation framework can be defined in the grammar rule library 10 or in the parsing engine 30, and there is no limitation on the definition area of the intermediate representation framework. The intermediate representation framework allows the transcoder developer to develop grammar rules for multiple languages and conversion rules between multiple languages.

[0174] Figure 8 A schematic diagram of a source code conversion example provided in an embodiment of the present application. This example includes two scenarios, one scenario in which the first code is the source code and the second code is the target code; the other scenario in which the first code is the target code and the second code is the source code. The execution process of the above two scenarios is the same. In this case, the source code conversion method provided in an embodiment of the present application can realize code interchange between different languages, and has strong versatility and applicability. The source code conversion example is explained below based on the scenario in which the first code is the source code and the second code is the target code in the above two scenarios.

[0175] The electronic device may obtain a first code, wherein the first code is in a first language. The electronic device may convert the first code in the first language into a second code in a second language in response to a first operation of a user, wherein the first language and the second language are different.

[0176] In some embodiments, the electronic device may use a parsing engine to perform grammatical parsing on the first code to obtain a first intermediate code. For example, the parsing engine in the electronic device may perform grammatical parsing on the first code to determine the basic elements of the first code. The parsing engine may read the grammatical rules corresponding to the first code from a grammatical rule library based on the basic elements of the first code. The parsing engine may convert the first code into the first intermediate code based on the determined grammatical rules.

[0177] like Figure 8 As shown, after obtaining the first intermediate code, the electronic device may further perform language conversion on the first intermediate code through a conversion engine to obtain a second intermediate code. For example, after reading the first intermediate code, the conversion engine may determine a conversion rule corresponding to the first intermediate code from a conversion rule library based on the first language and the second language of the second code. The conversion engine may convert the first intermediate code into the second intermediate code based on the determined conversion rule.

[0178] After obtaining the second intermediate code, the electronic device may further perform syntax parsing on the second intermediate code through a parsing engine, converting the second intermediate code into the second code. For example, the parsing engine of the electronic device may further perform syntax parsing on the second intermediate code, determine the framework corresponding to the second intermediate code, and generate the second code based on the second intermediate code. The parsing engine that performs syntax parsing on the second intermediate code is the same parsing engine that performs syntax parsing on the first code.

[0179] For example, the first language is C, the second language is JavaScript, and the first code and the second code are variable declarations in their respective corresponding languages. When the electronic device converts the variable declaration in the first language C into the declaration in the second language JavaScript, it converts the C type + variable name + equal sign + literal declaration method into the let + variable + equal sign + literal declaration method. The specific execution process of the electronic device performing language conversion is as follows: the electronic device can determine that the source code is the C variable declaration int a = 1; the electronic device can read the grammatical rules of variable declaration from the grammatical rule library of C. The electronic device can convert the source code into the first intermediate code according to the determined grammatical rules:

[0180] <variabledeclaration>

[0181] int a=1

[0182] < / variabledeclaration> ; Among them, the basic types corresponding to int, a, =, and 1 in the first intermediate code are type, id, AssignmentOp, and literal. The electronic device can also read the conversion rules of variable declarations from the c2js conversion rule library in the conversion rule library. The electronic device can use the universal conversion engine to convert the variable declarations according to the conversion rules read out. <type> <id> <assignmentop> <literal>and<let key> <id> <assignmentop> <literal>The conversion rule is to replace the language of the first intermediate code with JavaScript to obtain the second intermediate code. The second intermediate code is:

[0183] <variabledeclaration>

[0184] let a=1

[0185] < / variabledeclaration> The basic types corresponding to the basic elements let, a, =, and 1 in the second intermediate code are, respectively, let key, id, AssignmentOp, and literal. The electronic device can read the grammatical rules for variable declarations from the JavaScript grammar rule library. The electronic device processes the second intermediate code using a parsing engine to obtain the JavaScript code let a = 1.

[0186] In other embodiments, when the first language is a dynamic language and the second language is a static language, the electronic device can use a parsing engine to perform syntax analysis on the first code to obtain a first intermediate code. Figure 8 As shown, after obtaining the first intermediate code, the electronic device can also perform language conversion on the first intermediate code through a conversion engine to obtain a second intermediate code. Because the second language is a static language and the first language is a dynamic language, when converting the first intermediate code to the second intermediate code, the electronic device needs to determine the specific basic elements corresponding to the base type through type inference. For example, the electronic device can add a type inference plug-in to the code converter to infer the specific basic elements of the base type, complete the language conversion, and obtain the second intermediate code.

[0187] After obtaining the second intermediate code, the electronic device may further perform syntax analysis on the second intermediate code through a parsing engine to convert the second intermediate code into a second code.

[0188] For example, the first language is a dynamic language JavaScript, the second language is a static language C, and the first code and the second code are variable declarations in their respective languages. The electronic device can determine that the source code is a variable declaration leta=1 in JavaScript; the electronic device can read the grammar rules of variable declaration from the JavaScript grammar rule library. <variabledeclaration>:<let key> <id> <assignmentop> <literal>The electronic device can convert the source code into the first intermediate code according to the determined grammatical rule:

[0189] <variabledeclaration>

[0190] let a=1

[0191] < / variabledeclaration> ; Among them, the basic types corresponding to the basic elements let, a, =, 1 in the first intermediate code are let key, id, AssignmentOp, literal. The electronic device can also use the conversion engine to read the conversion rules of variable declaration from the c2js conversion rule library in the conversion rule library. The electronic device can use the conversion engine to read the conversion rules according to the conversion rules read out.

[0192] <type> <id> <assignmentop> <literal>and<let key> <id> <assignmentop> <literal>The rules of mutual conversion are to replace the language of the first intermediate code with JavaScript to obtain the second intermediate code. For example, the electronic device can convert the basic type set of "let a=1" in the first intermediate code into<let key> <id> <assignmentop> <literal>Replace with <type> <id> <assignmentop> <literal>, and determines the basic element of the replaced basic type. The electronic device can determine that the basic element of the replaced id is a, the basic element of AssignmentOp is =, and the basic element of literal is 1. However, the electronic device cannot directly determine the specific syntax element of the basic element of type. Therefore, the JavaScript first intermediate code cannot be directly converted into the second intermediate code of C. The electronic device can add a type analyzer to the code converter to analyze the specific basic elements of type, thereby outputting the second intermediate code of C:

[0193] <variabledeclaration>

[0194] int a=1

[0195] < / variabledeclaration> The electronic device may read the grammatical rules for variable declaration from the JavaScript grammatical rule library. The electronic device may process the second intermediate code through a parsing engine to obtain the second code of C: int a=1.

[0196] Based on the contents described in the above embodiments, the electronic device can convert the source code into an intermediate code under a unified framework, and perform language conversion under the intermediate code under the unified framework to obtain the target code, so that the electronic device can realize the mutual conversion of codes between any languages. Moreover, when converting a new language, the electronic device can connect the new language to the set intermediate representation framework for code conversion, which has strong scalability. When converting a new language based on an old language, the electronic device can develop the new language generated based on the old language based on the grammar rules of the old language in the grammar rule library, which shortens the development time.

[0197] Based on the above embodiments and the same technical concept, the present application also provides a source code conversion method, as shown in the following steps B1 to B2, which may include:

[0198] B1: The electronic device displays the first code of the first language.

[0199] For example, the first code may be the source code in the aforementioned embodiment. Regarding the source code, reference may be made to the description of the source code in the aforementioned embodiment, which will not be repeated here.

[0200] B2: The electronic device displays a second code in a second language in response to the first operation triggered by the user.

[0201] Among them, the first operation is used to convert a first code in a first language into a second code in a second language. The first language and the second language are different programming languages. The second code is obtained by language conversion of the first intermediate code based on the conversion rule. The first intermediate code is obtained by parsing the first code based on the grammar rule. The conversion rule is used to indicate the conversion relationship between the first language and the second language under the intermediate representation framework. The grammar rule is used to parse the first code into the first intermediate code under the intermediate representation framework.

[0202] Exemplarily, the first operation may be the first operation in the aforementioned embodiment, the second code may be the target code in the aforementioned embodiment, the grammatical rules may be the grammatical rules in the aforementioned embodiment, the conversion rules may be the conversion rules described in the aforementioned embodiment, the first intermediate code may be the first intermediate code in the aforementioned embodiment, and the intermediate representation framework may be the intermediate representation framework in the aforementioned embodiment. Regarding the first operation, reference may be made to the description of the first operation in the aforementioned embodiment, which will not be repeated here. Regarding the second code, reference may be made to the description of the target code in the aforementioned embodiment, which will not be repeated here. Regarding the grammatical rules, reference may be made to the description of the grammatical rules in the aforementioned embodiment, which will not be repeated here. Regarding the conversion rules, reference may be made to the description of the conversion rules in the aforementioned embodiment, which will not be repeated here. Regarding the first intermediate code, reference may be made to the description of the first intermediate code in the aforementioned embodiment, which will not be repeated here. Regarding the intermediate representation framework, reference may be made to the description of the intermediate representation framework in the aforementioned embodiment, which will not be repeated here.

[0203] In some embodiments of the present application, the intermediate representation framework includes multiple framework elements, any one of the multiple framework elements is composed of multiple basic types, and any one of the multiple basic types is used to indicate the type of basic elements constituting the programming language.

[0204] For example, the basic type is the basic type described in the aforementioned embodiment, and the framework element is the framework element described in the aforementioned embodiment. Regarding the basic type, please refer to the description of the basic type in the aforementioned embodiment, which will not be repeated here. Regarding the framework element, please refer to the description of the framework element in the aforementioned embodiment, which will not be repeated here.

[0205] In some embodiments of the present application, as an optional implementation, the electronic device may determine the second code through the following steps C1 to C3.

[0206] C1: The electronic device parses the first code according to the grammatical rules of the first language to obtain a first intermediate code.

[0207] In some embodiments of the present application, as an optional implementation, the electronic device can read the grammatical rules of the first language from the grammatical rules through a parsing engine, and parse the first code through the parsing engine to obtain a first intermediate code. The parsing engine is the parsing engine described in the previous embodiment. Regarding the parsing engine, reference can be made to the description of the parsing engine in the aforementioned embodiment, which will not be repeated here. Exemplarily, the electronic device can parse the first code through the parsing engine to determine the multiple first basic elements that constitute the first code; wherein, the multiple first basic elements are the multiple basic elements of the first code described in the aforementioned embodiment, which will not be described in detail here. The electronic device can determine the first basic types corresponding to the multiple first basic elements from the grammatical rules of the first language through the parsing engine, and determine the framework elements composed of the multiple first basic types; the electronic device can also generate the first intermediate code based on the multiple first basic elements and the framework elements through the parsing engine. Regarding the specific implementation of the method, reference can be made to the method for the electronic device to determine the first intermediate code described in the aforementioned embodiment, which will not be described in detail here.

[0208] C2: The electronic device converts the language of the first intermediate code into the second language according to the conversion rules between the first language and the second language, thereby obtaining a second intermediate code. The second intermediate code is a code in the second language within the same intermediate representation framework as the first intermediate code. The second intermediate code is the second intermediate code described in the preceding embodiment. For details about the second intermediate code, please refer to the description of the second intermediate code in the preceding embodiment and will not be repeated here.

[0209] In some embodiments of the present application, as an optional implementation, the electronic device may use a conversion engine to read conversion rules between the first language and the second language from a conversion rule library, and use the conversion engine to convert the first intermediate code into the second intermediate code. The conversion engine is the conversion engine described in the aforementioned embodiment. Regarding the conversion engine, reference may be made to the description of the conversion engine in the aforementioned embodiment, and will not be repeated here. Exemplarily, the electronic device may use the conversion engine to determine a framework element in the first intermediate code, and, from the conversion rules between the first language and the second language, determine a conversion rule corresponding to the framework element. The conversion rule corresponding to the framework element indicates a mapping relationship between multiple first basic types in the first language that constitute the framework element and multiple second basic types in the second language that constitute the framework element. The electronic device may also use the conversion engine to convert the first intermediate code into the second intermediate code based on the conversion rules. Exemplarily, the electronic device may also determine, based on the multiple first basic elements in the first intermediate code and the conversion rules, the second basic elements corresponding to each of the second basic types. The electronic device may also generate the second intermediate code based on the framework element and the multiple second basic elements. The specific implementation of this method may refer to the method for converting a first intermediate code into a second intermediate code described in the aforementioned embodiment, and will not be described in detail here.

[0210] Optionally, if the conversion rule library does not include conversion rules for the first language and the second language, the electronic device can also read the grammatical rules of the first language and the grammatical rules of the second language from the grammatical rule library; the electronic device can also establish a mapping relationship between the framework elements in the grammatical rules of the first language and the framework elements in the grammatical rules of the second language based on the intermediate representation framework to obtain conversion rules for the first language and the second language; the electronic device can also store the conversion rules between the first language and the second language in the conversion rule library. Regarding the specific implementation of this method, reference can be made to the method for the electronic device to recommend conversion rules described in the aforementioned embodiment, which will not be described in detail here.

[0211] C3: The electronic device converts the second intermediate code into a second code according to grammatical rules of the second language.

[0212] In some embodiments of the present application, the electronic device may also read the grammatical rules of the second language from the grammatical rule library through a parsing engine, and parse the second intermediate code through the parsing engine to obtain the second code. The specific implementation of this method can refer to the relevant methods described in the aforementioned embodiments and will not be described in detail here.

[0213] In the above method, the specific steps executed by the electronic device can be found in the relevant introduction of the above embodiments, and will not be described in detail here.

[0214] Based on the above content and the same technical concept, the present application provides an electronic device, including a memory and one or more processors, the memory is used to store computer program code, and the computer program code includes computer instructions; the one or more processors are used to execute the computer program instructions stored in the memory, so that the electronic device performs the steps performed by the electronic device in the above method embodiment.

[0215] Based on the above content and the same concept, the present application provides a computer-readable storage medium, which stores a computer program or instruction. When the computer program or instruction is executed by a computing device, the computing device executes the steps performed by the electronic device in the above method embodiment.

[0216] Based on the above content and the same concept, the present application provides a computer program product, which includes a computer program or instructions. When the computer program or instructions are executed by a computing device, the computing device executes the steps performed by the electronic device in the above method embodiment.

[0217] Those skilled in the art will appreciate that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application can adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.

[0218] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0219] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.

[0220] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0221] Obviously, those skilled in the art may make various changes and modifications to this application without departing from the spirit and scope of this application. Thus, if these modifications and variations of this application fall within the scope of the claims of this application and their equivalents, this application is intended to include these modifications and variations.< / literal> < / assignmentop> < / id> < / type> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / type> < / literal> < / assignmentop> < / id> < / variabledeclaration> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / type> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / type> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / type> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / type> < / variabledeclaration> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / type> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / type> < / variabledeclaration> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / type> < / literal> < / assignmentop> < / id> < / variabledeclaration> < / literal> < / assignmentop> < / id> < / type> < / variabledeclaration> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / type> < / literal> < / assignmentop> < / id> < / literal> < / assignmentop> < / id> < / type> < / literal> < / assignmentop> < / id> < / type> < / variabledeclaration> < / literal> < / assignmentop> < / id> < / variabledeclaration> < / literal> < / assignmentop> < / id> < / variabledeclaration> < / string> < / number> < / literal> < / string> < / number> < / literal> < / assignmentop> < / assignmentop> < / var> < / var> < / literal> < / assignmentop> < / id> < / var> < / literal> < / assignmentop> < / id> < / variabledeclaration> < / literal> < / assignmentop> < / id> < / var> < / literal> < / assignmentop> < / id> < / variabledeclaration>

Claims

1. A source code conversion method, characterized in that: The method comprises: The first code showing the first language; In response to a first operation triggered by a user, a second code in a second language is displayed; wherein, the first operation is used to convert the first code in the first language into a second code in the second language, the first language and the second language are different programming languages, the second code is obtained by language conversion of the first intermediate code based on a conversion rule, and the first intermediate code is obtained by parsing the first code based on a grammar rule, the conversion rule is used to indicate the conversion relationship between the first language and the second language under an intermediate representation framework, and the grammar rule is used to parse the first code into the first intermediate code under the intermediate representation framework.

2. The method according to claim 1, characterized in that The second code is determined by: Parsing the first code according to the grammatical rules of the first language to obtain the first intermediate code; Converting the language of the first intermediate code into the second language according to a conversion rule between the first language and the second language to obtain a second intermediate code, where the second intermediate code is a code in the second language within the same intermediate representation framework as the first intermediate code; The second intermediate code is converted into the second code according to the grammatical rules of the second language.

3. The method according to claim 2, characterized in that The step of parsing the first code according to the grammatical rules of the first language to obtain the first intermediate code includes: Reading the grammatical rules of the first language from a grammatical rule library through a parsing engine; The first code is parsed by the parsing engine to obtain the first intermediate code.

4. The method according to any one of claims 1 to 3, characterized in that The intermediate representation framework includes multiple framework elements, any one of the multiple framework elements is composed of multiple basic types, and any one of the multiple basic types is used to indicate the type of basic elements constituting a programming language.

5. The method according to claim 3 or 4, characterized in that The step of parsing the first code by the parsing engine to obtain the first intermediate code includes: Parsing the first code by the parsing engine to determine a plurality of first basic elements constituting the first code; Determining, by the parsing engine, from the grammatical rules of the first language, the first basic types corresponding to the respective first basic elements, and determining a framework element composed of the plurality of first basic types; The first intermediate code is generated by the parsing engine according to the multiple first basic elements and the framework elements.

6. The method according to any one of claims 2 to 5, characterized in that: The converting the language of the first intermediate code into the second language according to the conversion rule between the first language and the second language to obtain the second intermediate code includes: Reading conversion rules between the first language and the second language from a conversion rule library through a conversion engine; The first intermediate code is converted into the second intermediate code by the conversion engine.

7. The method according to claim 6, characterized in that If the conversion rule library does not include conversion rules between the first language and the second language, the method further includes: Reading the grammar rules of the first language and the grammar rules of the second language from the grammar rule library; Establishing a mapping relationship between frame elements in the grammatical rules of the first language and frame elements in the grammatical rules of the second language based on the intermediate representation framework to obtain conversion rules between the first language and the second language; The conversion rules between the first language and the second language are stored in the conversion rule library.

8. The method according to claim 7, characterized in that The converting the first intermediate code into the second intermediate code by the conversion engine includes: Determining, by the conversion engine, a framework element in the first intermediate code; determining, by the conversion engine, a conversion rule corresponding to the framework element from conversion rules of the first language and the second language, the conversion rule corresponding to the framework element being used to indicate a mapping relationship between a plurality of first basic types in the first language constituting the framework element and a plurality of second basic types in the second language constituting the framework element; The first intermediate code is converted into the second intermediate code according to the conversion rule by the conversion engine.

9. The method according to claim 8, characterized in that The converting the first intermediate code into the second intermediate code according to the conversion rule includes: Determining, according to the plurality of first basic elements in the first intermediate code and the conversion rule, second basic elements corresponding to the plurality of second basic types; The second intermediate code is generated according to the framework element and a plurality of second basic elements.

10. The method according to any one of claims 2 to 9, characterized in that: The converting the second intermediate code into the second code according to the grammatical rules of the second language includes: reading the grammar rules of the second language from a grammar rule library through a parsing engine; The second intermediate code is parsed by the parsing engine to obtain the second code.

11. An electronic device, characterized in that: include: one or more processors; one or more memories; The one or more memories are used to store one or more computer programs and data information; wherein the one or more computer programs include instructions; When the instructions are executed by the one or more processors, the electronic device is caused to perform the method according to any one of claims 1 to 10.

12. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed on the electronic device, the electronic device performs the method according to any one of claims 1 to 10.