Compilation method of computer language generated based on framework

By designing the compilation framework and class interface of the target language, combining semantic analysis and optimization, cross-language compilation is realized, solving the problem of LLVM backend customization, and promoting the innovation and application of computer languages.

CN120335808APending Publication Date: 2025-07-18北京龙码飞腾技术服务中心
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410069558.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-01-18
Publication Date
2025-07-18

AI Technical Summary

Technical Problem

The existing technology is difficult to quickly customize the LLVM backend, which makes it difficult to develop new computer languages, and the lack of modular design of Microsoft compilers is not convenient for expansion, which limits the development and application of computer languages.

Method used

Using the method of combining framework code and business code, we design the compilation framework and class interface of the target language, generate abstract semantics trees through semantic analysis and optimize the target code to achieve cross-language compilation.

Benefits of technology

It reduces the technical difficulty of developing new computer languages, promotes innovation and expansion of computer languages, realizes the solution of problems in different fields, and supports the compilation and optimization of multiple computer languages.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120335808A_ABST
    Figure CN120335808A_ABST
Patent Text Reader

Abstract

The invention discloses a compiling method. The method comprises the following steps: designing a compiling framework of a target language; designing a class interface and a class of the target language; loading a compiling framework of a target language and a class interface definition; loading a source program to be compiled; performing semantic analysis on the source program and generating an abstract semantic tree; analyzing each node of the semantic tree, and analyzing each node into a plurality of codes of a target language according to definitions and rules in the compiling framework; the generated target code is optimized; and outputting and storing the target language code. The method has the beneficial effects that a computer language code with a complex grammar can be compiled from a computer language code with a simple grammar, so that the learning difficulty is reduced, the programming efficiency is improved, and the application field of a computer language is expanded.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and particularly to a compilation method for computer languages. Background Art

[0002] The compilation process is a process of "translating" one computer language into a target language. The current compilation methods mainly have two major technical routes: LLVM: This is a compiler infrastructure developed by Apple Inc. It provides a set of common intermediate representations, as well as a set of common code generation and optimization frameworks. LLVM has now become the underlying implementation of many compilers. Visual Studio: This is a proprietary development environment developed by Microsoft Corporation. It provides compilation and debugging functions for multiple programming languages. Among them, LLVM has become the current mainstream compilation method. It is divided into a front end and a back end. The front end is responsible for parsing the source code into the intermediate code LLVM IR, and the back end consists of a group of code generation analyzers and optimization processes. These processes convert LLVM IR into target code. The back end has a pipeline structure, and instructions need to go through multiple different processes. By customizing the LLVM back end, efficient machine code for a specific hardware platform can be generated. However, customizing a back end requires very high technical capabilities, and there are almost no new computer languages invented based on LLVM in China. The main principle of the compiler developed by Microsoft Corporation is similar to that of LLVM. The main difference is that it does not implement modular design and is not convenient for rapid expansion. Summary of the Invention

[0003] The present invention provides a brand-new compilation method. By means of the technology of combining framework code and business code, one computer language is compiled into another computer language. The other computer languages compiled and output can be recompiled into efficient machine code by using their mature compilers and run on corresponding devices. The present invention has the following advantages: It reduces the technical difficulty of inventing a new computer language and can promote the development and innovation of computer languages. It reduces the difficulty of customizing back-end compilation and provides technical possibilities for using one computer language to solve problems in different fields. Thus, it can be achieved: 2.1 Compile all other computer languages with one computer language, providing a solution for maintaining software developed in computer languages that have "become extinct" in the future; 2.2 It is possible to develop only with one computer language in fields where other computer languages are good at.

[0004] A compilation method provided by the present invention includes: Design the compilation framework of the target language; Design the class interfaces and classes of the target language; Load the compilation framework of the target language and the class interface definition; Load the source program to be compiled; Perform semantic analysis on the source program. If there are syntax errors, abort the compilation and feedback the error content; Generate an abstract semantic tree; Parse each node of the semantic tree and parse each node into multiple pieces of target language code according to the definitions and rules in the compilation framework; Optimize the generated target code; Output and save the target language code. Description of the Drawings

