Programming language interpretation execution method and device, electronic equipment and medium
By using cross-platform parsing tools and recursive access functions to parse and interpret and execute programming language programs, the flexibility and cross-platform problems of native compiler limitations are solved, and the efficiency and unity of interpretation and execution are improved.
Patent Information
- Application Number
- CN202411505429.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-25
- Publication Date
- 2025-02-18
AI Technical Summary
When the prior art uses native compilers of programming languages to perform interpretation execution, it limits the flexibility of interpreted execution programming languages, cannot achieve cross-platform operation, and the language interfaces under multiple platforms cannot be unified, reducing interpretation execution efficiency.
The programming language program is parsed and transformed through preset cross-platform parsing tools, an abstract syntax tree is generated, and the expression is interpreted and executed using recursive access functions to realize the WEB operation of the programming language in multiple language environments.
It improves the flexibility and efficiency of interpretation and execution of programming languages, realizes cross-platform operation, and unifies language interfaces under multiple platforms.
Smart Images

Figure CN119473499A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of programming language technology, and in particular to a method, device, electronic device and medium for interpreting and executing a programming language. Background Art
[0002] When interpreting and executing a programming language, it is generally compiled and executed through the native compiler of the programming language. For example, the native compiler of the C++ language is the G++ compiler, and the G++ compiler is a binary file that runs directly on the system. The embedded G++ mode is used on the Windows platform.
[0003] Currently, when using the native compiler of a programming language to interpret and execute the programming language, the interpreter can only be called to interpret the program and then executed through the native compiler. This limits the flexibility of interpreting and executing the programming language, makes it impossible to run it across platforms, and the language interfaces on multiple platforms cannot be unified, thereby reducing the efficiency of interpreting and executing the programming language. Summary of the invention
[0004] In view of this, the purpose of the present application is to provide a method, device, electronic device and medium for interpreting and executing a programming language, which parses and converts the programming language program through a preset cross-platform parsing tool, generates an abstract syntax tree corresponding to the programming language program, realizes the WEB operation of the programming language in multiple language environments, interprets and executes the expressions under each object type for the recursive access functions corresponding to the multiple object types included in the abstract syntax tree, and obtains the execution results and interpretation information corresponding to the programming language program, thereby improving the flexibility of interpreting and executing the programming language, and further improving the efficiency of interpreting and executing the programming language.
[0005] The present application provides a method for interpreting and executing a programming language, the method comprising:
[0006] In response to obtaining a programming language program input from the outside, using a preset cross-platform parsing tool to parse and convert the programming language program to generate an abstract syntax tree corresponding to the programming language program;
[0007] For each object type among the multiple object types included in the abstract syntax tree, based on the recursive access function corresponding to each object type, calling a preset running component to interpret and execute the expression under each object type to obtain a first execution result;
[0008] Based on the first execution result and the target instance thread in the running component, the running component is used to recursively interpret multiple expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program.
[0009] Furthermore, in response to obtaining the externally input programming language program, using a preset cross-platform parsing tool to parse and convert the programming language program to generate an abstract syntax tree corresponding to the programming language program includes:
[0010] In response to obtaining an externally input programming language program, a predefined grammar file is compiled using a preset cross-platform parsing tool to obtain a parsing file corresponding to the cross-platform parsing tool; wherein the programming language program includes a definition domain program and an entry main function program;
[0011] The programming language program is parsed and converted using the parsing file and the cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program.
[0012] Further, for each object type among the multiple object types included in the abstract syntax tree, based on the recursive access function corresponding to each object type, calling a preset running component to interpret and execute the expression under each object type to obtain a first execution result, including:
[0013] For a plurality of first object types corresponding to the domain program in the abstract syntax tree, based on a recursive access function corresponding to each first object type, calling a preset running component to perform cyclic interpretation and execution on an expression under each first object type, to obtain a second execution result and a registration method for caching;
[0014] caching the registration method using the running component, and judging whether the definition domain program has an error based on the second execution result;
[0015] If there is no error in the definition domain program, then for the multiple second object types corresponding to the entry main function program in the abstract syntax tree, based on the recursive access function corresponding to each second object type, the running component is called to interpret and execute the expression under each second object type to obtain a first execution result.
[0016] Further, for the multiple first object types corresponding to the domain program in the abstract syntax tree, based on the recursive access function corresponding to each first object type, calling a preset running component to perform cyclic interpretation and execution on the expression under each first object type to obtain a second execution result and a registration method for caching, includes:
[0017] For a plurality of first object types corresponding to the domain program in the abstract syntax tree, determining a recursive access function corresponding to each of the plurality of first object types;
[0018] Based on the recursive access function, calling a preset iterative loop method in a preset running component to perform loop interpretation and execution on the expression under each first object type, and obtaining a second execution result and a function signature for representing the corresponding relationship between the expressions;
[0019] Based on the function signature, a registration method for caching corresponding to the function signature is obtained using a registration function preset in the running component.
[0020] Further, for the plurality of second object types corresponding to the entry main function program in the abstract syntax tree, based on the recursive access function corresponding to each second object type, calling the running component to interpret and execute the expression under each second object type to obtain a first execution result includes:
[0021] Using the running component to obtain the entry main function program, and obtaining a plurality of second object types corresponding to the entry main function program in the abstract syntax tree;
[0022] Based on each second object type in the plurality of second object types, determining a recursive access function corresponding to each second object type;
[0023] Based on the recursive access function, the running component is called to interpret and execute the expression under each second object type to obtain a first execution result.
[0024] Further, based on the first execution result and the target instance thread in the running component, the running component is used to recursively interpret multiple expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program, including:
[0025] Based on the first execution result, calling a startup method preset in the running component, and instantiating a preset thread in the startup method to obtain a target instance thread in the running component;
[0026] Based on the target instance thread, obtaining a method handle in the registration method;
[0027] Based on the method handle, the execution component is used to recursively interpret multiple expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program.
[0028] The embodiment of the present application further provides a programming language interpretation and execution device, the interpretation and execution device comprising:
[0029] A parsing and conversion module, configured to, in response to obtaining a programming language program input from an external source, parse and convert the programming language program using a preset cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program;
[0030] An interpretation and execution module is used to call a preset running component to interpret and execute the expression under each object type among the multiple object types included in the abstract syntax tree based on the recursive access function corresponding to each object type, so as to obtain a first execution result;
[0031] A recursive interpretation module is used to recursively interpret multiple expressions in the abstract syntax tree using the running component based on the first execution result and the target instance thread in the running component to obtain interpretation information corresponding to the programming language program.
[0032] Further, when the parsing and conversion module is used to respond to the programming language program obtained from the external input, parse and convert the programming language program using a preset cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program, the parsing and conversion module is used to:
[0033] In response to obtaining an externally input programming language program, a predefined grammar file is compiled using a preset cross-platform parsing tool to obtain a parsing file corresponding to the cross-platform parsing tool; wherein the programming language program includes a definition domain program and an entry main function program;
[0034] The programming language program is parsed and converted using the parsing file and the cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program.
[0035] Further, when the interpretation and execution module is used to interpret and execute, for each object type among the multiple object types included in the abstract syntax tree, based on the recursive access function corresponding to each object type, the preset running component is called to interpret and execute the expression under each object type, and when the first execution result is obtained, the interpretation and execution module is used to:
[0036] For a plurality of first object types corresponding to the domain program in the abstract syntax tree, based on a recursive access function corresponding to each first object type, calling a preset running component to perform cyclic interpretation and execution on an expression under each first object type, to obtain a second execution result and a registration method for caching;
[0037] caching the registration method using the running component, and judging whether the definition domain program has an error based on the second execution result;
[0038] If there is no error in the definition domain program, then for the multiple second object types corresponding to the entry main function program in the abstract syntax tree, based on the recursive access function corresponding to each second object type, the running component is called to interpret and execute the expression under each second object type to obtain a first execution result.
[0039] Further, when the interpretation and execution module is used to call a preset running component to perform cyclic interpretation and execution on the expression under each first object type based on the recursive access function corresponding to each first object type for the multiple first object types corresponding to the definition domain program in the abstract syntax tree, and obtain the second execution result and the registration method for caching, the interpretation and execution module is used to:
[0040] For a plurality of first object types corresponding to the domain program in the abstract syntax tree, determining a recursive access function corresponding to each of the plurality of first object types;
[0041] Based on the recursive access function, calling a preset iterative loop method in a preset running component to perform loop interpretation and execution on the expression under each first object type, and obtaining a second execution result and a function signature for representing the corresponding relationship between the expressions;
[0042] Based on the function signature, a registration method for caching corresponding to the function signature is obtained using a registration function preset in the running component.
[0043] Further, when the interpretation and execution module is used to interpret and execute the expression under each second object type for the multiple second object types corresponding to the entry main function program in the abstract syntax tree based on the recursive access function corresponding to each second object type, and obtain the first execution result, the interpretation and execution module is used to:
[0044] Using the running component to obtain the entry main function program, and obtaining a plurality of second object types corresponding to the entry main function program in the abstract syntax tree;
[0045] Based on each second object type in the plurality of second object types, determining a recursive access function corresponding to each second object type;
[0046] Based on the recursive access function, the running component is called to interpret and execute the expression under each second object type to obtain a first execution result.
[0047] Further, when the recursive interpretation module is used to recursively interpret multiple expressions in the abstract syntax tree using the running component based on the first execution result and the target instance thread in the running component to obtain interpretation information corresponding to the programming language program, the recursive interpretation module is used to:
[0048] Based on the first execution result, calling a startup method preset in the running component, and instantiating a preset thread in the startup method to obtain a target instance thread in the running component;
[0049] Based on the target instance thread, obtaining a method handle in the registration method;
[0050] Based on the method handle, the execution component is used to recursively interpret multiple expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program.
[0051] An embodiment of the present application also provides an electronic device, comprising: a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate through the bus, and when the machine-readable instructions are executed by the processor, the steps of the interpretation and execution method of the programming language as described above are performed.
[0052] An embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the method for interpreting and executing the programming language as described above are executed.
[0053] The embodiments of the present application provide a method, device, electronic device and medium for interpreting and executing a programming language. The method for interpreting and executing the programming language includes: in response to obtaining a programming language program input from an external source, using a preset cross-platform parsing tool to parse and convert the programming language program to generate an abstract syntax tree corresponding to the programming language program; for each of a plurality of object types included in the abstract syntax tree, based on a recursive access function corresponding to each object type, calling a preset running component to interpret and execute the expression under each object type to obtain a first execution result; based on the first execution result and a target instance thread in the running component, using the running component to recursively interpret a plurality of expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program.
[0054] Compared with the method in the prior art that uses the native compiler of the programming language to interpret and execute the programming language, the programming language program is parsed and converted through a preset cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program, thereby realizing the WEB operation of the programming language in multiple language environments. For the recursive access functions corresponding to the multiple object types included in the abstract syntax tree, the expressions under each object type are interpreted and executed to obtain the execution results and interpretation information corresponding to the programming language program, thereby improving the flexibility of interpreting and executing the programming language, and thereby improving the efficiency of interpreting and executing the programming language.
[0055] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0057] Figure 1 A flowchart of a method for interpreting and executing a programming language provided in an embodiment of the present application;
[0058] Figure 2 A schematic diagram of the structure of a programming language interpretation and execution device provided in an embodiment of the present application;
[0059] Figure 3 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0060] To make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all of the embodiments. The components of the embodiments of the present application usually described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, each other embodiment obtained by those skilled in the art without making creative work belongs to the scope of protection of the present application.
[0061] Research has found that, at present, when using the native compiler of a programming language to interpret and execute the programming language, the interpreter can only be called to interpret the program, and then executed through the native compiler. This limits the flexibility of interpreting and executing the programming language, and cannot achieve cross-platform operation. In addition, the language interface under multiple platforms cannot be unified, which reduces the efficiency of interpreting and executing the programming language.
[0062] Based on this, an embodiment of the present application provides a method for interpreting and executing a programming language, which parses and converts the programming language program through a preset cross-platform parsing tool, generates an abstract syntax tree corresponding to the programming language program, realizes the WEB operation of the programming language in multiple language environments, and interprets and executes the expressions under each object type for the recursive access functions corresponding to the multiple object types included in the abstract syntax tree, and obtains the execution results and interpretation information corresponding to the programming language program, thereby improving the flexibility of interpreting and executing the programming language, and thereby improving the efficiency of interpreting and executing the programming language.
[0063] See also Figure 1 , Figure 1 This is a flowchart of a method for interpreting and executing a programming language provided in an embodiment of the present application. Figure 1 As shown in , the method for interpreting and executing a programming language provided in the embodiment of the present application includes:
[0064] S101 . In response to obtaining a programming language program input from an external source, using a preset cross-platform parsing tool to parse and convert the programming language program to generate an abstract syntax tree corresponding to the programming language program.
[0065] It should be noted that the programming language interpretation and execution method provided in the embodiment of the present application is generally applied to a programming language interpreter that can run in a JavaScript environment, so as to achieve cross-platform operation through the cross-platform characteristics of the Web in the JavaScript environment, and unify the language interface under multiple platforms. For example, the programming language interpreter supports running on the browser of the Pad, or executing in WeChat H5.
[0066] The programming language interpreter running in the JavaScript environment refers to a tool or library that can convert codes of other programming languages into JavaScript codes and execute them in the JavaScript runtime environment.
[0067] In the embodiment of the present application, the programming language program includes but is not limited to C language program, C++ language program, C# language program, Python language program and Java program, etc.
[0068] In an embodiment of the present application, the preset cross-platform parsing tool includes a PEG.js tool, which is a JavaScript parser generator. It provides a PEG.js language (Parser Expression Grammar JavaScript). The PEG.js language is a language used to define a grammar parser and generate parser code that can be used directly in JavaScript; for example, to generate a js file, you must first define a PEG.js grammar rule, and then use the PEG.js compiler to compile it into JavaScript code.
[0069] In this way, the PEG.js tool can parse the compiled file to generate a cross-platform programming language program according to the Parsing Expression Grammar (PEG) provided by the PEG.js tool, allowing developers to easily create parsers for parsing specific languages or formats, thereby simplifying language processing tasks.
[0070] Here, the Abstract Syntax Tree (AST) corresponding to the programming language program is a data structure, which is usually used to represent the grammatical structure of source code (programming language, markup language, etc.). It is a tree structure in which the nodes represent elements in the grammatical structure, such as variables, function calls, operators, etc.
[0071] In one implementation of the present application, during specific implementation, step S101 may include:
[0072] S1011. In response to obtaining a programming language program input from the outside, compile a predefined grammar file using a preset cross-platform parsing tool to obtain a parsing file corresponding to the cross-platform parsing tool; wherein the programming language program includes a definition domain program and an entry main function program.
[0073] In the embodiment of the present application, since the entry function of the programming language can only be the main function, if the main function is not executed, other functions will not be executed. Therefore, the contents of the programming language program other than the entry main function program are all domain programs, that is, the programming language program includes the domain program and the entry main function program.
[0074] It should be noted that before using the cross-platform parsing tool, the cross-platform parsing tool needs to be configured; in the embodiment of the present application, the PEG.js command is installed to the local project through Npm, and the PEG.js command is used to generate a JS syntax template from the .pegjs syntax file, and then another package "pegjs-util" is installed to perform the task of parsing the abstract syntax tree.
[0075] In this step, during the specific implementation, first, in response to obtaining the programming language program input from the outside, a grammar file corresponding to a cross-platform parsing tool is predefined and created; then, the predefined grammar file is compiled using the cross-platform parsing tool; finally, a parsing file corresponding to the cross-platform parsing tool is obtained.
[0076] As an example of predefining and creating a grammar file corresponding to a cross-platform parsing tool, when the grammar file is an "ast.pegjs" file, part of the grammar file includes: "{function buildRecursiveBinop(a,b){var ret=a;for(var i=0;i <b.length;i++){ret=addPositionInfo({type:'BinOpExpression',left:ret,op:b[i][0],right:b[i][1]});}return ret;};”。
[0077] In the embodiment of the present application, by using the "pegjs ast.pegjs ast.js" command and using a cross-platform parsing tool to compile the predefined syntax file, a parsed file "ast.js" is obtained.
[0078] S1012: Utilize the parsing file and the cross-platform parsing tool to parse and convert the programming language program to generate an abstract syntax tree corresponding to the programming language program.
[0079] In this step, during the specific implementation, first, the generated parsing file is introduced and the cross-platform parsing tool is called; then, the parsing file and the cross-platform parsing tool are used to parse and convert the programming language program to generate an abstract syntax tree corresponding to the programming language program; finally, it is determined whether there are any lexical errors in the parsing process.
[0080] In the embodiment of the present application, the generated parsing file is introduced by the statement "const ast = require('. / ast.js'); const code;" the cross-platform parsing tool is called by the statement "import * as PEGUtil from "pegjs-util";" the abstract syntax tree corresponding to the programming language program is generated by the statement "const result = PEGUtil.parse(ast, code); interpreter.run(result.ast, code);"
[0081] Among them, "result.ast" represents the abstract syntax tree corresponding to the programming language program; result.ast is the AST abstract syntax tree parsed by the cross-platform parsing tool peg.JS, and a JS iterative generator object (Generator Function, which can be paused during the step-by-step execution and resumed later) can be obtained through the "interpreter.run" method.
[0082] Here, during the logical runtime, the abstract syntax tree repeatedly parses and executes the lexical units (for example, operators, expressions, method calls, etc.) in the abstract syntax tree through the iterative generator object (Generator Function). The relationship between each lexical unit is a tree structure, and the calling form of the lexical unit is a method call. Therefore, it is necessary to execute the programming language program through the entry main function.
[0083] S102. For each object type among the multiple object types included in the abstract syntax tree, based on the recursive access function corresponding to each object type, call a preset running component to interpret and execute the expression under each object type to obtain a first execution result.
[0084] In an embodiment of the present application, since the abstract syntax tree is a tree structure and includes multiple layers, and each layer is an object, and each object corresponds to an object type, the abstract syntax tree includes multiple object types, wherein the object type represents the expression and its parameters included in the layer in the abstract syntax tree.
[0085] For example, suppose that a certain layer of the abstract syntax tree includes four object types, including: TranslationUnit, FunctionDefinition, ExpressionStatement and JumpStatement_return object types; among them, "TranslationUnit" is an inherent entry, indicating that the program begins to enter the interpretation unit; "FunctionDefinition" refers to the declaration of the function; "ExpressionStatement" refers to the declaration of the cout statement; JumpStatement_return refers to the return value.
[0086] In one implementation of the present application, during specific implementation, step S102 may include:
[0087] S1021. For multiple first object types corresponding to the definition domain program in the abstract syntax tree, based on the recursive access function corresponding to each first object type, call the preset running component to perform cyclic interpretation and execution on the expression under each first object type to obtain a second execution result and a registration method for caching.
[0088] In an embodiment of the present application, the content and execution logic included in the object type are implemented by the recursive visitor function (Visitor function) corresponding to the object type. Each recursive visitor function returns the operation results for multiple object types in the abstract syntax tree. In addition, the content and execution logic included in the object type executed by the recursive visit function is also the operating logic of the programming language interpreter.
[0089] Furthermore, the recursive access function corresponding to the object type needs to call a preset running component when interpreting the content included in the execution object type, wherein the preset running component is used to provide a public running component interface, such as type conversion, type verification, value comparison, throwing exceptions, etc.; in addition, the recursive access function is also used to cache the relationship between different expressions.
[0090] Here, the second execution result refers to the execution result corresponding to the definition domain program, and the second execution result may include a Generator function for defining a programming language program.
[0091] In one implementation of the present application, during specific implementation, step S1021 may include:
[0092] S10211. For a plurality of first object types corresponding to the domain program in the abstract syntax tree, determine a recursive access function corresponding to each of the plurality of first object types.
[0093] In this step, during the specific implementation, first, determine the branch or layer where the domain program is located in the abstract syntax tree; then, determine the multiple first object types corresponding to the domain program in the abstract syntax tree; finally, determine the recursive access function corresponding to each first object type based on the specific content and execution logic of the multiple first object types.
[0094] S10212. Based on the recursive access function, call the preset iterative loop method in the preset running component to perform loop interpretation and execution on the expression under each first object type, and obtain a second execution result and a function signature for representing the corresponding relationship between the expressions.
[0095] In this step, during the specific implementation, first, find the entry file corresponding to the domain program; then, determine the domain object in the entry file; finally, based on the recursive access function corresponding to the first object type, call the preset iterative loop method in the preset running component to perform loop interpretation and execution on each expression and domain object under the first object type, and obtain a second execution result and a function signature used to represent the correspondence between expressions.
[0096] Exemplarily, in the entry file of "main.ts", based on the "generator.next" method in the "visitor function", the expression under the first object type and the "Definiton Generator" domain object are executed, and the "while true" iterative loop method is used to perform loop interpretation and execution to obtain the second execution result and the function signature used to represent the correspondence between the expressions.
[0097] S10213. Based on the function signature, use the registration function preset in the running component to obtain the registration method for caching corresponding to the function signature.
[0098] In this step, the obtained function signature is registered into the global variable in the running component using the registration function preset in the running component, and the registration method for caching corresponding to the function signature is obtained so that the registration method can be accessed when the entry main function is interpreted and executed.
[0099] S1022. Utilize the running component to cache the registration method, and determine whether there is an error in the definition domain program based on the second execution result.
[0100] In this step, the registration method is cached using the running component, and it is determined whether the second execution result includes error information to determine whether there is an error in the definition domain program.
[0101] Exemplarily, the obtained function signature (function signature) is registered in the global variable (runtime.globals) in the runtime component (runtime) using the registration function (rt.regFunc) preset in the runtime component, so that the registration method can be accessed using the statement "runtime.types['()'].handlers.functions[signature]=f" when the entry main function is interpreted and executed.
[0102] Furthermore, if there is an error in the definition domain program, a preset trigger exception function (RaiseException) is called to throw the error in the definition domain program and send it to the outside.
[0103] S1023. If there is no error in the definition domain program, then for the multiple second object types corresponding to the entry main function program in the abstract syntax tree, based on the recursive access function corresponding to each second object type, call the running component to interpret and execute the expression under each second object type to obtain a first execution result.
[0104] In an embodiment of the present application, when interpreting and executing a domain program, the entry main function in the programming language program is not called. It is necessary to obtain the entry main function program and interpret and execute it after the interpretation and execution of the domain program is completed.
[0105] Here, the first execution result refers to the execution result corresponding to the entry main function program, and the first execution result may include a Generator function for interpreting and executing the function content in the actual programming language program.
[0106] In one implementation of the present application, during specific implementation, step S1023 may include:
[0107] S10231. Utilize the running component to obtain the entry main function program, and obtain a plurality of second object types corresponding to the entry main function program in the abstract syntax tree.
[0108] In this step, the running component is used to obtain the entry main function program representing the global main method; then, based on the entry main function program, the expressions under the multiple second object types corresponding to the entry main function program in the abstract syntax tree are obtained.
[0109] Exemplarily, the global main method, ie, the entry main function program (main), is obtained by running the rt.getFunc method in the runtime component (runtime).
[0110] Here, the storage method of the entry main function is to cache it in the running component when the function is defined.
[0111] S10232. Based on each second object type in the multiple second object types, determine a recursive access function corresponding to each second object type.
[0112] In this step, during the specific implementation, first, determine the branch or layer where the entry main function program is located in the abstract syntax tree; then, determine the multiple second object types corresponding to the entry main function program in the abstract syntax tree; finally, determine the recursive access function corresponding to each second object type based on the specific content and execution logic of each second object type.
[0113] S10233. Based on the recursive access function, call the running component to interpret and execute the expression under each second object type to obtain a first execution result.
[0114] In this step, the running component is called to interpret and execute the program, wherein, based on the recursive access function corresponding to each second object type, the expression under each second object type is interpreted and executed respectively to obtain the first execution result corresponding to the entry main function program.
[0115] S103: Based on the first execution result and the target instance thread in the running component, the running component is used to recursively interpret multiple expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program.
[0116] In this step, during the specific implementation, first, based on the first execution result, the preset startup method in the running component is called, and the preset thread is instantiated in the startup method to obtain the target instance thread in the running component; then, based on the target instance thread, the method handle in the registration method is obtained; finally, based on the method handle, the running component is used to recursively interpret multiple expressions in the abstract syntax tree to obtain the interpretation information corresponding to the programming language program.
[0117] In one implementation of the present application, during specific implementation, step S103 may include:
[0118] S1031. Based on the first execution result, call a startup method preset in the running component, and instantiate a preset thread in the startup method to obtain a target instance thread in the running component.
[0119] In this step, during the specific implementation, first, in response to determining that there is no error in the first execution result, the startup method preset in the running component is called; then, the preset thread is instantiated in the startup method; finally, the instantiated preset thread is added to the thread array in the running component to obtain the target instance thread in the running component.
[0120] Exemplarily, the start method (start) preset in the running component (runtime) is called, the start method (start) instantiates the preset thread (Thread), and the preset thread (Thread) after the instance is added to the thread array (this.threads) in the running component (runtime) to obtain the target instance thread.
[0121] Here, when the programming language program is interpreted and executed and stopped, that is, when the preset operation stop method is executed, the parameters in the thread array in the operation component will be cleared.
[0122] S1032. Based on the target instance thread, obtain a method handle in the registration method.
[0123] In this step, after obtaining the target instance thread, based on the function signature corresponding to the target instance thread and the definition domain program, the cached registration method is obtained in the global variable in the running component, and the method handle in the registration method is obtained.
[0124] The method handle can be directly interpreted and called, and is used to represent an expression in an interpretable abstract syntax tree.
[0125] S1033. Based on the method handle, use the execution component to recursively interpret multiple expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program.
[0126] In this step, based on the method handle, the execution component is used to recursively interpret multiple expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program.
[0127] The method for interpreting and executing a programming language provided in an embodiment of the present application parses and converts a programming language program through a preset cross-platform parsing tool, generates an abstract syntax tree corresponding to the programming language program, realizes the WEB operation of the programming language in multiple language environments, interprets and executes the expressions under each object type for the recursive access functions corresponding to the multiple object types included in the abstract syntax tree, obtains the execution result and interpretation information corresponding to the programming language program, improves the flexibility of interpreting and executing the programming language, and further improves the efficiency of interpreting and executing the programming language.
[0128] See also Figure 2 , Figure 2 This is a schematic diagram of the structure of a programming language interpretation and execution device provided in an embodiment of the present application. Figure 2 As shown in , the interpretation and execution device 200 includes:
[0129] The parsing and conversion module 210 is used to, in response to obtaining a programming language program input from the outside, parse and convert the programming language program using a preset cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program;
[0130] The interpretation and execution module 220 is used for calling a preset running component to interpret and execute the expression under each object type in the plurality of object types included in the abstract syntax tree based on the recursive access function corresponding to each object type, so as to obtain a first execution result;
[0131] The recursive interpretation module 230 is used to recursively interpret multiple expressions in the abstract syntax tree using the running component based on the first execution result and the target instance thread in the running component to obtain interpretation information corresponding to the programming language program.
[0132] Further, when the parsing and conversion module 210 is used to parse and convert the programming language program in response to obtaining an externally input programming language program using a preset cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program, the parsing and conversion module 210 is used to:
[0133] In response to obtaining an externally input programming language program, a predefined grammar file is compiled using a preset cross-platform parsing tool to obtain a parsing file corresponding to the cross-platform parsing tool; wherein the programming language program includes a definition domain program and an entry main function program;
[0134] The programming language program is parsed and converted using the parsing file and the cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program.
[0135] Further, when the interpretation and execution module 220 is used to interpret and execute the expression under each object type in the multiple object types included in the abstract syntax tree based on the recursive access function corresponding to each object type, and obtain the first execution result, the interpretation and execution module 220 is used to:
[0136] For a plurality of first object types corresponding to the domain program in the abstract syntax tree, based on a recursive access function corresponding to each first object type, calling a preset running component to perform cyclic interpretation and execution on an expression under each first object type, to obtain a second execution result and a registration method for caching;
[0137] caching the registration method using the running component, and judging whether the definition domain program has an error based on the second execution result;
[0138] If there is no error in the definition domain program, then for the multiple second object types corresponding to the entry main function program in the abstract syntax tree, based on the recursive access function corresponding to each second object type, the running component is called to interpret and execute the expression under each second object type to obtain a first execution result.
[0139] Further, when the interpretation and execution module 220 is used to call a preset running component to perform cyclic interpretation and execution on the expression under each first object type based on the recursive access function corresponding to each first object type for the multiple first object types corresponding to the definition domain program in the abstract syntax tree, and obtain the second execution result and the registration method for caching, the interpretation and execution module 220 is used to:
[0140] For a plurality of first object types corresponding to the domain program in the abstract syntax tree, determining a recursive access function corresponding to each of the plurality of first object types;
[0141] Based on the recursive access function, calling a preset iterative loop method in a preset running component to perform loop interpretation and execution on the expression under each first object type, and obtaining a second execution result and a function signature for representing the corresponding relationship between the expressions;
[0142] Based on the function signature, a registration method for caching corresponding to the function signature is obtained using a registration function preset in the running component.
[0143] Further, when the interpretation and execution module 220 is used to interpret and execute the expression under each second object type for the multiple second object types corresponding to the entry main function program in the abstract syntax tree based on the recursive access function corresponding to each second object type, and obtain the first execution result, the interpretation and execution module 220 is used to:
[0144] Using the running component to obtain the entry main function program, and obtaining a plurality of second object types corresponding to the entry main function program in the abstract syntax tree;
[0145] Based on each second object type in the plurality of second object types, determining a recursive access function corresponding to each second object type;
[0146] Based on the recursive access function, the running component is called to interpret and execute the expression under each second object type to obtain a first execution result.
[0147] Further, when the recursive interpretation module 230 is used to recursively interpret multiple expressions in the abstract syntax tree using the running component based on the first execution result and the target instance thread in the running component to obtain the interpretation information corresponding to the programming language program, the recursive interpretation module 230 is used to:
[0148] Based on the first execution result, calling a startup method preset in the running component, and instantiating a preset thread in the startup method to obtain a target instance thread in the running component;
[0149] Based on the target instance thread, obtaining a method handle in the registration method;
[0150] Based on the method handle, the execution component is used to recursively interpret multiple expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program.
[0151] The apparatus for interpreting and executing a programming language provided in an embodiment of the present application parses and converts a programming language program through a preset cross-platform parsing tool, generates an abstract syntax tree corresponding to the programming language program, realizes WEB operation of the programming language in multiple language environments, interprets and executes expressions under each object type for recursive access functions corresponding to multiple object types included in the abstract syntax tree, obtains execution results and interpretation information corresponding to the programming language program, improves the flexibility of interpreting and executing the programming language, and further improves the efficiency of interpreting and executing the programming language.
[0152] See also Figure 3 , Figure 3 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 3 As shown in , the electronic device 300 includes a processor 310 , a memory 320 and a bus 330 .
[0153] The memory 320 stores machine-readable instructions executable by the processor 310. When the electronic device 300 is running, the processor 310 communicates with the memory 320 via the bus 330. When the machine-readable instructions are executed by the processor 310, the above-mentioned Figure 1 The specific implementation of the steps of the method for interpreting and executing the programming language in the method embodiment shown can be found in the method embodiment, and will not be repeated here.
[0154] The present application also provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the computer program can execute the above-mentioned Figure 1 The specific implementation of the steps of the method for interpreting and executing the programming language in the method embodiment shown can be found in the method embodiment, and will not be repeated here.
[0155] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0156] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of devices or units can be electrical, mechanical or other forms.
[0157] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0158] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0159] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present application can essentially be embodied in the form of a software product, or in other words, the part that contributes to the prior art or the part of the technical solution. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0160] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present application, which are used to illustrate the technical solutions of the present application, rather than to limit them. The protection scope of the present application is not limited thereto. Although the present application is described in detail with reference to the above-mentioned embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-mentioned embodiments within the technical scope disclosed in the present application, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application, and should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.
Claims
1. A method for interpreting and executing a programming language, characterized in that: The interpretation and execution method comprises: In response to obtaining a programming language program input from the outside, using a preset cross-platform parsing tool to parse and convert the programming language program to generate an abstract syntax tree corresponding to the programming language program; For each object type among the multiple object types included in the abstract syntax tree, based on the recursive access function corresponding to each object type, calling a preset running component to interpret and execute the expression under each object type to obtain a first execution result; Based on the first execution result and the target instance thread in the running component, the running component is used to recursively interpret multiple expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program.
2. The method according to claim 1, characterized in that In response to obtaining the externally input programming language program, parsing and converting the programming language program using a preset cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program includes: In response to obtaining an externally input programming language program, a predefined grammar file is compiled using a preset cross-platform parsing tool to obtain a parsing file corresponding to the cross-platform parsing tool; wherein the programming language program includes a definition domain program and an entry main function program; The programming language program is parsed and converted using the parsing file and the cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program.
3. The method according to claim 2, characterized in that For each object type among the multiple object types included in the abstract syntax tree, based on the recursive access function corresponding to each object type, calling a preset running component to interpret and execute the expression under each object type to obtain a first execution result, including: For a plurality of first object types corresponding to the domain program in the abstract syntax tree, based on a recursive access function corresponding to each first object type, calling a preset running component to perform cyclic interpretation and execution on an expression under each first object type, to obtain a second execution result and a registration method for caching; caching the registration method using the running component, and judging whether the definition domain program has an error based on the second execution result; If there is no error in the definition domain program, then for the multiple second object types corresponding to the entry main function program in the abstract syntax tree, based on the recursive access function corresponding to each second object type, the running component is called to interpret and execute the expression under each second object type to obtain a first execution result.
4. The method according to claim 3, characterized in that The method of calling a preset running component to perform loop interpretation and execution on the expression under each first object type based on the recursive access function corresponding to each first object type and obtaining a second execution result and a registration method for caching for the multiple first object types corresponding to the domain program in the abstract syntax tree includes: For a plurality of first object types corresponding to the domain program in the abstract syntax tree, determining a recursive access function corresponding to each of the plurality of first object types; Based on the recursive access function, calling a preset iterative loop method in a preset running component to perform loop interpretation and execution on the expression under each first object type, and obtaining a second execution result and a function signature for representing the corresponding relationship between the expressions; Based on the function signature, a registration method for caching corresponding to the function signature is obtained using a registration function preset in the running component.
5. The method according to claim 3, characterized in that: The method further comprises: for the plurality of second object types corresponding to the entry main function program in the abstract syntax tree, calling the running component to interpret and execute the expression under each second object type based on the recursive access function corresponding to each second object type, and obtaining a first execution result, including: Using the running component to obtain the entry main function program, and obtaining a plurality of second object types corresponding to the entry main function program in the abstract syntax tree; Based on each second object type in the plurality of second object types, determining a recursive access function corresponding to each second object type; Based on the recursive access function, the running component is called to interpret and execute the expression under each second object type to obtain a first execution result.
6. The method according to claim 3, characterized in that The step of recursively interpreting multiple expressions in the abstract syntax tree using the running component based on the first execution result and the target instance thread in the running component to obtain interpretation information corresponding to the programming language program includes: Based on the first execution result, calling a startup method preset in the running component, and instantiating a preset thread in the startup method to obtain a target instance thread in the running component; Based on the target instance thread, obtaining a method handle in the registration method; Based on the method handle, the execution component is used to recursively interpret multiple expressions in the abstract syntax tree to obtain interpretation information corresponding to the programming language program.
7. A programming language interpretation and execution device, characterized in that: The interpretation and execution device comprises: A parsing and conversion module, configured to, in response to obtaining a programming language program input from an external source, parse and convert the programming language program using a preset cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program; An interpretation and execution module is used to call a preset running component to interpret and execute the expression under each object type in the multiple object types included in the abstract syntax tree based on the recursive access function corresponding to each object type, so as to obtain a first execution result; A recursive interpretation module is used to recursively interpret multiple expressions in the abstract syntax tree using the running component based on the first execution result and the target instance thread in the running component to obtain interpretation information corresponding to the programming language program.
8. The interpretation and execution device according to claim 7, characterized in that: When the parsing and conversion module is used to respond to the programming language program obtained from the external input, parse and convert the programming language program using a preset cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program, the parsing and conversion module is used to: In response to obtaining an externally input programming language program, a predefined grammar file is compiled using a preset cross-platform parsing tool to obtain a parsing file corresponding to the cross-platform parsing tool; wherein the programming language program includes a definition domain program and an entry main function program; The programming language program is parsed and converted using the parsing file and the cross-platform parsing tool to generate an abstract syntax tree corresponding to the programming language program.
9. An electronic device, characterized in that: include: A processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate through the bus, and the machine-readable instructions are executed by the processor to perform the steps of the method for interpreting and executing the programming language as described in any one of claims 1 to 6.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method for interpreting and executing a programming language as claimed in any one of claims 1 to 6 are executed.
Citation Information
Cited By
PEG-based AADL code-to-graph multi-level conversion method
CN120029667A
Executive script full-path verification method and device based on executor rewriting, medium and program product
CN121524030A