CN111158665A - Code generation method and device, electronic equipment and storage medium - Google Patents

Code generation method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN111158665A
CN111158665A CN201911376502.3A CN201911376502A CN111158665A CN 111158665 A CN111158665 A CN 111158665A CN 201911376502 A CN201911376502 A CN 201911376502A CN 111158665 A CN111158665 A CN 111158665A
Authority
CN
China
Prior art keywords
code
source code
language
key information
abstract syntax
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN201911376502.3A
Other languages
Chinese (zh)
Other versions
CN111158665B (en
Inventor
陈泽欣
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Netease Hangzhou Network Co Ltd
Original Assignee
Netease Hangzhou Network Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Netease Hangzhou Network Co Ltd filed Critical Netease Hangzhou Network Co Ltd
Priority to CN201911376502.3A priority Critical patent/CN111158665B/en
Publication of CN111158665A publication Critical patent/CN111158665A/en
Application granted granted Critical
Publication of CN111158665B publication Critical patent/CN111158665B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The embodiment of the invention provides a code generation method and device, electronic equipment and a storage medium.

Description

Code generation method and device, electronic equipment and storage medium
Technical Field
The present invention relates to computer technologies, and in particular, to a code generation method and apparatus, an electronic device, and a storage medium.
Background
Most game engines and game frames are written by using C + + language and then matched with scripting language, some game engines and game frames use python scripting language, and when a module with sensitive performance needs to be written, the C + + language is matched with the scripting language to complete function realization. To interface the C + + language with the scripting language, binding code is typically written to cause the scripting language to call the C + + language module.
The existing binding code is manually written by a developer by using a C interface provided by Python or an expansion library packaged with the C interface. The binding code is directly written by using the C interface, the code running efficiency is fastest, but developers need to write a large number of repeated and burdensome codes, errors are easy to occur, and the coding efficiency is low. And the binding code is compiled by using the expansion library packaged with the C interface, so that the workload of a developer for compiling the code can be reduced, but the developer needs to learn the use specification of the expansion library and has higher requirements on the developer.
Because the existing binding code is manually written by developers, the requirement on the developers is high, and the code generation efficiency is low.
Disclosure of Invention
The embodiment of the invention provides a code generation method and device, electronic equipment and a storage medium, and aims to solve the technical problems that the existing code generation method has high requirements on developers and the code generation efficiency is low.
In a first aspect, the present invention provides a code generation method, including:
acquiring an abstract syntax tree of a first source code; wherein the first source code is written in a first language;
extracting key information for interacting the first source code and the second source code from the abstract syntax tree; wherein the second source code is written in a second language;
and writing the key information into a preset code template to generate a binding code for enabling the first source code and the second source code to interact.
Optionally, writing the key information into a preset code template, and generating a binding code for enabling the first source code and the second source code to interact, where the method further includes:
the code template is written using an extended library for interacting the first language and the second language.
Optionally, after writing the key information into a preset code template and generating binding code for enabling the first source code and the second source code to interact, the method further includes:
and compiling the binding code, the extension library and the first source code to generate a link library.
Optionally, obtaining the abstract syntax tree of the first source code specifically includes:
the first source code is analyzed using a syntax analysis tool to generate an abstract syntax tree.
Optionally, the first language is a C + + language; the second language is the python language.
Optionally, the syntax analysis tool comprises a libblang tool.
Optionally, the key information includes at least any one of:
variable names, function parameters, class names, constant names, and constant values.
Optionally, the extended library is: a pybind11 extended library, or a boost.
In a second aspect, the present invention provides a code generation apparatus, comprising:
the obtaining module is used for obtaining an abstract syntax tree of the first source code; wherein the first source code is written in a first language;
the extraction module is used for extracting key information for enabling the first source code and the second source code to interact from the abstract syntax tree; wherein the second source code is written in a second language;
and the generating module is used for writing the key information into a preset code template and generating a binding code for enabling the first source code and the second source code to interact.
Optionally, the apparatus further comprises: compiling a module;
the writing module is to write a code template using an extended library for interacting the first language and the second language.
Optionally, the generating module is further configured to:
and compiling the binding code, the extension library and the first source code to generate a link library.
Optionally, the obtaining module is specifically configured to:
the first source code is analyzed using a syntax analysis tool to generate an abstract syntax tree.
Optionally, the first language is a C + + language; the second language is python.
Optionally, the syntax analysis tool comprises a libblang tool.
Optionally, the key information includes at least any one of:
variable names, function parameters, class names, constant names, and constant values.
Optionally, the extended library is: a pybind11 extended library, or a boost.
In a third aspect, the present invention provides an electronic device comprising:
a memory for storing a program;
a processor for executing the program stored in the memory, the processor being adapted to perform the code generation method according to the first aspect and the alternative when the program is executed.
In a fourth aspect, the present invention provides a storage medium having a computer program stored thereon, wherein the computer program is arranged to perform the code generation method according to the first aspect and the alternative when running.
The embodiment of the invention provides a code generation method and device, electronic equipment and a storage medium.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings needed to be used in the description of the embodiments or the prior art will be briefly introduced below, and it is obvious that the drawings in the following description are some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to these drawings without creative efforts.
FIG. 1 is a flow diagram illustrating a code generation method in accordance with an exemplary embodiment of the present invention;
FIG. 2 is a flow diagram illustrating a code generation method in accordance with another illustrative embodiment of the present invention;
FIG. 3 is a diagram of the relationship of the common structure in the C + + language;
FIG. 4 is a flow diagram for generating an abstract syntax tree;
FIG. 5 is a block diagram illustrating a code generation apparatus according to an exemplary embodiment of the present invention;
FIG. 6 is a schematic diagram illustrating the structure of an electronic device in accordance with an exemplary embodiment of the present invention;
fig. 7 is a data flow diagram of a code generation apparatus provided in the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The existing binding codes are all manually written by developers, and the developers can directly write by using a C interface provided by Python and can write by using an encapsulated extension library. If the C interface is directly used for writing, the operation efficiency of binding the codes is high, but developers need to write a large amount of repeated codes, errors are easy to occur, and the efficiency is not high. If a developer writes the extended library encapsulated by the C interface, the developer needs to master the writing specification of the extended library, and the requirement on the developer is high. Therefore, the existing mode of manually writing the binding code has low efficiency and high requirements on developers.
The invention conception of the invention is as follows: and adopting an automatic compiling mode to replace the existing manual compiling mode, firstly analyzing the first source code to generate an abstract syntax tree of the first source code, then further extracting key information from the abstract syntax tree, wherein the key information is used for realizing the interaction of the first source code and the second source code, and then writing the key information into a code template to generate the binding code. The method can automatically generate the binding code, has high efficiency and reduces the requirement on developers.
FIG. 1 is a flow diagram illustrating a method of code generation in accordance with an exemplary embodiment of the present invention. As shown in fig. 1, the code generation method provided by the present invention includes the following steps:
s101, obtaining an abstract syntax tree of a first source code.
More specifically, the first source code is written in a first language, and an Abstract Syntax Tree (AST) of the first source code is generated by parsing the first source code, wherein the Abstract Syntax Tree represents a Syntax structure of the first source code in a Tree form, and each node on the Tree represents one structure in the source code.
And S102, extracting key information for enabling the first source code and the second source code to interact from the abstract syntax tree.
More specifically, by analyzing the structure of the abstract syntax tree, key information for enabling the first source program to interact with the second source program is found from each node.
And S103, writing the key information into a preset code template to generate a binding code for enabling the second source code to interact with the first source code.
More specifically, the code template is used for generating the binding code, most of the code in the code template is written, and the binding code writing can be completed only by filling key information for interaction into the code template. The binding code is used to implement interaction of a second source code and the first source code, wherein the second source code is written in a second language.
In the code generation method provided by the embodiment, the abstract syntax tree of the source code is obtained by analyzing the syntax structure of the first source code, then the key information in the abstract syntax tree is extracted, the key information in the first source code is written into the code template, and finally the binding code is generated.
Fig. 2 is a flowchart illustrating a code generation method according to another exemplary embodiment of the present invention. As shown in fig. 2, the code generation method provided by the present invention includes the following steps:
s201, obtaining an abstract syntax tree of the first source code.
More specifically, obtaining the abstract syntax tree of the first source code specifically includes: the first source code is analyzed using a syntax analysis tool to generate an abstract syntax tree. If the first source code is written by using the C + + language, the first source code is analyzed by using a libblang tool to generate an abstract syntax tree.
When the first source code is written in C + + language, the commonly used structures are shown in table 1 below, and fig. 3 is a relational diagram of the commonly used structures in C + + language.
TABLE 1C + + language middle structure
Namespace Name space Class Class/structure
Function Ordinary function CXXMethod Membership functions
Variable Common variables Param Function type parameter
Enum Enumeration Field Member variable
The libblang tool provides functionality for the compilation front-end of cpp files, which can be used to parse the cpp files and generate abstract syntax trees. The analysis of the first source code by using the libblang tool is mainly a recursive process, and an abstract syntax tree is generated by searching key information, wherein concrete codes for generating the abstract syntax tree in the libblang tool are as follows:
CXChildVisitResult TravelNamespace(CXCursor cursor,CXCursor parent,CXClientData client_data);
CXChildVisitResult TravelClass(CXCursor cursor,CXCursor parent,CXClientData client_data);
CXChildVisitResult TravelFunction(CXCursor cursor,CXCursor parent,CXClientData client_data);
CXChildVisitResult TravelCXXMethod(CXCursor cursor,CXCursor parent,CXClientData client_data);
CXChildVisitResult TravelEnum(CXCursor cursor,CXCursor parent,CXClientData client_data);
CXChildVisitResult TravelVariable(CXCursor cursor,CXCursor parent,CXClientData client_data);
CXChildVisitResult TravelField(CXCursor cursor,CXCursor parent,CXClientData client_data);
CXChildVisitResult TravelParam(CXCursor cursor,CXCursor parent,CXClientData client_data);
fig. 4 is a flowchart for generating an abstract syntax tree, and as shown in fig. 4, a Namespace is traversed first, if the Namespace is nested in a sub-Namespace, a structure in the sub-Namespace is traversed, and if the Namespace is not nested in the sub-Namespace, a class (or a structure), an enumeration, a variable, and a function in the Namespace are traversed. When the class (or the structure) is traversed, the member variables and the member functions in the class (or the structure) are traversed, and when the member functions are traversed, the parameters in the member functions are traversed. When traversing a function, parameters in the function are also traversed.
And S202, extracting key information for enabling the first source code and the second source code to interact from the abstract syntax tree.
More specifically, if the first language is C + + and the second language is python, the key information includes at least one of: variable names, function parameters, class names, constant names, and constant values.
And S203, writing a code template by using an extended library for enabling the first language and the second language to interact.
More specifically, the extension library is used for realizing a library of interaction between a first language and a second language, and a code template is generated according to an example specification of binding codes in the extension library. If the first language is C + + language and the second language is python language, then the code template is written using the pybind11 extended library, and the written code template is as follows:
member function method
"(%s.def("%s",overload_cast2<%s>::instance(&%s));)"
Constructor method
"(%s.def(py::init<%s>());)"
Static member function method
"(%s.def_static("%s",overload_cast2<%s>::instance(&%s));)"
Member variable
"(def_field(%s,&%s,"%s");)"
Static member variables
"(def_static_field(%s,&%s,"%s");)"
In this embodiment, the code template may also be written using a boost.
The step is not limited to the step between S202 and S204 as long as it is before S204.
And S204, writing the key information into a preset code template, and generating a binding code for enabling the second source code to interact with the first source code.
More specifically, the key information is written into a preset code template, and if the key information is extracted to be any one or more of a variable name, a function parameter, a class name, a constant name and a constant value, the key information is filled into the code template to generate the binding code.
S205, compiling the binding code, the extension library and the first source code to generate a link library.
More specifically, after the binding code is generated, the binding code, the extension library and the first source code are compiled to generate a link library for the second source code to call.
If bound code is generated using the pybind11 extension library, the bound code, the first source code written in the C + + language, and the pybind11 extension library are compiled together to generate a linked library for use by the second source code written in the python language.
Compared with the manual writing of developers, the code generation method provided by the embodiment can realize the automatic generation of the bound codes, reduces the requirements on the developers, has high code generation efficiency and also improves the accuracy of code generation.
Fig. 5 is a schematic structural diagram of a code generation apparatus according to an exemplary embodiment of the present invention. As shown in fig. 5, the present invention provides a code generating apparatus, the apparatus 300 comprising:
an obtaining module 301, configured to obtain an abstract syntax tree of a first source code; wherein the first source code is written in a first language;
an extracting module 302, configured to extract key information from the abstract syntax tree;
a generating module 303, configured to write the key information into a preset code template, and generate a binding code for enabling the second source code to interact with the first source code; wherein the second source code is written in a second language.
Optionally, the apparatus further comprises: a writing module 304;
the writing module is used for writing a code template by using an extended library for realizing the interaction of the first source code and the second source code.
Optionally, the generating module 303 is further configured to:
and compiling the binding code, the extension library and the first source code to generate a link library.
Optionally, the obtaining module 301 is specifically configured to:
the first source code is analyzed using a syntax analysis tool to generate an abstract syntax tree.
Optionally, the syntax analysis tool comprises a libblang tool.
Optionally, the first language is a C + + language; the second language is the python language.
Optionally, the key information includes at least any one of:
variable names, function parameters, class names, constant names, and constant values.
Optionally, the extension library comprises: a pybind11 extended library or a boost.
Fig. 6 is a schematic structural diagram of an electronic device according to an exemplary embodiment of the present invention, and as shown in fig. 6, an electronic device 400 provided in this embodiment includes: a processor 401 and a memory 402.
A memory 402 for storing computer-executable instructions;
the processor 401 is configured to execute the computer-executable instructions stored in the memory to implement the steps performed by the code generation method in the above embodiments. Reference may be made specifically to the relevant description in the foregoing code generation method embodiments.
Alternatively, the memory 402 may be separate or integrated with the processor 401.
When the memory 402 is provided separately, the electronic device further comprises a bus 403 for connecting the memory 402 and the processor 401.
An embodiment of the present invention further provides a computer-readable storage medium, where computer-executable instructions are stored in the computer-readable storage medium, and when a processor executes the computer-executable instructions, the code generation method performed by the electronic device is implemented.
Next, the effect of the method is analyzed, fig. 7 is a data flow chart of the code generation method provided by the present invention, and as shown in fig. 7, the cpp file recorded with the source code is input into the code generation apparatus, so that the binding code written by using the pybind11 extension library can be output.
Wherein, the source code is as follows:
Figure BDA0002341117080000091
Figure BDA0002341117080000101
Figure BDA0002341117080000111
the binding code is as follows:
Figure BDA0002341117080000112
Figure BDA0002341117080000121
Figure BDA0002341117080000131
those of ordinary skill in the art will understand that: all or a portion of the steps of implementing the above-described method embodiments may be performed by hardware associated with program instructions. The program may be stored in a computer-readable storage medium. When executed, the program performs steps comprising the method embodiments described above; and the aforementioned storage medium includes: various media that can store program codes, such as ROM, RAM, magnetic or optical disks.
Finally, it should be noted that: the above embodiments are only used to illustrate the technical solution of the present invention, and not to limit the same; while the invention has been described in detail and with reference to the foregoing embodiments, it will be understood by those skilled in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some or all of the technical features may be equivalently replaced; and the modifications or the substitutions do not make the essence of the corresponding technical solutions depart from the scope of the technical solutions of the embodiments of the present invention.

Claims (11)

1. A method of code generation, the method comprising:
acquiring an abstract syntax tree of a first source code; wherein the first source code is written in a first language;
extracting key information for interacting the first source code and the second source code from the abstract syntax tree; wherein the second source code is written in a second language;
and writing the key information into a preset code template to generate a binding code for enabling the first source code and the second source code to interact.
2. The method of claim 1, before the writing the key information into a preset code template and generating binding code for enabling the first source code and the second source code to interact with each other, further comprising:
writing the code template using an extended library for interacting the first language and the second language.
3. The method of claim 2, wherein after the writing the key information into a preset code template to generate binding code for enabling the first source code and the second source code to interact with each other, further comprising:
compiling the binding code, the extension library and the first source code to generate a link library.
4. The method according to any one of claims 1 to 3, wherein the obtaining the abstract syntax tree of the first source code specifically comprises:
and analyzing the first source code by using a syntax analysis tool to generate the abstract syntax tree.
5. The method of claim 4, wherein the first language is a C + + language; the second language is a python language.
6. The method of claim 4, wherein the parsing tool comprises a libblang tool.
7. The method of claim 1, wherein the key information comprises at least one of:
variable names, function parameters, class names, constant names, and constant values.
8. The method of claim 2, wherein the extended library is a pybind11 extended library or a boost.
9. An apparatus for generating code, the apparatus comprising:
the obtaining module is used for obtaining an abstract syntax tree of the first source code; wherein the first source code is written in a first language;
an extraction module, configured to extract key information for interacting the first source code and the second source code from the abstract syntax tree; wherein the second source code is written in a second language;
and the generating module is used for writing the key information into a preset code template and generating a binding code for enabling the first source code and the second source code to interact.
10. An electronic device, comprising:
a memory for storing a program;
a processor for executing the program stored by the memory, the processor being configured to perform the code generation method of any of claims 1 to 8 when the program is executed.
11. A storage medium, in which a computer program is stored, wherein the computer program is arranged to execute a code generation method according to any one of claims 1 to 8 when running.
CN201911376502.3A 2019-12-27 2019-12-27 Code generation method and device, electronic equipment and storage medium Active CN111158665B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201911376502.3A CN111158665B (en) 2019-12-27 2019-12-27 Code generation method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201911376502.3A CN111158665B (en) 2019-12-27 2019-12-27 Code generation method and device, electronic equipment and storage medium

Publications (2)

Publication Number Publication Date
CN111158665A true CN111158665A (en) 2020-05-15
CN111158665B CN111158665B (en) 2023-07-18

Family

ID=70558442

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201911376502.3A Active CN111158665B (en) 2019-12-27 2019-12-27 Code generation method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN111158665B (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111625224A (en) * 2020-05-28 2020-09-04 北京百度网讯科技有限公司 Code generation method, device, equipment and storage medium
CN112114861A (en) * 2020-09-29 2020-12-22 北京云族佳科技有限公司 Method and device for counting skills used in program development and electronic equipment

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110262783A (en) * 2019-05-22 2019-09-20 深圳华科云动力科技有限公司 A kind of interface creation method, device and terminal device
US20190339947A1 (en) * 2018-05-07 2019-11-07 Red Hat, Inc. Automatically generating code for application programming interfaces
CN110609693A (en) * 2019-08-15 2019-12-24 平安国际智慧城市科技股份有限公司 Code updating method and device based on data standardization and terminal equipment

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20190339947A1 (en) * 2018-05-07 2019-11-07 Red Hat, Inc. Automatically generating code for application programming interfaces
CN110262783A (en) * 2019-05-22 2019-09-20 深圳华科云动力科技有限公司 A kind of interface creation method, device and terminal device
CN110609693A (en) * 2019-08-15 2019-12-24 平安国际智慧城市科技股份有限公司 Code updating method and device based on data standardization and terminal equipment

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111625224A (en) * 2020-05-28 2020-09-04 北京百度网讯科技有限公司 Code generation method, device, equipment and storage medium
CN111625224B (en) * 2020-05-28 2023-11-24 北京百度网讯科技有限公司 Code generation method, device, equipment and storage medium
CN112114861A (en) * 2020-09-29 2020-12-22 北京云族佳科技有限公司 Method and device for counting skills used in program development and electronic equipment
CN112114861B (en) * 2020-09-29 2024-03-26 北京云族佳科技有限公司 Method and device for counting skills used in program development and electronic equipment

Also Published As

Publication number Publication date
CN111158665B (en) 2023-07-18

Similar Documents

Publication Publication Date Title
US8028276B1 (en) Method and system for generating a test file
CN110149800B (en) Apparatus for processing abstract syntax tree associated with source code of source program
CN106611044B (en) SQL optimization method and equipment
US20130125098A1 (en) Transformation of Computer Programs
CN106796525B (en) On-demand loading dynamic script language codes are to reduce memory use
CN108469955B (en) Annotation-based Android injection framework implementation method
CN111427583A (en) Component compiling method and device, electronic equipment and computer readable storage medium
WO2017128952A1 (en) Stack protection method and device
CN108595334B (en) Method and device for calculating dynamic slices of Java program and readable storage medium
CN108241720B (en) Data processing method, device and computer readable storage medium
CN111158665B (en) Code generation method and device, electronic equipment and storage medium
CN110543299A (en) Cloud computing management platform code generation method and device
CN104049974A (en) Dynamic control assembly method and system
CN107797805B (en) Code packaging method and device and computer readable storage medium
CN105867886B (en) Method and device for writing table
US10642714B2 (en) Mapping dynamic analysis data to source code
CN113138755A (en) JSON serialization and deserialization optimization method and system
CN111475150A (en) Cross-language binding method, device, equipment and storage medium
CN105786465A (en) Scripting language execution method and device
US9697021B2 (en) Modifiable high-level intermediate representation of source code
CN115951890A (en) Method, system and device for code conversion between different front-end frames
CN109460236B (en) Program version construction and checking method and system
EP2535813B1 (en) Method and device for generating an alert during an analysis of performance of a computer application
CN111367512B (en) Method and device for creating Android library module dependency relationship in application development
CN112596737A (en) Method, system, equipment and storage medium for scanning function call relation

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant