Compiling method and related device
By adding custom compilation rules to the compilation phase of the host language compiler and generating a second compiler, the problem of internal DSL language developers being restricted by the host language is solved, and flexible expression and security of the first language are achieved.
Patent Information
- Application Number
- PCT/CN2025/070687
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2024-06-17
- Filing Date
- 2025-01-06
- Publication Date
- 2025-10-09
AI Technical Summary
Internal DSL language developers are restricted by the host language compiler when developing or ordinary developers use internal DSL to write code, and cannot achieve flexible expression.
By adding custom compilation rules during the compilation phase of the host language compiler, a second compiler for compiling the first language is generated to achieve conversion from the first language to the host language and/or constraints and optimization of the first language code, including lexical and grammatical conversion rules, constraints and optimization rules.
It improves the flexible expression ability of the internal DSL, reduces the restrictions of the host language on the first language, avoids the emergence of non-compliant or destructive code, and improves the security and operation efficiency of the code.
Smart Images

Figure CN2025070687_09102025_PF_FP_ABST
Abstract
Description
A compilation method and related device
[0001] This application claims priority to the Chinese patent application with application number 202410408204.2 filed with the State Intellectual Property Office of China on April 3, 2024, priority to the Chinese patent application with invention name “A compilation method”, priority to the Chinese patent application with application number 202410783110.3 filed with the State Intellectual Property Office of China on June 17, 2024, and priority to the Chinese patent application with invention name “A compilation method and related equipment”, all contents of which are incorporated by reference into this application. Technical Field
[0002] The present application relates to the field of computers, and more specifically, to a compilation method, a computing apparatus, a computing device cluster, a chip system, a computer program product, and a computer-readable storage medium. Background Art
[0003] Current domain-specific languages (DSLs) include external DSLs and internal DSLs. External DSLs are designed based on the domain's independent syntax and independently implement compilers and toolchains, resulting in a high creation workload. In other words, external DSLs require language developers to independently develop a programming language based on the domain's characteristics and develop a compiler for that language, allowing ordinary developers to use that language and compiler to develop code files with specific functionality. Internal DSLs are designed based on the host language and domain, leveraging the host language's DSL capabilities (such as syntactic sugar) and compiling and running them with the host language's compiler and other toolchains, resulting in a lower creation workload. In other words, internal DSLs require language developers to develop specific code statements based on the host language's rules and compile them using the host language's compiler. This means that internal DSL language developers do not need to develop a separate compiler. Because internal DSL languages require the host language's compiler for compilation, internal DSL language developers face significant limitations when developing the language, as do ordinary developers when writing code using the internal DSL, hindering the flexible expression of internal DSLs.
[0004] Therefore, how to improve the flexible expression capability of internal DSL becomes an urgent problem to be solved. Summary of the Invention
[0005] The present application provides a compilation method, a computing apparatus, a computing device cluster, a chip system, a computer program product, and a computer-readable storage medium, which can improve the flexible expression capability of internal DSL.
[0006] In a first aspect, a compilation method is provided. The method includes: receiving a first code block, the first code block indicating compilation rules added after a first stage of a first compiler, the first compiler being configured to compile a code file written in a host language; generating a second compiler based on the first code block and the first compiler, the second compiler being configured to compile a code file written in a first language, the first language being different from the host language and being a DSL; wherein the compilation rules are configured to convert the first language into the host language, and / or the compilation rules are configured to constrain and / or optimize the code file written in the first language.
[0007] In an embodiment of the present application, a DSL language developer or cloud vendor can add custom compilation rules during the compilation phase of the host language compiler to generate a compiler for compiling code files written in the first language, thereby achieving conversion from the first language to the host language, or constraining and optimizing the code written in the first language, thereby reducing the restrictions of the host language on the first language and improving the flexible expression capabilities of the first language.
[0008] Illustratively, the first language is an internal DSL developed by a DSL language developer based on the host language. The embodiment of the present application does not limit the specific type of the first language.
[0009] In combination with the first aspect, in some implementations, the first stage includes one or two of the following compilation stages: a lexical analysis stage, a syntax analysis stage, a semantic analysis stage, and an intermediate representation (IR) conversion stage.
[0010] In combination with the first aspect, in some implementations, when the first stage includes one of the compilation stages, the first code block is added to after the first stage of the first compiler, and the second compiler is generated; or, when the first stage includes two of the compilation stages, the first code block is added between two of the compilation stages included in the first stage of the first compiler, and the second compiler is generated.
[0011] In an embodiment of the present application, a DSL language developer or a cloud vendor may determine, based on the function of the first code block, the specific location at which to add the first code block to the compilation stage included in the first compiler, thereby facilitating implementation of the function corresponding to the first code block when the second compiler compiles a code file written in the first language.
[0012] In combination with the first aspect, in some implementations, the compilation rules include at least one of the following: lexical conversion rules between the first language and the host language, grammatical conversion rules between the first language and the host language, constraints of the first language or the host language, and optimization rules of the host language; wherein the lexical conversion rules are used to convert phrases in a code file written in the first language into phrases or abstract syntax trees (AST) that conform to the lexical rules of the host language, the grammatical conversion rules are used to convert the syntax in the code file written in the first language into the syntax that conforms to the host language, the constraints are used to constrain the AST and / or IR corresponding to the code file written in the first language, and the optimization rules are used to execute and / or optimize part or all of the calculation processes in the code file written in the first language.
[0013] In an embodiment of the present application, a DSL language developer or a cloud vendor can add lexical conversion rules and / or grammatical conversion rules during the compilation phase of the first compiler, thereby converting the code written in the first language into code that conforms to the lexical rules and / or grammatical rules of the host language, and then compiling it according to the second compiler. Since the newly added compilation rules can realize the conversion between the first language and the host language, the host language restrictions on ordinary developers when using the first language can be reduced, thereby improving the flexible expression ability of the first language. In addition, the DSL language developer can add constraints and / or optimization rules during the compilation phase of the first compiler to avoid the appearance of destructive code statements in the code file written in the first language, or improve the running efficiency of the code file written in the first language, thereby improving the flexible expression ability of the first language.
[0014] In combination with the first aspect, in some implementations, the lexical analysis stage is used to decompose a code file written in the host language into at least one phrase, the syntax analysis stage is used to generate an untyped AST based on at least one phrase, the semantic analysis stage is used to generate a typed AST based on at least one phrase or the untyped AST, and the IR conversion stage is used to generate IR based on the typed AST.
[0015] In combination with the first aspect, in some implementations, lexical conversion rules are added between the lexical analysis stage and the syntactic analysis stage of the first compiler, or the lexical conversion rules are added between the lexical analysis stage and the semantic analysis stage of the first compiler; or, the syntactic conversion rules are added between the syntactic analysis stage and the semantic analysis stage of the first compiler, or the syntactic conversion rules are added between the lexical analysis stage and the semantic analysis stage of the first compiler; or, the constraints are added between the syntactic analysis stage and the semantic analysis stage of the first compiler, or the constraints are added between the lexical analysis stage and the semantic analysis stage of the first compiler, or the constraints are added after the IR conversion stage of the first compiler; or, the optimization rules are added between the semantic analysis stage and the IR conversion stage of the first compiler, or, the optimization rules are added after the IR conversion stage of the first compiler.
[0016] In an embodiment of the present application, the compilation rules newly added by the DSL language developer or cloud vendor can be added to the first compiler after any of the lexical analysis stage, syntax analysis stage, semantic analysis stage, or IR conversion stage, thereby generating a compiler for compiling code files written in the first language, thereby improving the flexible expression capabilities of the first language.
[0017] In conjunction with the first aspect, in some implementations, the constraint condition includes at least one of the following: a constraint on the type of data in a code file written in the first language, and a constraint on data flow and / or control flow in the IR.
[0018] In the embodiments of the present application, DSL language developers or cloud vendors can use newly added constraints to prevent ordinary developers who use the first language from developing destructive code. For example, this can prevent problems such as the data type in the code not conforming to the rules of the first language, or the code contacting or accessing data that it is not allowed to contact or access.
[0019] In combination with the first aspect, in some implementations, the first code block is written according to the host language, the first code block includes a code statement for importing the target code library, and the target code library includes at least one code statement written according to the host language and used to compile the code written according to the host language.
[0020] In an embodiment of the present application, a DSL language developer or cloud vendor calls code (e.g., methods, functions) in a target code library, so that the first code block can implement corresponding functions during the compilation phase of the first compiler, thereby improving the flexible expression capability of the first language.
[0021] In combination with the first aspect, in some implementations, first request information and a first code block are received, where the first request information is used to request that a compilation rule be added after a first stage of a first compiler.
[0022] In combination with the first aspect, in some implementations, first request information is received, and the first request information is used to request the addition of new compilation rules in a first compiler; in response to the first request information, a first graphical interface is displayed, and the first graphical interface is used to receive a first stage and / or a first code block.
[0023] In combination with the first aspect, in some implementations, first request information is received, and the first request information is used to request the addition of new compilation rules after the first stage in the first compiler; in response to the first request information, a first graphical interface is displayed, and the first graphical interface is used to receive a first code block.
[0024] In an embodiment of the present application, a DSL language developer can input a request for adding a new compilation rule, the new compilation rule, and the compilation phase corresponding to the compilation rule through the same graphical interface. Alternatively, the DSL language developer can first input a request for adding a new compilation rule, and then input the specific compilation rule and the compilation phase corresponding to the compilation rule in the first graphical interface in response. Alternatively, the DSL language developer can first input a request for adding a new compilation rule, indicate the compilation phase corresponding to the new compilation rule in the request, and then input the specific compilation rule in the first graphical interface in response.
[0025] In combination with the first aspect, in some implementations, a second code block is received, the second code block is used to indicate a check rule, and the check rule is used to check during the compilation phase of a code file written in a first language, and the second code block is written in a host language; based on the second code block and the second compiler, a third compiler is generated, and the third compiler is used to check the code file written in the first language.
[0026] In an embodiment of the present application, DSL language developers and cloud vendors can also generate a third compiler by adding new checking rules during the compilation phase of code files written in the first language, thereby checking the code files written in the first language, avoiding problems such as code statements in the code files written in the first language not complying with the rules of the first language, or problems such as code statements with destructive effects in the code files written in the first language, thereby improving the security control capabilities of the code files written in the first language, and hoping to improve the flexible expression capabilities of the first language.
[0027] In some embodiments, the rules of the first language include lexical rules and / or grammatical rules. The lexical rules include rules for tokens in the source code written in the first language, and the grammatical rules include rules for the grammar in the source code written in the first language. Similarly, the rules of the host language include lexical rules and / or grammatical rules. The lexical rules include rules for tokens in the source code written in the host language, and the grammatical rules include rules for the grammar in the source code written in the host language.
[0028] In combination with the first aspect, in some implementations, the checking rules include at least one of the following: a checking rule for at least one phrase in a code file written in the first language, a checking rule for the AST corresponding to the code file written in the first language, and a checking rule for the IR corresponding to the code file written in the first language.
[0029] In an embodiment of the present application, DSL language developers or cloud vendors can add check rules in each compilation stage of code files written in the first language, thereby performing security checks on code files written in the first language, thereby improving security control capabilities.
[0030] In some embodiments, the AST corresponding to the code file written in the first language includes: an untyped AST and / or a typed AST.
[0031] In combination with the first aspect, in some implementations, the inspection rules also include: shielding rules, disabling rules, and editing rules. The shielding rules are used to shield alarms when the rules between the first language and the host language are different. The disabling rules are used to issue alarms for code statements that are allowed by the host language but not allowed by the first language. The editing rules are used to issue alarms for code statements that do not comply with the rules of the first language and / or code statements that damage the environment when writing code according to the first language.
[0032] In the embodiments of the present application, DSL language developers or cloud vendors can enhance the flexible expression capabilities of the first language by adding blocking rules and / or disabling rules during the compilation phase of code files written in the first language. Furthermore, by adding editing rules during the compilation phase of code files written in the first language, timely warnings can be issued to ordinary developers using the first language during the code writing process, preventing ordinary developers from writing code statements that do not comply with the rules of the first language and / or have destructive effects.
[0033] In combination with the first aspect, in some implementations, second request information and a second code block are received, where the second request information is used to request adding a compilation rule during a compilation phase of a code file written in a first language.
[0034] In combination with the first aspect, in some implementations, a second request message is received, and the second request message is used to request the addition of new compilation rules in the compilation stage of a code file written in the first language; in response to the second request message, a second graphical interface is displayed, and the second graphical interface is used to receive a second code block and / or the compilation stage corresponding to the second code block.
[0035] In an embodiment of the present application, a DSL language developer can input a request to add a new check rule, along with the newly added check rule and the corresponding compilation phase of the check rule, through the same graphical interface. Alternatively, the DSL language developer can first input a request to add a new check rule, and then, in a second graphical interface in response, input the specific check rule and the corresponding compilation phase of the check rule. Alternatively, the DSL language developer can first input a request to add a new check rule, indicate the corresponding compilation phase of the newly added check rule in the request, and then, in a second graphical interface in response, input the specific check rule.
[0036] In combination with the first aspect, in some implementations, a first source code is checked according to a third compiler, where the first source code is a code file written according to a first language; when there is a problem with the first source code, an alarm is issued; when there is no problem with the first source code, the first source code is compiled according to a second compiler to obtain a target executable file.
[0037] In an embodiment of the present application, the first source code may be checked by a third compiler before being compiled to prevent the first source code from containing code statements that do not comply with the rules and / or code statements that have destructive effects. After the third compiler checks, if the first source code has no problems, the second compiler may compile the first source code to obtain a target executable file for executing the first source code.
[0038] In some embodiments, the first source code having a problem includes: at least one code statement in the first source code not complying with at least one check rule in the third compiler. The first source code having no problem includes: each code statement in the first source code complying with each check rule in the third compiler.
[0039] In a second aspect, a compilation method is provided. The method includes: receiving a second code block, the second code block indicating a check rule, the check rule being used to check during the compilation phase of a code file written in a first language, the second code block being written in a host language, the first language being different from the host language, and the first language being a DSL; generating a third compiler based on the second compiler and the check rule, the third compiler being used to check the code file written in the first language, the second compiler being used to compile the code file written in the first language, wherein during the compilation of the code file written in the first language by the second compiler, the first language is converted into the host language.
[0040] In an embodiment of the present application, DSL language developers and cloud vendors can generate a third compiler by adding new checking rules during the compilation phase of code files written in the first language, thereby checking code files written in the first language, avoiding problems such as code statements in the code files written in the first language not complying with the rules of the first language or code files written in the first language including destructive code statements, thereby improving the security control capabilities of code files written in the first language and improving the flexible expression capabilities of the first language.
[0041] In some embodiments, during the process of the second compiler compiling the code file written in the first language, the first language is converted into the host language, including: during the process of the second compiler compiling the code file written in the first language, phrases that conform to the lexical rules of the first language are converted into phrases that conform to the lexical rules of the host language, and / or, grammar that conforms to the grammatical rules of the first language is converted into grammar that conforms to the grammatical rules of the host language.
[0042] In some embodiments, the rules of the first language include lexical rules and / or grammatical rules. The lexical rules include rules for tokens in the source code written in the first language, and the grammatical rules include rules for the grammar in the source code written in the first language. Similarly, the rules of the host language include lexical rules and / or grammatical rules. The lexical rules include rules for tokens in the source code written in the host language, and the grammatical rules include rules for the grammar in the source code written in the host language.
[0043] In combination with the second aspect, in some implementations, the checking rules include at least one of the following: a checking rule for at least one phrase in a code file written in the first language, a checking rule for the AST corresponding to the code file written in the first language, and a checking rule for the IR corresponding to the code file written in the first language.
[0044] In some embodiments, the AST corresponding to the code file written in the first language includes: an untyped AST and / or a typed AST.
[0045] In combination with the second aspect, in some implementations, the inspection rules also include: shielding rules, disabling rules, and editing rules. The shielding rules are used to shield alarms when the rules between the first language and the host language are different. The disabling rules are used to issue alarms for code statements that are allowed by the host language but not allowed by the first language. The editing rules are used to issue alarms for code statements that do not comply with the rules of the first language and / or code statements that damage the environment when writing code according to the first language.
[0046] In conjunction with the second aspect, in some implementations, second request information and a second code block are received, where the second request information is used to request adding a compilation rule during a compilation phase of a code file written in a first language.
[0047] In combination with the second aspect, in some implementations, a second request message is received, and the second request message is used to request the addition of new compilation rules in the compilation stage of a code file written in the first language; in response to the second request message, a second graphical interface is displayed, and the second graphical interface is used to receive a second code block and / or the compilation stage corresponding to the second code block.
[0048] In combination with the second aspect, in some implementations, a first source code is checked according to a third compiler, where the first source code is a code file written in a first language; when there is a problem with the first source code, an alarm is issued; when there is no problem with the first source code, the first source code is compiled according to the second compiler to obtain a target executable file.
[0049] In some embodiments, the first source code having a problem includes: at least one code statement in the first source code not complying with at least one check rule in the third compiler. The first source code having no problem includes: each code statement in the first source code complying with each check rule in the third compiler.
[0050] It should be understood that the technical effects that can be achieved by certain implementations of the second aspect are similar to the technical effects that can be achieved by certain implementations of the corresponding first aspect, and will not be repeated here.
[0051] In a third aspect, a computing device is provided. The computing device includes a transceiver unit and a processing unit. The transceiver unit is configured to receive a first code block, the first code block being configured to indicate compilation rules added after the first stage of a first compiler, the first compiler being configured to compile code files written in a host language; the processing unit is configured to generate a second compiler based on the first code block and the first compiler, the second compiler being configured to compile code files written in a first language, the first language being different from the host language and being a DSL; wherein the compilation rules are configured to convert the first language into the host language, and / or the compilation rules are configured to constrain and / or optimize the code written in the first language.
[0052] In conjunction with the third aspect, in some implementations, the first stage includes one or two of the following compilation stages: a lexical analysis stage, a syntax analysis stage, a semantic analysis stage, and an IR conversion stage.
[0053] In combination with the third aspect, in some implementations, the processing unit is specifically used to: when the first stage includes one of the compilation stages, add the first code block to after the first stage of the first compiler to generate the second compiler; or, when the first stage includes two of the compilation stages, add the first code block between two of the compilation stages included in the first stage of the first compiler to generate the second compiler.
[0054] In combination with the third aspect, in some implementations, the compilation rules include at least one of the following: lexical conversion rules between the first language and the host language, grammatical conversion rules between the first language and the host language, constraints of the first language or the host language, and optimization rules of the host language; wherein the lexical conversion rules are used to convert phrases in a code file written in the first language into phrases or ASTs that conform to the lexical rules of the host language, the grammatical conversion rules are used to convert the grammar in a code file written in the first language into the grammar that conforms to the host language, the constraints are used to constrain the AST and / or IR corresponding to the code file written in the first language, and the optimization rules are used to execute and / or optimize part or all of the calculation processes in the code file written in the first language.
[0055] In combination with the third aspect, in some implementations, lexical conversion rules are added between the lexical analysis stage and the syntactic analysis stage of the first compiler, or the lexical conversion rules are added between the lexical analysis stage and the semantic analysis stage of the first compiler; or, the syntactic conversion rules are added between the syntactic analysis stage and the semantic analysis stage of the first compiler, or the syntactic conversion rules are added between the lexical analysis stage and the semantic analysis stage of the first compiler; or, the constraints are added between the syntactic analysis stage and the semantic analysis stage of the first compiler, or the constraints are added between the lexical analysis stage and the semantic analysis stage of the first compiler, or the constraints are added after the IR conversion stage of the first compiler; or, the optimization rules are added between the semantic analysis stage and the IR conversion stage of the first compiler, or, the optimization rules are added after the IR conversion stage of the first compiler.
[0056] In conjunction with the third aspect, in some implementations, the constraint condition includes at least one of the following: a constraint on the type of data in a code file written in the first language, and a constraint on data flow and / or control flow in the IR.
[0057] In combination with the third aspect, in some implementations, the lexical analysis stage is used to decompose a code file written in the host language into at least one phrase, the syntax analysis stage is used to generate an untyped AST based on at least one phrase, the semantic analysis stage is used to generate a typed AST based on at least one phrase or the untyped AST, and the IR conversion stage is used to generate IR based on the typed AST.
[0058] In combination with the third aspect, in some implementations, the first code block is written according to the host language, the first code block includes code statements for importing the target code library, and the target code library includes at least one code statement written according to the host language and used to compile the code written according to the host language.
[0059] In combination with the third aspect, in some implementations, the transceiver unit is specifically used to receive first request information and a first code block, where the first request information is used to request that a new compilation rule be added after the first stage of the first compiler.
[0060] In combination with the third aspect, in some implementations, the transceiver unit is also used to: receive first request information, the first request information is used to request the addition of new compilation rules in the first compiler; in response to the first request information, display a first graphical interface, the first graphical interface is used to receive the first stage and / or the first code block.
[0061] In combination with the third aspect, in some implementations, the transceiver unit is further used to: receive a first request message, the first request message being used to request adding new compilation rules after the first stage in the first compiler; and display a first graphical interface in response to the first request message, the first graphical interface being used to receive a first code block.
[0062] In combination with the third aspect, in some implementations, the transceiver unit is further used to receive a second code block, the second code block is used to indicate a check rule, and the check rule is used to perform a check during the compilation phase of a code file written in a first language, and the second code block is written in a host language; the processing unit is further used to generate a third compiler based on the second code block and the second compiler, and the third compiler is used to check the code file written in the first language.
[0063] In combination with the third aspect, in some implementations, the checking rules include at least one of the following: a checking rule for at least one phrase in a code file written in the first language, a checking rule for the AST corresponding to the code file written in the first language, and a checking rule for the IR corresponding to the code file written in the first language.
[0064] In combination with the third aspect, in some implementations, the inspection rules also include: shielding rules, disabling rules, and editing rules. The shielding rules are used to shield alarms when the rules between the first language and the host language are different. The disabling rules are used to issue alarms for code statements that are allowed by the host language but not allowed by the first language. The editing rules are used to issue alarms for code statements that do not comply with the rules of the first language and / or code statements that damage the environment when writing code according to the first language.
[0065] In combination with the third aspect, in some implementations, the transceiver unit is further configured to receive second request information and a second code block, where the second request information is used to request adding a compilation rule during a compilation phase of a code file written in the first language.
[0066] In combination with the third aspect, in some implementations, the transceiver unit is also used to: receive a second request message, the second request message is used to request the addition of new compilation rules in the compilation stage of the code file written in the first language; in response to the second request message, display a second graphical interface, the second graphical interface is used to receive the second code block and / or the compilation stage corresponding to the second code block.
[0067] In combination with the third aspect, in some implementations, the processing unit is further used to: check the first source code according to the third compiler, where the first source code is a code file written according to the first language; issue an alarm when there is a problem with the first source code; and compile the first source code according to the second compiler to obtain a target executable file when there is no problem with the first source code.
[0068] In a fourth aspect, a computing device is provided. The computing device includes a transceiver unit and a processing unit. The transceiver unit is configured to receive a second code block, the second code block being configured to indicate a check rule, the check rule being configured to be checked during the compilation phase of a code file written in a first language, the second code block being written in a host language, the first language being different from the host language, and the first language being a DSL; the processing unit is configured to generate a third compiler based on the second compiler and the check rule, the third compiler being configured to check the code file written in the first language, the second compiler being configured to compile the code file written in the first language, wherein the first language is converted into the host language during the compilation of the code file written in the first language by the second compiler.
[0069] In combination with the fourth aspect, in some implementations, the checking rules include at least one of the following: a checking rule for at least one phrase in a code file written in the first language, a checking rule for the AST corresponding to the code file written in the first language, and a checking rule for the IR corresponding to the code file written in the first language.
[0070] In combination with the fourth aspect, in some implementations, the inspection rules also include: shielding rules, disabling rules, and editing rules. The shielding rules are used to shield alarms when the rules between the first language and the host language are different. The disabling rules are used to issue alarms for code statements that are allowed by the host language but not allowed by the first language. The editing rules are used to issue alarms for code statements that do not comply with the rules of the first language and / or code statements that damage the environment when writing code according to the first language.
[0071] In conjunction with the fourth aspect, in some implementations, the transceiver unit is used to receive second request information and a second code block, where the second request information is used to request adding a compilation rule during a compilation phase of a code file written in the first language.
[0072] In combination with the fourth aspect, in some implementations, the transceiver unit is also used to: receive a second request message, the second request message is used to request the addition of new compilation rules in the compilation stage of the code file written in the first language; in response to the second request message, display a second graphical interface, the second graphical interface is used to receive the second code block and / or the compilation stage corresponding to the second code block.
[0073] In combination with the fourth aspect, in some implementations, the processing unit is further used to: check the first source code according to the third compiler, where the first source code is a code file written according to the first language; issue an alarm when there is a problem with the first source code; and compile the first source code according to the second compiler to obtain a target executable file when there is no problem with the first source code.
[0074] In a fifth aspect, a computing device cluster is provided, comprising at least one computing device. The computing device includes a processor, the processor being coupled to a memory to read and execute instructions and / or program code in the memory to perform the method described in any of the first or second aspects or any possible implementation of either aspect.
[0075] In the sixth aspect, a chip system is provided, which includes a logic circuit, which is used to couple with an input / output interface and transmit data through the input / output interface to execute the method described in any aspect of the first aspect or the second aspect or any possible implementation of any aspect.
[0076] In a seventh aspect, a computer-readable medium is provided, which stores computer program instructions. When the computer program instructions are executed on a computing device cluster, the computing device cluster executes the method described in any one of the first aspect or the second aspect or any possible implementation of any one of the aspects.
[0077] In an eighth aspect, a computer program product comprising instructions is provided, which, when executed on a computing device cluster, causes the computing device cluster to execute the method described in any one of the first aspect or the second aspect or any possible implementation of any one of the aspects. BRIEF DESCRIPTION OF THE DRAWINGS
[0078] FIG1 is a schematic structural block diagram of a compilation system according to an embodiment of the present application.
[0079] FIG2 is a schematic diagram of the compilation phase of a first compiler according to an embodiment of the present application.
[0080] FIG3 is a schematic flowchart of a compilation method according to an embodiment of the present application.
[0081] FIG4 is a schematic flowchart of a compilation method according to another embodiment of the present application.
[0082] FIG5 is a schematic diagram of the compilation phase of a second compiler according to an embodiment of the present application.
[0083] FIG6 is a schematic flowchart of a compilation method according to another embodiment of the present application.
[0084] FIG7 is a schematic diagram of the compilation stage of a third compiler according to an embodiment of the present application.
[0085] FIG8 is a schematic flowchart of a compilation method according to another embodiment of the present application.
[0086] FIG9 is a schematic flowchart of a compiling and running method according to an embodiment of the present application.
[0087] FIG10 is a schematic structural block diagram of a computing device according to an embodiment of the present application.
[0088] FIG11 is a schematic structural diagram of a computing device according to an embodiment of the present application.
[0089] FIG12 is a schematic structural diagram of a computing device cluster according to an embodiment of the present application.
[0090] FIG13 is a schematic diagram of a connection between computing devices 1100A and 1100B via a network according to one embodiment of the present application. DETAILED DESCRIPTION
[0091] The technical solution in this application will be described below with reference to the accompanying drawings.
[0092] The embodiments of the present application will present various aspects, embodiments, or features around a system including multiple devices, components, modules, etc. It should be understood and appreciated that each system may include additional devices, components, modules, etc., and / or may not include all of the devices, components, modules, etc. discussed in conjunction with the figures. Furthermore, combinations of these solutions may also be used.
[0093] Additionally, in the embodiments of this application, words such as "exemplary" and "for example" are used to indicate examples, illustrations, or descriptions. Any embodiment or design described as "exemplary" in the embodiments of this application should not be construed as being preferred or advantageous over other embodiments or designs. Rather, the use of the word "exemplary" is intended to present concepts in a concrete manner.
[0094] The business scenarios described in the embodiments of the present application are intended to more clearly illustrate the technical solutions of the embodiments of the present application, and do not constitute a limitation on the technical solutions provided in the embodiments of the present application. Ordinary technicians in this field will know that with the evolution of technology and the emergence of new business scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0095] References to "one embodiment" or "some embodiments" in this specification mean that a particular feature, structure, or characteristic described in conjunction with that embodiment is included in one or more embodiments of the present application. Thus, phrases such as "in one embodiment," "in some embodiments," "in other embodiments," and "in yet other embodiments" appearing in various places in this specification do not necessarily refer to the same embodiment, but rather mean "one or more but not all embodiments," unless otherwise specifically emphasized. The terms "including," "comprising," "having," and variations thereof mean "including but not limited to," unless otherwise specifically emphasized.
[0096] 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: including 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 items or plural items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, c can be single or multiple.
[0097] The technical solutions in the embodiments of the present application can be applied to computing devices, such as servers, host computers, personal computers, laptop computers, desktop computers, and other devices with computing capabilities. When the technical solutions in the embodiments of the present application are applied to a server, the server can be a local server or a cloud server, etc., and the embodiments of the present application are not limited to this.
[0098] Figure 1 is a schematic structural block diagram of a compilation system provided by an embodiment of the present application. The compilation system 100 in Figure 1 includes a first computing device 110, and the first computing device 110 includes a compilation module 112. The compilation module 112 can receive user input, such as a first code block. The compilation module 112 can also generate a second compiler based on the first code block and the first compiler. The first code block is used to indicate the compilation rules newly added after the first stage of the first compiler. The first compiler is used to compile code files written in the host language, that is, the first compiler is a compiler for the host language. The second compiler is used to compile a first source code, which is a code file written in the first language, that is, the second compiler is a compiler for the first language. The first language is different from the host language. The first language is a DSL. The compilation rules are used to convert the first language into the host language, and / or, the compilation rules are used to constrain and / or optimize the first source code.
[0099] In some embodiments, the first language and the host language being different includes: at least one lexical rule and / or at least one grammatical rule specified by the first language and the host language being different.
[0100] Illustratively, the difference between at least one lexical rule defined in the first language and the host language includes: a first lexical rule defined in the first language and a second lexical rule defined in the host language have similar functions, but the first lexical rule and the second lexical rule are different; or the lexical rules defined in the first language include lexical rules not defined in the host language; or the lexical rules defined in the first language do not include one or more lexical rules defined in the host language. Similarly, the difference between at least one grammatical rule defined in the first language and the host language includes: a first grammatical rule defined in the first language and a second grammatical rule defined in the host language have similar functions, but the first grammatical rule and the second grammatical rule are different; or the grammatical rules defined in the first language include grammatical rules not defined in the host language; or the grammatical rules defined in the first language do not include one or more grammatical rules defined in the host language.
[0101] In some embodiments, the compilation rules include at least one of the following: lexical conversion rules between the first language and the host language, grammatical conversion rules between the first language and the host language, constraints of the first language or the host language, and optimization rules of the host language. The lexical conversion rules are used to convert phrases in the first source code (i.e., phrases that conform to the lexical rules of the first language) into phrases or abstract syntax trees (ASTs) that conform to the lexical rules of the host language. The grammatical conversion rules are used to convert the syntax in the first source code (i.e., syntax that conforms to the first language) into syntax that conforms to the host language. The constraints are used to constrain the AST and / or intermediate representation (IR) corresponding to the first source code. The AST includes an untyped AST and / or a typed AST. The untyped AST includes: an untyped AST that conforms to the grammatical rules of the first language and / or an untyped AST that conforms to the grammatical rules of the host language. The typed AST includes a typed AST that conforms to the grammatical rules of the host language. The data in the untyped AST does not include data type information, while the data in the typed AST includes data type information. The optimization rules are used to execute and / or optimize part or all of the computational processes in the first source code.
[0102] In some embodiments, the first computing device 110 further includes a storage module 111. The storage module 111 is configured to store intermediate data or final results generated by the first computing device 110 during execution of the methods of the embodiments of the present application. For example, the storage module 111 is configured to store at least one of the following: a first compiler, a first code block, a second compiler, a second code block, a third compiler, an object code library, etc.
[0103] In some embodiments, when the first compiler compiles the code file, it includes at least one of the following compilation stages: a lexical analysis stage, a syntax analysis stage, a semantic analysis stage, and an IR conversion stage, as shown in Figure 2. Figure 2 is a schematic diagram of the compilation stages included when the first compiler compiles the code file. Among them, the lexical analysis stage is used to decompose the second source code into at least one phrase (token). The second source code is a code file written in accordance with the host language. The syntax analysis stage is used to generate an untyped AST based on the at least one token. The semantic analysis stage is used to generate a typed AST based on the untyped AST. The IR conversion stage is used to generate an IR based on the typed AST.
[0104] In some embodiments, the first phase includes one of the compilation phases, and the first code block is added after the first phase. Alternatively, the first phase includes two of the compilation phases, and the first code block is added between the two compilation phases included in the first phase.
[0105] Exemplarily, the compilation phase of the first compiler is the compilation phase that the code file goes through when the first compiler compiles the code file.
[0106] In some embodiments, the compilation module 112 is used to add lexical conversion rules to after the lexical analysis phase of the first compiler. For example, the compilation module 112 is used to add lexical conversion rules between the lexical analysis phase and the grammatical analysis phase of the first compiler, or between the lexical analysis phase and the semantic analysis phase of the first compiler. The compilation module 112 is also used to add grammatical forwarding rules to the lexical analysis phase or after the grammatical analysis phase of the first compiler. For example, the compilation module 112 is also used to add grammatical forwarding rules between the grammatical analysis phase and the semantic analysis phase of the first compiler, or between the lexical analysis phase and the semantic analysis phase of the first compiler. The compilation module 112 is also used to add constraints to after any one of the lexical analysis phase, the grammatical analysis phase, or the IR conversion phase of the first compiler. For example, the compilation module 112 is also used to add constraints between the grammatical analysis phase and the semantic analysis phase of the first compiler, or between the lexical analysis phase and the semantic analysis phase of the first compiler, or after the IR conversion phase of the first compiler. The compiler 112 is further configured to add the optimization rule to the semantic analysis phase or after the IR conversion phase of the first compiler. For example, the compiler 112 is further configured to add the optimization rule between the semantic analysis phase and the IR conversion phase of the first compiler, or after the IR conversion phase of the first compiler.
[0107] In some embodiments, the constraint condition includes at least one of the following: a constraint on the type of data included in the first source code, a security constraint on the data flow and / or control flow in the IR. The constraint on the type of data included in the first source code is used to prevent users who use the first language to develop project code from defining unallowed types for data. The security constraint on the data flow and / or control flow in the IR is used to prevent users who use the first language to develop project code from developing code that destroys public scenarios (such as multi-tenant public scenarios) or application environments, etc.
[0108] In some embodiments, the first code block is written in a host language. The first code block includes a code statement for importing a target code library. The target code library includes at least one code statement written in the host language and used to compile code written in the host language.
[0109] In some embodiments, the compilation module 112 is further configured to receive first request information for requesting to add a compilation rule after the first phase of the first compiler, or for requesting to add a compilation rule in the first compiler.
[0110] Exemplarily, the compilation module 112 receives the first request information and the first code block at the same time. Alternatively, the compilation module 112 receives the first request information, which is used to request the addition of a new compilation rule in the first compiler. In response to the first request information, the compilation module 112 displays a first graphical interface, thereby receiving a specific compilation stage and a first code block in the first graphical interface. Alternatively, the compilation module 112 receives the first request information, which is used to request the addition of a new compilation rule after the first stage of the first compiler. In response to the first request information, the compilation module 112 displays a first graphical interface, thereby receiving the first code block in the first graphical interface. The embodiment of the present application does not limit the specific form of expression of the first graphical interface.
[0111] In some embodiments, the first computing device 110 may further include a checking module 113. The checking module 113 is configured to receive the second code block and generate a third compiler based on the second code block and the second compiler. The second code block is configured to indicate a checking rule, which is used to perform a check during the compilation phase of the first source code. The second code block is written in the host language. The third compiler is configured to check the first source code.
[0112] In some embodiments, the checking rule includes at least one of the following: a checking rule for at least one phrase in the first source code, a checking rule for an AST corresponding to the first source code, and a checking rule for an IR corresponding to the first source code.
[0113] Exemplarily, the AST corresponding to the first source code includes an untyped AST and / or a typed AST.
[0114] In some embodiments, the inspection rules also include: shielding rules, disabling rules, and editing rules. The shielding rules are used to shield alarms when the rules between the first language and the host language are different. The disabling rules are used to issue alarms for code statements that are allowed by the host language but not allowed by the first language. The editing rules are used to issue alarms for code statements that do not comply with the rules of the first language and / or code statements that destroy the environment when writing code according to the first language. Among them, the rules of the first language include lexical rules and / or grammatical rules, the lexical rules include rules for phrases (tokens) in the source code written in the first language, and the grammatical rules include rules for grammar in the source code written in the first language. Similarly, the rules of the host language include lexical rules and / or grammatical rules. Among them, the lexical rules include rules for tokens in the source code written in the host language, and the grammatical rules include rules for grammar in the source code written in the host language.
[0115] In some embodiments, the inspection module 113 receives the second request information and the second code block at the same time. Alternatively, the inspection module 113 receives the second request information, and the second request information is used to request the addition of a new compilation rule in the second compiler. In response to the second request information, the inspection module 113 displays a second graphical interface, thereby receiving a specific compilation stage and a second code block in the second graphical interface. Alternatively, the inspection module 113 receives the second request information, and the second request information is used to request the addition of a new compilation rule after the second stage of the second compiler. In response to the second request information, the inspection module 113 displays a second graphical interface, thereby receiving a second code block in the second graphical interface. The embodiment of the present application does not limit the specific form of expression of the second graphical interface. The second stage includes one or two of the compilation stages shown in Figure 2.
[0116] In some embodiments, first computing device 110 obtains a first source code and checks the first source code using a third compiler. If there is a problem with the first source code, first computing device 110 issues an alert. If there is no problem with the first source code, first computing device 110 compiles the first source code using a second compiler to obtain a target executable file.
[0117] In some embodiments, the first source code having a problem includes: at least one code statement in the first source code not complying with at least one check rule in a third compiler. The first source code having no problem includes: every code statement in the first source code complying with every check rule in the third compiler. The check rules in the third compiler include: the check rules in the second compiler and newly added check rules.
[0118] In some embodiments, the first computing device 110 further includes a running module 114. The running module 114 is configured to run a target executable file corresponding to the first source code.
[0119] In some embodiments, when the execution module 114 executes the target executable file, it calls a first instruction to check the first source code. The first instruction is generated based on a third code block. The third code block is used to indicate a runtime check rule that is newly added during the execution phase of the first source code. The runtime check rule is used to check the target executable file when it is executed, thereby achieving the purpose of checking the first source code.
[0120] Exemplarily, the first computing device 110 is further configured to receive a third code block and compile the third code block to generate the first instruction.
[0121] Exemplarily, the runtime checking rule includes a checking rule for code statements in the first source code, such as a check for the array length of a specific array.
[0122] When the first computing device 110 is a remote computing device, the compilation system 100 further includes a second computing device 120. The second computing device 120 is a user device. The user can input at least one of the following through the second computing device 120: first request information, first code block, second request information, second code block, third code block, etc. The second computing device 120 can transmit the user input to the first computing device 110.
[0123] When the compilation system 100 includes the second computing device 120, some modules in the first computing device 110 can be deployed in the second computing device. For example, one or more of the storage module 111, compilation module 112, inspection module 113, and execution module 114 can be deployed in the second computing device 120.
[0124] In some embodiments, the first computing device 120 and / or the second computing device 130 belong to a computing device cluster. The computing device cluster includes at least one computing device. Any computing device in the computing device cluster can execute the method in the embodiment of the present application, or multiple computing devices in the computing device cluster can jointly execute the method in the embodiment of the present application. The multiple computing devices in the computing device cluster can be directly connected or connected through a network, which can be, for example, a wide area network or a local area network. When the first computing device 110 belongs to a computing device cluster, one or more of the storage module 111, the compilation module 112, the inspection module 113, and the execution module 114 can be deployed in one or more computing devices in the computing device cluster.
[0125] Figure 3 is a schematic flow chart of a compilation method provided by an embodiment of the present application. The method in Figure 3 can be executed by the computing device in Figure 1 (e.g., the first computing device 110 or the second computing device 120). The method in Figure 3 includes the following steps.
[0126] S310: Receive a first code block.
[0127] A computing device receives a first code block indicating compilation rules added after the first stage of a first compiler. The first compiler is used to compile code files written in a host language, i.e., the first compiler is a compiler for the host language. The compilation rules are used to convert the first language into the host language and / or to constrain and / or optimize the first source code. The first source code is a code file written in the first language. The first language is different from the host language. The first language is a DSL.
[0128] In some embodiments, the first language and the host language being different includes: at least one lexical rule and / or at least one grammatical rule specified by the first language and the host language being different.
[0129] Illustratively, the difference between at least one lexical rule defined in the first language and the host language includes: a first lexical rule defined in the first language and a second lexical rule defined in the host language have similar functions, but the first lexical rule and the second lexical rule are different; or the lexical rules defined in the first language include lexical rules not defined in the host language; or the lexical rules defined in the first language do not include one or more lexical rules defined in the host language. Similarly, the difference between at least one grammatical rule defined in the first language and the host language includes: a first grammatical rule defined in the first language and a second grammatical rule defined in the host language have similar functions, but the first grammatical rule and the second grammatical rule are different; or the grammatical rules defined in the first language include grammatical rules not defined in the host language; or the grammatical rules defined in the first language do not include one or more grammatical rules defined in the host language.
[0130] In some embodiments, the first language is an internal DSL developed based on the host language. For example, the lexical rules and / or grammatical rules of the first language are designed and developed based on the lexical rules and / or grammatical rules of the host language. When a code file written in the first language is compiled by a first compiler, code statements in the code file are converted into tokens and / or untyped ASTs that conform to the lexical rules and / or grammatical rules of the host language, thereby achieving compilation.
[0131] Optionally, the computing device displays a third graphical interface for receiving the first request information and the first code block. The first request information is used to request the addition of a new compilation rule after the first phase of the first compiler. In other words, the user enters the first request information and the first code block in the third graphical interface, causing the computing device to receive the first request information and the first code block. The embodiments of the present application do not limit the specific form of the third graphical interface.
[0132] Optionally, the computing device displays a third graphical interface, which is used to receive a first request message, and the first request message is used to request that a new compilation rule be added to the first compiler. In response to the first request message, the computing device displays a first graphical interface, which is used to receive the first stage, or the first graphical interface is used to receive the first stage and the first code block. When the first graphical interface is used to receive the first stage, the computing device displays a fourth graphical interface in response to the first stage, which is used to receive the first code block. The embodiment of the present application does not limit the specific form of expression of the fourth graphical interface.
[0133] Optionally, the computing device displays a third graphical interface for receiving a first request message for requesting the addition of a compilation rule after the first phase of the first compiler. In response to the first request message, the computing device displays a first graphical interface for receiving the first code block.
[0134] In some embodiments, the first code block is written in a host language. The first code block includes a code statement for importing a target code library. The target code library includes at least one code statement written in the host language and used to compile code written in the host language.
[0135] Exemplarily, the target code library includes a first code library and / or a second code library. The first code library includes at least one code statement, and the code statement in the first code library is used to insert the first code block after the first stage to execute the function of the first code block during compilation. The second code library includes at least one code statement. The code statement in the second code library is used to perform an operation on at least one of the following: a token, an untyped AST, a typed AST, a node in an IR, and a type of control flow included in the IR.
[0136] Exemplarily, the target code library is a code library provided by the host language. For example, the first code library is a compilePass library. The second code library includes an AST library and / or an IR library. The code statements in the AST library are used to perform operations on at least one of a token, an untyped AST, and a typed AST. The code statements in the IR library are used to perform operations on nodes in the IR and / or the types of control flows included in the IR.
[0137] Illustratively, the user may call code statements (eg, methods, functions, classes, packages, etc.) in the target code library in the first code block through an import statement, thereby implementing the functionality of the first code block when compiling the first source code.
[0138] S320: Generate a second compiler according to the first code block and the first compiler.
[0139] After receiving the first code block, the computing device generates a second compiler based on the first code block and the first compiler. The second compiler is used to compile a code file written in the first language (e.g., the first source code). During the process of compiling the first source code by the second compiler, the first language is converted into a host language.
[0140] In some embodiments, converting the first language into the host language includes converting phrases that conform to lexical rules of the first language into phrases that conform to lexical rules of the host language, and / or converting grammar that conforms to grammatical rules of the first language into grammar that conforms to grammatical rules of the host language.
[0141] Optionally, the compilation rules indicated by the first code block include at least one of the following: lexical conversion rules between the first language and the host language, grammatical conversion rules between the first language and the host language, constraints of the first language or the host language, and optimization rules of the host language. Among them, the lexical conversion rules are used to convert phrases in the first source code (i.e., phrases that conform to the lexical rules of the first language) into phrases or ASTs that conform to the lexical rules of the host language. The grammatical conversion rules are used to convert the grammar in the first source code (i.e., the grammar that conforms to the first language) into the grammar that conforms to the host language. The constraints are used to constrain the AST and / or IR corresponding to the first source code. The AST includes an untyped AST and / or a typed AST. The untyped AST includes: an untyped AST that conforms to the grammatical rules of the first language and / or an untyped AST that conforms to the grammatical rules of the host language. The typed AST includes a typed AST that conforms to the grammatical rules of the host language. The optimization rules are used to execute and / or optimize part or all of the computational processes in the first source code.
[0142] In some embodiments, the operation object of the lexical conversion rule is at least one token obtained after parsing the first source code according to a lexical analyzer (lexer). The result obtained according to the lexical conversion rule is at least one token that conforms to the lexical rules of the host language or an untyped AST that conforms to the grammatical rules of the host language. That is, the input of the lexical conversion rule is at least one token that conforms to the lexical rules of the first language, and the output of the lexical conversion rule is at least one token that conforms to the lexical rules of the host language. Alternatively, the output of the lexical conversion rule is an untyped AST that conforms to the grammatical rules of the host language.
[0143] For example, suppose that a code file written in a first language includes the following code statements:
[0144] (1) a=[a1,a2,a3];
[0145] (2) b = [b1, b2, b3];
[0146] (3) c = [c1, c2, c3];
[0147] (4) c = a + b;
[0148] Among them, code statement (1) is used to indicate that a is an array including 3 integer elements, and the 3 elements are a1, a2 and a3 respectively. Code statement (2) is used to indicate that b is an array including 3 integer elements, and the 3 elements are b1, b2 and b3 respectively. Code statement (3) is used to indicate that c is an array including 3 integer elements, and the 3 elements are c1, c2 and c3 respectively. Code statement (4) is used to indicate that each integer element in c is obtained by adding the corresponding integer elements in array a and array b. Assuming that the host language only supports integer addition and does not support array addition, code statement (4) will be judged as not complying with the rules of the host language when compiled by the first compiler, and an alarm will be issued. Assuming that the compilation rules include a first lexical conversion rule, the first lexical conversion rule includes: converting each token representing an array into a token representing each element in the array. After parsing the code statement (4) according to the lexer, at least one token conforming to the first language is obtained, and the at least one token conforming to the first language includes: "c", "=", "a", "+", "b", ";". After converting the at least one token conforming to the first language according to the first lexical conversion rule, at least one token conforming to the host language is obtained, which includes: "c1", "=", "a1", "+", "b1", ";", "c2", "=", "a2", "+", "b2", ";", "c3", "=", "a3", "+", "b3", ";".
[0149] It should be understood that the above-mentioned first lexical conversion rule is only an exemplary illustration. The lexical conversion rule can be specifically designed according to the specific application scenario. The embodiment of the present application does not limit the specific function of the lexical conversion rule.
[0150] In some embodiments, the operation object of the grammar transformation rule is an untyped AST obtained by parsing at least one token according to a grammar parser. The result obtained according to the grammar transformation rule is an untyped AST that conforms to the grammar rules of the host language. That is, the input of the grammar transformation rule is an untyped AST that conforms to the grammar rules of the first language, and the output of the grammar transformation rule is an untyped AST that conforms to the grammar rules of the host language.
[0151] For example, assume that the first source code includes a code statement (5): INT d=500. The code statement (5) is used to indicate that the data type of d is INT and the value is 500. Assume that the first language limits the value range of data of data type INT to 0 to 100. Assume that the host language cannot support the data type INT, but the host language can support the data type int, and the value of data of data type int is 0 to 1000. According to the lexer, the at least one token that conforms to the first language is obtained by parsing the code statement (5): "INT", "d", "=", "500". According to the parser, the untyped AST that conforms to the first language is obtained by parsing the at least one token that conforms to the first language: INT d=500. Assume that the compilation rule includes a first grammar conversion rule, and the first grammar conversion rule includes: converting the data type INT to the data type int, and adding a judgment statement that the data of type INT is greater than or equal to 0, and a judgment statement that the data of type INT is less than or equal to 100. The untyped AST conforming to the host language obtained by converting the untyped AST conforming to the first language according to the first grammar conversion rule includes: assignment statement 1, judgment statement 1, and judgment statement 2. Assignment statement 1 conforms to the grammar rules of the host language and is used to assign 500 to data d of the int data type. Judgment statement 1 conforms to the grammar rules of the host language and is used to determine whether d is greater than or equal to 0. Judgment statement 2 conforms to the grammar rules of the host language and is used to determine whether d is less than or equal to 100.
[0152] It should be understood that the above-mentioned first grammar conversion rule is only an exemplary description, and the grammar conversion rule can be specifically designed according to the specific application scenario. The embodiment of the present application does not limit the specific function of the grammar conversion rule.
[0153] In some embodiments, the constraints include first-class constraints and / or second-class constraints. The operation object of the first-class constraints is at least one token or an untyped AST, the at least one token conforms to the lexical rules of the first language or the host language, and the untyped AST conforms to the grammatical rules of the first language or the host language. The first-class constraints are used to check at least one token or an untyped AST to determine whether the at least one token or an untyped AST satisfies the constraints. The operation object of the second-class constraints is an IR, which is an IR that conforms to the host language. The second-class constraints are used to check the IR to determine whether the IR satisfies the constraints.
[0154] Exemplarily, the first type of constraint condition includes at least one constraint condition. Similarly, the second type of constraint condition includes at least one constraint condition.
[0155] Exemplarily, the first type of constraints include constraints on the types of data included in the AST. For example, the first type of constraints include at least one of the following constraints: all data in the first source code are non-negative numbers, the number of digits after the decimal point in the data in the first source code is greater than a preset threshold, and all data in the first source code are double-precision floating-point numbers. This first type of constraints is used to prevent users developing project code in the first language from defining unallowed data types.
[0156] Exemplarily, the second type of constraints include security constraints on data flows and / or control flows in the IR. For example, the second type of constraints include at least one of the following constraints: the first source code prohibits access to the first data structure, the first source code prohibits the use of the first permission, etc. The first data structure is any data structure involving a public scene or application environment, and the first permission is any permission involving a public scene or application environment. The second type of constraints is used to prevent users who use the first language to develop project code from developing code that destroys a public scene (such as a multi-tenant public scene) or an application environment.
[0157] In some embodiments, the optimization rules include a first type of optimization rules and / or a second type of optimization rules. The operation object of the first type of optimization rules is a typed AST, which is a typed AST that conforms to the host language. The first type of optimization rules analyzes and optimizes part of the calculation process in the first source code based on the typed AST, and / or the first type of optimization rules are used to execute part of the calculation in the first source code. The operation object of the second type of optimization rules is IR, which is an IR that conforms to the host language. The second type of optimization rules optimize part of the code in the first source code based on the control flow graph and / or data flow graph of IR.
[0158] Exemplarily, the first type of optimization rules includes at least one optimization rule. Similarly, the second type of optimization rules includes at least one optimization rule.
[0159] Exemplarily, the first type of optimization rule is, for example: computational optimization of differential functions and adjoint functions in an automatic differentiation scenario.
[0160] Illustratively, the second type of optimization rule is, for example, discarding isolated nodes in the data flow graph and / or control flow graph of the IR. The isolated node is a node in the data flow graph and / or control flow graph of the IR that is not connected to any other nodes except the isolated node. The isolated node corresponds to one or more code statements included in the first source code and not actually used during the compilation phase.
[0161] Optionally, the computing device adds the first code block to after a compilation stage or between two compilation stages of the first compiler, thereby generating a second compiler. For specific implementation, see the description in FIG4 .
[0162] Optionally, after the computing device generates the second compiler, it can also compile the first source code using the second compiler to generate a target executable file. The computing device can also run the target executable file using a host language runtime. The host language runtime is used to run the executable file corresponding to the source code written in the first language or the host language. The host language runtime is the runtime of the host language.
[0163] Optionally, before the computing device compiles the first source code through the second compiler, the computing device may further check the first source code through a third compiler. The third compiler is described in FIG6 .
[0164] In an embodiment of the present application, a computing device receives custom compilation rules newly added to a compiler of a host language, generates a compiler for compiling a code file written in a first language, thereby realizing conversion from the first language to the host language, or constraining and optimizing the code written in the first language, thereby reducing the restrictions of the host language on the first language and improving the flexible expression capabilities of the first language.
[0165] Figure 4 is a schematic flow chart of a compilation method provided by an embodiment of the present application. The method in Figure 4 can be executed by the computing device in Figure 1 (eg, the first computing device 110 or the second computing device 120). The method in Figure 4 includes the following steps.
[0166] S410: Determine a first code block and a first stage.
[0167] The first code block is described in FIG3 . The first stage includes one or two of the four compilation stages shown in FIG2 .
[0168] Optionally, before step S410 , the computing device receives the first code block through step S310 .
[0169] Optionally, the computing device receives the first stage while receiving the first code block, or receives the first stage before receiving the first code block.
[0170] S420: Add the first code block after the first stage or between two compilation stages included in the first stage.
[0171] When the first stage includes one compilation stage, the computing device adds the first code block after the first stage. Alternatively, when the first stage includes two compilation stages, the computing device adds the first code block between the two compilation stages included in the first stage.
[0172] When the first code block is used to indicate the lexical conversion rules between the first language and the host language, the computing device adds the first code block to after the lexical analysis stage of the first compiler. The lexical conversion rules are described in S320. For example, the computing device adds the first code block between the lexical analysis stage and the grammatical analysis stage of the first compiler, or between the lexical analysis stage and the semantic analysis stage of the first compiler. In other words, the computing device adds the first code block after the lexical analyzer (lexer), so that the first compiler runs the first code block after performing lexical analysis by the lexer, thereby converting the phrases that conform to the lexical rules of the first language into the phrases that conform to the lexical rules of the host language, or converting the phrases that conform to the lexical rules of the first language into the untyped AST that conforms to the grammatical rules of the host language. That is, when the first code block is used to indicate the lexical conversion rules between the first language and the host language, the first stage includes the lexical analysis stage. Alternatively, the first stage includes the lexical analysis stage and the grammatical analysis stage. Alternatively, the first stage includes the lexical analysis stage and the semantic analysis stage.
[0173] When the first code block is used to indicate the grammatical conversion rules between the first language and the host language, the computing device adds the first code block to the lexical analysis phase or after the grammatical analysis phase of the first compiler. The grammatical conversion rules are described in S320. For example, the computing device adds the first code block between the grammatical analysis phase and the semantic analysis phase of the first compiler, or between the lexical analysis phase and the semantic analysis phase of the first compiler. In other words, the computing device adds the first code block after the lexer so that the first compiler runs the first code block after performing lexical analysis by the lexer, thereby converting the untyped AST that conforms to the grammatical rules of the first language into an untyped AST that conforms to the grammatical rules of the host language. Alternatively, the computing device adds the first code block after the grammatical analyzer (parser) so that the first compiler runs the first code block after performing grammatical analysis by the parser, thereby converting the untyped AST that conforms to the grammatical rules of the first language into an untyped AST that conforms to the grammatical rules of the host language. That is, when the first code block is used to indicate the grammatical conversion rules between the first language and the host language, the first phase includes the lexical analysis phase or the grammatical analysis phase. Alternatively, the first phase includes a lexical analysis phase and a semantic analysis phase. Alternatively, the first phase includes a syntactic analysis phase and a semantic analysis phase.
[0174] When the first code block is used to indicate a first-class constraint, the computing device adds the first code block to the first compiler after the lexical analysis phase or the syntactic analysis phase. For the first-class constraint, see the description in S320. For example, the computing device adds the first code block between the syntactic analysis phase and the semantic analysis phase of the first compiler, or between the lexical analysis phase and the semantic analysis phase of the first compiler. In other words, the computing device adds the first code block after the lexer, so that the first compiler runs the first code block after performing lexical analysis by the lexer, thereby checking the obtained at least one phrase or untyped AST. Alternatively, the computing device adds the first code block after the parser, so that the first compiler runs the first code block after performing syntactic analysis by the parser, thereby checking the obtained untyped AST. That is, when the first code block is used to indicate a first-class constraint, the first phase includes the lexical analysis phase or the syntactic analysis phase. Alternatively, the first phase includes the lexical analysis phase and the semantic analysis phase. Alternatively, the first phase includes the syntactic analysis phase and the semantic analysis phase.
[0175] When the first code block is used to indicate the first type of optimization rule, the computing device adds the first code block to after the semantic analysis stage of the first compiler. The first type of optimization rule is described in step S320. For example, the computing device adds the first code block between the semantic analysis stage and the IR conversion stage of the first compiler. In other words, the computing device adds the first code block after the semantic analyzer so that the first compiler runs the first code block after performing semantic analysis through the semantic analyzer, thereby optimizing the obtained typed AST. That is, when the first code block is used to indicate the first type of optimization rule, the first stage includes the semantic analysis stage. Alternatively, the first stage includes the semantic analysis stage and the IR conversion stage.
[0176] When the first code block is used to indicate the second type of constraints and / or the second type of optimization rules, the computing device adds the first code block to the IR conversion stage of the first compiler. The second type of constraints and / or the second type of optimization rules are described in step S320. In other words, the computing device adds the first code block after the IR converter so that the first compiler runs the first code block after IR conversion through the IR converter, thereby optimizing or checking the obtained IR. That is, when the first code block is used to indicate the second type of constraints and / or the second type of optimization rules, the first stage includes the IR conversion stage.
[0177] S430: Generate a second compiler.
[0178] The computing device adds the first code block to the first compiler after a compilation phase or between two compilation phases to generate a second compiler. The computing device can also provide the second compiler to a user so that the user can compile the first source code using the second compiler.
[0179] In some embodiments, when the computing device runs the second compiler, the computing device uses the code statement for importing the target code library in the first code block to enable the first code block to execute the corresponding function in the second compiler.
[0180] In some embodiments, a computing device compiles the first source code using a second compiler. The specific process is shown in Figure 5. Figure 5 is a schematic diagram of the compilation phase of the second compiler. The computing device inputs the first source code into the second compiler. The second compiler, based on a lexical analyzer 510, parses the first source code into at least one token that conforms to the lexical rules of the first language. The second compiler, based on a lexical conversion rule, converts the at least one token that conforms to the lexical rules of the first language into at least one token that conforms to the lexical rules of the host language, or converts the at least one token that conforms to the lexical rules of the first language into an untyped AST that conforms to the grammatical rules of the first language. Upon obtaining the at least one token, the second compiler, based on a grammatical analyzer 520, converts the at least one token into an untyped AST that conforms to the grammatical rules of the first language. After obtaining the untyped AST that conforms to the grammatical rules of the first language, the second compiler, based on a grammatical conversion rule, converts the untyped AST that conforms to the grammatical rules of the first language into an untyped AST that conforms to the grammatical rules of the host language. And / or, the second compiler checks the untyped AST that conforms to the grammatical rules of the first language or the untyped AST that conforms to the grammatical rules of the host language according to the first type of constraints. When the typeless AST meets each constraint in the first category of constraints, it is determined that there is no problem with the typeless AST and a semantic analysis is performed on the typeless AST. When the typeless AST does not meet at least one constraint in the first category of constraints, it is determined that there is a problem with the typeless AST and an alarm is issued. When there is no problem with the typeless AST, the second compiler converts the typeless AST into a typed AST according to the semantic analyzer 530. After obtaining the typed AST, the second compiler optimizes the typed AST according to the first category of optimization rules to obtain an optimized typed AST. The second compiler converts the optimized typed AST into IR through the IR converter 540. After obtaining the IR, the second compiler checks the IR according to the second category of constraints and / or optimizes the IR according to the second category of optimization rules. When the IR meets each constraint in the second category of constraints, it is determined that there is no problem with the IR and the IR is converted into a target executable file. When the IR does not meet one or more constraints in the second category of constraints, it is determined that there is a problem with the IR and an alarm is issued.
[0181] Exemplarily, when the computing device compiles the first source code through the second compiler, it may not execute at least one of the following compilation rules: lexical conversion rules, grammatical conversion rules, first-class constraints, second-class constraints, first-class optimization rules, or second-class optimization rules.
[0182] Figure 6 is a schematic flow chart of a compilation method provided by an embodiment of the present application. The method in Figure 6 can be executed by the computing device in Figure 1 (eg, the first computing device 110 or the second computing device 120). The method in Figure 6 includes the following steps.
[0183] S610: Receive a second code block.
[0184] A computing device receives a second code block, the second code block indicating a check rule for checking during a compilation phase of a first source code. The first source code is a code file written in a first language. The second code block is written in a host language. The first language is different from the host language and is a DSL.
[0185] In some embodiments, the first language and the host language being different includes: at least one lexical rule and / or at least one grammatical rule specified by the first language and the host language being different.
[0186] Illustratively, the difference between at least one lexical rule defined in the first language and the host language includes: a first lexical rule defined in the first language and a second lexical rule defined in the host language have similar functions, but the first lexical rule and the second lexical rule are different; or the lexical rules defined in the first language include lexical rules not defined in the host language; or the lexical rules defined in the first language do not include one or more lexical rules defined in the host language. Similarly, the difference between at least one grammatical rule defined in the first language and the host language includes: a first grammatical rule defined in the first language and a second grammatical rule defined in the host language have similar functions, but the first grammatical rule and the second grammatical rule are different; or the grammatical rules defined in the first language include grammatical rules not defined in the host language; or the grammatical rules defined in the first language do not include one or more grammatical rules defined in the host language.
[0187] In some embodiments, the first language is an internal DSL developed based on the host language. Exemplarily, the lexical rules and / or grammatical rules of the first language are designed and developed based on the lexical rules and / or grammatical rules of the host language.
[0188] Optionally, the computing device displays a fifth graphical interface for receiving the second request information and the second code block. The second request information is used to request that a new check rule be added to the second compiler. In other words, the user enters the second request information and the second code block in the fifth graphical interface, causing the computing device to receive the second request information and the second code block. The embodiments of the present application do not limit the specific form of the fifth graphical interface.
[0189] Optionally, the computing device displays a fifth graphical interface, which is used to receive a second request message, and the second request message is used to request that a new compilation rule be added to the second compiler. In response to the second request message, the computing device displays a second graphical interface, which is used to receive the second stage, or the second graphical interface is used to receive the second stage and the second code block. When the second graphical interface is used to receive the second stage, the computing device displays a sixth graphical interface in response to the second stage, which is used to receive the second code block. The embodiment of the present application does not limit the specific form of expression of the sixth graphical interface. The second stage includes one or two of the compilation stages shown in Figure 2.
[0190] Optionally, the computing device displays a fifth graphical interface for receiving a second request message for requesting the addition of a new compilation rule in the second compiler. In response to the second request message, the computing device displays a second graphical interface for receiving a second code block.
[0191] In some embodiments, the second code block is written in the host language. The second code block includes a code statement for importing a target code library. The target code library includes at least one code statement written in the host language and used to compile code written in the host language. The target code library is described in S310 and will not be repeated here.
[0192] S620: Generate a third compiler according to the second code block and the second compiler.
[0193] After receiving the second code block, the computing device generates a third compiler based on the second code block and the second compiler. The third compiler is used to check the first source code. The second compiler is described in S320.
[0194] Optionally, the check rule indicated by the second code block includes at least one of the following: a first check rule, a second check rule, a third check rule, or a fourth check rule. The first check rule is a check rule for at least one phrase in the first source code. The second check rule is a check rule for an untyped AST corresponding to the first source code. The third check rule is a check rule for a typed AST corresponding to the first source code. The fourth check rule is a check rule for an IR corresponding to the first source code.
[0195] Exemplarily, the at least one phrase targeted by the first check rule includes: at least one phrase in the first source code directly parsed by a lexer, and at least one phrase converted according to a lexical conversion rule. For the lexical analyzer and the lexical conversion rule, see the description in S320. The typeless AST targeted by the second check rule includes: the typeless AST corresponding to the first source code directly obtained by a grammatical analyzer, and the typeless AST converted according to a grammatical conversion rule. For the grammatical analyzer and the grammatical conversion rule, see the description in S320.
[0196] Exemplarily, the first check rule is used to check the code style and format specifications of the first source code, such as the naming rules of identifiers, etc. The second check rule is used to check whether the first source code complies with the grammatical rules of the first language and / or the host language, and whether the first source code includes code statements that are destructive to public scenarios or application environments. The third check rule is used to check semantic errors in the first source code, such as data type mismatch, etc. The fourth check rule is used to analyze the definition and use of variables in the first source code, help identify uninitialized variables, redundant assignments, etc., and is also used to analyze and construct the data flow graph and control flow graph of the first source code, check possible code execution paths, and help find dead code, dead loops, and code statements that are destructive to public scenarios or application environments.
[0197] Optionally, the inspection rules further include: shielding rules, disabling rules, and editing rules. Shielding rules are used to block alerts when the rules between the first language and the host language differ. Disabling rules are used to generate alerts for code statements permitted by the host language but not by the first language. Editing rules are used to generate alerts for code statements that do not comply with the rules of the first language and / or that damage the environment when writing code in the first language.
[0198] Exemplarily, the rules of the first language include lexical rules and / or grammatical rules. The lexical rules include rules for tokens in the source code written in the first language, and the grammatical rules include rules for the grammar in the source code written in the first language. Similarly, the rules of the host language include lexical rules and / or grammatical rules. The lexical rules include rules for tokens in the source code written in the host language, and the grammatical rules include rules for the grammar in the source code written in the host language.
[0199] In some embodiments, the shielding rule includes at least one of the following: a first check rule, a second check rule, a third check rule, or a fourth check rule. For example, when the shielding rule includes the first check rule, the shielding rule is used to not issue an alarm when at least one token in the first source code does not comply with the first lexical rule of the first language but complies with the second lexical rule of the host language. The first lexical rule is any lexical rule specified in the first language, and the second lexical rule is a lexical rule specified in the host language. The first lexical rule and the second lexical rule correspond to each other, or the functions of the first lexical rule and the second lexical rule are similar. Alternatively, when the shielding rule includes the first check rule, the shielding rule is used to not issue an alarm when at least one token in the first source code complies with the first lexical rule of the first language but does not comply with the second lexical rule of the host language. When the shielding rule includes any one of the second check rule, the third check rule, or the fourth check rule, the shielding rule is used in a similar manner to when the shielding rule includes the first check rule, and will not be repeated here.
[0200] In some embodiments, the disabling rule includes at least one of the following: a first check rule, a second check rule, a third check rule, or a fourth check rule. For example, when the disabling rule includes the first check rule, the disabling rule is used to generate an alarm when at least one token in the first source code does not conform to the first lexical rule of the first language but conforms to the second lexical rule of the host language. When the disabling rule includes any one of the second check rule, the third check rule, or the fourth check rule, the disabling rule is used in a manner similar to that when the disabling rule includes the first check rule, and will not be further described here.
[0201] When a disabling rule includes a second check rule, the disabling rule checks the untyped AST. For example, the disabling rule checks the untyped AST for nodes or patterns that are destructive to common scenarios or application environments. In other words, the disabling rule is used to generate an alert when a node or pattern that is destructive to common scenarios or application environments exists in the untyped AST.
[0202] In some embodiments, the editing rule includes at least one of the following: a first check rule, a second check rule, a third check rule, or a fourth check rule. For example, when the editing rule includes the first check rule, the editing rule is used to issue an alarm when the code is written in the first language if at least one token in the written code does not comply with the first lexical rule of the first language and / or does not comply with the second lexical rule of the host language. When the editing rule includes any one of the second check rule, the third check rule, or the fourth check rule, the editing rule is used in a similar manner to when the editing rule includes the first check rule, and will not be repeated here.
[0203] For example, when generating an alert based on an editing rule, the computing device may display a special mark, such as a highlight or underline, in the graphical interface of the source code editor. Alternatively, the computing device may display an error or warning message in the graphical interface of the source code editor. Alternatively, the computing device may display a repair suggestion in the graphical interface of the source code editor.
[0204] Optionally, the computing device adds the second code block to after the second stage of the second compiler or between two compilation stages included in the second stage, thereby generating a third compiler.
[0205] In some embodiments, when the second stage includes one of the compilation stages shown in FIG2 , the second code block is added after the second stage. Alternatively, when the second stage includes two of the compilation stages shown in FIG2 , the second code block is added between the two compilation stages included in the second stage.
[0206] When the second code block is used to indicate the first check rule, the computing device adds the first check rule to after the lexical analysis stage of the second compiler. For example, the computing device adds the first check rule between the lexical analysis stage and the grammatical analysis stage. Alternatively, the computing device adds the first check rule between the lexical analysis stage and the semantic analysis stage. In other words, the computing device adds the second code block after the lexical analyzer (lexer) of the second compiler, so that the second compiler runs the second code block after performing lexical analysis through the lexer, thereby checking at least one token in the first source code. That is, when the second code block is used to indicate the first check rule, the second stage includes the lexical analysis stage. Alternatively, the second stage includes the lexical analysis stage and the grammatical analysis stage. Alternatively, the second stage includes the lexical analysis stage and the semantic analysis stage.
[0207] When the second code block is used to indicate the second check rule, the computing device adds the second check rule to the lexical analysis phase or the grammatical analysis phase of the second compiler after the second check rule. For example, the computing device adds the second check rule between the lexical analysis phase and the semantic analysis phase, or the computing device adds the second check rule between the grammatical analysis phase and the semantic analysis phase. In other words, the computing device adds the second code block after the lexer of the second compiler, so that the second compiler runs the second code block after the lexer performs lexical analysis, thereby checking the typeless AST corresponding to the first source code. Alternatively, the computing device adds the second code block after the grammatical analyzer of the second compiler, so that the second compiler runs the second code block after the parser performs grammatical analysis, thereby checking the typeless AST corresponding to the first source code. That is, when the second code block is used to indicate the second check rule, the second phase includes the lexical analysis phase or the grammatical analysis phase. Alternatively, the second phase includes the lexical analysis phase and the semantic analysis phase. Alternatively, the second phase includes the grammatical analysis phase and the semantic analysis phase.
[0208] When the second code block is used to indicate the third check rule, the computing device adds the third check rule to after the semantic analysis stage of the second compiler. For example, the computing device adds the third check rule to between the semantic analysis stage and the IR conversion stage of the second compiler. In other words, the computing device adds the second code block after the semantic analyzer of the second compiler, so that the second compiler runs the second code block after performing semantic analysis through the semantic analyzer, thereby checking the typed AST corresponding to the first source code. That is, when the first code block is used to indicate the third check rule, the second stage includes the semantic analysis stage. Alternatively, the second stage includes the semantic analysis stage and the IR conversion stage.
[0209] When the second code block indicates the fourth check rule, the computing device adds the fourth check rule to the second compiler after the IR conversion stage. In other words, the computing device adds the second code block after the second compiler's IR converter, so that the second compiler runs the second code block after IR conversion by the IR converter, thereby checking the IR corresponding to the first source code. That is, when the second code block indicates the fourth check rule, the second stage includes the IR conversion stage.
[0210] Optionally, after generating the third compiler, the computing device provides the third compiler to the user. The computing device may also check the first source code using the third compiler, as shown in FIG7 .
[0211] Figure 7 is a schematic diagram of the compilation phase of the third compiler. A computing device inputs the first source code into the third compiler. The third compiler, using a lexical analyzer 710, parses the first source code into at least one token that conforms to the lexical rules of the first language. The lexical analyzer 710 may also, based on the lexical conversion rules shown in Figure 5, convert the at least one token that conforms to the lexical rules of the first language into at least one token that conforms to the lexical rules of the host language, or convert the at least one token that conforms to the lexical rules of the first language into an untyped AST that conforms to the grammatical rules of the first language. The third compiler checks the at least one token that conforms to the lexical rules of the host language and / or the first language according to a first checking rule. If the at least one token conforms to the first checking rule, the third compiler determines that the at least one token is correct and performs grammatical analysis on the at least one token. If the at least one token does not conform to the first checking rule, the third compiler determines that there is an issue with the at least one token and issues an alert. If the at least one token is correct, the third compiler, using a grammatical analyzer 720, converts the at least one token into an untyped AST that conforms to the grammatical rules of the first language. The parser 720 may also convert the untyped AST that conforms to the grammatical rules of the first language into an untyped AST that conforms to the grammatical rules of the host language according to the grammatical conversion rules in FIG5 . And / or, the parser 720 may also check the untyped AST that conforms to the grammatical rules of the first language or the untyped AST that conforms to the grammatical rules of the host language according to the first category of constraints in FIG5 . If the untyped AST conforms to each of the first category of constraints, the third compiler determines that the untyped AST has no problems, checks the untyped AST according to the second checking rules, or converts the untyped AST into a typed AST. If the untyped AST does not conform to at least one of the first category of constraints, the third compiler determines that there is a problem with the untyped AST and issues an alarm. Alternatively, after obtaining the untyped AST, the third compiler checks the untyped AST that conforms to the grammatical rules of the host language and / or the first language according to the second checking rules. If the untyped AST conforms to the second checking rules, the third compiler determines that there are no problems with the untyped AST, checks the untyped AST according to the first category of constraints, or performs semantic analysis on the untyped AST. When the untyped AST does not comply with the second check rule, the third compiler determines that there is a problem with the untyped AST and issues a warning. When there is no problem with the untyped AST, the third compiler converts the untyped AST that complies with the host language into a typed AST that complies with the host language through the semantic analyzer 730.The semantic analyzer 730 may also optimize the typed AST according to the first-category optimization rules in FIG5 to obtain an optimized typed AST. The third compiler may also check the typed AST according to the third checking rules. If the typed AST complies with the third checking rules, the third compiler determines that the typed AST is correct and performs IR conversion on the typed AST. If the typed AST does not comply with the third checking rules, the third compiler determines that there is an issue with the typed AST and issues an alert. The third compiler converts the optimized typed AST into an IR using the IR converter 740. The IR converter 740 checks the IR according to the second-category constraints and / or optimizes the IR according to the second-category optimization rules. If the IR complies with each of the second-category constraints, the third compiler determines that there is no issue with the IR and checks the IR according to the fourth checking rules, or converts the IR into a target executable file. If the IR does not comply with at least one of the second-category constraints, the third compiler determines that there is an issue with the IR and issues an alert. Alternatively, after obtaining the IR, the third compiler checks the IR according to the fourth checking rules. If the IR meets the fourth check rule, the third compiler determines that the IR has no problems. The third compiler checks the IR according to the second type of constraints or issues a first prompt message. The first prompt message is used to indicate that there are no problems with the first source code. If the IR does not meet the fourth check rule, the third compiler determines that there are problems with the IR and issues a warning.
[0212] Exemplarily, when the computing device checks the first source code through the third compiler, at least one of the following checking rules may not be executed: the first checking rule, the second checking rule, the third checking rule, or the fourth checking rule.
[0213] In an embodiment of the present application, a computing device receives a check rule newly added during the compilation phase of a first source code and generates a third compiler, thereby checking the first source code written in a first language to avoid problems such as code statements in the first source code not complying with the rules of the first language, or problems such as the first source code including destructive code statements, thereby improving the security control capability of the first source code and improving the flexible expression capability of the first language.
[0214] Figure 8 is a schematic diagram of a compilation method provided by an embodiment of the present application. The method in Figure 8 can be executed by the computing device in Figure 1 (e.g., the first computing device 110 or the second computing device 120). Figure 8 includes the following steps.
[0215] S810: Input the first source code into a third compiler and start compiling.
[0216] The computing device inputs the first source code into a third compiler, which then compiles the first source code sequentially according to the compilation phases of the third compiler. The compilation phases of the third compiler include: a lexical analysis phase, a syntax analysis phase, a semantic analysis phase, and an IR conversion phase. The first source code is a code file written in a first language. The third compiler is described in FIG6 .
[0217] S820: Obtain first data of the first source code during the compilation process.
[0218] When the computing device compiles the first source code according to the third compiler, different types of first data are generated.
[0219] For example, the computing device performs lexical analysis on the first source code according to the lexical analyzer in the third compiler, thereby generating at least one token. The at least one token is the first data of the lexical analysis stage. The computing device performs syntactic analysis on the at least one token according to the syntactic analyzer in the third compiler, thereby generating an untyped AST. The untyped AST is the first data of the syntactic analysis stage. The computing device performs semantic analysis on the untyped AST according to the semantic analyzer in the third compiler, thereby generating a typed AST. The typed AST is the first data of the semantic analysis stage. The computing device performs IR conversion on the typed AST according to the IR converter in the third compiler, thereby generating an IR. The IR is the first data of the IR conversion stage.
[0220] S830: Determine, based on first data in the compilation process of the first source code, a compilation phase and a checking rule corresponding to the first data.
[0221] After obtaining the first data of the first source code during compilation, the computing device can determine the current compilation stage based on the type of the first data and the check rule corresponding to the current compilation stage.
[0222] For example, when the computing device determines that the first data is at least one token, the computing device determines that the current compilation stage is the lexical analysis stage, and determines that the check rules corresponding to the lexical analysis stage include the first check rule. When the computing device determines that the first data is an untyped AST, the computing device determines that the current compilation stage is the syntax analysis stage, and determines that the check rules corresponding to the syntax analysis stage include the second check rule. When the computing device determines that the first data is a typed AST, the computing device determines that the current compilation stage is the semantic analysis stage, and determines that the check rules corresponding to the semantic analysis stage include the third check rule. When the computing device determines that the first data is IR, the computing device determines that the current compilation stage is the IR conversion stage, and determines that the check rules corresponding to the IR conversion stage include the fourth check rule. The first check rule, the second check rule, the third check rule and the fourth check rule are described in Figure 6.
[0223] Optionally, after determining the current compilation stage, the computing device determines a check rule corresponding to the first data based on a first configuration file. The first configuration file is used to indicate at least one check rule that needs to be checked and / or at least one check rule that does not need to be checked, corresponding to at least one compilation stage of the third compiler. Determining the check rule that needs to be checked based on the first configuration file eliminates the need for the computing device to check the first data according to all check rules, thereby reducing constraints on the first data and improving the flexible expression capabilities of the first language.
[0224] Exemplarily, the third configuration file includes at least one inspection rule. Alternatively, the third configuration file includes an index of at least one inspection rule in an inspection rule set. The inspection rule set includes at least one inspection rule. The inspection rule set includes inspection rules provided by the host language and / or the newly added inspection rules in FIG6 .
[0225] Exemplarily, the computing device receives the first configuration file before step S830. For example, the first configuration file may be input by a user, such as a cloud vendor or a DSL developer.
[0226] S840: Check the first data according to a check rule corresponding to the first data.
[0227] After determining at least one check rule corresponding to the first data, the computing device checks the first data according to each check rule. If the first data does not comply with one or more check rules corresponding to the first data, the computing device determines that there is a problem with the first data and issues an alarm. If the first data complies with each check rule corresponding to the first data, the computing device determines that there is no problem with the first data and executes step S850.
[0228] S850: Determine whether the current compilation stage is the final compilation stage.
[0229] After checking the first data according to the check rules corresponding to the first data, the computing device determines whether the current compilation stage is the final compilation stage. If the current compilation stage is the final compilation stage, the computing device executes step S860. If the current compilation stage is not the final compilation stage, the computing device repeats steps S820-S850. That is, if the current compilation stage is not the final compilation stage, the computing device continues compiling in the next compilation stage and obtains the first data of the next compilation stage.
[0230] S860, end compilation.
[0231] When the current compilation stage is the final compilation stage, the computing device ends the compilation.
[0232] In some embodiments, after the compilation is completed, the computing device sends a second prompt message, where the second prompt message is used to prompt that there is no problem with the first source code.
[0233] Optionally, in the methods of FIG. 3 , FIG. 4 , or FIG. 6 , the computing device may further receive a third code block. The third code block is used to indicate a runtime check rule newly added during the execution phase of the first source code. The computing device compiles the third code block to generate a first instruction corresponding to the third code block. The first instruction is used to perform a check when executing the target executable file corresponding to the first source code.
[0234] In some embodiments, when the computing device runs the target executable file corresponding to the first source code using the host language runtime, the computing device calls the first instruction, thereby implementing the inspection of the first source code.
[0235] In some embodiments, the runtime check rule includes constraints on the target executable file, such as limiting the length of a specific array.
[0236] Exemplarily, the third code block is written in the first language. Alternatively, the third code block is written in the host language.
[0237] Exemplarily, when the third code block is written in the first language, the computing device compiles the third code block using the second compiler. When the third code block is written in the host language, the computing device compiles the third code block using the first compiler.
[0238] In some embodiments, the host language runtime provides an interface RegisterHook() for registering a callback. The user implements the function of the third code block by calling the interface RegisterHook() in the third code block. That is, the third code block includes at least one code statement that calls the interface RegisterHook(). The interface RegisterHook() includes different callback code statements, and each callback code statement corresponds to a different usage scenario. Each callback code statement is used to indicate a code function called in different usage scenarios. The computing device compiles the third code block and generates a call instruction of RegisterHook(callback). When running the first source code, the host language runtime calls the call instruction corresponding to the third code block, thereby checking the target executable file corresponding to the first source code according to the runtime check rules indicated by the third code block, thereby achieving the purpose of checking the first source code.
[0239] In some embodiments, the use permission of the interface RegisterHook() is only provided to cloud vendors and / or first language developers, and is not provided to ordinary users who use the first language for program development. In other words, the computing device needs to perform permission verification before receiving the third code block. The embodiments of the present application do not limit the specific method of permission verification.
[0240] In an embodiment of the present application, by calling the first instruction generated by the newly added third code block when running the target executable file at host runtime, code statements that do not comply with the rules of the first language or the host language are avoided from appearing in the target executable file corresponding to the first source code, or code statements that are destructive to public scenarios or application environments are avoided from appearing in the target executable file corresponding to the first source code, thereby enhancing the runtime security checking capability and improving the flexible expression capability of the first language.
[0241] FIG9 is a schematic flow chart of a method for compiling and executing a first source code according to an embodiment of the present application. The method in FIG9 can be executed by the computing device in FIG1 (e.g., the first computing device 110 or the second computing device 120). The method in FIG9 includes the following steps.
[0242] S910: Obtain a first source code.
[0243] The computing device may directly receive input from a user to obtain the first source code. Alternatively, the computing device may receive the first source code from another device connected to the computing device. The user may develop program code in a first language. The first source code may be a code file written in the first language. The first language may be an internal DSL developed in a host language. The first language, host language, and first source code are described in FIG3 .
[0244] S920: Check the first source code using the third compiler.
[0245] After obtaining the first source code, the computing device checks the first source code using the third compiler, as shown in FIG8 . The third compiler is described in FIG6 .
[0246] S930: When there is a problem with the first source code, issue an alarm.
[0247] During the process of checking the first source code, if at least one code statement in the first source code does not comply with one or more checking rules in the third compiler, the computing device determines that there is a problem with the first source code and issues an alarm.
[0248] S940 : When there is no problem with the first source code, compile the first source code using a second compiler to generate a target executable file.
[0249] During the process of checking the first source code, if each code statement in the first source code complies with each check rule in the third compiler, the computing device determines that there is no problem with the first source code and compiles the first source code according to the second compiler. The specific implementation is shown in FIG5. The second compiler is described in FIG3 or FIG4.
[0250] S950: Run the target executable file according to the host language runtime.
[0251] After generating the target executable file, the computing device runs the target executable file according to the host language runtime, thereby obtaining the running result of the first source code.
[0252] In some embodiments, when the host language runtime runs the target executable file, it calls a first instruction, which is described above.
[0253] In an embodiment of the present application, the first source code may be checked by a third compiler before being compiled, thereby preventing the first source code from containing code statements that do not comply with the rules and / or code statements that have destructive effects. After being checked by the third compiler, if the first source code is correct, the first source code may be compiled by a second compiler to obtain a target executable file for executing the first source code. When the computing device runs the target executable file through the host language runtime, it can improve security checking capabilities by calling the first instruction, thereby facilitating the improvement of the flexible expression capabilities of the first language.
[0254] FIG10 is a schematic block diagram of a computing device according to an embodiment of the present application. The computing device 1000 in FIG10 includes a transceiver unit 1010 and a processing unit 1020. The computing device 1000 in FIG10 can be used to execute the methods of FIG3 through FIG9. The computing device 1000 in FIG10 can be applied to a computing device, such as any of the computing devices in FIG1.
[0255] When computing device 1000 is used to execute the method in FIG3 , transceiver unit 1010 is used to receive the first code block and execute S310 in FIG3 . Processing unit 1020 is used to generate a second compiler based on the first code block and the first compiler. Processing unit 1020 is used to execute S320 in FIG3 .
[0256] When the computing device 1000 is used to execute the method in FIG. 4 , the processing unit 1020 is used to execute S410 to S430 in FIG. 4 .
[0257] When computing device 1000 is used to execute the method in FIG6 , transceiver unit 1010 is used to receive the second code block and execute S610 in FIG6 . Processing unit 1020 is used to generate a third compiler based on the second code block and the second compiler. Processing unit 1020 is used to execute S620 in FIG6 .
[0258] When the computing device 1000 is used to execute the method in FIG. 8 , the processing unit 1020 is used to execute S810 to S860 in FIG. 8 .
[0259] When the computing device 1000 is used to execute the method in FIG. 9 , the transceiver unit 1010 is used to execute S910 in FIG. 9 , and the processing unit 1020 is used to execute S920 to S950 in FIG. 9 .
[0260] The transceiver unit 1010 and the processing unit 1020 can be implemented by software or hardware. For example, the implementation of the processing unit 1020 will be described below using the processing unit 1020 as an example. Similarly, the implementation of the transceiver unit 1010 can refer to the implementation of the processing unit 1020.
[0261] As an example of a software functional unit, the processing unit 1020 may include code running on a computing instance. The computing instance may include at least one of a physical host (computing device), a virtual machine, and a container. Furthermore, the computing instance may be one or more. For example, the processing unit 1020 may include code running on multiple hosts / virtual machines / containers. It should be noted that the multiple hosts / virtual machines / containers used to run the code may be distributed in the same region or in different regions. Furthermore, the multiple hosts / virtual machines / containers used to run the code may be distributed in the same availability zone (AZ) or in different AZs, each AZ including one data center or multiple geographically close data centers. Typically, a region may include multiple AZs.
[0262] Similarly, multiple hosts / virtual machines / containers running the code can be distributed within the same VPC or across multiple VPCs. Typically, a VPC is set up within a region. Cross-region communication between two VPCs within the same region, or between VPCs in different regions, requires a communication gateway within each VPC to interconnect the VPCs.
[0263] As an example of a hardware functional unit, a module may include at least one computing device, such as a server. Alternatively, the processing unit 1020 may be implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD). The PLD may be a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0264] The multiple computing devices included in processing unit 1020 can be distributed in the same region or in different regions. The multiple computing devices included in processing unit 1020 can be distributed in the same AZ or in different AZs. Similarly, the multiple computing devices included in processing unit 1020 can be distributed in the same VPC or in multiple VPCs. The multiple computing devices can be any combination of servers, ASICs, PLDs, CPLDs, FPGAs, GALs, and other computing devices.
[0265] Therefore, the modules of each example described in the embodiments of this application can be implemented with electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0266] It should be noted that: when the device provided in the above embodiment executes the above method, only the division of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. For example, the transceiver unit 1010 can be used to execute any step in the above method, and the processing unit 1020 can be used to execute any step in the above method. The steps that the transceiver unit 1010 and the processing unit 1020 are responsible for implementing can be specified as needed, and all the functions of the above device can be realized by implementing different steps in the above method by the transceiver unit 1010 and the processing unit 1020 respectively.
[0267] In addition, the device and method embodiments provided in the above embodiments belong to the same concept, and their specific implementation processes are detailed in the method embodiments above and will not be repeated here.
[0268] The method provided in the embodiment of the present application can be performed by a computing device, which can also be referred to as a computer system. It includes a hardware layer, an operating system layer running on the hardware layer, and an application layer running on the operating system layer. The hardware layer includes hardware such as a processing unit, a memory and a memory control unit, and the function and structure of the hardware are subsequently described in detail. The operating system is any one or more computer operating systems that implement business processing through a process, for example, a Linux operating system, a Unix operating system, an Android operating system, an iOS operating system or a Windows operating system. The application layer includes applications such as a browser, an address book, a word processing software, and an instant messaging software. Furthermore, optionally, the computer system is a handheld device such as a smart phone, or a terminal device such as a personal computer, and this application is not particularly limited, as long as the method provided in the embodiment of the present application can be used. The execution subject of the method provided in the embodiment of the present application can be a computing device, or a functional module in a computing device that can call a program and execute a program.
[0269] FIG11 is a schematic block diagram of a computing device 1100 provided in an embodiment of the present application. The computing device 1100 may be a server, a computer, or other device with computing capabilities. The computing device 1100 shown in FIG11 includes at least one processor 1110 and a memory 1120.
[0270] It should be understood that this application does not limit the number of processors and memories in the computing device 1100.
[0271] The processor 1110 executes the instructions in the memory 1120 so that the computing device 1100 implements the method provided in this application. Alternatively, the processor 1110 executes the instructions in the memory 1120 so that the computing device 1100 implements the functional modules provided in this application, thereby implementing the method provided in this application.
[0272] Optionally, the computing device 1100 further includes a communication interface 1130. The communication interface 1130 uses a transceiver module such as, but not limited to, a network interface card or a transceiver to implement communication between the computing device 1100 and other devices or a communication network.
[0273] Optionally, the computing device 1100 further includes a system bus 1140, wherein the processor 1110, the memory 1120, and the communication interface 1130 are respectively connected to the system bus 1140. The processor 1110 can access the memory 1120 through the system bus 1140. For example, the processor 1110 can read and write data or execute code in the memory 1120 through the system bus 1140. The system bus 1140 is a peripheral component interconnect express (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The system bus 1140 is divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in Figure 11, but it does not mean that there is only one bus or one type of bus.
[0274] In one possible implementation, the processor 1110 is primarily responsible for interpreting computer program instructions (or codes) and processing data in the computer software. The computer program instructions and the data in the computer software may be stored in the memory 1120 or the cache of the processor 1110.
[0275] Optionally, processor 1110 may be an integrated circuit chip with signal processing capabilities. By way of example and not limitation, processor 1110 is a general-purpose processor, a digital signal processor (DSP), an ASIC, an FPGA, or other programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component. A general-purpose processor is a microprocessor, for example. For example, processor 1110 is a central processing unit (CPU).
[0276] Memory 1120 can provide runtime space for processes in computing device 1100. For example, memory 1120 stores computer programs (specifically, program code) used to generate processes. After the processor executes the computer program to generate a process, the processor allocates corresponding storage space for the process in memory 1120. Furthermore, the aforementioned storage space further includes a text segment, an initialized data segment, a bit-initialized data segment, a stack segment, a heap segment, and the like. Memory 1120 stores data generated during the execution of the process, such as intermediate data or process data, in the storage space corresponding to the aforementioned process.
[0277] Alternatively, the memory is also referred to as the internal memory. It is used to temporarily store data being processed by the processor 1110, as well as data exchanged with external storage devices such as a hard disk. As long as the computer is running, the processor 1110 will load the data to be processed into the internal memory for processing, and then transmit the result after the calculation is completed.
[0278] By way of example and not limitation, memory 1120 is a volatile memory or a non-volatile memory, or may include both volatile and non-volatile memories. The non-volatile storage medium may be, for example, a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory is a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), and direct rambus DRAM (DRDRAM). It should be noted that the memory 1120 of the systems and methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.
[0279] The structure of the computing device 1100 listed above is only an example description, and the present application is not limited thereto. The computing device 1100 of the embodiment of the present application includes various hardware in the computer system in the prior art. For example, the computing device 1100 also includes other memories in addition to the memory 1120, such as disk storage, etc. Those skilled in the art should understand that the computing device 1100 may also include other devices necessary to achieve normal operation. At the same time, according to specific needs, those skilled in the art should understand that the above-mentioned computing device 1100 may also include hardware devices that implement other additional functions. In addition, those skilled in the art should understand that the above-mentioned computing device 1100 may also include only the devices necessary to implement the embodiment of the present application, and does not necessarily include all the devices shown in Figure 11.
[0280] The present application also provides a computing device cluster. The computing device cluster includes at least one computing device. The computing device may be a server. In some embodiments, the computing device may also be a terminal device such as a desktop computer, a laptop computer, or a smartphone.
[0281] As shown in Figure 12, the computing device cluster includes at least one computing device 1100. The memory 1120 of one or more computing devices 1100 in the computing device cluster may store the same instructions for executing the above method.
[0282] In some possible implementations, the memory 1120 of one or more computing devices 1100 in the computing device cluster may also store some instructions for executing the above method. In other words, the combination of one or more computing devices 1100 can jointly execute the instructions of the above method.
[0283] It should be noted that the memory 1120 in different computing devices 1100 in the computing device cluster can store different instructions, each for performing part of the functions of the above-mentioned apparatus. In other words, the instructions stored in the memory 1120 in different computing devices 1100 can implement the functions of one or more modules in the above-mentioned apparatus.
[0284] In some possible implementations, one or more computing devices in a computing device cluster can be connected via a network. The network can be a wide area network (WAN) or a local area network (LAN), among others. FIG. 13 illustrates one possible implementation. As shown in FIG. 13 , two computing devices 1100A and 1100B are connected via a network. Specifically, each computing device is connected to the network via a communication interface within the computing device.
[0285] It should be understood that the functionality of the computing device 1100A shown in FIG13 may also be implemented by multiple computing devices 1100. Similarly, the functionality of the computing device 1100B may also be implemented by multiple computing devices 1100.
[0286] In an embodiment of the present application, a computer program product including instructions is also provided. The computer program product may be software or a program product including instructions that can be run on a computing device cluster or stored in any available medium. When executed by a computing device cluster, the computer program product causes the computing device cluster to perform the method provided above, or causes the computing device cluster to implement the functions of the apparatus provided above.
[0287] In an embodiment of the present application, a computer-readable storage medium is also provided. The computer-readable storage medium can be any available medium capable of storing data on a computing device, or a data storage device such as a data center that includes one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a magnetic tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive). The computer-readable storage medium includes instructions that, when executed by a computing device cluster, cause the computing device cluster to perform the method provided above.
[0288] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0289] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0290] In the several embodiments provided in this application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0291] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.
[0292] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0293] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0294] The above description is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
Claims
1. A compilation method, characterized in that: include: receiving a first code block, the first code block being used to indicate a compilation rule newly added after a first stage of a first compiler, the first compiler being used to compile a code file written in a host language; generating a second compiler based on the first code block and the first compiler, wherein the second compiler is used to compile a code file written in a first language, where the first language is different from the host language and the first language is a domain-specific language (DSL); The compilation rules are used to convert the first language into the host language, and / or the compilation rules are used to constrain and / or optimize the code written in the first language.
2. The method according to claim 1, characterized in that The first stage includes one or two of the following compilation stages: lexical analysis stage, syntax analysis stage, semantic analysis stage, and intermediate expression form IR conversion stage.
3. The method according to claim 1 or 2, characterized in that Generating a second compiler according to the first code block and the first compiler includes: In the case where the first stage includes one of the compilation stages, generating the second compiler after adding the first code block to the first stage of the first compiler; or In a case where the first stage includes two compilation stages, the first code block is added between two compilation stages included in the first stage of the first compiler to generate the second compiler.
4. The method according to any one of claims 1 to 3, characterized in that The compilation rules include at least one of the following: lexical conversion rules between the first language and the host language, grammatical conversion rules between the first language and the host language, constraints of the first language or the host language, and optimization rules of the host language; Among them, the lexical conversion rules are used to convert phrases in the code file written in the first language into phrases or abstract syntax trees AST that comply with the lexical rules of the host language, the syntax conversion rules are used to convert the syntax in the code file written in the first language into the syntax that complies with the host language, the constraints are used to constrain the AST and / or IR corresponding to the code file written in the first language, and the optimization rules are used to execute and / or optimize part or all of the calculation processes in the code file written in the first language.
5. The method according to claim 4, characterized in that The lexical conversion rules are added between the lexical analysis stage and the syntax analysis stage of the first compiler, or the lexical conversion rules are added between the lexical analysis stage and the semantic analysis stage of the first compiler; or, The grammar conversion rule is added between the grammar analysis stage and the semantic analysis stage of the first compiler, or the grammar conversion rule is added between the lexical analysis stage and the semantic analysis stage of the first compiler; or The constraint condition is added between the syntax analysis stage and the semantic analysis stage of the first compiler, or the constraint condition is added between the lexical analysis stage and the semantic analysis stage of the first compiler, or the constraint condition is added after the IR conversion stage of the first compiler; or, The optimization rules are added between the semantic analysis stage and the IR conversion stage of the first compiler, or the optimization rules are added after the IR conversion stage of the first compiler.
6. The method according to claim 4 or 5, characterized in that The constraint condition includes at least one of the following: a constraint on the type of data in a code file written according to the first language, and a constraint on the data flow and / or control flow in the IR.
7. The method according to claim 2 or 5, characterized in that The lexical analysis stage is used to decompose a code file written in the host language into at least one phrase token, the syntax analysis stage is used to generate an untyped AST based on the at least one phrase, the semantic analysis stage is used to generate a typed AST based on the at least one phrase or the untyped AST, and the IR conversion stage is used to generate IR based on the typed AST.
8. The method according to any one of claims 1 to 7, characterized in that The first code block is written according to the host language, and the first code block includes a code statement for importing a target code library, and the target code library includes at least one code statement written according to the host language and used to compile the code written according to the host language.
9. The method according to any one of claims 1 to 8, characterized in that The receiving of the first code block comprises: First request information and the first code block are received, where the first request information is used to request that a compilation rule be added after the first stage of the first compiler.
10. The method according to any one of claims 1 to 8, characterized in that Before receiving the first code block, the method further includes: receiving first request information, where the first request information is used to request adding a new compilation rule in the first compiler; In response to the first request information, a first graphical interface is displayed, where the first graphical interface is used to receive the first stage and / or the first code block.
11. The method according to any one of claims 1 to 8, characterized in that Before receiving the first code block, the method further includes: receiving first request information, where the first request information is used to request adding a compilation rule after the first stage in the first compiler; In response to the first request information, a first graphical interface is displayed, where the first graphical interface is used to receive the first code block.
12. The method according to any one of claims 1 to 11, characterized in that The method further comprises: receiving a second code block, the second code block being used to indicate a check rule, the check rule being used to perform a check during a compile phase of a code file written in the first language, the second code block being written in the host language; A third compiler is generated according to the second code block and the second compiler, and the third compiler is used to check a code file written in the first language.
13. The method according to claim 12, characterized in that The checking rules include at least one of the following: a checking rule for at least one phrase in a code file written in the first language, a checking rule for an AST corresponding to a code file written in the first language, and a checking rule for an IR corresponding to a code file written in the first language.
14. The method according to claim 12 or 13, characterized in that The inspection rules also include: shielding rules, disabling rules, and editing rules. The shielding rules are used to shield alarms when the rules between the first language and the host language are different. The disabling rules are used to issue alarms for code statements that are allowed by the host language but not allowed by the first language. The editing rules are used to issue alarms for code statements that do not comply with the rules of the first language and / or code statements that damage the environment when writing code according to the first language.
15. The method according to any one of claims 12 to 14, characterized in that The method further comprises: receiving a second request message, wherein the second request message is used to request adding a new check rule during a compilation phase of a code file written in the first language; In response to the second request information, a second graphical interface is displayed, where the second graphical interface is used to receive the second code block.
16. The method according to any one of claims 12 to 15, characterized in that The method further comprises: Checking a first source code according to the third compiler, where the first source code is a code file written in the first language; When there is a problem with the first source code, issuing an alarm; When there is no problem with the first source code, the first source code is compiled using the second compiler to obtain a target executable file.
17. A computing device, characterized in that: The method comprises means for implementing the method according to any one of claims 1 to 16.
18. A computing device cluster, characterized in that: comprising at least one computing device, each computing device including a processor and a memory; The processor of the at least one computing device is configured to execute instructions stored in a memory of the at least one computing device, so that the computing device cluster executes the method according to any one of claims 1 to 16.
19. A chip system, characterized in that: include: A logic circuit, the logic circuit being configured to be coupled to an input / output interface and to transmit data via the input / output interface so as to execute the method according to any one of claims 1 to 16.
20. A computer program product comprising instructions, characterized in that When the instructions are executed by a computing device cluster, the computing device cluster is caused to perform the method according to any one of claims 1 to 16.
21. A computer-readable storage medium, characterized in that The method comprises computer program instructions, which, when executed by a computing device cluster, perform the method according to any one of claims 1 to 16.
Citation Information
Patent Citations
A compiling method and a compiler
CN109918081A
LLVM-based ST language compiling method and system and compiler
CN110825384A
Code language conversion method and device, electronic equipment and storage medium
CN113138767A
Multi-output compiling method and system based on flex and Bison syntax analysis
CN116257245A
Cross-language compiling method and system and readable storage medium
CN116257248A