Code generation method and device, equipment and storage medium
The code generation method and device improve software development efficiency by generating target code based on functional requirements, addressing the inefficiencies of manual coding and code review in existing methods.
Patent Information
- Application Number
- CN202310786387.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-29
- Publication Date
- 2024-12-31
AI Technical Summary
Existing software development methods require high professional knowledge and are time-consuming and labor-intensive, particularly in the coding and code review stages, necessitating improved efficiency in code generation.
A code generation method and device that extracts reference code and generation type from an instruction, performs code function identification, and generates target code based on functional requirement information, including options for optimization, translation, and review, utilizing a large language model for assistance.
Enhances code development efficiency by generating target code according to functional requirements, reducing the need for manual line-by-line coding by software engineers.
Smart Images

Figure CN119225729A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a code generation method, device, equipment and storage medium. Background Art
[0002] Existing software development is generally divided into the following steps: Requirements analysis: In order to determine the requirements of the software, analysts communicate with customers or end users. The requirements document will determine the goals, functions, performance requirements, etc. of the software. Design: Software engineers convert requirements into design documents. This usually includes the architecture, modules, interfaces, etc. of the software. The design document should ensure the scalability, maintainability and readability of the software. Coding: Programmers write code based on the design documents. They use specific programming languages (such as Java, Python, C++) to convert functions and logic into statements that can be understood by computers. Testing: After completing the coding, testers will test the software... In the software coding and codereview stages, code developers and reviewers are required to have relatively strong professional knowledge, the threshold is relatively high, and it is time-consuming and labor-intensive. Therefore, how to assist developers in coding and improve the efficiency of code development has become a technical problem that needs to be solved urgently.
[0003] The above contents are only used to assist in understanding the technical solution of the present invention and do not constitute an admission that the above contents are prior art. Summary of the invention
[0004] The main purpose of the present invention is to provide a code generation method, device, equipment and storage medium, aiming to solve the technical problems of code generation X in the prior art.
[0005] To achieve the above object, the present invention provides a code generation method, which comprises the following steps:
[0006] In response to the triggered code generation instruction, extracting the reference code and generation type in the code generation instruction;
[0007] Performing code function identification on the reference code to obtain function requirement information corresponding to the reference code;
[0008] Generate a target code corresponding to the reference code according to the generation type and the functional requirement information.
[0009] Optionally, the step of generating a target code corresponding to the reference code according to the generation type and the functional requirement information includes:
[0010] When the generation type is code optimization, display optional optimization directions;
[0011] Obtain a target optimization direction selected by a user based on the optimization direction;
[0012] The reference code is optimized according to the target optimization direction and the functional requirement information to generate a target code.
[0013] Optionally, the step of optimizing the reference code according to the target optimization direction and the functional requirement information to generate the target code includes:
[0014] Optimizing the reference code according to the target optimization direction to generate an intermediate code;
[0015] Performing a functional test on the intermediate code according to the functional requirement information to obtain a functional test result;
[0016] When the function test result is consistent with the code function in the function requirement information, the intermediate code is determined to be the target code.
[0017] Optionally, the step of generating a target code corresponding to the reference code according to the generation type and the functional requirement information includes:
[0018] When the generation type is code translation, displaying translatable translation code languages;
[0019] Acquire a target language selected by a user based on the translation code language;
[0020] A target code corresponding to the reference code is generated according to the target language and the functional requirement information.
[0021] Optionally, the step of generating a target code corresponding to the reference code according to the target language and the functional requirement information includes:
[0022] Obtaining a reference code language corresponding to the reference code;
[0023] Determining first grammatical information corresponding to the reference code language;
[0024] Determining second grammar information corresponding to the target language;
[0025] Generate a target code corresponding to the reference code according to the first grammatical information, the functional requirement information and the second grammatical information.
[0026] Optionally, the step of generating a target code corresponding to the reference code according to the first grammatical information, the functional requirement information and the second grammatical information includes:
[0027] Generate a code to be verified according to the first grammatical information, the functional requirement information and the second grammatical information;
[0028] Determine the test cases corresponding to the functional requirement information;
[0029] Performing a functional test on the code to be verified according to the test case to obtain a functional test result;
[0030] When the function test result is consistent with the function requirement information, the target code corresponding to the reference code is the code to be verified.
[0031] Optionally, the step of generating a target code corresponding to the reference code according to the generation type and the functional requirement information includes:
[0032] Determining a code language corresponding to the reference code;
[0033] Generate a target code corresponding to the reference code according to the code language, the functional requirement information and the generation type.
[0034] Optionally, before the step of extracting the reference code and generation type in the code generation instruction in response to the triggering code generation instruction, the step further includes:
[0035] Obtaining a current focus position, and determining whether a target code file corresponding to the current focus position meets a preset condition;
[0036] When the target code file corresponding to the current focus position meets a preset condition, a code language corresponding to the target code file is determined.
[0037] Optionally, the step of generating a target code corresponding to the reference code according to the generation type and the functional requirement information includes:
[0038] When the generation type is code review, judging whether the function of the reference code is complete according to the functional requirement information;
[0039] When the functionality of the reference code is complete, determining the code structure and code performance of the reference code;
[0040] A target code corresponding to the reference code is generated according to the code structure and the code performance.
[0041] Optionally, after the step of generating the target code corresponding to the reference code according to the generation type and the functional requirement information, the method further includes:
[0042] Displaying the object code;
[0043] Obtaining an insertion code and an insertion position selected by a user based on the displayed target code;
[0044] The insertion code is inserted into the target code file based on the insertion position.
[0045] Optionally, the step of generating a target code corresponding to the reference code according to the generation type and the functional requirement information includes:
[0046] Determining the character length corresponding to the reference code;
[0047] When the character length is greater than a preset character length threshold, clipping the reference code according to the preset character length threshold to obtain a clipped reference code;
[0048] Generate a target code corresponding to the clipped reference code according to the generation type and the functional requirement information.
[0049] In addition, to achieve the above object, the present invention also provides a code generation device, the device comprising:
[0050] A response module, configured to extract a reference code and a generation type in the code generation instruction in response to the triggered code generation instruction;
[0051] A function identification module, used to perform code function identification on the reference code to obtain function requirement information corresponding to the reference code;
[0052] A generation module is used to generate a target code corresponding to the reference code according to the generation type and the functional requirement information.
[0053] In addition, to achieve the above-mentioned purpose, the present invention also proposes a code generation device, which includes: a memory, a processor, and a code generation program stored in the memory and executable on the processor, wherein the code generation program is configured to implement the steps of the code generation method described above.
[0054] In addition, to achieve the above-mentioned purpose, the present invention further proposes a storage medium, on which a code generation program is stored, and when the code generation program is executed by a processor, the steps of the code generation method described above are implemented.
[0055] The present invention responds to a triggered code generation instruction, extracts the reference code and generation type in the code generation instruction; performs code function identification on the reference code to obtain functional requirement information corresponding to the reference code; and generates the target code corresponding to the reference code according to the generation type and the functional requirement information. Since the present invention generates the target code corresponding to the reference code according to the functional requirement information and generation type of the reference code. Compared with the existing method of completing software development only by software engineers manually writing code line by line, the above method of the present invention can generate target code according to the functional requirements of the code, thereby improving the efficiency of code development. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] Figure 1 It is a structural diagram of a code generation device of a hardware operating environment involved in an embodiment of the present invention;
[0057] Figure 2 This is a flow chart of a first embodiment of a code generation method of the present invention;
[0058] Figure 3 This is a flow chart of a second embodiment of a code generation method according to the present invention;
[0059] Figure 4 A schematic diagram of optional optimization directions of the second embodiment of the code generation method of the present invention;
[0060] Figure 5 This is a flow chart of a third embodiment of a code generation method according to the present invention;
[0061] Figure 6 It is a structural block diagram of the first embodiment of the code generation device of the present invention.
[0062] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0063] It should be understood that the specific embodiments described herein are only used to explain the present invention, and are not used to limit the present invention.
[0064] Reference Figure 1 , Figure 1 The present invention is a schematic diagram of the structure of a code generation device for a hardware operating environment according to an embodiment of the present invention.
[0065] like Figure 1As shown, the code generation device may include: a processor 1001, such as a central processing unit (CPU), a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. Among them, the communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 may include a display screen (Display), an input unit such as a keyboard (Keyboard), and the optional user interface 1003 may also include a standard wired interface and a wireless interface. The network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a wireless fidelity (Wireless-Fidelity, WI-FI) interface). The memory 1005 may be a high-speed random access memory (Random Access Memory, RAM), or a stable non-volatile memory (Non-Volatile Memory, NVM), such as a disk storage. The memory 1005 may also be a storage device independent of the aforementioned processor 1001.
[0066] Those skilled in the art will understand that Figure 1 The structure shown in the figure does not constitute a limitation on the code generating device, and may include more or less components than shown in the figure, or combine certain components, or arrange the components differently.
[0067] like Figure 1 As shown, the memory 1005 as a storage medium may include an operating system, a network communication module, a user interface module, and a code generation program.
[0068] exist Figure 1 In the code generating device shown, the network interface 1004 is mainly used for data communication with the network server; the user interface 1003 is mainly used for data interaction with the user; the processor 1001 and the memory 1005 in the code generating device of the present invention can be set in the code generating device, and the code generating device calls the code generating program stored in the memory 1005 through the processor 1001, and executes the code generating method provided by the embodiment of the present invention.
[0069] Based on the above code generation device, the embodiment of the present invention provides a code generation method, referring to Figure 2 , Figure 2 FIG. 1 is a flow chart of the first embodiment of the code generation method of the present invention.
[0070] In this embodiment, the code generation method includes the following steps:
[0071] Step S10: In response to the triggered code generation instruction, extract the reference code and generation type in the code generation instruction.
[0072] It should be noted that the execution subject of this embodiment can be a computing service device with data processing, network communication and program running functions, such as a mobile phone, a tablet computer, a personal computer, etc., or an electronic device or a code generation device that can realize the above functions. The following takes the code generation device as an example to illustrate this embodiment and the following embodiments.
[0073] In order to improve information processing capabilities, in some embodiments, a large language model can be installed on the code generation device. The large language model can be a program built based on natural language processing technology of the Generative Pre-trained Transformer (GPT) model. The large language model can receive user input text, parse the input text into a computer-understandable data format, and then use a pre-trained neural network model to generate response text.
[0074] In the specific implementation, the large language model uses the neural network in deep learning technology and is trained through a large amount of corpus data, so that it can learn the structure, grammatical rules and semantic information of the language. After the user enters the text, the large language model will first segment and parse the text, convert it into a computer-readable vector form, and then send it to the pre-trained neural network model for inference. During the inference process, the large language model will predict the most likely next response based on the text entered by the user and the previous context. This prediction process is based on the model's learning of a large amount of corpus data, so it can largely restore the way humans express natural language and generate highly coherent and natural response text. Finally, the large language model returns the generated response text to the user to complete a conversation interaction. Throughout the process, the large language model will continue to learn and optimize to provide more accurate and user-friendly answers.
[0075] It should be noted that the code generation instruction may be a command triggered by a user to generate corresponding code according to the reference code and generation type selected by the user. The generation type may include: code recommendation, code translation, code review, code comment generation, code optimization, etc. The reference code may be a code selected or input by the user as a reference to generate the target code. For example, if the user selects a piece of code A and the generation type is code comment, the user requirement corresponding to the code generation instruction may be to generate a code comment corresponding to the code A selected by the user.
[0076] Furthermore, in order to accurately capture the needs of the user, before step S10, the method further includes: obtaining a current focus position, and determining whether a target code file corresponding to the current focus position meets a preset condition;
[0077] When the target code file corresponding to the current focus position meets a preset condition, a code language corresponding to the target code file is determined.
[0078] It should be noted that the current focus position may be the currently active element or input area in the user interface, such as a text box, button or other operable interface element. In many programming languages and frameworks, there are relevant APIs and methods to query and set the focus position. The target code file corresponding to the current focus position may be the code file corresponding to the current focus position. Determining whether the target code file corresponding to the current focus position meets the preset conditions may be determining whether the target code file is a readable and writable file, and when the target code file is a readable and writable file, determining that the target code file meets the preset conditions.
[0079] Step S20: performing code function identification on the reference code to obtain function requirement information corresponding to the reference code.
[0080] It should be noted that the code function identification of the reference code may be to identify the function of the reference code, such as bubble sort, product, function call, etc. The function requirement information may be the code function corresponding to the reference or the function that the user may want to enter the reference code. For example, if the user enters the code S, it is inferred that the user may want to use the code SUM for summation.
[0081] Step S30: Generate a target code corresponding to the reference code according to the generation type and the functional requirement information.
[0082] It should be noted that the target code may be a code that meets user expectations and is generated based on the functional requirement information corresponding to the reference code. Generating the target code corresponding to the reference code according to the generation type and the functional requirement information may be inputting the generation type, the functional requirement information and the reference code into the large language model to obtain the target code output by the large language model.
[0083] Furthermore, in order to accurately generate the target code corresponding to the reference code, the step S30 may include: determining the code language corresponding to the reference code;
[0084] Generate a target code corresponding to the reference code according to the code language, the functional requirement information and the generation type.
[0085] It should be noted that the code language may be the programming language of the reference code, which may be determined in the following manner: File extension: check the file extension of the reference code, such as .py (Python), .js (JavaScript), .java (Java), etc.
[0086] Syntax and structure: Observe the keywords, grammar rules and code structure in the code. Each programming language has its own unique syntax and structure characteristics.
[0087] Comment style: Pay attention to the comment style in the code, such as Python's # single-line comment, C / C++'s / / single-line comment, and / ** / multi-line comment.
[0088] Libraries and functions: View the libraries and functions called in your code and learn which programming languages they typically belong to.
[0089] Programming paradigm: Observe the programming style of the code (object-oriented, functional, procedural, etc.) and understand which programming language is more likely to use this style.
[0090] Further, in order to assist developers in writing codes, the step S30 may include: when the generation type is code review, judging whether the function of the reference code is complete according to the function requirement information;
[0091] When the functionality of the reference code is complete, determining the code structure and code performance of the reference code;
[0092] A target code corresponding to the reference code is generated according to the code structure and the code performance.
[0093] It should be noted that the code review is a key activity in the software development process, which involves checking and evaluating the code written by others to ensure the quality, readability, maintainability and compliance with project requirements and programming specifications of the code. The determination of whether the function of the reference code is complete based on the functional requirement information can be to detect whether the reference code can complete the corresponding function in the functional requirement information when running, and whether there is an error or other situation that the code cannot run. Generating the target code corresponding to the reference code based on the code structure and the code performance can be to obtain the target code by optimizing the code structure and the code performance.
[0094] In a specific implementation, the reference code may be:
[0095] By conducting a code review on the above code, the review results can be: 1. Type annotations and variable declarations can be combined into one line;
[0096] 2. You can use for...of instead of for loop;
[0097] 3. Deconstruction assignment can simplify the code.
[0098] The resulting target code can be:
[0099]
[0100] Further, in order to facilitate the user to use the code generation method in this embodiment, after the step S30, it may include: displaying the target code;
[0101] Obtaining an insertion code and an insertion position selected by a user based on the displayed target code;
[0102] The insertion code is inserted into the target code file based on the insertion position.
[0103] It should be noted that when the generation type is code recommendation, the generated target code may contain multiple insertable codes because the user's exact intention is uncertain. In this case, all optional target codes can be displayed for the user to select. Alternatively, after the target code is generated, the target code can be inserted into the target code file according to the insertion position selected by the user.
[0104] In a specific implementation, when the generation type is code recommendation, the code recommendation generates code based on the code context or prompt. For example, when the user enters S, the target code can be various functions starting with the letter S, such as sum, strdup function, and strcpy function. At this time, the user can select the function to be used based on the needs of code development without manually spelling the function.
[0105] In a specific implementation, when the generation type is to generate code comments, the large language model can generate each line of comments corresponding to the reference code according to the functional requirement information of the reference code, and generate a target code containing code comments according to the generated comments and the corresponding code. For example, for the selected reference code of bubble sort, the generated target code can be:
[0106]
[0107]
[0108] Furthermore, due to the limitation of the number of characters in the large language model, when the number of characters in the reference code is large, all characters may not be recognized. Therefore, the step S30 includes: determining the character length corresponding to the reference code;
[0109] When the character length is greater than a preset character length threshold, clipping the reference code according to the preset character length threshold to obtain a clipped reference code;
[0110] Generate a target code corresponding to the clipped reference code according to the generation type and the functional requirement information.
[0111] It should be noted that the preset character length threshold may be the maximum character length that can be recognized by a large language model or other model or device. The trimming of the reference code according to the preset character length threshold may be trimming the reference code into a plurality of code segments whose number of characters is not greater than the preset character length threshold according to the preset character length threshold. The generating of the target code corresponding to the trimmed reference code according to the generation type and the functional requirement information may be generating target codes corresponding to a plurality of trimmed reference codes respectively, and then combining the target codes corresponding to the plurality of trimmed reference codes to obtain a complete target code.
[0112] In response to the triggered code generation instruction, this embodiment extracts the reference code and generation type in the code generation instruction; performs code function identification on the reference code to obtain functional requirement information corresponding to the reference code; and generates the target code corresponding to the reference code according to the generation type and the functional requirement information. Since this embodiment generates the target code corresponding to the reference code according to the functional requirement information and generation type of the reference code. Compared with the existing method of completing software development only by software engineers manually writing code line by line, the above method of this embodiment can generate target code according to the functional requirements of the code, thereby improving the efficiency of code development.
[0113] refer to Figure 3 , Figure 3 Schematic diagram of the flow chart of the second embodiment of the code generation method of the present invention.
[0114] Based on the above first embodiment, in this embodiment, step S30 includes:
[0115] Step S301: When the generation type is code optimization, optional optimization directions are displayed.
[0116] It should be noted that, please refer to Figure 4 , Figure 3 Schematic diagram of optional optimization directions of the first embodiment of the code generation method of the present invention. Figure 4It can be seen that when the user selects the generation type as code optimization, the user can Figure 4 Select the optimization direction you want in Optimization Direction.
[0117] Step S302: Obtain a target optimization direction selected by a user based on the optimization direction.
[0118] It should be noted that the target optimization direction can be one or more optimization directions selected by the user based on the displayed optional optimization directions. For example, you can choose to optimize by algorithm and optimize by style at the same time, and you can further choose to optimize according to the naming conventions in style optimization, and you can select the naming rules.
[0119] Step S303: Optimize the reference code according to the target optimization direction and the functional requirement information to generate a target code.
[0120] It should be noted that the reference code is optimized according to the target optimization direction and the functional requirement information to generate the target code. The reference code is optimized based on the target optimization direction to obtain the optimized code, and then the optimized code is detected to see whether it meets the functional requirement information. If so, the optimized code is used as the target code.
[0121] In a specific implementation, the reference code may be:
[0122] If the target optimization direction selected by the user is performance optimization, then according to the above reference code, the functional requirement information corresponding to the reference code is bubble sort. Based on the functional requirement information and the optimization type of performance optimization, the optimization suggestions can be:
[0123] 1. For an array that has been sorted, the entire double loop will still be executed, so you can add judgments and optimize unnecessary loop operations;
[0124] 2. If no elements are swapped in a certain loop, it means that the array has been sorted and you can exit the loop directly.
[0125] The generated object code can be:
[0126] Further, in order to prevent the generated target code from failing to meet the functional requirement information corresponding to the reference code, the step S303 may include: optimizing the reference code according to the target optimization direction to generate an intermediate code;
[0127] Performing a functional test on the intermediate code according to the functional requirement information to obtain a functional test result;
[0128] When the function test result is consistent with the code function in the function requirement information, the intermediate code is determined to be the target code.
[0129] It should be noted that the functional test of the intermediate code according to the functional requirement information can be based on the functional requirement information to test whether the intermediate code has the function corresponding to the functional requirement information, and obtain a functional test result; when the functional test result is consistent with the code function in the functional requirement information, the intermediate code is determined to be the target code. When the functional test result is inconsistent with the code function in the functional requirement information, it means that the intermediate code has defects. At this time, the step of optimizing the reference code according to the target optimization direction can be returned to regenerate a new intermediate code and perform a functional test until the functional test result of the generated intermediate code is consistent with the code function in the functional requirement information.
[0130] In this embodiment, when the generation type is code optimization, optional optimization directions are displayed; the target optimization direction selected by the user based on the optimization direction is obtained; the reference code is optimized according to the target optimization direction and the functional requirement information to generate the target code. In this embodiment, when the generation type is code optimization, the reference code can be optimized according to the target optimization direction selected by the user to generate the target code. It can improve the code development efficiency and user experience.
[0131] refer to Figure 5 , Figure 5 Schematic diagram of the flow chart of the third embodiment of the code generation method of the present invention.
[0132] Based on the above embodiments, in this embodiment, step S30 includes:
[0133] Step S304: when the generation type is code translation, displaying translatable translation code languages.
[0134] It should be noted that the code translation may be to convert the reference code into other code languages. For example, if the reference code is Java, the code translation may be to convert the reference code in Java into code in C++. The translatable code languages include but are not limited to: C++, C, C#, HTML, PHP, JAVA, JS, etc.
[0135] Step S305: obtaining a target language selected by the user based on the translation code language.
[0136] It should be noted that the target language may be a conversion language of the reference code selected by the user based on the translatable translation code language, for example, if the user selects C++, the target language is C++. The user desires to convert the reference code into C++ language.
[0137] Step S306: Generate a target code corresponding to the reference code according to the target language and the functional requirement information.
[0138] It should be noted that the target code corresponding to the reference code generated according to the target language and the functional requirement information may be converted into an intermediate code written in the target language, and then the intermediate code is detected based on the functional requirement information to see whether the function is consistent with that of the reference code. If they are consistent, it means that the conversion is successful, and the intermediate code is used as the target code. If they are inconsistent, it means that the conversion fails, and the step of converting the reference code into the intermediate code written in the target language is returned, and its function is detected until the function of the generated intermediate code is consistent with the code function in the functional requirement information.
[0139] Further, in order to improve the efficiency of code conversion, the step S306 includes: obtaining a reference code language corresponding to the reference code;
[0140] Determining first grammatical information corresponding to the reference code language;
[0141] Determining second grammar information corresponding to the target language;
[0142] Generate a target code corresponding to the reference code according to the first grammatical information, the functional requirement information and the second grammatical information.
[0143] It should be noted that the reference code language may be a code language corresponding to the reference code, such as C++, C, C#, HTML, PHP, JAVA, JS. The first grammatical information may include the grammar, structure and language characteristics of the reference code language, such as data types, control structures, functions and libraries. The second grammatical information may include the grammar, structure and language characteristics corresponding to the target language. The step of generating the target code corresponding to the reference code according to the first grammatical information, the functional requirement information and the second grammatical information includes:
[0144] Generate a code to be verified according to the first grammatical information, the functional requirement information and the second grammatical information;
[0145] Determine the test cases corresponding to the functional requirement information;
[0146] Performing a functional test on the code to be verified according to the test case to obtain a functional test result;
[0147] When the function test result is consistent with the function requirement information, the target code corresponding to the reference code is the code to be verified.
[0148] In a specific implementation, the large language model generates the code to be verified according to the first grammatical information, the functional requirement information and the second grammatical information; and determines the test case used to test whether the code has the functional requirement information through the functional requirement information of the reference code; performs a functional test on the code to be verified according to the test case to obtain a functional test result; when the functional test result is consistent with the functional requirement information, the target code corresponding to the reference code is the code to be verified. When the functional test result is inconsistent with the functional requirement information, the step of generating the code to be verified according to the first grammatical information, the functional requirement information and the second grammatical information is returned.
[0149] In a specific implementation, the user can select the code segment to be translated, i.e., the reference code, with the mouse, and then use a shortcut command, such as control+option+t, to open a command search box, which displays a variety of optional translation code languages. Based on the displayed optional translation code languages, select the language of translation, i.e., the target language. For example, the reference code selected by the user can be:
[0150]
[0151] The target language selected by the user is Python. Based on the above reference code, it can be known that the function corresponding to the reference code is bubble sort, so the generated target code can be:
[0152]
[0153] In this embodiment, when the generation type is code translation, the translatable translation code language is displayed; the target language selected by the user based on the translation code language is obtained; and the target code corresponding to the reference code is generated according to the target language and the functional requirement information. In this embodiment, when the generation type is code translation, the target code corresponding to the reference code is generated according to the target language selected by the user and the functional requirement information. The reference code can be translated into a language, reducing the workload of software developers in code translation and improving code development efficiency.
[0154] Reference Figure 6 , Figure 6 It is a structural block diagram of the first embodiment of the code generation device of the present invention.
[0155] like Figure 6 As shown, the code generation device proposed in the embodiment of the present invention includes:
[0156] A response module 10, configured to extract a reference code and a generation type in the code generation instruction in response to the triggered code generation instruction;
[0157] A function identification module 20, used to perform code function identification on the reference code to obtain function requirement information corresponding to the reference code;
[0158] The generating module 30 is used to generate the target code corresponding to the reference code according to the generating type and the functional requirement information.
[0159] In response to the triggered code generation instruction, this embodiment extracts the reference code and generation type in the code generation instruction; performs code function identification on the reference code to obtain functional requirement information corresponding to the reference code; and generates the target code corresponding to the reference code according to the generation type and the functional requirement information. Since this embodiment generates the target code corresponding to the reference code according to the functional requirement information and generation type of the reference code. Compared with the existing method of completing software development only by software engineers manually writing code line by line, the above method of this embodiment can generate target code according to the functional requirements of the code, thereby improving the efficiency of code development.
[0160] It should be noted that the workflow described above is merely illustrative and does not limit the scope of protection of the present invention. In practical applications, technicians in this field can select part or all of them according to actual needs to achieve the purpose of the present embodiment, and no limitation is made here.
[0161] In addition, for technical details not fully described in this embodiment, reference can be made to the code generation method provided in any embodiment of the present invention, and will not be repeated here.
[0162] Based on the above-mentioned first embodiment of the code generating device of the present invention, a second embodiment of the code generating device of the present invention is proposed.
[0163] In this embodiment, the generating module 30 is further used to display optional optimization directions when the generating type is code optimization;
[0164] Obtain a target optimization direction selected by a user based on the optimization direction;
[0165] The reference code is optimized according to the target optimization direction and the functional requirement information to generate a target code.
[0166] Furthermore, the generating module 30 is further used to optimize the reference code according to the target optimization direction to generate an intermediate code;
[0167] Performing a functional test on the intermediate code according to the functional requirement information to obtain a functional test result;
[0168] When the function test result is consistent with the code function in the function requirement information, the intermediate code is determined to be the target code.
[0169] Furthermore, the generating module 30 is further configured to display translatable translation code languages when the generating type is code translation;
[0170] Acquire a target language selected by a user based on the translation code language;
[0171] A target code corresponding to the reference code is generated according to the target language and the functional requirement information.
[0172] Furthermore, the generating module 30 is further used to obtain a reference code language corresponding to the reference code;
[0173] Determining first grammatical information corresponding to the reference code language;
[0174] Determining second grammar information corresponding to the target language;
[0175] Generate a target code corresponding to the reference code according to the first grammatical information, the functional requirement information and the second grammatical information.
[0176] Furthermore, the generating module 30 is further configured to generate a code to be verified according to the first grammatical information, the functional requirement information and the second grammatical information;
[0177] Determine the test cases corresponding to the functional requirement information;
[0178] Performing a functional test on the code to be verified according to the test case to obtain a functional test result;
[0179] When the function test result is consistent with the function requirement information, the target code corresponding to the reference code is the code to be verified.
[0180] Furthermore, the generating module 30 is further used to determine the code language corresponding to the reference code;
[0181] Generate a target code corresponding to the reference code according to the code language, the functional requirement information and the generation type.
[0182] Furthermore, the generating module 30 is further used to obtain the current focus position and determine whether the target code file corresponding to the current focus position meets the preset conditions;
[0183] When the target code file corresponding to the current focus position meets a preset condition, a code language corresponding to the target code file is determined.
[0184] Furthermore, the generating module 30 is further configured to determine whether the function of the reference code is complete according to the function requirement information when the generating type is code review;
[0185] When the functionality of the reference code is complete, determining the code structure and code performance of the reference code;
[0186] A target code corresponding to the reference code is generated according to the code structure and the code performance.
[0187] Furthermore, the generating module 30 is also used to display the target code;
[0188] Obtaining an insertion code and an insertion position selected by a user based on the displayed target code;
[0189] The insertion code is inserted into the target code file based on the insertion position.
[0190] Furthermore, the generating module 30 is further used to determine the character length corresponding to the reference code;
[0191] When the character length is greater than a preset character length threshold, clipping the reference code according to the preset character length threshold to obtain a clipped reference code;
[0192] Generate a target code corresponding to the clipped reference code according to the generation type and the functional requirement information.
[0193] Other embodiments or specific implementations of the code generation device of the present invention can refer to the above-mentioned method embodiments, which will not be described in detail here.
[0194] In addition, an embodiment of the present invention further provides a storage medium, on which a code generation program is stored. When the code generation program is executed by a processor, the steps of the code generation method described above are implemented.
[0195] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or system. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or system including the element.
[0196] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0197] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as a read-only memory / random access memory, a magnetic disk, or an optical disk), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present invention.
[0198] The above are only preferred embodiments of the present invention, and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.
[0199] The present invention discloses A1, a code generation method, the code generation method comprising the following steps:
[0200] In response to the triggered code generation instruction, extracting the reference code and generation type in the code generation instruction;
[0201] Performing code function identification on the reference code to obtain function requirement information corresponding to the reference code;
[0202] Generate a target code corresponding to the reference code according to the generation type and the functional requirement information.
[0203] The present invention discloses A2, a code generation method as described in A1, wherein the step of generating a target code corresponding to the reference code according to the generation type and the functional requirement information comprises:
[0204] When the generation type is code optimization, display optional optimization directions;
[0205] Obtain a target optimization direction selected by a user based on the optimization direction;
[0206] The reference code is optimized according to the target optimization direction and the functional requirement information to generate a target code.
[0207] A3. The code generation method as described in A2, wherein the step of optimizing the reference code according to the target optimization direction and the functional requirement information to generate the target code comprises:
[0208] Optimizing the reference code according to the target optimization direction to generate an intermediate code;
[0209] Performing a functional test on the intermediate code according to the functional requirement information to obtain a functional test result;
[0210] When the function test result is consistent with the code function in the function requirement information, the intermediate code is determined to be the target code.
[0211] A4. The code generation method as described in A1, wherein the step of generating the target code corresponding to the reference code according to the generation type and the functional requirement information comprises:
[0212] When the generation type is code translation, displaying translatable translation code languages;
[0213] Acquire a target language selected by a user based on the translation code language;
[0214] A target code corresponding to the reference code is generated according to the target language and the functional requirement information.
[0215] A5. The code generation method as described in A4, wherein the step of generating the target code corresponding to the reference code according to the target language and the functional requirement information comprises:
[0216] Obtaining a reference code language corresponding to the reference code;
[0217] Determining first grammatical information corresponding to the reference code language;
[0218] Determining second grammar information corresponding to the target language;
[0219] Generate a target code corresponding to the reference code according to the first grammatical information, the functional requirement information and the second grammatical information.
[0220] A6. The code generation method as described in A5, wherein the step of generating the target code corresponding to the reference code according to the first grammatical information, the functional requirement information and the second grammatical information comprises:
[0221] Generate a code to be verified according to the first grammatical information, the functional requirement information and the second grammatical information;
[0222] Determine the test cases corresponding to the functional requirement information;
[0223] Performing a functional test on the code to be verified according to the test case to obtain a functional test result;
[0224] When the function test result is consistent with the function requirement information, the target code corresponding to the reference code is the code to be verified.
[0225] A7. In the code generation method described in A1, the step of generating the target code corresponding to the reference code according to the generation type and the functional requirement information comprises:
[0226] Determining a code language corresponding to the reference code;
[0227] Generate a target code corresponding to the reference code according to the code language, the functional requirement information and the generation type.
[0228] A8. The code generation method as described in A7, before the step of extracting the reference code and generation type in the code generation instruction in response to the triggering code generation instruction, further comprises:
[0229] Obtaining a current focus position, and determining whether a target code file corresponding to the current focus position meets a preset condition;
[0230] When the target code file corresponding to the current focus position meets a preset condition, a code language corresponding to the target code file is determined.
[0231] A9. The code generation method as described in A1, wherein the step of generating the target code corresponding to the reference code according to the generation type and the functional requirement information comprises:
[0232] When the generation type is code review, judging whether the function of the reference code is complete according to the functional requirement information;
[0233] When the functionality of the reference code is complete, determining the code structure and code performance of the reference code;
[0234] A target code corresponding to the reference code is generated according to the code structure and the code performance.
[0235] A10. The code generation method as described in any one of A1 to A9, after the step of generating the target code corresponding to the reference code according to the generation type and the functional requirement information, further comprising:
[0236] Displaying the object code;
[0237] Obtaining an insertion code and an insertion position selected by a user based on the displayed target code;
[0238] The insertion code is inserted into the target code file based on the insertion position.
[0239] A11. The code generation method as described in any one of A1 to A9, wherein the step of generating the target code corresponding to the reference code according to the generation type and the functional requirement information comprises:
[0240] Determining the character length corresponding to the reference code;
[0241] When the character length is greater than a preset character length threshold, clipping the reference code according to the preset character length threshold to obtain a clipped reference code;
[0242] Generate a target code corresponding to the clipped reference code according to the generation type and the functional requirement information.
[0243] The present invention discloses B12, a code generating device, the code generating device comprising:
[0244] A response module, configured to extract a reference code and a generation type in the code generation instruction in response to the triggered code generation instruction;
[0245] A function identification module, used to perform code function identification on the reference code to obtain function requirement information corresponding to the reference code;
[0246] A generation module is used to generate a target code corresponding to the reference code according to the generation type and the functional requirement information.
[0247] B13. The code generating device as described in B12, wherein the generating module is further configured to display optional optimization directions when the generating type is code optimization;
[0248] Obtain a target optimization direction selected by a user based on the optimization direction;
[0249] The reference code is optimized according to the target optimization direction and the functional requirement information to generate a target code.
[0250] B14. The code generating device as described in B13, wherein the generating module is further used to optimize the reference code according to the target optimization direction to generate an intermediate code;
[0251] Performing a functional test on the intermediate code according to the functional requirement information to obtain a functional test result;
[0252] When the function test result is consistent with the code function in the function requirement information, the intermediate code is determined to be the target code.
[0253] B15. The code generating device as described in B12, wherein the generating module is further configured to display translatable translation code languages when the generating type is code translation;
[0254] Acquire a target language selected by a user based on the translation code language;
[0255] A target code corresponding to the reference code is generated according to the target language and the functional requirement information.
[0256] B16. The code generating device as described in B15, wherein the generating module is further used to obtain a reference code language corresponding to the reference code;
[0257] Determining first grammatical information corresponding to the reference code language;
[0258] Determining second grammar information corresponding to the target language;
[0259] Generate a target code corresponding to the reference code according to the first grammatical information, the functional requirement information and the second grammatical information.
[0260] B17. The code generating device as described in B16, wherein the generating module is further used to generate a code to be verified according to the first grammatical information, the functional requirement information and the second grammatical information;
[0261] Determine the test cases corresponding to the functional requirement information;
[0262] Performing a functional test on the code to be verified according to the test case to obtain a functional test result;
[0263] When the function test result is consistent with the function requirement information, the target code corresponding to the reference code is the code to be verified.
[0264] B18. The code generating device as described in B12, wherein the generating module is further used to determine the code language corresponding to the reference code;
[0265] Generate a target code corresponding to the reference code according to the code language, the functional requirement information and the generation type.
[0266] The present invention discloses C19, a code generation device, which includes: a memory, a processor, and a code generation program stored in the memory and executable on the processor, wherein the code generation program is configured to implement the steps of the code generation method described in any one of A1 to A11.
[0267] The present invention discloses D20, a storage medium, on which a code generation program is stored. When the code generation program is executed by a processor, the steps of the code generation method as described in any one of A1 to A11 are implemented.
Claims
1. A code generation method, characterized in that: The code generation method comprises the following steps: In response to the triggered code generation instruction, extracting the reference code and generation type in the code generation instruction; Performing code function identification on the reference code to obtain function requirement information corresponding to the reference code; Generate a target code corresponding to the reference code according to the generation type and the functional requirement information.
2. The code generation method according to claim 1, characterized in that: The step of generating the target code corresponding to the reference code according to the generation type and the functional requirement information comprises: When the generation type is code optimization, display optional optimization directions; Obtain a target optimization direction selected by a user based on the optimization direction; The reference code is optimized according to the target optimization direction and the functional requirement information to generate a target code.
3. The code generation method according to claim 2, characterized in that: The step of optimizing the reference code according to the target optimization direction and the functional requirement information to generate the target code includes: Optimizing the reference code according to the target optimization direction to generate an intermediate code; Performing a functional test on the intermediate code according to the functional requirement information to obtain a functional test result; When the function test result is consistent with the code function in the function requirement information, the intermediate code is determined to be the target code.
4. The code generation method according to claim 1, characterized in that: The step of generating the target code corresponding to the reference code according to the generation type and the functional requirement information comprises: When the generation type is code translation, displaying translatable translation code languages; Obtaining a target language selected by a user based on the translation code language; A target code corresponding to the reference code is generated according to the target language and the functional requirement information.
5. The code generation method according to claim 4, characterized in that: The step of generating a target code corresponding to the reference code according to the target language and the functional requirement information comprises: Obtaining a reference code language corresponding to the reference code; Determining first grammatical information corresponding to the reference code language; Determining second grammar information corresponding to the target language; Generate a target code corresponding to the reference code according to the first grammatical information, the functional requirement information and the second grammatical information.
6. The code generation method according to claim 5, characterized in that: The step of generating a target code corresponding to the reference code according to the first grammatical information, the functional requirement information and the second grammatical information comprises: Generate a code to be verified according to the first grammatical information, the functional requirement information and the second grammatical information; Determine the test cases corresponding to the functional requirement information; Performing a functional test on the code to be verified according to the test case to obtain a functional test result; When the function test result is consistent with the function requirement information, the target code corresponding to the reference code is the code to be verified.
7. The code generation method according to claim 1, characterized in that: The step of generating the target code corresponding to the reference code according to the generation type and the functional requirement information comprises: Determining a code language corresponding to the reference code; Generate a target code corresponding to the reference code according to the code language, the functional requirement information and the generation type.
8. A code generating device, characterized in that: The code generating device comprises: A response module, configured to extract a reference code and a generation type in the code generation instruction in response to the triggered code generation instruction; A function identification module, used to perform code function identification on the reference code to obtain function requirement information corresponding to the reference code; A generation module is used to generate a target code corresponding to the reference code according to the generation type and the functional requirement information.
9. A code generating device, characterized in that: The device comprises: a memory, a processor, and a code generation program stored in the memory and executable on the processor, wherein the code generation program is configured to implement the steps of the code generation method according to any one of claims 1 to 7.
10. A storage medium, characterized in that: The storage medium stores a code generation program, and when the code generation program is executed by the processor, the steps of the code generation method according to any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Code generation method and device
CN120045171A