[0005] To more clearly illustrate the specific embodiments of the present invention, the drawings required for the specific embodiments will be briefly introduced below. In all the drawings, similar elements or parts are generally identified by similar reference numerals. In the drawings, the various elements or parts are not necessarily drawn to scale.

[0006] Figure 1 Shows a flowchart of a compilation method provided by an embodiment of the present invention. Embodiments

[0007] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the drawings in the embodiments of the present invention. Obviously, the described embodiments are some, but not all, of the embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.

[0008] In this specification and the appended claims, "source program" or "source code" both refer to the input file to be compiled, and "target program" or "target code" both refer to the output file of the compilation.

[0009] In this specification and the appended claims, "current language" or "source language" refers to the computer language used in the source program, and "target language" refers to the computer language used in the compiled output program.

[0010] It should be understood that when used in this specification and the appended claims, the terms "include" and "comprise" indicate the presence of the described features, wholes, steps, operations, elements, and / or components, but do not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components, and / or their combinations.

[0011] It should also be understood that the terms used in the specification of the present invention are merely for the purpose of describing specific embodiments and are not intended to limit the present invention. As used in the specification of the present invention and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an" and "the" are intended to include the plural forms.

[0012] It should be further understood that the term "and / or" used in the specification of the present invention and the appended claims refers to any combination and all possible combinations of one or more of the associated listed items, and includes these combinations.

[0013] It should be further understood that the term "function" used in the specification of the present invention and the appended claims refers to the method that needs to be implemented in the compilation method, which is equivalent to the function or object method, interface, etc. in the compilation system. The term "framework" includes not only the frameworks defined in software development, but also other external source programs and related supporting files.

[0014] As used in this specification and the appended claims, the term "various" can be interpreted as "multiple" according to the context.

[0015] It should be noted that unless otherwise specified, the technical terms or scientific terms used in this application should have the ordinary meaning understood by those skilled in the art to which the present invention belongs.

[0016] Figure 1 The following shows a compilation method provided by an embodiment of the present invention, including:

[0017] S101, designing a compilation framework for the target language.

[0018] Specifically, design a code framework for the target language, design one or more statement conversion functions for the target language, and provide a statement conversion interface, and construct the code framework and the statement conversion functions into a library file (such as dll, jar, pyd, etc.).

[0019] S102, designing the class interfaces and classes of the target language.

[0020] Specifically, design one or more interface files for the classes of the target language and save them as structure files (such as XML, INI, etc.); design one or more class files (such as cpp, java, py, etc.) for the target language according to the interface files.

[0021] S103, loading the compilation framework of the target language and the class interface definitions.

[0022] Specifically, load the library file of the compilation framework designed in S101, create target code variables (in the form of lists, maps, dictionaries, structures, etc.), parse the code framework of the target language read and store it in the target code variables; obtain the interface for statement conversion, which will be called in S107; load the class interface file of the target language designed in S102, read the class interface definition, and store it in the variable of class definition (in the form of lists, maps, dictionaries, structures, etc.).

[0023] S104, load the source program to be compiled.

[0024] Specifically, read the source program file to be compiled currently, remove contents such as comments, and store it line by line in memory. The source program to be compiled can be a source program written in any computer language, such as loongpy (a computer language for WEB3.0).

[0025] S105, perform semantic analysis on the source program. If there are syntax errors, abort the compilation and feedback the error content.

[0026] Specifically, according to the language name of the source program, call the semantic analysis module of the source language, retrieve the source code from memory, first perform lexical analysis, and then check whether the syntax of the source program language is correct and conforms to the specifications of the language. If an error is found, return the error content and location; At the same time, according to the definition of each class in the variable of class definition, check whether the objects in the source program conform to the definition of the class to which the object belongs. If an error is found, return the error content and location.

[0027] S106, generate an abstract semantic tree.

[0028] Specifically, call the semantic analysis module of the source language, and use a tree-like structure that conforms to the syntax rules of the source language to store the statements of the source program.

[0029] S107, parse each node of the semantic tree, and parse each node into multiple pieces of target language code according to the definitions and rules in the compilation framework.

[0030] Specifically, traverse the nodes of the semantic tree of the source language. At each node, call the statement conversion interface read in S103, convert the statement of the source language at this node into multiple pieces of target language code, and insert it into the target code variable created in S103.

[0031] S108, optimize the generated target code.

[0032] Specifically, analyze the target code variable, insert some auxiliary code into the target code variable, and merge some code in the target code variable, with the aim of optimizing the running efficiency of the target code.

[0033] S109, output and save the target language code.

[0034] Specifically, create the folder and subfolders of the target program, read the content of the target code variable, and save it to an external file in a streaming manner. This file is the main program of the target program. At the same time, copy the class files and interface files designed in S102 to the main program directory or module directory. In this way, a complete target program written in the target language is completed.

Claims

1. A compilation method based on framework technology, characterized in that , including: The compilation framework of the designed target language; The class interfaces and classes of the designed target language; Loading the compilation framework of the target language and the class interface definitions; Loading the source program to be compiled; Performing semantic analysis on the source program. If there are syntax errors, abort the compilation and feedback the error content; Generating an abstract semantic tree; Parsing each node of the semantic tree and parsing each node into multiple pieces of target language code according to the definitions and rules in the compilation framework; Optimizing the generated target code; Outputting and saving the target language code.

2. The compilation method based on the framework technology according to claim 1, characterized in that The compilation framework of the designed target language further includes: The compilation framework of the target language contains the basic class code of the target language, contains the rules for converting the target language, and contains a framework description configuration file.

3. The compilation method based on the framework technology according to claim 1, characterized in that The compilation framework of the designed target language further includes: Different compilation frameworks of the target language can be designed according to different scenarios, that is, a target language can have multiple compilation frameworks, and different compilation frameworks compile different target codes.

4. The compilation method based on the framework technology according to claim 1, characterized in that The class interfaces and classes of the designed target language further includes: The class interface of the target language is a separate configuration file, and the class is a program file written in the target language and can call the methods and properties of these classes.

5. The compilation method based on the framework technology according to claim 1, wherein The class interfaces and classes of the designed target language further includes: The class of the target language is a program file written in the target language. The program file can be the target language code or the binary code after compilation of the target code.

6. The compilation method based on the framework technology according to claim 1, characterized in that The class interfaces and classes of the designed target language further includes: The same interface file can correspond to multiple class files of the target language.

7. The compilation method based on the framework technology according to claim 1, characterized in that Loading the compilation framework of the target language and the class interface definitions further includes: This compilation method will load the corresponding compilation framework according to the framework description configuration file and can load multiple compilation frameworks at the same time.

8. The compilation method based on the framework technology according to claim 1, characterized in that Loading the compilation framework of the target language and the class interface definitions further includes: This compilation method will import the corresponding class interfaces according to the import statements in the source code to be compiled and import the class files corresponding to the class interfaces to the specified location at the same time.

9. The compilation method based on the framework technology according to claim 1, characterized in that Performing semantic analysis on the source program. If there are syntax errors, abort the compilation and feedback the error content further includes: The compilation system first performs lexical analysis on the source code to be compiled, and then performs semantic analysis on the source code according to the syntax rules of the source code and the rules in the compilation framework and class interfaces and points out the problems.

10. The compilation method based on the framework technology according to claim 1, characterized in that Parsing each node of the semantic tree and parsing each node into multiple pieces of target language code according to the definitions and rules in the compilation framework further includes: Select the corresponding compilation framework and the class interfaces in the import statements before compilation, and then read the rules in the compilation framework and the class interface rules according to this information.

11. The compilation method based on the framework technology according to claim 1, characterized in that Parsing each node of the semantic tree and parsing each node into multiple pieces of target language code according to the definitions and rules in the compilation framework further includes: Converting the statement to be compiled into one or more statements of the target language according to the rules in the compilation framework, performing calculations based on some rules in the framework, and finally placing these statements of the target language at the specified positions in the framework.